TUIConversationSelectBaseDataProvider.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // TUIConversationSelectBaseDataProvider.m
  3. // TXIMSDK_TUIKit_iOS
  4. //
  5. // Created by xiangzhang on 2021/6/25.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIConversationSelectBaseDataProvider.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. @interface TUIConversationSelectBaseDataProvider ()
  11. @property(nonatomic, strong) NSMutableArray<V2TIMConversation *> *localConvList;
  12. @end
  13. @implementation TUIConversationSelectBaseDataProvider
  14. - (void)loadConversations {
  15. __weak typeof(self) weakSelf = self;
  16. [[V2TIMManager sharedInstance] getConversationList:0
  17. count:INT_MAX
  18. succ:^(NSArray<V2TIMConversation *> *list, uint64_t lastTS, BOOL isFinished) {
  19. __strong typeof(weakSelf) strongSelf = weakSelf;
  20. [strongSelf updateConversation:list];
  21. }
  22. fail:^(int code, NSString *msg) {
  23. NSLog(@"getConversationList failed");
  24. }];
  25. }
  26. - (void)updateConversation:(NSArray *)convList {
  27. /**
  28. * 更新 UI 会话列表,如果 UI 会话列表有新增的会话,就替换,如果没有,就新增
  29. * Update the conversation list on the UI, if it is an existing conversation, replace it, otherwise add it
  30. */
  31. for (int i = 0; i < convList.count; ++i) {
  32. V2TIMConversation *conv = convList[i];
  33. BOOL isExit = NO;
  34. for (int j = 0; j < self.localConvList.count; ++j) {
  35. V2TIMConversation *localConv = self.localConvList[j];
  36. if ([localConv.conversationID isEqualToString:conv.conversationID]) {
  37. [self.localConvList replaceObjectAtIndex:j withObject:conv];
  38. isExit = YES;
  39. break;
  40. }
  41. }
  42. if (!isExit) {
  43. [self.localConvList addObject:conv];
  44. }
  45. }
  46. NSMutableArray *dataList = [NSMutableArray array];
  47. for (V2TIMConversation *conv in self.localConvList) {
  48. if ([self filteConversation:conv]) {
  49. continue;
  50. }
  51. Class cls = [self getConversationCellClass];
  52. if (cls) {
  53. TUIConversationCellData *data = (TUIConversationCellData *)[[cls alloc] init];
  54. data.conversationID = conv.conversationID;
  55. data.groupID = conv.groupID;
  56. data.userID = conv.userID;
  57. data.title = conv.showName;
  58. data.faceUrl = conv.faceUrl;
  59. data.unreadCount = 0;
  60. data.draftText = @"";
  61. data.subTitle = [[NSMutableAttributedString alloc] initWithString:@""];
  62. if (conv.type == V2TIM_C2C) {
  63. data.avatarImage = DefaultAvatarImage;
  64. } else {
  65. data.avatarImage = DefaultGroupAvatarImageByGroupType(conv.groupType);
  66. }
  67. [dataList addObject:data];
  68. }
  69. }
  70. [self sortDataList:dataList];
  71. self.dataList = dataList;
  72. }
  73. - (BOOL)filteConversation:(V2TIMConversation *)conv {
  74. if ([conv.groupType isEqualToString:@"AVChatRoom"]) {
  75. return YES;
  76. }
  77. return NO;
  78. }
  79. - (void)sortDataList:(NSMutableArray<TUIConversationCellData *> *)dataList {
  80. /**
  81. * 按时间排序,最近会话在上
  82. * Sorted by time, the latest conversation is at the top of the conversation list
  83. */
  84. [dataList sortUsingComparator:^NSComparisonResult(TUIConversationCellData *obj1, TUIConversationCellData *obj2) {
  85. return [obj2.time compare:obj1.time];
  86. }];
  87. /**
  88. * 将置顶会话固定在最上面
  89. * Pinned conversations are at the top of the conversation list
  90. */
  91. NSArray *topList = [[TUIConversationPin sharedInstance] topConversationList];
  92. int existTopListSize = 0;
  93. for (NSString *convID in topList) {
  94. int userIdx = -1;
  95. for (int i = 0; i < dataList.count; i++) {
  96. if ([dataList[i].conversationID isEqualToString:convID]) {
  97. userIdx = i;
  98. dataList[i].isOnTop = YES;
  99. break;
  100. }
  101. }
  102. if (userIdx >= 0 && userIdx != existTopListSize) {
  103. TUIConversationCellData *data = dataList[userIdx];
  104. [dataList removeObjectAtIndex:userIdx];
  105. [dataList insertObject:data atIndex:existTopListSize];
  106. existTopListSize++;
  107. }
  108. }
  109. }
  110. - (NSArray<TUIConversationCellData *> *)dataList {
  111. if (_dataList == nil) {
  112. _dataList = [NSMutableArray array];
  113. }
  114. return _dataList;
  115. }
  116. - (NSMutableArray<V2TIMConversation *> *)localConvList {
  117. if (_localConvList == nil) {
  118. _localConvList = [NSMutableArray array];
  119. }
  120. return _localConvList;
  121. }
  122. #pragma mark Override func
  123. - (Class)getConversationCellClass {
  124. // subclass override
  125. return nil;
  126. }
  127. @end