diff --git a/example/lib/main.dart b/example/lib/main.dart index 6670eb6..1924881 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,7 +1,6 @@ -import 'package:flutter/material.dart'; -import 'dart:async'; +import 'dart:io'; -import 'package:flutter/services.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_openim_sdk/flutter_openim_sdk.dart'; void main() { @@ -16,6 +15,7 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { @override void initState() { + _initSDK(); super.initState(); } @@ -28,21 +28,81 @@ class _MyAppState extends State { ), body: Column( children: [ - TextButton(onPressed: _testApi, child: Text('api')), + TextButton(onPressed: _login, child: Text('login')), ], ), ), ); } - void _testApi() { - // FlutterOpenimSdk.iMManager.initSDK( - // platform: platform, - // ipApi: ipApi, - // ipWs: ipWs, - // dbPath: dbPath, - // listener: listener, - // ); + void _login() { + if (OpenIM.iMManager.isInitialized) + OpenIM.iMManager.login(uid: '', token: ''); + } + + void _initSDK() async { + // Initialize SDK + OpenIM.iMManager + ..initSDK( + platform: Platform.isAndroid ? IMPlatform.android : IMPlatform.ios, + ipApi: '', + ipWs: '', + dbPath: '', + listener: OnInitSDKListener( + connecting: () {}, + connectFailed: (code, error) {}, + connectSuccess: () {}, + kickedOffline: () {}, + userSigExpired: () {}, + selfInfoUpdated: (user) {}, + ), + ) + + // Add message listener (remove when not in use) + ..messageManager.addAdvancedMsgListener(OnAdvancedMsgListener( + recvMessageRevoked: (msgId) {}, + recvC2CReadReceipt: (list) {}, + recvNewMessage: (msg) {}, + )) + + // Set up message sending progress listener + ..messageManager.setMsgSendProgressListener(OnMsgSendProgressListener( + progressCallback: (msgId, progress) {}, + )) + + // Set up friend relationship listener + ..friendshipManager.setFriendshipListener(OnFriendshipListener( + blackListAdd: (u) {}, + blackListDeleted: (u) {}, + friendApplicationListAccept: (u) {}, + friendApplicationListAdded: (u) {}, + friendApplicationListDeleted: (u) {}, + friendApplicationListReject: (u) {}, + friendInfoChanged: (u) {}, + friendListAdded: (u) {}, + friendListDeleted: (u) {}, + )) + + // Set up conversation listener + ..conversationManager.setConversationListener(OnConversationListener( + conversationChanged: (list) {}, + newConversation: (list) {}, + totalUnreadMsgCountChanged: (count) {}, + syncServerFailed: () {}, + syncServerFinish: () {}, + syncServerStart: () {}, + )) + // Set up group listener + ..groupManager.setGroupListener(OnGroupListener( + applicationProcessed: (groupId, opUser, agreeOrReject, opReason) {}, + groupCreated: (groupId) {}, + groupInfoChanged: (groupId, info) {}, + memberEnter: (groupId, list) {}, + memberInvited: (groupId, opUser, list) {}, + memberKicked: (groupId, opUser, list) {}, + memberLeave: (groupId, info) {}, + receiveJoinApplication: (groupId, info, opReason) {}, + )); } } diff --git a/lib/src/manager/im_manager.dart b/lib/src/manager/im_manager.dart index f80f072..b8c912e 100644 --- a/lib/src/manager/im_manager.dart +++ b/lib/src/manager/im_manager.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:developer'; import 'package:flutter/services.dart'; import 'package:flutter_openim_sdk/flutter_openim_sdk.dart'; @@ -15,6 +14,7 @@ class IMManager { late InitSDKListener _initSDKListener; late String uid; late UserInfo uInfo; + bool isInitialized = false; IMManager(this._channel) { conversationManager = ConversationManager(_channel); @@ -277,17 +277,29 @@ class IMManager { required InitSDKListener listener, }) { _initSDKListener = listener; - return _channel.invokeMethod( - 'initSDK', - _buildParam( - {"platform": platform, "ipApi": ipApi, "ipWs": ipWs, "dbDir": dbPath}, - ), - ); + return _channel + .invokeMethod( + 'initSDK', + _buildParam( + { + "platform": platform, + "ipApi": ipApi, + "ipWs": ipWs, + "dbDir": dbPath + }, + )) + .then((value) { + isInitialized = true; + return value; + }); } /// Future unInitSDK() { - return _channel.invokeMethod('unInitSDK', _buildParam({})); + return _channel.invokeMethod('unInitSDK', _buildParam({})).then((value) { + isInitialized = false; + return value; + }); } /// login sdk