TUIMergeMessageListController.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //
  2. // TUIMergeMessageListController.m
  3. // Pods
  4. //
  5. // Created by harvy on 2020/12/9.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIMergeMessageListController.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TIMCommon/TUISystemMessageCell.h>
  11. #import <TUICore/TUICore.h>
  12. #import <TUICore/TUIDarkModel.h>
  13. #import <TUICore/TUIGlobalization.h>
  14. #import <TUICore/TUIThemeManager.h>
  15. #import "TUIFaceMessageCell.h"
  16. #import "TUIFileMessageCell.h"
  17. #import "TUIFileViewController.h"
  18. #import "TUIImageMessageCell.h"
  19. #import "TUIJoinGroupMessageCell.h"
  20. #import "TUILinkCell.h"
  21. #import "TUIMediaView.h"
  22. #import "TUIMergeMessageCell.h"
  23. #import "TUIMessageDataProvider.h"
  24. #import "TUIMessageSearchDataProvider.h"
  25. #import "TUIReferenceMessageCell.h"
  26. #import "TUIRepliesDetailViewController.h"
  27. #import "TUIReplyMessageCell.h"
  28. #import "TUIReplyMessageCellData.h"
  29. #import "TUITextMessageCell.h"
  30. #import "TUIVideoMessageCell.h"
  31. #import "TUIVoiceMessageCell.h"
  32. #import "TUIMessageCellConfig.h"
  33. #import "TUIChatConfig.h"
  34. #define STR(x) @ #x
  35. @interface TUIMergeMessageListController () <TUIMessageCellDelegate, TUIMessageBaseDataProviderDataSource,TUINotificationProtocol>
  36. @property(nonatomic, strong) NSArray<V2TIMMessage *> *imMsgs;
  37. @property(nonatomic, strong) NSMutableArray<TUIMessageCellData *> *uiMsgs;
  38. @property(nonatomic, strong) NSMutableDictionary *stylesCache;
  39. @property(nonatomic, strong) TUIMessageSearchDataProvider *msgDataProvider;
  40. @property(nonatomic, strong) TUIMessageCellConfig *messageCellConfig;
  41. @end
  42. @implementation TUIMergeMessageListController
  43. - (instancetype)init {
  44. self = [super init];
  45. if (self) {
  46. [TUICore registerEvent:TUICore_TUIPluginNotify
  47. subKey:TUICore_TUIPluginNotify_DidChangePluginViewSubKey
  48. object:self];
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. _uiMsgs = [[NSMutableArray alloc] init];
  55. [self loadMessages];
  56. [self setupViews];
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated {
  59. [self updateCellStyle:YES];
  60. if (self.willCloseCallback) {
  61. self.willCloseCallback();
  62. }
  63. }
  64. - (void)updateCellStyle:(BOOL)recover {
  65. if (recover) {
  66. TUIMessageCellLayout.incommingMessageLayout.avatarInsets = UIEdgeInsetsFromString(self.stylesCache[STR(incomingAvatarInsets)]);
  67. TUIMessageCellLayout.incommingTextMessageLayout.avatarInsets = UIEdgeInsetsFromString(self.stylesCache[STR(incomingAvatarInsets)]);
  68. TUIMessageCellLayout.incommingVoiceMessageLayout.avatarInsets = UIEdgeInsetsFromString(self.stylesCache[STR(incomingAvatarInsets)]);
  69. TUIMessageCellLayout.incommingMessageLayout.messageInsets = UIEdgeInsetsFromString(self.stylesCache[STR(incommingMessageInsets)]);
  70. [TUITextMessageCell setOutgoingTextColor:self.stylesCache[STR(outgoingTextColor)]];
  71. [TUITextMessageCell setIncommingTextColor:self.stylesCache[STR(incomingTextColor)]];
  72. return;
  73. }
  74. UIEdgeInsets incomingAvatarInsets = TUIMessageCellLayout.incommingTextMessageLayout.avatarInsets;
  75. TUIMessageCellLayout.incommingMessageLayout.avatarInsets = UIEdgeInsetsMake(0, 10, 0, 10);
  76. TUIMessageCellLayout.incommingTextMessageLayout.avatarInsets = UIEdgeInsetsMake(0, 10, 0, 10);
  77. TUIMessageCellLayout.incommingVoiceMessageLayout.avatarInsets = UIEdgeInsetsMake(0, 10, 0, 10);
  78. [self.stylesCache setObject:NSStringFromUIEdgeInsets(incomingAvatarInsets) forKey:STR(incomingAvatarInsets)];
  79. UIEdgeInsets incommingMessageInsets = TUIMessageCellLayout.incommingMessageLayout.messageInsets;
  80. TUIMessageCellLayout.incommingMessageLayout.messageInsets = UIEdgeInsetsMake(5, 5, 0, 0);
  81. [self.stylesCache setObject:NSStringFromUIEdgeInsets(incommingMessageInsets) forKey:STR(incommingMessageInsets)];
  82. UIColor *outgoingTextColor = [TUITextMessageCell outgoingTextColor];
  83. [TUITextMessageCell setOutgoingTextColor:TUIChatDynamicColor(@"chat_text_message_send_text_color", @"#000000")];
  84. [self.stylesCache setObject:outgoingTextColor forKey:STR(outgoingTextColor)];
  85. UIColor *incomingTextColor = [TUITextMessageCell incommingTextColor];
  86. [TUITextMessageCell setIncommingTextColor:TUIChatDynamicColor(@"chat_text_message_receive_text_color", @"#000000")];
  87. [self.stylesCache setObject:incomingTextColor forKey:STR(incomingTextColor)];
  88. }
  89. - (void)loadMessages {
  90. @weakify(self);
  91. [self.mergerElem
  92. downloadMergerMessage:^(NSArray<V2TIMMessage *> *msgs) {
  93. @strongify(self);
  94. self.imMsgs = msgs;
  95. [self updateCellStyle:NO];
  96. [self getMessages:self.imMsgs];
  97. }
  98. fail:^(int code, NSString *desc) {
  99. [self updateCellStyle:NO];
  100. }];
  101. }
  102. - (void)getMessages:(NSArray *)msgs {
  103. NSMutableArray *uiMsgs = [self transUIMsgFromIMMsg:msgs];
  104. @weakify(self);
  105. [self.msgDataProvider preProcessMessage:uiMsgs
  106. callback:^{
  107. @strongify(self);
  108. @weakify(self);
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. @strongify(self);
  111. if (uiMsgs.count != 0) {
  112. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, uiMsgs.count)];
  113. [self.uiMsgs insertObjects:uiMsgs atIndexes:indexSet];
  114. [self.tableView reloadData];
  115. [self.tableView layoutIfNeeded];
  116. }
  117. });
  118. }];
  119. }
  120. - (NSMutableArray *)transUIMsgFromIMMsg:(NSArray *)msgs {
  121. NSMutableArray *uiMsgs = [NSMutableArray array];
  122. for (NSInteger k = 0; k < msgs.count; k++) {
  123. V2TIMMessage *msg = msgs[k];
  124. if ([self.delegate respondsToSelector:@selector(messageController:onNewMessage:)]) {
  125. TUIMessageCellData *data = [self.delegate messageController:nil onNewMessage:msg];
  126. if (data) {
  127. TUIMessageCellLayout *layout = TUIMessageCellLayout.incommingMessageLayout;
  128. if ([data isKindOfClass:TUITextMessageCellData.class] || [data isKindOfClass:TUIReferenceMessageCellData.class]) {
  129. layout = TUIMessageCellLayout.incommingTextMessageLayout;
  130. } else if ([data isKindOfClass:TUIVoiceMessageCellData.class]) {
  131. layout = TUIMessageCellLayout.incommingVoiceMessageLayout;
  132. }
  133. data.cellLayout = layout;
  134. data.direction = MsgDirectionIncoming;
  135. data.innerMessage = msg;
  136. data.showName = YES;
  137. [uiMsgs addObject:data];
  138. continue;
  139. }
  140. }
  141. TUIMessageCellData *data = [TUIMessageDataProvider getCellData:msg];
  142. if (!data) {
  143. continue;
  144. }
  145. TUIMessageCellLayout *layout = TUIMessageCellLayout.incommingMessageLayout;
  146. if ([data isKindOfClass:TUITextMessageCellData.class]) {
  147. layout = TUIMessageCellLayout.incommingTextMessageLayout;
  148. } else if ([data isKindOfClass:TUIReplyMessageCellData.class] || [data isKindOfClass:TUIReferenceMessageCellData.class]) {
  149. layout = TUIMessageCellLayout.incommingTextMessageLayout;
  150. TUIReferenceMessageCellData *textData = (TUIReferenceMessageCellData *)data;
  151. textData.textColor = TUIChatDynamicColor(@"chat_text_message_receive_text_color", @"#000000");
  152. textData.showRevokedOriginMessage = YES;
  153. } else if ([data isKindOfClass:TUIVoiceMessageCellData.class]) {
  154. TUIVoiceMessageCellData *voiceData = (TUIVoiceMessageCellData *)data;
  155. voiceData.cellLayout = [TUIMessageCellLayout incommingVoiceMessageLayout];
  156. voiceData.voiceImage = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_normal")];
  157. voiceData.voiceAnimationImages =
  158. [NSArray arrayWithObjects:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_1")],
  159. [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_2")],
  160. [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_3")], nil];
  161. voiceData.voiceTop = 10;
  162. msg.localCustomInt = 1;
  163. layout = TUIMessageCellLayout.incommingVoiceMessageLayout;
  164. }
  165. data.cellLayout = layout;
  166. data.direction = MsgDirectionIncoming;
  167. data.innerMessage = msg;
  168. data.showName = YES;
  169. [uiMsgs addObject:data];
  170. }
  171. return uiMsgs;
  172. }
  173. - (void)setupViews {
  174. self.title = TIMCommonLocalizableString(TUIKitRelayChatHistory);
  175. self.tableView.scrollsToTop = NO;
  176. self.tableView.estimatedRowHeight = 0;
  177. [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
  178. self.tableView.backgroundColor = TUIChatDynamicColor(@"chat_controller_bg_color", @"#FFFFFF");
  179. self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
  180. [self.messageCellConfig bindTableView:self.tableView];
  181. }
  182. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  183. return _uiMsgs.count;
  184. }
  185. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  186. static CGFloat screenWidth = 0;
  187. if (screenWidth == 0) {
  188. screenWidth = Screen_Width;
  189. }
  190. if (indexPath.row < _uiMsgs.count) {
  191. TUIMessageCellData *cellData = _uiMsgs[indexPath.row];
  192. CGFloat height = [self.messageCellConfig getHeightFromMessageCellData:cellData];
  193. return height;
  194. } else {
  195. return 0;
  196. }
  197. }
  198. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  199. TUIMessageCellData *data = _uiMsgs[indexPath.row];
  200. data.showMessageTime = YES;
  201. data.showCheckBox = NO;
  202. TUIMessageCell *cell = nil;
  203. if ([self.delegate respondsToSelector:@selector(messageController:onShowMessageData:)]) {
  204. cell = [self.delegate messageController:nil onShowMessageData:data];
  205. if (cell) {
  206. cell.delegate = self;
  207. return cell;
  208. }
  209. }
  210. cell = [tableView dequeueReusableCellWithIdentifier:data.reuseId forIndexPath:indexPath];
  211. cell.delegate = self;
  212. [cell fillWithData:_uiMsgs[indexPath.row]];
  213. return cell;
  214. }
  215. #pragma mark - TUIMessageCellDelegate
  216. - (void)onSelectMessage:(TUIMessageCell *)cell {
  217. if (TUIChatConfig.defaultConfig.eventConfig.chatEventListener &&
  218. [TUIChatConfig.defaultConfig.eventConfig.chatEventListener respondsToSelector:@selector(onMessageClicked:messageCellData:)]) {
  219. BOOL result = [TUIChatConfig.defaultConfig.eventConfig.chatEventListener onMessageClicked:cell messageCellData:cell.messageData];
  220. if (result) {
  221. return;
  222. }
  223. }
  224. if ([cell isKindOfClass:[TUIImageMessageCell class]]) {
  225. [self showImageMessage:(TUIImageMessageCell *)cell];
  226. }
  227. if ([cell isKindOfClass:[TUIVoiceMessageCell class]]) {
  228. [self playVoiceMessage:(TUIVoiceMessageCell *)cell];
  229. }
  230. if ([cell isKindOfClass:[TUIVideoMessageCell class]]) {
  231. [self showVideoMessage:(TUIVideoMessageCell *)cell];
  232. }
  233. if ([cell isKindOfClass:[TUIFileMessageCell class]]) {
  234. [self showFileMessage:(TUIFileMessageCell *)cell];
  235. }
  236. if ([cell isKindOfClass:[TUIMergeMessageCell class]]) {
  237. TUIMergeMessageListController *mergeVc = [[TUIMergeMessageListController alloc] init];
  238. mergeVc.mergerElem = [(TUIMergeMessageCell *)cell mergeData].mergerElem;
  239. mergeVc.delegate = self.delegate;
  240. [self.navigationController pushViewController:mergeVc animated:YES];
  241. }
  242. if ([cell isKindOfClass:[TUILinkCell class]]) {
  243. [self showLinkMessage:(TUILinkCell *)cell];
  244. }
  245. if ([cell isKindOfClass:[TUIReplyMessageCell class]]) {
  246. [self showReplyMessage:(TUIReplyMessageCell *)cell];
  247. }
  248. if ([cell isKindOfClass:[TUIReferenceMessageCell class]]) {
  249. [self showReplyMessage:(TUIReplyMessageCell *)cell];
  250. }
  251. if ([self.delegate respondsToSelector:@selector(messageController:onSelectMessageContent:)]) {
  252. [self.delegate messageController:nil onSelectMessageContent:cell];
  253. }
  254. }
  255. - (BOOL)checkIfMessageExistsInLocal:(V2TIMMessage *)locateMessage {
  256. NSInteger index = 0;
  257. for (TUIMessageCellData *uiMsg in self.uiMsgs) {
  258. if ([uiMsg.innerMessage.msgID isEqualToString:locateMessage.msgID]) {
  259. return YES;
  260. }
  261. index++;
  262. }
  263. if (index == self.uiMsgs.count) {
  264. return NO;
  265. }
  266. return NO;
  267. }
  268. - (void)scrollToLocateMessage:(V2TIMMessage *)locateMessage matchKeyword:(NSString *)msgAbstract {
  269. CGFloat offsetY = 0;
  270. NSInteger index = 0;
  271. for (TUIMessageCellData *uiMsg in self.uiMsgs) {
  272. if ([uiMsg.innerMessage.msgID isEqualToString:locateMessage.msgID]) {
  273. break;
  274. }
  275. offsetY += [uiMsg heightOfWidth:Screen_Width];
  276. index++;
  277. }
  278. if (index == self.uiMsgs.count) {
  279. return;
  280. }
  281. offsetY -= self.tableView.frame.size.height / 2.0;
  282. if (offsetY <= TMessageController_Header_Height) {
  283. offsetY = TMessageController_Header_Height + 0.1;
  284. }
  285. if (offsetY > TMessageController_Header_Height) {
  286. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]
  287. atScrollPosition:UITableViewScrollPositionMiddle
  288. animated:YES];
  289. }
  290. [self highlightKeyword:msgAbstract locateMessage:locateMessage];
  291. }
  292. - (void)highlightKeyword:(NSString *)keyword locateMessage:(V2TIMMessage *)locateMessage {
  293. TUIMessageCellData *cellData = nil;
  294. for (TUIMessageCellData *tmp in self.uiMsgs) {
  295. if ([tmp.msgID isEqualToString:locateMessage.msgID]) {
  296. cellData = tmp;
  297. break;
  298. }
  299. }
  300. if (cellData == nil) {
  301. return;
  302. }
  303. CGFloat time = 0.5;
  304. UITableViewRowAnimation animation = UITableViewRowAnimationFade;
  305. if ([cellData isKindOfClass:TUITextMessageCellData.class]) {
  306. time = 2;
  307. animation = UITableViewRowAnimationNone;
  308. }
  309. @weakify(self);
  310. dispatch_async(dispatch_get_main_queue(), ^{
  311. @strongify(self);
  312. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.uiMsgs indexOfObject:cellData] inSection:0];
  313. cellData.highlightKeyword = keyword;
  314. TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  315. [cell fillWithData:cellData];
  316. });
  317. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  318. @strongify(self);
  319. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.uiMsgs indexOfObject:cellData] inSection:0];
  320. cellData.highlightKeyword = nil;
  321. TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  322. [cell fillWithData:cellData];
  323. });
  324. }
  325. - (void)showReplyMessage:(TUIReplyMessageCell *)cell {
  326. [UIApplication.sharedApplication.keyWindow endEditing:YES];
  327. NSString *originMsgID = @"";
  328. NSString *msgAbstract = @"";
  329. if ([cell isKindOfClass:TUIReplyMessageCell.class]) {
  330. TUIReplyMessageCell *acell = (TUIReplyMessageCell *)cell;
  331. TUIReplyMessageCellData *cellData = acell.replyData;
  332. originMsgID = cellData.messageRootID;
  333. msgAbstract = cellData.msgAbstract;
  334. } else if ([cell isKindOfClass:TUIReferenceMessageCell.class]) {
  335. TUIReferenceMessageCell *acell = (TUIReferenceMessageCell *)cell;
  336. TUIReferenceMessageCellData *cellData = acell.referenceData;
  337. originMsgID = cellData.originMsgID;
  338. msgAbstract = cellData.msgAbstract;
  339. }
  340. TUIMessageCellData *originMemoryMessageData = nil;
  341. for (TUIMessageCellData *uiMsg in self.uiMsgs) {
  342. if ([uiMsg.innerMessage.msgID isEqualToString:originMsgID]) {
  343. originMemoryMessageData = uiMsg;
  344. break;
  345. }
  346. }
  347. if (originMemoryMessageData && [cell isKindOfClass:TUIReplyMessageCell.class]) {
  348. [self onJumpToRepliesDetailPage:originMemoryMessageData];
  349. }
  350. else {
  351. [(TUIMessageSearchDataProvider *)self.msgDataProvider
  352. findMessages:@[ originMsgID ?: @"" ]
  353. callback:^(BOOL success, NSString *_Nonnull desc, NSArray<V2TIMMessage *> *_Nonnull msgs) {
  354. if (!success) {
  355. [TUITool makeToast:TIMCommonLocalizableString(TUIKitReplyMessageNotFoundOriginMessage)];
  356. return;
  357. }
  358. V2TIMMessage *message = msgs.firstObject;
  359. if (message == nil) {
  360. [TUITool makeToast:TIMCommonLocalizableString(TUIKitReplyMessageNotFoundOriginMessage)];
  361. return;
  362. }
  363. if (message.status == V2TIM_MSG_STATUS_HAS_DELETED || message.status == V2TIM_MSG_STATUS_LOCAL_REVOKED) {
  364. [TUITool makeToast:TIMCommonLocalizableString(TUIKitReplyMessageNotFoundOriginMessage)];
  365. return;
  366. }
  367. if ([cell isKindOfClass:TUIReplyMessageCell.class]) {
  368. [self jumpDetailPageByMessage:message];
  369. } else if ([cell isKindOfClass:TUIReferenceMessageCell.class]) {
  370. BOOL existInLocal = [self checkIfMessageExistsInLocal:message];
  371. if (!existInLocal) {
  372. [TUITool makeToast:TIMCommonLocalizableString(TUIKitReplyMessageNotFoundOriginMessage)];
  373. return;
  374. }
  375. [self scrollToLocateMessage:message matchKeyword:msgAbstract];
  376. }
  377. }];
  378. }
  379. }
  380. - (void)jumpDetailPageByMessage:(V2TIMMessage *)message {
  381. NSMutableArray *uiMsgs = [self transUIMsgFromIMMsg:@[ message ]];
  382. [self.msgDataProvider preProcessMessage:uiMsgs
  383. callback:^{
  384. for (TUIMessageCellData *cellData in uiMsgs) {
  385. if ([cellData.innerMessage.msgID isEqual:message.msgID]) {
  386. [self onJumpToRepliesDetailPage:cellData];
  387. return;
  388. }
  389. }
  390. }];
  391. }
  392. - (void)onJumpToRepliesDetailPage:(TUIMessageCellData *)data {
  393. TUIRepliesDetailViewController *repliesDetailVC = [[TUIRepliesDetailViewController alloc] initWithCellData:data conversationData:self.conversationData];
  394. repliesDetailVC.delegate = self.delegate;
  395. [self.navigationController pushViewController:repliesDetailVC animated:YES];
  396. repliesDetailVC.parentPageDataProvider = self.parentPageDataProvider;
  397. __weak typeof(self) weakSelf = self;
  398. repliesDetailVC.willCloseCallback = ^() {
  399. [weakSelf.tableView reloadData];
  400. };
  401. }
  402. - (void)showImageMessage:(TUIImageMessageCell *)cell {
  403. CGRect frame = [cell.thumb convertRect:cell.thumb.bounds toView:[UIApplication sharedApplication].delegate.window];
  404. TUIMediaView *mediaView = [[TUIMediaView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
  405. [mediaView setThumb:cell.thumb frame:frame];
  406. [mediaView setCurMessage:cell.messageData.innerMessage allMessages:self.imMsgs];
  407. [[UIApplication sharedApplication].keyWindow addSubview:mediaView];
  408. }
  409. - (void)playVoiceMessage:(TUIVoiceMessageCell *)cell {
  410. for (NSInteger index = 0; index < _uiMsgs.count; ++index) {
  411. if (![_uiMsgs[index] isKindOfClass:[TUIVoiceMessageCellData class]]) {
  412. continue;
  413. }
  414. TUIVoiceMessageCellData *uiMsg = (TUIVoiceMessageCellData *)_uiMsgs[index];
  415. if (uiMsg == cell.voiceData) {
  416. [uiMsg playVoiceMessage];
  417. cell.voiceReadPoint.hidden = YES;
  418. } else {
  419. [uiMsg stopVoiceMessage];
  420. }
  421. }
  422. }
  423. - (void)showVideoMessage:(TUIVideoMessageCell *)cell {
  424. CGRect frame = [cell.thumb convertRect:cell.thumb.bounds toView:[UIApplication sharedApplication].delegate.window];
  425. TUIMediaView *mediaView = [[TUIMediaView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
  426. [mediaView setThumb:cell.thumb frame:frame];
  427. [mediaView setCurMessage:cell.messageData.innerMessage allMessages:self.imMsgs];
  428. [[UIApplication sharedApplication].keyWindow addSubview:mediaView];
  429. }
  430. - (void)showFileMessage:(TUIFileMessageCell *)cell {
  431. TUIFileViewController *file = [[TUIFileViewController alloc] init];
  432. file.data = [cell fileData];
  433. [self.navigationController pushViewController:file animated:YES];
  434. }
  435. - (void)showLinkMessage:(TUILinkCell *)cell {
  436. TUILinkCellData *cellData = cell.customData;
  437. if (cellData.link) {
  438. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cellData.link]];
  439. }
  440. }
  441. - (NSMutableDictionary *)stylesCache {
  442. if (_stylesCache == nil) {
  443. _stylesCache = [NSMutableDictionary dictionary];
  444. }
  445. return _stylesCache;
  446. }
  447. - (UIImage *)bubbleImage {
  448. CGRect rect = CGRectMake(0, 0, 100, 40);
  449. UIGraphicsBeginImageContext(rect.size);
  450. CGContextRef context = UIGraphicsGetCurrentContext();
  451. CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
  452. CGContextFillRect(context, rect);
  453. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  454. UIGraphicsEndImageContext();
  455. return img;
  456. }
  457. - (TUIMessageSearchDataProvider *)msgDataProvider {
  458. if (_msgDataProvider == nil) {
  459. _msgDataProvider = [[TUIMessageSearchDataProvider alloc] init];
  460. _msgDataProvider.dataSource = self;
  461. }
  462. return _msgDataProvider;
  463. }
  464. - (TUIMessageCellConfig *)messageCellConfig {
  465. if (_messageCellConfig == nil) {
  466. _messageCellConfig = [[TUIMessageCellConfig alloc] init];
  467. }
  468. return _messageCellConfig;
  469. }
  470. #pragma mark - TUIMessageBaseDataProviderDataSource
  471. - (void)dataProviderDataSourceWillChange:(TUIMessageBaseDataProvider *)dataProvider {
  472. // do nothing
  473. }
  474. - (void)dataProviderDataSourceChange:(TUIMessageBaseDataProvider *)dataProvider
  475. withType:(TUIMessageBaseDataProviderDataSourceChangeType)type
  476. atIndex:(NSUInteger)index
  477. animation:(BOOL)animation {
  478. // do nothing
  479. }
  480. - (void)dataProviderDataSourceDidChange:(TUIMessageBaseDataProvider *)dataProvider {
  481. [self.tableView reloadData];
  482. }
  483. - (void)dataProvider:(TUIMessageBaseDataProvider *)dataProvider onRemoveHeightCache:(TUIMessageCellData *)cellData {
  484. if (cellData) {
  485. [self.messageCellConfig removeHeightCacheOfMessageCellData:cellData];
  486. }
  487. }
  488. #pragma mark - TUINotificationProtocol
  489. - (void)onNotifyEvent:(NSString *)key subKey:(NSString *)subKey object:(id)anObject param:(NSDictionary *)param {
  490. if ([key isEqualToString:TUICore_TUIPluginNotify] && [subKey isEqualToString:TUICore_TUIPluginNotify_DidChangePluginViewSubKey]) {
  491. TUIMessageCellData *data = param[TUICore_TUIPluginNotify_DidChangePluginViewSubKey_Data];
  492. [self.messageCellConfig removeHeightCacheOfMessageCellData:data];
  493. [self reloadAndScrollToBottomOfMessage:data.innerMessage.msgID section:0];
  494. }
  495. }
  496. - (void)reloadAndScrollToBottomOfMessage:(NSString *)messageID section:(NSInteger)section {
  497. // Dispatch the task to RunLoop to ensure that they are executed after the UITableView refresh is complete.
  498. dispatch_async(dispatch_get_main_queue(), ^{
  499. [self reloadCellOfMessage:messageID section:section];
  500. dispatch_async(dispatch_get_main_queue(), ^{
  501. [self scrollCellToBottomOfMessage:messageID section:section];
  502. });
  503. });
  504. }
  505. - (void)reloadCellOfMessage:(NSString *)messageID section:(NSInteger)section {
  506. NSIndexPath *indexPath = [self indexPathOfMessage:messageID section:section];
  507. // Disable animation when loading to avoid cell jumping.
  508. if (indexPath == nil) {
  509. return;
  510. }
  511. [UIView performWithoutAnimation:^{
  512. dispatch_async(dispatch_get_main_queue(), ^{
  513. [self.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
  514. });
  515. }];
  516. }
  517. - (void)scrollCellToBottomOfMessage:(NSString *)messageID section:(NSInteger)section {
  518. NSIndexPath *indexPath = [self indexPathOfMessage:messageID section:section];
  519. // Scroll the tableView only if the bottom of the cell is invisible.
  520. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  521. CGRect tableViewRect = self.tableView.bounds;
  522. BOOL isBottomInvisible = cellRect.origin.y < CGRectGetMaxY(tableViewRect) && CGRectGetMaxY(cellRect) > CGRectGetMaxY(tableViewRect);
  523. if (isBottomInvisible) {
  524. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  525. }
  526. }
  527. - (NSIndexPath *)indexPathOfMessage:(NSString *)messageID section:(NSInteger)section {
  528. for (int i = 0; i < self.uiMsgs.count; i++) {
  529. TUIMessageCellData *data = self.uiMsgs[i];
  530. if ([data.innerMessage.msgID isEqualToString:messageID]) {
  531. return [NSIndexPath indexPathForRow:i inSection:section];
  532. }
  533. }
  534. return nil;
  535. }
  536. @end