diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index a9a1303..345b4a5 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,7 @@ + + + _instance; + bool _isInitialized = false; + + IMManager._internal() { + print('IMManager instance created'); + } + + /// 初始化IM SDK + Future initialize(String appKey) async { + try { + if (_isInitialized) { + print('IM SDK already initialized'); + return true; + } + + // 创建EMOptions实例 + final options = EMOptions( + appKey: appKey, + autoLogin: false, + acceptInvitationAlways: false, + ); + + // 初始化SDK + await EMClient.getInstance.init(options); + + // 注册监听器 + _registerListeners(); + + _isInitialized = true; + print('IM SDK initialized successfully'); + return true; + } catch (e) { + print('Failed to initialize IM SDK: $e'); + return false; + } + } + + // 注册监听器 + void _registerListeners() { + try { + // 消息监听器 + + + // 连接监听器 + EMClient.getInstance.addConnectionEventHandler('', EMConnectionEventHandler( + onConnected: () { + print('Connected to IM server'); + }, + onDisconnected: () { + print('Disconnected from IM server:'); + }, + onTokenDidExpire: () { + print('IM token about to expire'); + }, + onUserKickedByOtherDevice: () { + print('User kicked out of IM server'); + }, + )); + } catch (e) { + print('Failed to register listeners: $e'); + } + } + + /// 登录IM服务 + Future login(String userId, String token) async { + try { + if (!_isInitialized) { + print('IM SDK not initialized'); + return false; + } + + await EMClient.getInstance.loginWithToken(userId, token); + print('IM login successful'); + return true; + } catch (e) { + print('IM login failed: $e'); + return false; + } + } + + /// 登出IM服务 + Future logout() async { + try { + await EMClient.getInstance.logout(); + print('IM logout successful'); + return true; + } catch (e) { + print('IM logout failed: $e'); + return false; + } + } + + /// 发送文本消息 + Future sendTextMessage( + String content, + String toChatUsername, + ) async { + try { + // 创建文本消息 + final message = EMMessage.createTxtSendMessage( + targetId: toChatUsername, + content: content, + ); + + // 发送消息 + await EMClient.getInstance.chatManager.sendMessage(message); + print('Text message sent successfully'); + return message; + } catch (e) { + print('Failed to send text message: $e'); + return null; + } + } + + /// 发送图片消息 + Future sendImageMessage( + String imagePath, + String toChatUsername, + ) async { + try { + // 创建图片消息 + final message = EMMessage.createImageSendMessage( + targetId: toChatUsername, + filePath: imagePath, + sendOriginalImage: false, + ); + + // 发送消息 + await EMClient.getInstance.chatManager.sendMessage(message); + print('Image message sent successfully'); + return message; + } catch (e) { + print('Failed to send image message: $e'); + return null; + } + } + + /// 获取会话列表 + Future> getConversations() async { + try { + return EMClient.getInstance.chatManager.loadAllConversations(); + } catch (e) { + print('Failed to get conversations: $e'); + return []; + } + } + + /// 获取指定会话的消息记录 + Future> getMessages( + String conversationId, { + int pageSize = 20, + String? startMsgId, + }) async { + EMConversationType convType = EMConversationType.Chat; + EMCursorResult cursor = await EMClient.getInstance.chatManager.fetchHistoryMessagesByOption( + conversationId, convType, pageSize: pageSize + ); + return cursor.data; + } + + /// 清理资源 + void dispose() { + try { + EMClient.getInstance.removeConnectionEventHandler(""); + } catch (e) { + print('Failed to dispose resources: $e'); + } + } +} + +// 导出单例实例 +final IMManager imManager = IMManager(); diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index dc8e21c..fb2baf8 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -3,7 +3,7 @@ import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:dating_touchme_app/network/user_api.dart'; -import 'package:dating_touchme_app/pages/mine/login_page.dart'; +// 移除未使用的导入 class MainPage extends StatefulWidget { const MainPage({super.key});