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 = [].obs; // 加载状态 final isLoading = false.obs; // 错误消息 final errorMessage = ''.obs; @override void onInit() { super.onInit(); // 初始化时加载会话列表 loadConversations(); } /// 加载会话列表 Future loadConversations() async { if (isLoading.value) return; try { isLoading.value = true; errorMessage.value = ''; // 从IMManager获取会话列表 final List 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 refreshConversations() async { await loadConversations(); } /// 获取会话的最新消息 String getLastMessageContent(EMMessage? message) { if(message?.body.type == MessageType.TXT){ } return '暂无消息'; } /// 获取会话的未读消息数量 Future 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 loadContact(String userId) async{ var data = await IMManager.instance.getContacts(userId); return data[userId]!; } Future lastMessage(EMConversation conversation) async{ return await conversation.latestMessage(); } }