addNativeCallback method

void addNativeCallback(
  1. MethodChannel _channel
)

Implementation

void addNativeCallback(MethodChannel _channel) {
  _channel.setMethodCallHandler((call) {
    try {
      if (call.method == ListenerType.simpleMsgListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        V2TimSimpleMsgListener? simpleMsgListener =
            this.simpleMessageListenerList[listenerUuid];
        Map<String, dynamic> params =
            data['data'] == null ? new Map<String, dynamic>() : data['data'];
        String type = data['type'];
        if (simpleMsgListener != null) {
          final msgID = params['msgID'];
          switch (type) {
            case 'onRecvC2CCustomMessage':
              // String msgID, V2TIMUserInfo sender, byte[] customData
              final sender = V2TimUserInfo.fromJson(params['sender']);
              _catchListenerError(() {
                simpleMsgListener.onRecvC2CCustomMessage(
                  msgID,
                  sender,
                  params['customData'],
                );
              });
              break;
            case 'onRecvC2CTextMessage':
              // String msgID, V2TIMUserInfo sender, String text
              final sender = V2TimUserInfo.fromJson(params['sender']);
              _catchListenerError(() {
                simpleMsgListener.onRecvC2CTextMessage(
                  msgID,
                  sender,
                  params['text'],
                );
              });
              break;
            case 'onRecvGroupCustomMessage':
              // String msgID, String groupID, V2TIMGroupMemberInfo sender, byte[] customData
              final groupSender =
                  V2TimGroupMemberInfo.fromJson(params['sender']);
              _catchListenerError(() {
                simpleMsgListener.onRecvGroupCustomMessage(
                  msgID,
                  params['groupID'],
                  groupSender,
                  params['customData'],
                );
              });
              break;
            case 'onRecvGroupTextMessage':
              // String msgID, String groupID, V2TIMGroupMemberInfo sender, String text
              final groupSender =
                  V2TimGroupMemberInfo.fromJson(params['sender']);
              _catchListenerError(() {
                simpleMsgListener.onRecvGroupTextMessage(
                  params['msgID'],
                  params['groupID'],
                  groupSender,
                  params['text'],
                );
              });
              break;
          }
        }
      } else if (call.method == ListenerType.initSDKListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        V2TimSDKListener? initSDKListener =
            this.initSDKListenerList[listenerUuid];
        Map<String, dynamic> params =
            data['data'] == null ? new Map<String, dynamic>() : data['data'];
        String type = data['type'];
        if (initSDKListener != null) {
          switch (type) {
            case 'onSelfInfoUpdated':
              final userInfo = V2TimUserFullInfo.fromJson(params);
              _catchListenerError(() {
                initSDKListener.onSelfInfoUpdated(userInfo);
              });
              break;
            case 'onConnectFailed':
              _catchListenerError(() {
                initSDKListener.onConnectFailed(
                  params['code'],
                  params['desc'],
                );
              });
              break;
            case 'onConnecting':
              _catchListenerError(() {
                initSDKListener.onConnecting();
              });
              break;
            case 'onConnectSuccess':
              _catchListenerError(() {
                initSDKListener.onConnectSuccess();
              });
              break;
            case 'onKickedOffline':
              _catchListenerError(() {
                initSDKListener.onKickedOffline();
              });
              break;
            case 'onUserSigExpired':
              _catchListenerError(() {
                initSDKListener.onUserSigExpired();
              });
              break;
            case 'onUserStatusChanged':
              List<dynamic> statusList = params['statusList'];
              List<V2TimUserStatus> list = List.empty(growable: true);
              statusList.forEach((element) {
                list.add(V2TimUserStatus.fromJson(
                    Map<String, dynamic>.from(element)));
              });
              _catchListenerError(() {
                initSDKListener.onUserStatusChanged(list);
              });
              break;
            case 'onLog':
              int level = params['level'];
              String log = params['content'];
              _catchListenerError(() {
                initSDKListener.onLog(level, log);
              });
              break;
          }
        }
      } else if (call.method == ListenerType.groupListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        V2TimGroupListener? groupListener =
            this.groupListenerList[listenerUuid];
        String type = data['type'];
        Map<String, dynamic> params = data['data'] == null
            ? new Map<String, dynamic>()
            : new Map<String, dynamic>.from(data['data']);

        String groupID = params['groupID'] == null ? '' : params['groupID'];
        String opReason =
            params['opReason'] == null ? '' : params['opReason'];
        bool isAgreeJoin =
            params['isAgreeJoin'] == null ? false : params['isAgreeJoin'];
        String customData =
            params['customData'] == null ? '' : params['customData'];
        String topicID = params["topicID"] == null ? "" : params["topicID"];
        List<String> topicIDList = params["topicIDList"] == null
            ? List.empty(growable: true)
            : List.castFrom(params["topicIDList"]);
        V2TimTopicInfo topicInfo = params["topicInfo"] == null
            ? V2TimTopicInfo()
            : V2TimTopicInfo.fromJson(params['topicInfo']);
        Map<String, String> groupAttributeMap =
            params['groupAttributeMap'] == null
                ? new Map<String, String>()
                : new Map<String, String>.from(params['groupAttributeMap']);

        List<Map<String, dynamic>> memberListMap =
            params['memberList'] == null
                ? List.empty(growable: true)
                : List.from(params['memberList']);

        List<Map<String, dynamic>> groupMemberChangeInfoListMap =
            params['groupMemberChangeInfoList'] == null
                ? List.empty(growable: true)
                : List.from(params['groupMemberChangeInfoList']);

        List<Map<String, dynamic>> groupChangeInfoListMap =
            params['groupChangeInfoList'] == null
                ? List.empty(growable: true)
                : List.from(params['groupChangeInfoList']);
        List<V2TimGroupChangeInfo> groupChangeInfoList =
            List.empty(growable: true);
        List<V2TimGroupMemberChangeInfo> groupMemberChangeInfoList =
            List.empty(growable: true);
        List<V2TimGroupMemberInfo> memberList = List.empty(growable: true);

        if (memberListMap.isNotEmpty) {
          memberListMap.forEach((element) {
            memberList.add(V2TimGroupMemberInfo.fromJson(element));
          });
        }
        if (groupMemberChangeInfoListMap.isNotEmpty) {
          groupMemberChangeInfoListMap.forEach((element) {
            groupMemberChangeInfoList
                .add(V2TimGroupMemberChangeInfo.fromJson(element));
          });
        }
        if (groupChangeInfoListMap.isNotEmpty) {
          groupChangeInfoListMap.forEach((element) {
            groupChangeInfoList.add(V2TimGroupChangeInfo.fromJson(element));
          });
        }
        late V2TimGroupMemberInfo opUser;
        late V2TimGroupMemberInfo member;
        if (params['opUser'] != null) {
          opUser = V2TimGroupMemberInfo.fromJson(params['opUser']);
        }
        if (params['member'] != null) {
          member = V2TimGroupMemberInfo.fromJson(params['member']);
        }
        if (groupListener != null) {
          switch (type) {
            case 'onMemberEnter':
              _catchListenerError(() {
                groupListener.onMemberEnter(
                  groupID,
                  memberList,
                );
              });
              break;
            case 'onMemberLeave':
              _catchListenerError(() {
                groupListener.onMemberLeave(
                  groupID,
                  member,
                );
              });
              break;
            case 'onMemberInvited':
              _catchListenerError(() {
                groupListener.onMemberInvited(
                  groupID,
                  opUser,
                  memberList,
                );
              });
              break;
            case 'onMemberKicked':
              _catchListenerError(() {
                groupListener.onMemberKicked(
                  groupID,
                  opUser,
                  memberList,
                );
              });
              break;
            case 'onMemberInfoChanged':
              _catchListenerError(() {
                groupListener.onMemberInfoChanged(
                  groupID,
                  groupMemberChangeInfoList,
                );
              });
              break;
            case 'onGroupCreated':
              _catchListenerError(() {
                groupListener.onGroupCreated(groupID);
              });
              break;
            case 'onGroupDismissed':
              _catchListenerError(() {
                groupListener.onGroupDismissed(
                  groupID,
                  opUser,
                );
              });
              break;
            case 'onGroupRecycled':
              _catchListenerError(() {
                groupListener.onGroupRecycled(
                  groupID,
                  opUser,
                );
              });
              break;
            case 'onGroupInfoChanged':
              _catchListenerError(() {
                groupListener.onGroupInfoChanged(
                  groupID,
                  groupChangeInfoList,
                );
              });
              break;
            case 'onReceiveJoinApplication':
              _catchListenerError(() {
                groupListener.onReceiveJoinApplication(
                  groupID,
                  member,
                  opReason,
                );
              });
              break;
            case 'onApplicationProcessed':
              _catchListenerError(() {
                groupListener.onApplicationProcessed(
                  groupID,
                  opUser,
                  isAgreeJoin,
                  opReason,
                );
              });
              break;
            case 'onGrantAdministrator':
              _catchListenerError(() {
                groupListener.onGrantAdministrator(
                  groupID,
                  opUser,
                  memberList,
                );
              });
              break;
            case 'onRevokeAdministrator':
              _catchListenerError(() {
                groupListener.onRevokeAdministrator(
                  groupID,
                  opUser,
                  memberList,
                );
              });
              break;
            case 'onQuitFromGroup':
              _catchListenerError(() {
                groupListener.onQuitFromGroup(groupID);
              });
              break;
            case 'onReceiveRESTCustomData':
              _catchListenerError(() {
                groupListener.onReceiveRESTCustomData(
                  groupID,
                  customData,
                );
              });
              break;
            case 'onGroupAttributeChanged':
              _catchListenerError(() {
                groupListener.onGroupAttributeChanged(
                  groupID,
                  groupAttributeMap,
                );
              });
              break;
            case "onTopicCreated":
              _catchListenerError(() {
                groupListener.onTopicCreated(
                  groupID,
                  topicID,
                );
              });
              break;
            case "onTopicDeleted":
              _catchListenerError(() {
                groupListener.onTopicDeleted(
                  groupID,
                  topicIDList,
                );
              });
              break;
            case "onTopicInfoChanged":
              _catchListenerError(() {
                groupListener.onTopicInfoChanged(
                  groupID,
                  topicInfo,
                );
              });
              break;
            case "onGroupCounterChanged":
              String key = params["key"] ?? "";
              int value = params["value"] ?? 0;
              _catchListenerError(() {
                groupListener.onGroupCounterChanged(
                  groupID,
                  key,
                  value,
                );
              });
              break;
          }
        }
      } else if (call.method == ListenerType.advancedMsgListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        V2TimAdvancedMsgListener? listener =
            this.v2TIMMessageManager.advancedMsgListenerList[listenerUuid];
        String type = data['type'];
        dynamic params =
            data['data'] == null ? new Map<String, dynamic>() : data['data'];

        if (listener != null) {
          switch (type) {
            case 'onRecvNewMessage':
              _catchListenerError(() {
                listener.onRecvNewMessage(V2TimMessage.fromJson(params));
              });
              break;
            case 'onRecvMessageModified':
              _catchListenerError(() {
                listener.onRecvMessageModified(V2TimMessage.fromJson(params));
              });
              break;
            case 'onRecvC2CReadReceipt':
              List dataList = params;
              List<V2TimMessageReceipt> receiptList =
                  List.empty(growable: true);
              dataList.forEach((element) {
                receiptList.add(V2TimMessageReceipt.fromJson(element));
              });
              _catchListenerError(() {
                listener.onRecvC2CReadReceipt(receiptList);
              });
              break;
            case 'onRecvMessageRevoked':
              _catchListenerError(() {
                listener.onRecvMessageRevoked(params);
              });
              break;
            case 'onSendMessageProgress':
              final message = V2TimMessage.fromJson(params['message']);
              _catchListenerError(() {
                listener.onSendMessageProgress(
                  message,
                  params['progress'],
                );
              });
              break;
            case 'onRecvMessageReadReceipts':
              List dataList = params;
              List<V2TimMessageReceipt> receiptList =
                  List.empty(growable: true);
              dataList.forEach((element) {
                receiptList.add(V2TimMessageReceipt.fromJson(element));
              });
              _catchListenerError(() {
                listener.onRecvMessageReadReceipts(receiptList);
              });
              break;
            case "onRecvMessageExtensionsChanged":
              Map<String, dynamic> cbdata = Map<String, Object>.from(params);
              String msgID = cbdata["msgID"] ?? "";
              List<Map<String, dynamic>> extensions =
                  List.from(cbdata["extensions"] ?? []);
              List<V2TimMessageExtension> resList =
                  List.empty(growable: true);
              extensions.forEach((element) {
                resList.add(V2TimMessageExtension(
                  extensionKey: element["extensionKey"] ?? "",
                  extensionValue: element["extensionValue"] ?? "",
                ));
              });
              _catchListenerError(() {
                listener.onRecvMessageExtensionsChanged(
                  msgID,
                  resList,
                );
              });
              break;
            case "onRecvMessageExtensionsDeleted":
              Map<String, dynamic> cbdata = Map<String, dynamic>.from(params);
              String msgID = cbdata["msgID"] ?? "";
              List<String> extensionKeys =
                  List.from(cbdata["extensionKeys"] ?? []);
              _catchListenerError(() {
                listener.onRecvMessageExtensionsDeleted(
                  msgID,
                  extensionKeys,
                );
              });
              break;
            case "onMessageDownloadProgressCallback":
              Map<String, dynamic> cbdata = Map<String, dynamic>.from(params);
              _catchListenerError(() {
                listener.onMessageDownloadProgressCallback(
                  V2TimMessageDownloadProgress.fromJson(cbdata),
                );
              });
              break;
          }
        }
      } else if (call.method == ListenerType.conversationListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        String type = data['type'];

        V2TimConversationListener? listener =
            this.v2ConversationManager.conversationListenerList[listenerUuid];
        if (listener != null) {
          switch (type) {
            case 'onSyncServerStart':
              _catchListenerError(() {
                listener.onSyncServerStart();
              });
              break;
            case 'onSyncServerFinish':
              _catchListenerError(() {
                listener.onSyncServerFinish();
              });
              break;
            case 'onSyncServerFailed':
              _catchListenerError(() {
                listener.onSyncServerFailed();
              });
              break;
            case 'onNewConversation':
              dynamic params = data['data'] == null
                  ? List.empty(growable: true)
                  : List.from(data['data']);
              List<V2TimConversation> conversationList =
                  List.empty(growable: true);
              params.forEach((element) {
                conversationList.add(V2TimConversation.fromJson(element));
              });
              _catchListenerError(() {
                listener.onNewConversation(conversationList);
              });
              break;
            case 'onConversationChanged':
              dynamic params = data['data'] == null
                  ? List.empty(growable: true)
                  : List.from(data['data']);
              List<V2TimConversation> conversationList =
                  List.empty(growable: true);
              params.forEach((element) {
                conversationList.add(V2TimConversation.fromJson(element));
              });

              _catchListenerError(() {
                listener.onConversationChanged(conversationList);
              });
              break;
            case 'onTotalUnreadMessageCountChanged':
              dynamic params = data['data'] == null ? 0 : data['data'];
              _catchListenerError(() {
                listener.onTotalUnreadMessageCountChanged(params);
              });
              break;
            case "onConversationGroupCreated":
              Map<String, dynamic> param = data["data"];
              String groupName = param["groupName"];
              List<V2TimConversation> list = List.empty(growable: true);
              List.from(param["conversationList"]).forEach((v) {
                list.add(V2TimConversation.fromJson(v));
              });
              _catchListenerError(() {
                listener.onConversationGroupCreated(groupName, list);
              });
              break;
            case "onConversationGroupDeleted":
              _catchListenerError(() {
                listener.onConversationGroupDeleted(data["data"]);
              });
              break;
            case "onConversationGroupNameChanged":
              Map<String, dynamic> param = data["data"];
              _catchListenerError(() {
                listener.onConversationGroupNameChanged(
                    param["oldName"], param["newName"]);
              });
              break;
            case "onConversationsAddedToGroup":
              Map<String, dynamic> param = data["data"];
              String groupName = param["groupName"];
              List<V2TimConversation> list = List.empty(growable: true);
              List.from(param["conversationList"]).forEach((v) {
                list.add(V2TimConversation.fromJson(v));
              });
              _catchListenerError(() {
                listener.onConversationsAddedToGroup(groupName, list);
              });
              break;
            case "onConversationsDeletedFromGroup":
              Map<String, dynamic> param = data["data"];
              String groupName = param["groupName"];
              List<V2TimConversation> list = List.empty(growable: true);
              List.from(param["conversationList"]).forEach((v) {
                list.add(V2TimConversation.fromJson(v));
              });
              _catchListenerError(() {
                listener.onConversationsDeletedFromGroup(groupName, list);
              });
              break;
          }
        }
      } else if (call.method == ListenerType.friendListener) {
        Map<String, dynamic> data = this.formatJson(call.arguments);
        String listenerUuid = data['listenerUuid'];
        String type = data['type'];
        dynamic params =
            data['data'] == null ? new Map<String, dynamic>() : data['data'];
        V2TimFriendshipListener? listener =
            this.v2TIMFriendshipManager.friendListenerList[listenerUuid];
        if (listener != null) {
          switch (type) {
            case 'onFriendApplicationListAdded':
              List applicationListMap = params;
              List<V2TimFriendApplication> applicationList =
                  List.empty(growable: true);
              applicationListMap.forEach((element) {
                applicationList.add(V2TimFriendApplication.fromJson(element));
              });
              _catchListenerError(() {
                listener.onFriendApplicationListAdded(applicationList);
              });
              break;
            case 'onFriendApplicationListDeleted':
              List<String> userIDList = List.from(params);
              _catchListenerError(() {
                listener.onFriendApplicationListDeleted(userIDList);
              });
              break;
            case 'onFriendApplicationListRead':
              _catchListenerError(() {
                listener.onFriendApplicationListRead();
              });
              break;
            case 'onFriendListAdded':
              List userMap = params;
              List<V2TimFriendInfo> users = List.empty(growable: true);
              userMap.forEach((element) {
                users.add(V2TimFriendInfo.fromJson(element));
              });
              _catchListenerError(() {
                listener.onFriendListAdded(users);
              });
              break;
            case 'onFriendListDeleted':
              List<String> userList = List.from(params);
              _catchListenerError(() {
                listener.onFriendListDeleted(userList);
              });
              break;
            case 'onBlackListAdd':
              List infoListMap = params;
              List<V2TimFriendInfo> infoList = List.empty(growable: true);
              infoListMap.forEach((element) {
                infoList.add(V2TimFriendInfo.fromJson(element));
              });
              _catchListenerError(() {
                listener.onBlackListAdd(infoList);
              });
              break;
            case 'onBlackListDeleted':
              List<String> userList = List.from(params);
              _catchListenerError(() {
                listener.onBlackListDeleted(userList);
              });
              break;
            case 'onFriendInfoChanged':
              List infoListMap = params;
              List<V2TimFriendInfo> infoList = List.empty(growable: true);
              infoListMap.forEach((element) {
                infoList.add(V2TimFriendInfo.fromJson(element));
              });
              _catchListenerError(() {
                listener.onFriendInfoChanged(infoList);
              });
              break;
          }
        }
      } else if (call.method == 'logFromSwift') {
        var data = call.arguments["data"];
        var msg = call.arguments["msg"];
        print('========> $msg: $data');
      } else if (call.method == ListenerType.signalingListener) {
        Map<String, dynamic> d = this.formatJson(call.arguments);
        String listenerUuid = d['listenerUuid'];
        String type = d['type'];
        Map<String, dynamic> params = d['data'];
        String inviteID =
            params['inviteID'] == null ? '' : params['inviteID'];
        String inviter = params['inviter'] == null ? '' : params['inviter'];
        String groupID = params['groupID'] == null ? '' : params['groupID'];
        List<String>? inviteeList = params['inviteeList'] == null
            ? null
            : List.from(params['inviteeList']);
        String data = params['data'] == null ? '' : params['data'];
        String invitee = params['invitee'] == null ? '' : params['invitee'];
        V2TimSignalingListener? listener =
            this.v2timSignalingManager.signalingListenerList[listenerUuid];
        switch (type) {
          case 'onReceiveNewInvitation':
            _catchListenerError(() {
              listener!.onReceiveNewInvitation(
                  inviteID, inviter, groupID, inviteeList!, data);
            });
            break;
          case 'onInviteeAccepted':
            _catchListenerError(() {
              listener!.onInviteeAccepted(inviteID, invitee, data);
            });
            break;
          case 'onInviteeRejected':
            _catchListenerError(() {
              listener!.onInviteeRejected(inviteID, invitee, data);
            });
            break;
          case 'onInvitationCancelled':
            _catchListenerError(() {
              listener!.onInvitationCancelled(inviteID, inviter, data);
            });
            break;
          case 'onInvitationTimeout':
            _catchListenerError(() {
              listener!.onInvitationTimeout(inviteID, inviteeList!);
            });
            break;
        }
      }
    } catch (err) {
      print(
          "重点关注,回调失败了,数据类型异常。$err ${call.method} ${call.arguments['type']} ${call.arguments['data']}");
    }
    return Future.value(null);
  });
}