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); /// Set up a friend relationship listener Future setFriendshipListener(FriendshipListener listener) { this.friendshipListener = listener; return _channel.invokeMethod('setFriendListener', _buildParam({})); } /// Get friend info by user id 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, sent to you by others Future> getFriendApplicationList() { return _channel .invokeMethod('getFriendApplicationList', _buildParam({})) .then((value) => _toList(value)); } /// Find all friends including those who have been added to the blacklist Future> getFriendList() { return _channel .invokeMethod('getFriendList', _buildParam({})) .then((value) => _toList(value)); } /// Find all friends including those who have been added to the blacklist Future> getFriendListMap() { return _channel .invokeMethod('getFriendList', _buildParam({})) .then((value) => _toListMap(value)); } /// Modify friend information, only [comment] can be modified Future setFriendInfo( {required String uid, required String comment}) { return _channel.invokeMethod( 'setFriendInfo', _buildParam({ 'uid': uid, 'comment': comment, })); } /// Add friends 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 List _toListMap(String? value) { var list = _formatJson(value); return list; } // static UserInfo _toObj(String value) => UserInfo.fromJson(_formatJson(value)); static dynamic _formatJson(value) { return jsonDecode(_printValue(value)); } static String _printValue(value) { return value; } }