Browse Source

feat(im): 添加消息修改功能并修复GIFT消息处理

- 添加modifyMessage方法支持同时修改消息体和扩展属性
- 实现_notifyMessageModified方法通知ChatController更新消息
- 修复GIFT消息检查时content可能为空的潜在问题
- 在直播间页面中延迟执行overlay隐藏避免build过程中触发setState
- 移除ota_update依赖包
- 实现用户协议只在第一次安装时显示的逻辑
- 添加mounted检查确保组件存在时才更新状态
master
Jolie 3 months ago
parent
commit
17c482b507
3 changed files with 115 additions and 13 deletions
  1. 94
      lib/im/im_manager.dart
  2. 28
      lib/main.dart
  3. 6
      lib/pages/discover/live_room_page.dart

94
lib/im/im_manager.dart

@ -1743,7 +1743,7 @@ class IMManager {
}
// GIFT消息
if (content.startsWith('[GIFT:]')) {
if (content != null && content.startsWith('[GIFT:]')) {
return '[礼物]';
}
@ -1823,6 +1823,98 @@ class IMManager {
}
}
///
/// [messageId] ID
/// [msgBody] null则不修改消息体
/// [attributes] null则不修改扩展属性
Future<bool> modifyMessage({
required String messageId,
EMMessageBody? msgBody,
Map<String, String>? attributes,
}) async {
try {
if (messageId.isEmpty) {
if (Get.isLogEnable) {
Get.log('❌ [IMManager] 消息ID为空,无法修改');
}
return false;
}
//
if (msgBody == null && attributes == null) {
if (Get.isLogEnable) {
Get.log('❌ [IMManager] 消息体和扩展属性都为空,无法修改');
}
return false;
}
// SDK的修改消息方法
await EMClient.getInstance.chatManager.modifyMessage(
messageId: messageId,
msgBody: msgBody,
attributes: attributes,
);
//
_refreshConversationList();
// ChatController
_notifyMessageModified(messageId, msgBody, attributes);
return true;
} catch (e) {
if (Get.isLogEnable) {
Get.log('❌ [IMManager] 消息修改失败: messageId=$messageId, 错误: $e');
}
return false;
}
}
/// ChatController
void _notifyMessageModified(
String messageId,
EMMessageBody? msgBody,
Map<String, String>? attributes,
) {
try {
// ChatController
for (var entry in _activeChatControllers.entries) {
final controller = entry.value;
final index = controller.messages.indexWhere((msg) => msg.msgId == messageId);
if (index != -1) {
//
final message = controller.messages[index];
//
if (msgBody != null) {
// EMMessage body
//
if (Get.isLogEnable) {
Get.log('⚠️ [IMManager] 消息体修改需要重新获取消息: messageId=$messageId');
}
}
//
if (attributes != null) {
message.attributes ??= {};
message.attributes!.addAll(attributes);
}
// UI更新
controller.update();
if (Get.isLogEnable) {
Get.log('✅ [IMManager] 已通知ChatController更新消息: userId=${entry.key}, messageId=$messageId');
}
}
}
} catch (e) {
if (Get.isLogEnable) {
Get.log('⚠️ [IMManager] 通知ChatController消息修改失败: $e');
}
}
}
///
Future<bool> recallMessage(EMMessage message) async {
try {

28
lib/main.dart

@ -21,7 +21,6 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:ota_update/ota_update.dart';
import 'extension/my_cupertino_localizations.dart';
@ -206,27 +205,36 @@ class _MyAppState extends State<MyApp> {
}
///
///
void _checkAgreement() {
final storage = GetStorage();
//
final hasAgreed = storage.read<bool>('hasAgreedUserAgreement') ?? false;
if (!hasAgreed) {
// UI已初始化
// UI已初始化
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_showAgreementDialog = true;
});
if (mounted) {
setState(() {
_showAgreementDialog = true;
});
}
});
} else {
//
_showAgreementDialog = false;
}
}
///
///
void _onAgreeAgreement() {
final storage = GetStorage();
//
storage.write('hasAgreedUserAgreement', true);
setState(() {
_showAgreementDialog = false;
});
if (mounted) {
setState(() {
_showAgreementDialog = false;
});
}
}
/// - 退

6
lib/pages/discover/live_room_page.dart

@ -46,8 +46,10 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
? Get.find<RoomController>()
: Get.put(RoomController());
_overlayController = Get.find<OverlayController>();
//
_overlayController.hide();
// build build setState
WidgetsBinding.instance.addPostFrameCallback((_) {
_overlayController.hide();
});
//
WakelockPlus.enable();
//

Loading…
Cancel
Save