You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.9 KiB
138 lines
3.9 KiB
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){
|
|
final body = message?.body as EMTextMessageBody;
|
|
return body.content;
|
|
}else if(message?.body.type == MessageType.IMAGE){
|
|
return '[图片]';
|
|
}else if(message?.body.type == MessageType.VOICE){
|
|
return '[语音]';
|
|
}else if(message?.body.type == MessageType.VIDEO){
|
|
return '[视频]';
|
|
}else if(message?.body.type == MessageType.FILE){
|
|
return '[文件]';
|
|
}else if(message?.body.type == MessageType.LOCATION){
|
|
return '[位置]';
|
|
}
|
|
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 formatMessageTime(int timestamp) {
|
|
DateTime messageTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
|
|
DateTime now = DateTime.now();
|
|
|
|
// 检查是否是今天
|
|
if (messageTime.year == now.year &&
|
|
messageTime.month == now.month &&
|
|
messageTime.day == now.day) {
|
|
// 今天显示时:分
|
|
return '${messageTime.hour.toString().padLeft(2, '0')}:${messageTime.minute.toString().padLeft(2, '0')}';
|
|
}
|
|
|
|
// 检查是否是昨天
|
|
DateTime yesterday = now.subtract(Duration(days: 1));
|
|
if (messageTime.year == yesterday.year &&
|
|
messageTime.month == yesterday.month &&
|
|
messageTime.day == yesterday.day) {
|
|
return '昨天';
|
|
}
|
|
|
|
// 其他日期显示月-日
|
|
return '${messageTime.month.toString().padLeft(2, '0')}-${messageTime.day.toString().padLeft(2, '0')}';
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
/// 删除会话
|
|
Future<bool> deleteConversation(String conversationId) async {
|
|
try {
|
|
final success = await IMManager.instance.deleteConversation(
|
|
conversationId,
|
|
);
|
|
if (success) {
|
|
conversations.removeWhere((element) => element.id == conversationId);
|
|
}
|
|
return success;
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('删除会话失败: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|