TUIConversationSelectBaseDataProvider.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Update the conversation list on the UI, if it is an existing conversation, replace it, otherwise add it
  29. */
  30. for (int i = 0; i < convList.count; ++i) {
  31. V2TIMConversation *conv = convList[i];
  32. BOOL isExit = NO;
  33. for (int j = 0; j < self.localConvList.count; ++j) {
  34. V2TIMConversation *localConv = self.localConvList[j];
  35. if ([localConv.conversationID isEqualToString:conv.conversationID]) {
  36. [self.localConvList replaceObjectAtIndex:j withObject:conv];
  37. isExit = YES;
  38. break;
  39. }
  40. }
  41. if (!isExit) {
  42. [self.localConvList addObject:conv];
  43. }
  44. }
  45. NSMutableArray *dataList = [NSMutableArray array];
  46. for (V2TIMConversation *conv in self.localConvList) {
  47. if ([self filteConversation:conv]) {
  48. continue;
  49. }
  50. Class cls = [self getConversationCellClass];
  51. if (cls) {
  52. TUIConversationCellData *data = (TUIConversationCellData *)[[cls alloc] init];
  53. data.conversationID = conv.conversationID;
  54. data.groupID = conv.groupID;
  55. data.userID = conv.userID;
  56. data.title = conv.showName;
  57. data.faceUrl = conv.faceUrl;
  58. data.unreadCount = 0;
  59. data.draftText = @"";
  60. data.subTitle = [[NSMutableAttributedString alloc] initWithString:@""];
  61. if (conv.type == V2TIM_C2C) {
  62. data.avatarImage = DefaultAvatarImage;
  63. } else {
  64. data.avatarImage = DefaultGroupAvatarImageByGroupType(conv.groupType);
  65. }
  66. [dataList addObject:data];
  67. }
  68. }
  69. [self sortDataList:dataList];
  70. self.dataList = dataList;
  71. }
  72. - (BOOL)filteConversation:(V2TIMConversation *)conv {
  73. if ([conv.groupType isEqualToString:@"AVChatRoom"]) {
  74. return YES;
  75. }
  76. return NO;
  77. }
  78. - (void)sortDataList:(NSMutableArray<TUIConversationCellData *> *)dataList {
  79. /**
  80. * Sorted by time, the latest conversation is at the top of the conversation list
  81. */
  82. [dataList sortUsingComparator:^NSComparisonResult(TUIConversationCellData *obj1, TUIConversationCellData *obj2) {
  83. return [obj2.time compare:obj1.time];
  84. }];
  85. /**
  86. * Pinned conversations are at the top of the conversation list
  87. */
  88. NSArray *topList = [[TUIConversationPin sharedInstance] topConversationList];
  89. int existTopListSize = 0;
  90. for (NSString *convID in topList) {
  91. int userIdx = -1;
  92. for (int i = 0; i < dataList.count; i++) {
  93. if ([dataList[i].conversationID isEqualToString:convID]) {
  94. userIdx = i;
  95. dataList[i].isOnTop = YES;
  96. break;
  97. }
  98. }
  99. if (userIdx >= 0 && userIdx != existTopListSize) {
  100. TUIConversationCellData *data = dataList[userIdx];
  101. [dataList removeObjectAtIndex:userIdx];
  102. [dataList insertObject:data atIndex:existTopListSize];
  103. existTopListSize++;
  104. }
  105. }
  106. }
  107. - (NSArray<TUIConversationCellData *> *)dataList {
  108. if (_dataList == nil) {
  109. _dataList = [NSMutableArray array];
  110. }
  111. return _dataList;
  112. }
  113. - (NSMutableArray<V2TIMConversation *> *)localConvList {
  114. if (_localConvList == nil) {
  115. _localConvList = [NSMutableArray array];
  116. }
  117. return _localConvList;
  118. }
  119. #pragma mark Override func
  120. - (Class)getConversationCellClass {
  121. // subclass override
  122. return nil;
  123. }
  124. @end