Browse Source
feat(message): 实现会话列表功能- 新增 ConversationController 管理会话数据
feat(message): 实现会话列表功能- 新增 ConversationController 管理会话数据
-从 IMManager 获取真实会话列表替代模拟数据 - 支持会话列表加载状态与错误处理 - 实现会话项 UI 展示包括头像、昵称、最后消息等 - 添加未读消息数与消息时间格式化显示 - 集成 GetX 状态管理与响应式更新 - 优化会话列表空状态与加载失败重试机制ios
4 changed files with 258 additions and 151 deletions
Unified View
Diff Options
-
113lib/controller/message/conversation_controller.dart
-
12lib/im/im_manager.dart
-
280lib/pages/message/conversation_tab.dart
-
4lib/pages/message/message_page.dart
@ -0,0 +1,113 @@ |
|||||
|
import 'package:get/get.dart'; |
||||
|
import 'package:get_storage/get_storage.dart'; |
||||
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
||||
|
import '../../im/im_manager.dart'; |
||||
|
|
||||
|
class ConversationController extends GetxController { |
||||
|
// 会话列表数据 |
||||
|
final conversations = <EMConversation>[].obs; |
||||
|
// 加载状态 |
||||
|
final isLoading = false.obs; |
||||
|
// 错误消息 |
||||
|
final errorMessage = ''.obs; |
||||
|
|
||||
|
@override |
||||
|
void onInit() { |
||||
|
super.onInit(); |
||||
|
// 初始化时加载会话列表 |
||||
|
loadConversations(); |
||||
|
} |
||||
|
|
||||
|
/// 加载会话列表 |
||||
|
Future<void> loadConversations() async { |
||||
|
if (isLoading.value) return; |
||||
|
|
||||
|
try { |
||||
|
isLoading.value = true; |
||||
|
errorMessage.value = ''; |
||||
|
|
||||
|
// 从IMManager获取会话列表 |
||||
|
final List<EMConversation> convList = await IMManager.instance.getConversations(); |
||||
|
// 更新会话列表 |
||||
|
conversations.value = convList; |
||||
|
|
||||
|
// 使用GetX日志系统 |
||||
|
if (Get.isLogEnable) { |
||||
|
Get.log('Loaded ${convList.length} conversations'); |
||||
|
} |
||||
|
} catch (e) { |
||||
|
// 使用GetX日志系统 |
||||
|
if (Get.isLogEnable) { |
||||
|
Get.log('Failed to load conversations: $e'); |
||||
|
} |
||||
|
errorMessage.value = '加载会话列表失败,请稍后重试'; |
||||
|
} finally { |
||||
|
isLoading.value = false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 刷新会话列表 |
||||
|
Future<void> refreshConversations() async { |
||||
|
await loadConversations(); |
||||
|
} |
||||
|
|
||||
|
/// 获取会话的最新消息 |
||||
|
String getLastMessageContent(EMMessage? message) { |
||||
|
if(message?.body.type == MessageType.TXT){ |
||||
|
|
||||
|
} |
||||
|
return '暂无消息'; |
||||
|
} |
||||
|
|
||||
|
/// 获取会话的未读消息数量 |
||||
|
Future<int> getUnreadCount(EMConversation conversation) async { |
||||
|
try { |
||||
|
// 简化实现,返回0 |
||||
|
return await conversation.unreadCount(); |
||||
|
} catch (e) { |
||||
|
if (Get.isLogEnable) { |
||||
|
Get.log('Error getting unread count: $e'); |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 获取会话最后消息的时间 |
||||
|
String getConversationLastMessageTime(EMConversation conversation) { |
||||
|
try { |
||||
|
// 返回默认时间 |
||||
|
return '刚刚'; |
||||
|
} catch (e) { |
||||
|
if (Get.isLogEnable) { |
||||
|
Get.log('Error getting last message time: $e'); |
||||
|
} |
||||
|
return ''; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 格式化消息时间 |
||||
|
String formatMessageTime(int timestamp) { |
||||
|
DateTime messageTime = DateTime.fromMillisecondsSinceEpoch(timestamp); |
||||
|
DateTime now = DateTime.now(); |
||||
|
Duration difference = now.difference(messageTime); |
||||
|
|
||||
|
if (difference.inDays > 0) { |
||||
|
return '${difference.inDays}天前'; |
||||
|
} else if (difference.inHours > 0) { |
||||
|
return '${difference.inHours}小时前'; |
||||
|
} else if (difference.inMinutes > 0) { |
||||
|
return '${difference.inMinutes}分钟前'; |
||||
|
} else { |
||||
|
return '刚刚'; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Future<EMUserInfo> loadContact(String userId) async{ |
||||
|
var data = await IMManager.instance.getContacts(userId); |
||||
|
return data[userId]!; |
||||
|
} |
||||
|
|
||||
|
Future<EMMessage?> lastMessage(EMConversation conversation) async{ |
||||
|
return await conversation.latestMessage(); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save