import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_openim_sdk/flutter_openim_sdk.dart'; class FriendshipManager { MethodChannel _channel; late FriendshipListener friendshipListener; FriendshipManager(this._channel); /// listener[FriendshipListener] /// Future setFriendshipListener(FriendshipListener listener) { this.friendshipListener = listener; return _channel.invokeMethod('setFriendListener', _buildParam({})); } /// get friend info by userid /// Future> getFriendsInfo({required List uidList}) { return _channel .invokeMethod('getFriendsInfo', _buildParam({"uidList": uidList})) .then((value) => _toList(value)); } /// send an friend application /// Future addFriend({required String uid, required String reason}) { return _channel.invokeMethod( 'addFriend', _buildParam({"uid": uid, "reqMessage": reason})); } /// get all friend application /// including those initiated by you and sent to you by others Future> getFriendApplicationList() { return _channel .invokeMethod('getFriendApplicationList', _buildParam({})) .then((value) => _toList(value)); } /// find all friends /// Future> getFriendList() { return _channel .invokeMethod('getFriendList', _buildParam({})) .then((value) => _toList(value)); } /// modify friend information, only [comment] can be modified /// Future setFriendInfo({required UserInfo info}) { return _channel.invokeMethod('setFriendInfo', _buildParam(info.toJson())); } /// add to blacklist /// Future addToBlackList({required String uid}) { return _channel.invokeMethod('addToBlackList', _buildParam({"uid": uid})); } /// find all blacklist /// Future> getBlackList() { return _channel .invokeMethod('getBlackList', _buildParam({})) .then((value) => _toList(value)); } /// remove from blacklist /// Future deleteFromBlackList({required String uid}) { return _channel.invokeMethod( 'deleteFromBlackList', _buildParam({"uid": uid})); } /// determine if there is a friendship by userId /// Future> checkFriend(List uidList) { return _channel .invokeMethod('checkFriend', _buildParam({'uidList': uidList})) .then((value) => _toList(value)); } /// dissolve friendship from friend list /// Future deleteFromFriendList({required String uid}) { return _channel.invokeMethod( 'deleteFromFriendList', _buildParam({"uid": uid})); } /// accept application of be friend /// Future acceptFriendApplication({required String uid}) { return _channel.invokeMethod( 'acceptFriendApplication', _buildParam({"uid": uid})); } /// refuse application of be friend /// Future refuseFriendApplication({required String uid}) { return _channel.invokeMethod( 'refuseFriendApplication', _buildParam({"uid": uid})); } /// /// Future forceSyncFriendApplication() { return _channel.invokeMethod('forceSyncFriendApplication', _buildParam({})); } /// /// Future forceSyncFriend() { return _channel.invokeMethod('forceSyncFriend', _buildParam({})); } /// Future forceSyncBlackList() { return _channel.invokeMethod('forceSyncBlackList', _buildParam({})); } static Map _buildParam(Map param) { param["ManagerName"] = "friendshipManager"; return param; } static List _toList(String? value) { var list = _formatJson(value); if (null == list) return []; return (list as List).map((e) => UserInfo.fromJson(e)).toList(); } static UserInfo _toObj(String value) => UserInfo.fromJson(_formatJson(value)); static dynamic _formatJson(value) { return jsonDecode(_printValue(value)); } static String _printValue(value) { return value; } }