init
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,335 @@
|
||||
// ignore_for_file: unnecessary_getters_setters
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/enum/V2TimConversationListener.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_callback.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_callback.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_conversation.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_search_param.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_search_param.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/tencent_im_sdk_plugin.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/life_cycle/conversation_life_cycle.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_chat_global_model.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_self_info_view_model.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/conversation/conversation_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/friendShip/friendship_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/message/message_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/ui/utils/platform.dart';
|
||||
|
||||
List<T> removeDuplicates<T>(List<T> list, bool Function(T first, T second) isEqual) {
|
||||
List<T> output = [];
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
bool found = false;
|
||||
for (var j = 0; j < output.length; j++) {
|
||||
if (isEqual(list[i], output[j])) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
output.add(list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
class TUIConversationViewModel extends ChangeNotifier {
|
||||
static const String conversationC2CPrefix = "c2c_";
|
||||
static const String conversationGroupPrefix = "group_";
|
||||
|
||||
final TUISelfInfoViewModel selfInfoViewModel = serviceLocator<TUISelfInfoViewModel>();
|
||||
final ConversationService _conversationService = serviceLocator<ConversationService>();
|
||||
final FriendshipServices _friendshipServices = serviceLocator<FriendshipServices>();
|
||||
final TUIChatGlobalModel _chatGlobalModel = serviceLocator<TUIChatGlobalModel>();
|
||||
final MessageService _messageService = serviceLocator<MessageService>();
|
||||
late V2TimConversationListener _conversationListener;
|
||||
List<V2TimConversation?> _conversationList = [];
|
||||
static V2TimConversation? _selectedConversation;
|
||||
Map<String, String> webDraftMap = {};
|
||||
|
||||
bool _haveMoreData = true;
|
||||
int _totalUnReadCount = 0;
|
||||
String? _scrollToConversation;
|
||||
final TUIChatGlobalModel globalChatModel = serviceLocator<TUIChatGlobalModel>();
|
||||
|
||||
String _nextSeq = "0";
|
||||
ConversationLifeCycle? _lifeCycle;
|
||||
|
||||
List<V2TimConversation?> get conversationList {
|
||||
if (PlatformUtils().isWeb) {
|
||||
try {
|
||||
_conversationList.sort((a, b) {
|
||||
return b!.lastMessage!.timestamp!.compareTo(a!.lastMessage!.timestamp!);
|
||||
});
|
||||
|
||||
final pinnedConversation = _conversationList.where((element) => element?.isPinned == true).toList();
|
||||
_conversationList.removeWhere((element) => element?.isPinned == true);
|
||||
_conversationList = [...pinnedConversation, ..._conversationList];
|
||||
// ignore: empty_catches
|
||||
} catch (e) {}
|
||||
} else {
|
||||
_conversationList.sort((a, b) => b!.orderkey!.compareTo(a!.orderkey!));
|
||||
}
|
||||
return _conversationList;
|
||||
}
|
||||
|
||||
V2TimConversation? getConversation(String conversationID) {
|
||||
return _conversationList.firstWhereOrNull((element) => element?.conversationID == conversationID);
|
||||
}
|
||||
|
||||
String? get scrollToConversation => _scrollToConversation;
|
||||
|
||||
set scrollToConversation(String? value) {
|
||||
_scrollToConversation = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearScrollToConversation() {
|
||||
_scrollToConversation = null;
|
||||
}
|
||||
|
||||
bool get haveMoreData {
|
||||
return _haveMoreData;
|
||||
}
|
||||
|
||||
int get totalUnReadCount => _totalUnReadCount;
|
||||
|
||||
set totalUnReadCount(int value) {
|
||||
_totalUnReadCount = value;
|
||||
}
|
||||
|
||||
set lifeCycle(ConversationLifeCycle? value) {
|
||||
_lifeCycle = value;
|
||||
}
|
||||
|
||||
set conversationList(List<V2TimConversation?> conversationList) {
|
||||
_conversationList = conversationList;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set selectedConversation(V2TimConversation? value) {
|
||||
_selectedConversation = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
V2TimConversation? get selectedConversation {
|
||||
return _selectedConversation;
|
||||
}
|
||||
|
||||
static V2TimConversation? of() {
|
||||
return _selectedConversation;
|
||||
}
|
||||
|
||||
TUIConversationViewModel() {
|
||||
_conversationListener = V2TimConversationListener(onConversationChanged: (conversationList) {
|
||||
_onConversationListChanged(conversationList);
|
||||
}, onNewConversation: (conversationList) {
|
||||
_addNewConversation(conversationList);
|
||||
}, onTotalUnreadMessageCountChanged: (totalUnread) {
|
||||
_totalUnReadCount = totalUnread;
|
||||
_chatGlobalModel.totalUnReadCount = totalUnread;
|
||||
notifyListeners();
|
||||
}, onSyncServerFinish: () {
|
||||
// Remove the process to load such a many of conversations after launching
|
||||
if (!PlatformUtils().isWeb) {
|
||||
loadInitConversation();
|
||||
}
|
||||
}, onConversationDeleted: (List<String> conversationIDList) {
|
||||
_onConversationDeleted(conversationIDList);
|
||||
for (var conversationID in conversationIDList) {
|
||||
String resultID = "";
|
||||
if (conversationID.startsWith(conversationC2CPrefix)) {
|
||||
resultID = conversationID.replaceFirst(conversationC2CPrefix, "");
|
||||
} else if (conversationID.startsWith(conversationGroupPrefix)) {
|
||||
resultID = conversationID.replaceFirst(conversationGroupPrefix, "");
|
||||
}
|
||||
|
||||
if (resultID != "") {
|
||||
_chatGlobalModel.removeMessageList(resultID);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
loadInitConversation() async {
|
||||
await loadData(count: 40);
|
||||
// Remove the process to load such a many of conversations after launching
|
||||
// if (selfInfoViewModel.globalConfig?.isPreloadMessagesAfterInit ?? true) {
|
||||
// _chatGlobalModel.initMessageMapFromLocalDatabase(_conversationList);
|
||||
// }
|
||||
}
|
||||
|
||||
initConversation() async {
|
||||
clearData();
|
||||
loadInitConversation();
|
||||
}
|
||||
|
||||
Future<void> loadData({required int count}) async {
|
||||
_haveMoreData = true;
|
||||
final isRefresh = _nextSeq == "0";
|
||||
final conversationResult = await _conversationService.getConversationList(nextSeq: _nextSeq, count: count);
|
||||
_nextSeq = conversationResult?.nextSeq ?? "";
|
||||
final conversationList = conversationResult?.conversationList;
|
||||
if (conversationList != null) {
|
||||
if (conversationList.isEmpty || conversationList.length < count) {
|
||||
_haveMoreData = false;
|
||||
}
|
||||
List<V2TimConversation?> combinedConversationList = [];
|
||||
if (isRefresh) {
|
||||
combinedConversationList = conversationList;
|
||||
} else {
|
||||
combinedConversationList = [..._conversationList, ...conversationList];
|
||||
}
|
||||
final List<V2TimConversation?> finalConversationList =
|
||||
await _lifeCycle?.conversationListWillMount(combinedConversationList) ?? combinedConversationList;
|
||||
_conversationList = removeDuplicates<V2TimConversation?>(
|
||||
finalConversationList, (item1, item2) => item1?.conversationID == item2?.conversationID);
|
||||
notifyListeners();
|
||||
}
|
||||
_totalUnReadCount = await _conversationService.getTotalUnreadCount();
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
void setSelectedConversation(V2TimConversation conversation) {
|
||||
_selectedConversation = conversation;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<V2TimCallback> pinConversation({
|
||||
required String conversationID,
|
||||
required bool isPinned,
|
||||
}) {
|
||||
return _conversationService.pinConversation(conversationID: conversationID, isPinned: isPinned);
|
||||
}
|
||||
|
||||
Future<V2TimCallback?> clearHistoryMessage({required String convID, required int convType}) async {
|
||||
if (_lifeCycle?.shouldClearHistoricalMessageForConversation != null &&
|
||||
await _lifeCycle!.shouldClearHistoricalMessageForConversation(convID) == false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
globalChatModel.setMessageList(convID, []);
|
||||
|
||||
if (convType == 1) {
|
||||
return _messageService.clearC2CHistoryMessage(userID: convID);
|
||||
} else {
|
||||
return _messageService.clearGroupHistoryMessage(groupID: convID);
|
||||
}
|
||||
}
|
||||
|
||||
searchFriends(String searchKey) async {
|
||||
final res = await _friendshipServices.searchFriends(searchParam: V2TimFriendSearchParam(keywordList: [searchKey]));
|
||||
return res;
|
||||
}
|
||||
|
||||
Future<V2TimCallback?> deleteConversation({required String conversationID}) async {
|
||||
if (_lifeCycle?.shouldDeleteConversation != null &&
|
||||
await _lifeCycle!.shouldDeleteConversation(conversationID) == false) {
|
||||
return null;
|
||||
}
|
||||
final res = await _conversationService.deleteConversation(conversationID: conversationID);
|
||||
if (res.code == 0) {
|
||||
_conversationList.removeWhere((element) => element?.conversationID == conversationID);
|
||||
notifyListeners();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
_onConversationListChanged(List<V2TimConversation> list) {
|
||||
for (int element = 0; element < list.length; element++) {
|
||||
int index = _conversationList.indexWhere((item) => item!.conversationID == list[element].conversationID);
|
||||
if (index > -1) {
|
||||
_conversationList.setAll(index, [list[element]] as List<V2TimConversation?>);
|
||||
} else {
|
||||
_conversationList.add(list[element]);
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
_onConversationDeleted(List<String> list) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
int index = _conversationList.indexWhere((item) => item!.conversationID == list[i]);
|
||||
if (index > -1) {
|
||||
_conversationList.removeAt(index);
|
||||
_conversationList = removeDuplicates<V2TimConversation?>(
|
||||
_conversationList, (item1, item2) => item1?.conversationID == item2?.conversationID);
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
_addNewConversation(List<V2TimConversation> list) {
|
||||
_conversationList.addAll(list);
|
||||
_conversationList = removeDuplicates<V2TimConversation?>(
|
||||
_conversationList, (item1, item2) => item1?.conversationID == item2?.conversationID);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
setConversationListener() {
|
||||
_conversationService.addConversationListener(listener: _conversationListener);
|
||||
}
|
||||
|
||||
removeConversationListener() {
|
||||
_conversationService.removeConversationListener(listener: _conversationListener);
|
||||
}
|
||||
|
||||
Future<V2TimCallback> setConversationDraft({
|
||||
required String conversationID,
|
||||
String? draftText,
|
||||
bool isTopic = false,
|
||||
String? groupID,
|
||||
bool isAllowWeb = true,
|
||||
}) async {
|
||||
assert(!isTopic || (groupID != null && groupID.isNotEmpty),
|
||||
"When 'isTopic' is true, 'groupID' must not be null or empty.");
|
||||
if (PlatformUtils().isWeb && isAllowWeb) {
|
||||
webDraftMap[conversationID] = draftText ?? "";
|
||||
return V2TimCallback(code: 0, desc: "");
|
||||
} else {
|
||||
if (isTopic) {
|
||||
final topicInfoList = await TencentImSDKPlugin.v2TIMManager
|
||||
.getGroupManager()
|
||||
.getTopicInfoList(groupID: groupID!, topicIDList: [conversationID]);
|
||||
final topicInfo = topicInfoList.data?.first.topicInfo;
|
||||
topicInfo?.draftText = draftText;
|
||||
final res = await TencentImSDKPlugin.v2TIMManager.getGroupManager().setTopicInfo(topicInfo: topicInfo!);
|
||||
return res;
|
||||
} else {
|
||||
return _conversationService.setConversationDraft(conversationID: conversationID, draftText: draftText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearWebDraft({
|
||||
required String conversationID,
|
||||
}) {
|
||||
webDraftMap[conversationID] = "";
|
||||
}
|
||||
|
||||
String? getWebDraft({
|
||||
required String conversationID,
|
||||
}) {
|
||||
return TencentUtils.checkString(webDraftMap[conversationID]);
|
||||
}
|
||||
|
||||
clearData() {
|
||||
_conversationList = [];
|
||||
_selectedConversation = null;
|
||||
_nextSeq = "0";
|
||||
_haveMoreData = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
refresh({int count = 100}) {
|
||||
_nextSeq = "0";
|
||||
_haveMoreData = true;
|
||||
loadData(count: count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/enum/V2TimFriendshipListener.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/enum/friend_application_type_enum.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/enum/friend_response_type_enum.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_application.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_application.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_info.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_info.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_operation_result.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_operation_result.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_info.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_group_info.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_member_full_info.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_group_member_full_info.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_user_status.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_user_status.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/life_cycle/block_list_life_cycle.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/life_cycle/friend_list_life_cycle.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/life_cycle/new_contact_life_cycle.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_self_info_view_model.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/friendShip/friendship_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/group/group_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
|
||||
|
||||
class TUIFriendShipViewModel extends ChangeNotifier {
|
||||
final FriendshipServices _friendshipServices = serviceLocator<FriendshipServices>();
|
||||
final GroupServices _groupServices = serviceLocator<GroupServices>();
|
||||
final TUISelfInfoViewModel selfInfoViewModel = serviceLocator<TUISelfInfoViewModel>();
|
||||
late V2TimFriendshipListener friendShipListener;
|
||||
List<V2TimFriendApplication?>? _friendApplicationList;
|
||||
List<V2TimFriendInfo>? _friendList;
|
||||
List<V2TimGroupInfo>? _groupList;
|
||||
List<V2TimUserStatus>? _userStatusList;
|
||||
int _friendApplicationAmount = 0;
|
||||
List<V2TimFriendInfo>? _blockList;
|
||||
NewContactLifeCycle? _newContactLifeCycle;
|
||||
FriendListLifeCycle? _contactListLifeCycle;
|
||||
BlockListLifeCycle? _blockListLifeCycle;
|
||||
|
||||
set newContactLifeCycle(NewContactLifeCycle? value) {
|
||||
_newContactLifeCycle = value;
|
||||
}
|
||||
|
||||
set contactListLifeCycle(FriendListLifeCycle? value) {
|
||||
_contactListLifeCycle = value;
|
||||
}
|
||||
|
||||
set blockListLifeCycle(BlockListLifeCycle? value) {
|
||||
_blockListLifeCycle = value;
|
||||
}
|
||||
|
||||
set userStatusList(List<V2TimUserStatus> value) {
|
||||
_userStatusList = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<V2TimUserStatus> get userStatusList => _userStatusList ?? [];
|
||||
|
||||
List<V2TimFriendInfo> get blockList {
|
||||
return _blockList ?? [];
|
||||
}
|
||||
|
||||
List<V2TimGroupInfo> get groupList {
|
||||
return _groupList ?? [];
|
||||
}
|
||||
|
||||
List<V2TimFriendInfo>? get friendList {
|
||||
return _friendList;
|
||||
}
|
||||
|
||||
int get friendApplicationAmount => _friendApplicationAmount;
|
||||
|
||||
List<V2TimFriendApplication?>? get friendApplicationList => _friendApplicationList;
|
||||
|
||||
TUIFriendShipViewModel() {
|
||||
friendShipListener = V2TimFriendshipListener(
|
||||
onFriendApplicationListAdded: (applicationList) {
|
||||
loadContactApplicationData();
|
||||
},
|
||||
onFriendApplicationListDeleted: (userIDList) {
|
||||
loadContactApplicationData();
|
||||
},
|
||||
onFriendApplicationListRead: () {
|
||||
loadContactApplicationData();
|
||||
},
|
||||
onFriendInfoChanged: (infoList) {
|
||||
loadContactListData();
|
||||
},
|
||||
onFriendListAdded: (users) async {
|
||||
await loadContactListData();
|
||||
loadUserStatus();
|
||||
},
|
||||
onFriendListDeleted: (userList) async {
|
||||
await loadContactListData();
|
||||
loadUserStatus();
|
||||
},
|
||||
onBlackListAdd: (infoList) async {
|
||||
await loadBlockListData();
|
||||
loadUserStatus();
|
||||
},
|
||||
onBlackListDeleted: (userList) async {
|
||||
await loadBlockListData();
|
||||
loadUserStatus();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
initFriendShipModel() {
|
||||
loadData();
|
||||
}
|
||||
|
||||
loadData() async {
|
||||
loadContactApplicationData();
|
||||
loadBlockListData();
|
||||
await loadContactListData();
|
||||
loadUserStatus();
|
||||
}
|
||||
|
||||
clearData() {
|
||||
_friendApplicationList = [];
|
||||
_friendApplicationAmount = 0;
|
||||
_friendList = [];
|
||||
_userStatusList = [];
|
||||
_blockList = [];
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
loadUserStatus() async {
|
||||
if (selfInfoViewModel.globalConfig?.isShowOnlineStatus == false || friendList == null || friendList!.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<List<String>> userIDSet = [];
|
||||
final int needHowManyRequest = ((friendList!.length) / 500).ceil();
|
||||
final int amountEachRequest = ((friendList!.length) / needHowManyRequest).ceil();
|
||||
|
||||
for (int i = 0; i < needHowManyRequest; i++) {
|
||||
userIDSet.add(friendList!
|
||||
.getRange(i * amountEachRequest, min(friendList!.length, (i + 1) * amountEachRequest))
|
||||
.map((e) => e.userID)
|
||||
.toList());
|
||||
}
|
||||
|
||||
final List<List<V2TimUserStatus>> userStatus = await Future.wait([
|
||||
...userIDSet.map((userIDList) async {
|
||||
return await _friendshipServices.getUserStatus(userIDList: userIDList);
|
||||
})
|
||||
]);
|
||||
|
||||
final List<V2TimUserStatus> flatUserStatus = [];
|
||||
for (var e in userStatus) {
|
||||
flatUserStatus.addAll(e);
|
||||
}
|
||||
userStatusList = flatUserStatus;
|
||||
}
|
||||
|
||||
loadContactApplicationData() async {
|
||||
final newContactRes = await _friendshipServices.getFriendApplicationList();
|
||||
// Only Received Application
|
||||
_friendApplicationList = newContactRes?.friendApplicationList
|
||||
?.where((item) => item!.type == FriendApplicationTypeEnum.V2TIM_FRIEND_APPLICATION_COME_IN.index)
|
||||
.toList();
|
||||
_friendApplicationAmount = _friendApplicationList?.length ?? 0;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> loadContactListData() async {
|
||||
final List<V2TimFriendInfo> res = await _friendshipServices.getFriendList() ?? [];
|
||||
final memberList = await _contactListLifeCycle?.friendListWillMount(res) ?? res;
|
||||
_friendList = memberList;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
Future<bool> isFriend(String userID) async {
|
||||
final List<V2TimFriendInfo> res = await _friendshipServices.getFriendList() ?? [];
|
||||
for (V2TimFriendInfo info in res) {
|
||||
if (info.userID == userID) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> loadBlockListData() async {
|
||||
final blockListRes = await _friendshipServices.getBlackList();
|
||||
_blockList = blockListRes ?? [];
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
loadGroupListData() async {
|
||||
final groupListRes = await _groupServices.getJoinedGroupList();
|
||||
_groupList = groupListRes ?? [];
|
||||
if (_groupList != null && _groupList!.isNotEmpty) {
|
||||
notifyListeners();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Future<List<V2TimFriendOperationResult>?> deleteFromBlockList(List<String> userIDList) async {
|
||||
if (_blockListLifeCycle?.shouldDeleteFromBlockList != null &&
|
||||
await _blockListLifeCycle!.shouldDeleteFromBlockList(userIDList) == false) {
|
||||
return null;
|
||||
}
|
||||
final res = await _friendshipServices.deleteFromBlackList(userIDList: userIDList);
|
||||
if (res != null) {
|
||||
return res;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<V2TimFriendOperationResult?> acceptFriendApplication(
|
||||
String userID,
|
||||
int type,
|
||||
) async {
|
||||
if (_newContactLifeCycle?.shouldAcceptContactApplication != null &&
|
||||
await _newContactLifeCycle!.shouldAcceptContactApplication(userID) == false) {
|
||||
return null;
|
||||
}
|
||||
final res = await _friendshipServices.acceptFriendApplication(
|
||||
responseType: FriendResponseTypeEnum.V2TIM_FRIEND_ACCEPT_AGREE_AND_ADD,
|
||||
type: FriendApplicationTypeEnum.values[type],
|
||||
userID: userID,
|
||||
);
|
||||
if (res != null) {
|
||||
return res;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<V2TimFriendOperationResult?> refuseFriendApplication(
|
||||
String userID,
|
||||
int type,
|
||||
) async {
|
||||
if (_newContactLifeCycle?.shouldRefuseContactApplication != null &&
|
||||
await _newContactLifeCycle!.shouldRefuseContactApplication(userID) == false) {
|
||||
return null;
|
||||
}
|
||||
final res = await _friendshipServices.refuseFriendApplication(
|
||||
type: FriendApplicationTypeEnum.values[type],
|
||||
userID: userID,
|
||||
);
|
||||
if (res != null) {
|
||||
return res;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<V2TimGroupMemberFullInfo?>> getGroupMembersInfo(
|
||||
{required String groupID, required List<String> memberList}) async {
|
||||
final res = await _groupServices.getGroupMembersInfo(groupID: groupID, memberList: memberList);
|
||||
return res.data ?? [];
|
||||
}
|
||||
|
||||
addFriendListener({V2TimFriendshipListener? listener}) {
|
||||
_friendshipServices.addFriendListener(listener: friendShipListener);
|
||||
}
|
||||
|
||||
removeFriendshipListener({V2TimFriendshipListener? listener}) {
|
||||
_friendshipServices.removeFriendListener(listener: friendShipListener);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// ignore_for_file: constant_identifier_names
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_conversation.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_info_result.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_info_result.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_friend_search_param.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_friend_search_param.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_info.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_group_info.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_search_param.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_group_search_param.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_message.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message_search_param.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_message_search_param.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message_search_result_item.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_message_search_result_item.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/friendShip/friendship_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/message/message_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/conversation/conversation_services.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/group/group_services.dart';
|
||||
|
||||
enum KeywordListMatchType { V2TIM_KEYWORD_LIST_MATCH_TYPE_OR, V2TIM_KEYWORD_LIST_MATCH_TYPE_AND }
|
||||
|
||||
class TUISearchViewModel extends ChangeNotifier {
|
||||
final FriendshipServices _friendshipServices = serviceLocator<FriendshipServices>();
|
||||
final MessageService _messageService = serviceLocator<MessageService>();
|
||||
final ConversationService _conversationService = serviceLocator<ConversationService>();
|
||||
final GroupServices _groupServices = serviceLocator<GroupServices>();
|
||||
|
||||
List<V2TimFriendInfoResult>? friendList = [];
|
||||
|
||||
List<V2TimMessageSearchResultItem>? msgList = [];
|
||||
int msgPage = 0;
|
||||
int totalMsgCount = 0;
|
||||
|
||||
int totalMsgInConversationCount = 0;
|
||||
List<V2TimMessage> currentMsgListForConversation = [];
|
||||
|
||||
List<V2TimGroupInfo>? groupList = [];
|
||||
|
||||
List<V2TimConversation?> conversationList = [];
|
||||
|
||||
Future<List<V2TimConversation?>?> initConversationMsg() async {
|
||||
final conversationResult = await _conversationService.getConversationList(nextSeq: "0", count: 500);
|
||||
final conversationListData = conversationResult?.conversationList;
|
||||
conversationList = conversationListData ?? [];
|
||||
notifyListeners();
|
||||
return conversationListData;
|
||||
}
|
||||
|
||||
void initSearch() {
|
||||
friendList = [];
|
||||
msgList = [];
|
||||
groupList = [];
|
||||
totalMsgCount = 0;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void searchFriendByKey(String searchKey) async {
|
||||
final searchResult =
|
||||
await _friendshipServices.searchFriends(searchParam: V2TimFriendSearchParam(keywordList: [searchKey]));
|
||||
friendList = searchResult;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void searchGroupByKey(String searchKey) async {
|
||||
final searchResult =
|
||||
await _groupServices.searchGroups(searchParam: V2TimGroupSearchParam(keywordList: [searchKey]));
|
||||
groupList = searchResult.data ?? [];
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void getMsgForConversation(String keyword, String conversationId, int page) async {
|
||||
void clearData() {
|
||||
currentMsgListForConversation = [];
|
||||
totalMsgInConversationCount = 0;
|
||||
}
|
||||
|
||||
if (page == 0) {
|
||||
clearData();
|
||||
}
|
||||
if (keyword.isEmpty) {
|
||||
clearData();
|
||||
return;
|
||||
}
|
||||
final searchResult = await _messageService.searchLocalMessages(
|
||||
searchParam: V2TimMessageSearchParam(
|
||||
keywordList: [keyword],
|
||||
pageIndex: page,
|
||||
pageSize: 30,
|
||||
searchTimePeriod: 0,
|
||||
searchTimePosition: 0,
|
||||
conversationID: conversationId,
|
||||
type: KeywordListMatchType.V2TIM_KEYWORD_LIST_MATCH_TYPE_OR.index,
|
||||
));
|
||||
if (searchResult.code == 0 && searchResult.data != null) {
|
||||
final messageSearchResultItems = searchResult.data!.messageSearchResultItems!
|
||||
.firstWhereOrNull((element) => element.conversationID == conversationId);
|
||||
totalMsgInConversationCount = messageSearchResultItems?.messageCount ?? 0;
|
||||
currentMsgListForConversation = [
|
||||
...currentMsgListForConversation,
|
||||
...(messageSearchResultItems?.messageList ?? [])
|
||||
];
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void searchMsgByKey(String searchKey, bool isFirst) async {
|
||||
if (isFirst == true) {
|
||||
msgPage = 0;
|
||||
msgList = [];
|
||||
totalMsgCount = 0;
|
||||
}
|
||||
final searchResult = await _messageService.searchLocalMessages(
|
||||
searchParam: V2TimMessageSearchParam(
|
||||
keywordList: [searchKey],
|
||||
pageIndex: msgPage,
|
||||
pageSize: 5,
|
||||
searchTimePeriod: 0,
|
||||
searchTimePosition: 0,
|
||||
type: KeywordListMatchType.V2TIM_KEYWORD_LIST_MATCH_TYPE_OR.index,
|
||||
));
|
||||
if (searchResult.code == 0 && searchResult.data != null) {
|
||||
msgPage++;
|
||||
msgList = [...?msgList, ...?searchResult.data!.messageSearchResultItems];
|
||||
totalMsgCount = searchResult.data!.totalCount ?? 0;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void searchByKey(String? searchKey) async {
|
||||
if (searchKey == null || searchKey.isEmpty) {
|
||||
friendList = [];
|
||||
groupList = [];
|
||||
msgList = [];
|
||||
totalMsgCount = 0;
|
||||
notifyListeners();
|
||||
} else {
|
||||
searchFriendByKey(searchKey);
|
||||
searchMsgByKey(searchKey, true);
|
||||
searchGroupByKey(searchKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_user_full_info.dart'
|
||||
if (dart.library.html) 'package:tencent_cloud_chat_sdk/web/compatible_models/v2_tim_user_full_info.dart';
|
||||
import 'package:tencent_cloud_chat_uikit/data_services/core/tim_uikit_config.dart';
|
||||
|
||||
class TUISelfInfoViewModel extends ChangeNotifier {
|
||||
V2TimUserFullInfo? _loginInfo;
|
||||
TIMUIKitConfig? _globalConfig;
|
||||
|
||||
TIMUIKitConfig? get globalConfig => _globalConfig;
|
||||
|
||||
set globalConfig(TIMUIKitConfig? value) {
|
||||
_globalConfig = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
V2TimUserFullInfo? get loginInfo {
|
||||
return _loginInfo;
|
||||
}
|
||||
|
||||
setLoginInfo(V2TimUserFullInfo? value) {
|
||||
_loginInfo = value;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class TUISettingModel extends ChangeNotifier {
|
||||
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
||||
final String keyTitle = "tencent_chat_uikit_";
|
||||
|
||||
double? _keyboardHeight;
|
||||
double get keyboardHeight => _keyboardHeight ?? 0;
|
||||
set keyboardHeight(double value) {
|
||||
if(value > 40 && _keyboardHeight != value){
|
||||
_keyboardHeight = value;
|
||||
updateLocalSetting("${keyTitle}keyboardHeight", value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
updateLocalSetting(String key, String value) async {
|
||||
SharedPreferences prefs = await _prefs;
|
||||
prefs.setString(key, value);
|
||||
}
|
||||
|
||||
init() async {
|
||||
SharedPreferences prefs = await _prefs;
|
||||
_keyboardHeight = double.parse(prefs.getString("${keyTitle}keyboardHeight") ?? "0");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user