Files
tongxunruanjian/mobile-next/lib/pages/contacts/friend_requests/friend_requests_logic.dart
T
2b2c2d28a4 fix(mobile-next): 把加载、失败、断网接到真实 SDK 与目录请求
会话列表、通讯录、好友申请、添加同事页覆盖 CompanyLoadingView/CompanyFailView;
主页断网横幅跟随 IMSdkStatus.connectionFailed。不伪造状态。

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-08-20 02:52:24 +08:00

113 lines
3.0 KiB
Dart

import 'dart:async';
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';
import '../../../core/controller/im_controller.dart';
import '../../home/home_logic.dart';
class FriendRequestsLogic extends GetxController {
final imLogic = Get.find<IMController>();
final homeLogic = Get.find<HomeLogic>();
final applicationList = <FriendApplicationInfo>[].obs;
final loading = true.obs;
final loadFailed = false.obs;
late StreamSubscription faSub;
@override
void onInit() {
faSub = imLogic.friendApplicationChangedSubject.listen((value) {
_getFriendRequestsList();
});
super.onInit();
}
@override
void onReady() {
_getFriendRequestsList();
super.onReady();
}
void retry() => _getFriendRequestsList();
@override
void onClose() {
faSub.cancel();
homeLogic.getUnhandledFriendApplicationCount();
super.onClose();
}
void _getFriendRequestsList() async {
loading.value = true;
loadFailed.value = false;
try {
final list = await Future.wait([
OpenIM.iMManager.friendshipManager
.getFriendApplicationListAsRecipient(),
OpenIM.iMManager.friendshipManager
.getFriendApplicationListAsApplicant(),
]);
final allList = <FriendApplicationInfo>[];
allList
..addAll(list[0])
..addAll(list[1]);
allList.sort((a, b) {
if (a.createTime! > b.createTime!) {
return -1;
} else if (a.createTime! < b.createTime!) {
return 1;
}
return 0;
});
var haveReadList = DataSp.getHaveReadUnHandleFriendApplication();
haveReadList ??= <String>[];
for (var e in list[0]) {
var id = IMUtils.buildFriendApplicationID(e);
if (!haveReadList.contains(id)) {
haveReadList.add(id);
}
}
DataSp.putHaveReadUnHandleFriendApplication(haveReadList);
applicationList.assignAll(allList);
} catch (e, s) {
Logger.print('friend requests e: $e $s');
loadFailed.value = true;
} finally {
loading.value = false;
}
}
bool isISendRequest(FriendApplicationInfo info) =>
info.fromUserID == OpenIM.iMManager.userID;
void acceptFriendApplication(FriendApplicationInfo info) async {
try {
await LoadingView.singleton.wrap(
asyncFunction: () => OpenIM.iMManager.friendshipManager
.acceptFriendApplication(userID: info.fromUserID!),
);
IMViews.showToast(StrRes.addSuccessfully);
_getFriendRequestsList();
} catch (_) {
IMViews.showToast(StrRes.addFailed);
}
}
void refuseFriendApplication(FriendApplicationInfo info) async {
try {
await LoadingView.singleton.wrap(
asyncFunction: () => OpenIM.iMManager.friendshipManager
.refuseFriendApplication(userID: info.fromUserID!),
);
IMViews.showToast(StrRes.rejectSuccessfully);
_getFriendRequestsList();
} catch (_) {
IMViews.showToast(StrRes.rejectFailed);
}
}
}