TUIBaseMessageController_Minimalist.m 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. //
  2. // TUIBaseMessageController.m
  3. // UIKit
  4. //
  5. // Created by annidyfeng on 2019/7/1.
  6. // Copyright © 2022 Tencent. All rights reserved.
  7. //
  8. #import "TUIBaseMessageController_Minimalist.h"
  9. #import <TIMCommon/TIMConfig.h>
  10. #import <TIMCommon/TIMDefine.h>
  11. #import <TIMCommon/TIMPopActionProtocol.h>
  12. #import <TIMCommon/TUISystemMessageCell.h>
  13. #import <TUICore/TUICore.h>
  14. #import <TUICore/TUILogin.h>
  15. #import <TUICore/TUIThemeManager.h>
  16. #import <TUICore/TUITool.h>
  17. #import <UIKit/UIWindow.h>
  18. #import "TUIChatCallingDataProvider.h"
  19. #import "TUIChatConversationModel.h"
  20. #import "TUIChatDataProvider.h"
  21. #import "TUIChatPopContextController.h"
  22. #import "TUIChatPopMenu.h"
  23. #import "TUIFaceMessageCell_Minimalist.h"
  24. #import "TUIFaceView.h"
  25. #import "TUIFileMessageCell_Minimalist.h"
  26. #import "TUIFileViewController_Minimalist.h"
  27. #import "TUIImageMessageCell_Minimalist.h"
  28. #import "TUIJoinGroupMessageCell_Minimalist.h"
  29. #import "TUILinkCell_Minimalist.h"
  30. #import "TUIMediaView_Minimalist.h"
  31. #import "TUIMergeMessageCell_Minimalist.h"
  32. #import "TUIMergeMessageListController_Minimalist.h"
  33. #import "TUIMessageDataProvider.h"
  34. #import "TUIMessageProgressManager.h"
  35. #import "TUIMessageReadViewController_Minimalist.h"
  36. #import "TUIOrderCell_Minimalist.h"
  37. #import "TUIReferenceMessageCell_Minimalist.h"
  38. #import "TUIRepliesDetailViewController_Minimalist.h"
  39. #import "TUIReplyMessageCellData.h"
  40. #import "TUIReplyMessageCell_Minimalist.h"
  41. #import "TUITextMessageCell_Minimalist.h"
  42. #import "TUIVideoMessageCell_Minimalist.h"
  43. #import "TUIVoiceMessageCell_Minimalist.h"
  44. #import "TUIMessageCellConfig_Minimalist.h"
  45. #import "TUIVoiceMessageCell_Minimalist.h"
  46. typedef NSString * CellDataClassName;
  47. typedef Class<TUIMessageCellProtocol> CellClass;
  48. typedef NSString * MessageID;
  49. typedef NSNumber * HeightNumber;
  50. @interface TUIBaseMessageController_Minimalist () <TUIMessageCellDelegate,
  51. TUIJoinGroupMessageCellDelegate_Minimalist,
  52. TUIMessageProgressManagerDelegate,
  53. TUIMessageDataProviderDataSource,
  54. TIMPopActionProtocol,
  55. TUINotificationProtocol>
  56. @property(nonatomic, strong) TUIMessageDataProvider *messageDataProvider;
  57. @property(nonatomic, strong) TUIMessageCellData *menuUIMsg;
  58. @property(nonatomic, strong) TUIMessageCellData *reSendUIMsg;
  59. @property(nonatomic, strong) TUIChatConversationModel *conversationData;
  60. @property(nonatomic, strong) UIActivityIndicatorView *indicatorView;
  61. @property(nonatomic, assign) BOOL isActive;
  62. @property(nonatomic, assign) BOOL showCheckBox;
  63. @property(nonatomic, assign) BOOL scrollingTriggeredByUser;
  64. @property(nonatomic, assign) BOOL isAutoScrolledToBottom;
  65. @property(nonatomic, assign) BOOL hasCoverPage;
  66. @property(nonatomic, strong) TUIChatPopContextController *popAlertController;
  67. @property(nonatomic, strong) TUIMessageCellConfig_Minimalist *messageCellConfig;
  68. @end
  69. @implementation TUIBaseMessageController_Minimalist
  70. + (void)initialize {
  71. [TUIMessageDataProvider setDataSourceClass:self];
  72. }
  73. + (void)asyncGetDisplayString:(NSArray<V2TIMMessage *> *)messageList callback:(void(^)(NSDictionary<NSString *, NSString *> *))callback {
  74. [TUIMessageDataProvider asyncGetDisplayString:messageList callback:callback];
  75. }
  76. + (nullable NSString *)getDisplayString:(V2TIMMessage *)message {
  77. return [TUIMessageDataProvider getDisplayString:message];
  78. }
  79. #pragma mark - Life Cycle
  80. - (void)viewDidLoad {
  81. [super viewDidLoad];
  82. [self setupViews];
  83. [self registerEvents];
  84. self.isActive = YES;
  85. [TUITool addUnsupportNotificationInVC:self];
  86. [TUIMessageProgressManager.shareManager addDelegate:self];
  87. }
  88. - (void)dealloc {
  89. [[NSNotificationCenter defaultCenter] removeObserver:self];
  90. [TUIMessageProgressManager.shareManager removeDelegate:self];
  91. [TUICore unRegisterEventByObject:self];
  92. }
  93. - (void)viewWillAppear:(BOOL)animated {
  94. self.isInVC = YES;
  95. [super viewWillAppear:animated];
  96. }
  97. - (void)viewDidAppear:(BOOL)animated {
  98. [super viewDidAppear:animated];
  99. [self sendVisibleReadGroupMessages];
  100. [self limitReadReport];
  101. }
  102. - (void)viewDidDisappear:(BOOL)animated {
  103. [super viewDidDisappear:animated];
  104. self.isInVC = NO;
  105. }
  106. - (void)applicationBecomeActive {
  107. self.isActive = YES;
  108. [self sendVisibleReadGroupMessages];
  109. }
  110. - (void)applicationEnterBackground {
  111. self.isActive = NO;
  112. }
  113. - (void)setupViews {
  114. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapViewController)];
  115. /**
  116. * Solve the problem that the touch event is not passed down, causing the gesture to conflict with the collectionView didselect
  117. */
  118. tap.cancelsTouchesInView = NO;
  119. [self.view addGestureRecognizer:tap];
  120. self.tableView.scrollsToTop = NO;
  121. [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
  122. self.tableView.backgroundColor = TUIChatDynamicColor(@"chat_controller_bg_color", @"#FFFFFF");
  123. self.indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, TMessageController_Header_Height)];
  124. self.indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  125. self.tableView.tableHeaderView = self.indicatorView;
  126. if (!self.indicatorView.isAnimating) {
  127. [self.indicatorView startAnimating];
  128. }
  129. [self.messageCellConfig bindTableView:self.tableView];
  130. }
  131. - (void)registerEvents {
  132. [TUICore registerEvent:TUICore_TUIPluginNotify subKey:TUICore_TUIPluginNotify_WillForwardTextSubKey object:self];
  133. [TUICore registerEvent:TUICore_TUIPluginNotify subKey:TUICore_TUIPluginNotify_DidChangePluginViewSubKey object:self];
  134. [[NSNotificationCenter defaultCenter] addObserver:self
  135. selector:@selector(applicationBecomeActive)
  136. name:UIApplicationDidBecomeActiveNotification
  137. object:nil];
  138. [[NSNotificationCenter defaultCenter] addObserver:self
  139. selector:@selector(applicationBecomeActive)
  140. name:UIApplicationWillEnterForegroundNotification
  141. object:nil];
  142. [[NSNotificationCenter defaultCenter] addObserver:self
  143. selector:@selector(applicationEnterBackground)
  144. name:UIApplicationDidEnterBackgroundNotification
  145. object:nil];
  146. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceivedSendMessageRequest:) name:TUIChatSendMessageNotification object:nil];
  147. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceivedSendMessageWithoutUpdateUIRequest:) name:TUIChatSendMessageWithoutUpdateUINotification object:nil];
  148. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceivedInsertMessageWithoutUpdateUIRequest:) name:TUIChatInsertMessageWithoutUpdateUINotification object:nil];
  149. }
  150. - (TUIMessageCellConfig_Minimalist *)messageCellConfig {
  151. if (_messageCellConfig == nil) {
  152. _messageCellConfig = [[TUIMessageCellConfig_Minimalist alloc] init];
  153. }
  154. return _messageCellConfig;
  155. }
  156. #pragma mark - Data Provider
  157. - (void)setConversation:(TUIChatConversationModel *)conversationData {
  158. self.conversationData = conversationData;
  159. if (!self.messageDataProvider) {
  160. self.messageDataProvider = [[TUIMessageDataProvider alloc] initWithConversationModel:conversationData];
  161. self.messageDataProvider.dataSource = self;
  162. self.messageDataProvider.mergeAdjacentMsgsFromTheSameSender = YES;
  163. }
  164. [self loadMessage];
  165. [self loadGroupInfo];
  166. }
  167. - (void)loadMessage {
  168. if (self.messageDataProvider.isLoadingData || self.messageDataProvider.isNoMoreMsg) {
  169. return;
  170. }
  171. __weak typeof(self) weakSelf = self;
  172. [self.messageDataProvider
  173. loadMessageSucceedBlock:^(BOOL isFirstLoad, BOOL isNoMoreMsg, NSArray<TUIMessageCellData *> *_Nonnull newMsgs) {
  174. if (isNoMoreMsg) {
  175. weakSelf.indicatorView.mm_h = 0;
  176. }
  177. if (newMsgs.count != 0) {
  178. [weakSelf.tableView reloadData];
  179. [weakSelf.tableView layoutIfNeeded];
  180. if (isFirstLoad) {
  181. [weakSelf scrollToBottom:NO];
  182. } else {
  183. CGFloat visibleHeight = 0;
  184. for (NSInteger i = 0; i < newMsgs.count; ++i) {
  185. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  186. visibleHeight += [weakSelf tableView:weakSelf.tableView heightForRowAtIndexPath:indexPath];
  187. }
  188. if (isNoMoreMsg) {
  189. visibleHeight -= TMessageController_Header_Height;
  190. }
  191. [weakSelf.tableView scrollRectToVisible:CGRectMake(0, weakSelf.tableView.contentOffset.y + visibleHeight, weakSelf.tableView.frame.size.width,
  192. weakSelf.tableView.frame.size.height)
  193. animated:NO];
  194. }
  195. }
  196. // [weakSelf.indicatorView stopAnimating];
  197. }
  198. FailBlock:^(int code, NSString *desc) {
  199. [TUITool makeToastError:code msg:desc];
  200. }];
  201. }
  202. - (void)loadGroupInfo {
  203. if (self.conversationData.groupID.length > 0) {
  204. __weak typeof(self) weakSelf = self;
  205. [self.messageDataProvider getPinMessageList];
  206. [self.messageDataProvider loadGroupInfo:^{
  207. [weakSelf.messageDataProvider getSelfInfoInGroup:^{}];
  208. }];
  209. self.messageDataProvider.groupRoleChanged = ^(V2TIMGroupMemberRole role) {
  210. if (weakSelf.groupRoleChanged) {
  211. weakSelf.groupRoleChanged(role);
  212. }
  213. };
  214. self.messageDataProvider.pinGroupMessageChanged = ^(NSArray * _Nonnull groupPinList) {
  215. if (weakSelf.pinGroupMessageChanged) {
  216. weakSelf.pinGroupMessageChanged(groupPinList);
  217. }
  218. };
  219. }
  220. }
  221. - (void)clearUImsg {
  222. [self.messageDataProvider clearUIMsgList];
  223. [self.tableView reloadData];
  224. [self.tableView layoutIfNeeded];
  225. if (self.indicatorView.isAnimating) {
  226. [self.indicatorView stopAnimating];
  227. }
  228. }
  229. - (void)reloadAndScrollToBottomOfMessage:(NSString *)messageID needScroll:(BOOL)isNeedScroll {
  230. // Dispatch the task to RunLoop to ensure that they are executed after the UITableView refresh is complete.
  231. dispatch_async(dispatch_get_main_queue(), ^{
  232. [self reloadCellOfMessage:messageID];
  233. dispatch_async(dispatch_get_main_queue(), ^{
  234. if (isNeedScroll) {
  235. [self scrollCellToBottomOfMessage:messageID];
  236. }
  237. });
  238. });
  239. }
  240. - (void)reloadAndScrollToBottomOfMessage:(NSString *)messageID {
  241. [self reloadAndScrollToBottomOfMessage:messageID needScroll:YES];
  242. }
  243. - (void)reloadCellOfMessage:(NSString *)messageID {
  244. NSIndexPath *indexPath = [self indexPathOfMessage:messageID];
  245. // Disable animation when loading to avoid cell jumping.
  246. if (indexPath == nil) {
  247. return;
  248. }
  249. [UIView performWithoutAnimation:^{
  250. [self.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
  251. }];
  252. }
  253. - (void)scrollCellToBottomOfMessage:(NSString *)messageID {
  254. if (self.hasCoverPage) {
  255. return;
  256. }
  257. NSIndexPath *indexPath = [self indexPathOfMessage:messageID];
  258. // Scroll the tableView only if the bottom of the cell is invisible.
  259. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  260. CGRect tableViewRect = self.tableView.bounds;
  261. BOOL isBottomInvisible = (cellRect.origin.y < CGRectGetMaxY(tableViewRect) && CGRectGetMaxY(cellRect) > CGRectGetMaxY(tableViewRect)) ||
  262. (cellRect.origin.y >= CGRectGetMaxY(tableViewRect));
  263. if (isBottomInvisible) {
  264. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  265. }
  266. if (self.isAutoScrolledToBottom) {
  267. [self scrollToBottom:YES];
  268. }
  269. }
  270. - (NSIndexPath *)indexPathOfMessage:(NSString *)messageID {
  271. for (int i = 0; i < self.messageDataProvider.uiMsgs.count; i++) {
  272. TUIMessageCellData *data = self.messageDataProvider.uiMsgs[i];
  273. if ([data.innerMessage.msgID isEqualToString:messageID]) {
  274. return [NSIndexPath indexPathForRow:i inSection:0];
  275. }
  276. }
  277. return nil;
  278. }
  279. #pragma mark - Event Response
  280. - (void)scrollToBottom:(BOOL)animate {
  281. if (self.messageDataProvider.uiMsgs.count > 0) {
  282. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messageDataProvider.uiMsgs.count - 1 inSection:0]
  283. atScrollPosition:UITableViewScrollPositionBottom
  284. animated:animate];
  285. self.isAutoScrolledToBottom = YES;
  286. }
  287. }
  288. - (void)didTapViewController {
  289. if (self.delegate && [self.delegate respondsToSelector:@selector(didTapInMessageController:)]) {
  290. [self.delegate didTapInMessageController:self];
  291. }
  292. }
  293. - (void)sendPlaceHolderUIMessage:(TUIMessageCellData *)cellData {
  294. [self.messageDataProvider sendPlaceHolderUIMessage:cellData];
  295. [self scrollToBottom:YES];
  296. }
  297. - (void)sendUIMessage:(TUIMessageCellData *)cellData {
  298. @weakify(self);
  299. cellData.innerMessage.needReadReceipt = self.isMsgNeedReadReceipt;
  300. [self.messageDataProvider sendUIMsg:cellData
  301. toConversation:self.conversationData
  302. willSendBlock:^(BOOL isReSend, TUIMessageCellData *_Nonnull dateUIMsg) {
  303. @strongify(self);
  304. if ([cellData isKindOfClass:[TUIVideoMessageCellData class]]||
  305. [cellData isKindOfClass:[TUIImageMessageCellData class]]) {
  306. dispatch_async(dispatch_get_main_queue(), ^{
  307. [self scrollToBottom:YES];
  308. });
  309. } else {
  310. [self scrollToBottom:YES];
  311. }
  312. [self setUIMessageStatus:cellData status:Msg_Status_Sending_2];
  313. }
  314. SuccBlock:^{
  315. @strongify(self);
  316. [self reloadUIMessage:cellData];
  317. [self setUIMessageStatus:cellData status:Msg_Status_Succ];
  318. }
  319. FailBlock:^(int code, NSString *desc) {
  320. @strongify(self);
  321. [self reloadUIMessage:cellData];
  322. [self setUIMessageStatus:cellData status:Msg_Status_Fail];
  323. [self makeSendErrorHud:code desc:desc];
  324. }];
  325. }
  326. - (void)setUIMessageStatus:(TUIMessageCellData *)cellData status:(TMsgStatus)status {
  327. switch (status) {
  328. case Msg_Status_Init:
  329. case Msg_Status_Succ:
  330. case Msg_Status_Fail:
  331. {
  332. [self changeMsg:cellData status:status];
  333. }
  334. break;
  335. case Msg_Status_Sending:
  336. case Msg_Status_Sending_2:
  337. {
  338. int delay = 1;
  339. if ([cellData isKindOfClass:[TUIImageMessageCellData class]] ||
  340. [cellData isKindOfClass:[TUIVideoMessageCellData class]]) {
  341. delay = 0;
  342. }
  343. if (0 == delay) {
  344. [self changeMsg:cellData status:Msg_Status_Sending_2];
  345. } else {
  346. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  347. if (cellData.innerMessage.status == V2TIM_MSG_STATUS_SENDING) {
  348. [self changeMsg:cellData status:Msg_Status_Sending_2];
  349. }
  350. });
  351. }
  352. }
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. - (void)makeSendErrorHud:(int)code desc:(NSString *)desc {
  359. // The text or image msg is sensitive, the cell height may change.
  360. if (code == 80001 || code == 80004) {
  361. [self scrollToBottom:YES];
  362. return;
  363. }
  364. NSString *errorMsg = @"";
  365. if (self.isMsgNeedReadReceipt && code == ERR_SDK_INTERFACE_NOT_SUPPORT) {
  366. errorMsg = [NSString stringWithFormat:@"%@%@", TUIKitLocalizableString(TUIKitErrorUnsupportIntefaceMessageRead),
  367. TUIKitLocalizableString(TUIKitErrorUnsupporInterfaceSuffix)];
  368. } else {
  369. errorMsg = [TUITool convertIMError:code msg:desc];
  370. }
  371. UIAlertController *ac = [UIAlertController alertControllerWithTitle:errorMsg message:nil preferredStyle:UIAlertControllerStyleAlert];
  372. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(Confirm) style:UIAlertActionStyleDefault handler:nil]];
  373. [self presentViewController:ac animated:YES completion:nil];
  374. }
  375. - (void)sendMessage:(V2TIMMessage *)message {
  376. [self sendMessage:message placeHolderCellData:nil];
  377. }
  378. - (void)sendMessage:(V2TIMMessage *)message placeHolderCellData:(TUIMessageCellData *)placeHolderCellData {
  379. TUIMessageCellData *cellData = nil;
  380. if (message.elemType == V2TIM_ELEM_TYPE_CUSTOM) {
  381. cellData = [self.delegate messageController:self onNewMessage:message];
  382. cellData.innerMessage = message;
  383. }
  384. if (!cellData) {
  385. cellData = [TUIMessageDataProvider getCellData:message];
  386. }
  387. if (cellData) {
  388. cellData.placeHolderCellData = placeHolderCellData;
  389. [self sendUIMessage:cellData];
  390. }
  391. }
  392. - (void)reloadUIMessage:(TUIMessageCellData *)msg {
  393. // innerMessage maybe changed, reload it
  394. NSInteger index = [self.messageDataProvider.uiMsgs indexOfObject:msg];
  395. NSMutableArray *newUIMsgs = [self.messageDataProvider transUIMsgFromIMMsg:@[ msg.innerMessage ]];
  396. if (newUIMsgs.count == 0) {
  397. return;
  398. }
  399. TUIMessageCellData *newUIMsg = newUIMsgs.firstObject;
  400. @weakify(self)
  401. [self.messageDataProvider preProcessMessage:@[ newUIMsg ]
  402. callback:^{
  403. @strongify(self)
  404. [UIView performWithoutAnimation:^{
  405. [self.messageDataProvider replaceUIMsg:newUIMsg atIndex:index];
  406. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]
  407. withRowAnimation:UITableViewRowAnimationNone];
  408. }];
  409. }];
  410. }
  411. - (void)changeMsg:(TUIMessageCellData *)msg status:(TMsgStatus)status {
  412. msg.status = status;
  413. NSInteger index = [self.messageDataProvider.uiMsgs indexOfObject:msg];
  414. if ([self.tableView numberOfRowsInSection:0] > index) {
  415. TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
  416. [cell fillWithData:msg];
  417. } else {
  418. NSLog(@"lack of cell");
  419. }
  420. [[NSNotificationCenter defaultCenter] postNotificationName:@"kTUINotifyMessageStatusChanged"
  421. object:nil
  422. userInfo:@{
  423. @"msg" : msg,
  424. @"status" : [NSNumber numberWithUnsignedInteger:status],
  425. @"msgSender" : self,
  426. }];
  427. }
  428. - (void)onReceivedSendMessageRequest:(NSNotification *)notification {
  429. NSDictionary *userInfo = notification.userInfo;
  430. if (!userInfo) {
  431. return;
  432. }
  433. V2TIMMessage *message = [userInfo objectForKey:TUICore_TUIChatService_SendMessageMethod_MsgKey];
  434. TUIMessageCellData *cellData = [userInfo objectForKey:TUICore_TUIChatService_SendMessageMethod_PlaceHolderUIMsgKey];
  435. if (cellData && !message) {
  436. [self sendPlaceHolderUIMessage:cellData];
  437. } else if (message) {
  438. [self sendMessage:message placeHolderCellData:cellData];
  439. }
  440. }
  441. - (void)onReceivedSendMessageWithoutUpdateUIRequest:(NSNotification *)notification {
  442. NSDictionary *userInfo = notification.userInfo;
  443. if (userInfo == nil) {
  444. return;
  445. }
  446. V2TIMMessage *message = [userInfo objectForKey:TUICore_TUIChatService_SendMessageMethodWithoutUpdateUI_MsgKey];
  447. if (message == nil) {
  448. return;
  449. }
  450. TUISendMessageAppendParams *param = [TUISendMessageAppendParams new];
  451. param.isOnlineUserOnly = YES;
  452. [TUIMessageDataProvider sendMessage:message
  453. toConversation:self.conversationData
  454. appendParams:param
  455. Progress:nil
  456. SuccBlock:^{
  457. NSLog(@"send message without updating UI succeed");
  458. }
  459. FailBlock:^(int code, NSString *desc) {
  460. NSLog(@"send message without updating UI failed, code: %d, desc: %@", code, desc);
  461. }];
  462. }
  463. - (void)onReceivedInsertMessageWithoutUpdateUIRequest:(NSNotification *)notification {
  464. NSDictionary *userInfo = notification.userInfo;
  465. if (userInfo == nil) {
  466. return;
  467. }
  468. V2TIMMessage *message = [userInfo objectForKey:@"message"];
  469. BOOL isNeedScrollToBottom = [userInfo objectForKey:@"needScrollToBottom"];
  470. if (message == nil) {
  471. return;
  472. }
  473. NSMutableArray *newUIMsgs = [self.messageDataProvider transUIMsgFromIMMsg:@[ message ]];
  474. if (newUIMsgs.count == 0) {
  475. return;
  476. }
  477. TUIMessageCellData *newUIMsg = newUIMsgs.firstObject;
  478. @weakify(self)
  479. [self.messageDataProvider preProcessMessage:@[ newUIMsg ]
  480. callback:^{
  481. @strongify(self)
  482. [UIView performWithoutAnimation:^{
  483. [self.tableView beginUpdates];
  484. @autoreleasepool {
  485. for (TUIMessageCellData *uiMsg in newUIMsgs) {
  486. [self.messageDataProvider addUIMsg:uiMsg];
  487. [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messageDataProvider.uiMsgs.count -1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  488. }
  489. }
  490. [self.tableView endUpdates];
  491. if (isNeedScrollToBottom) {
  492. [self scrollToBottom:YES];
  493. }
  494. }];
  495. }];
  496. }
  497. #pragma mark - TUINotificationProtocol
  498. - (void)onNotifyEvent:(NSString *)key subKey:(NSString *)subKey object:(id)anObject param:(NSDictionary *)param {
  499. if ([key isEqualToString:TUICore_TUIPluginNotify] && [subKey isEqualToString:TUICore_TUIPluginNotify_DidChangePluginViewSubKey]) {
  500. // Translation View is shown, hidden or changed.
  501. TUIMessageCellData *data = param[TUICore_TUIPluginNotify_DidChangePluginViewSubKey_Data];
  502. BOOL isAllowScroll2Bottom = YES;
  503. if ([param[TUICore_TUIPluginNotify_DidChangePluginViewSubKey_isAllowScroll2Bottom] isEqualToString:@"0"] ) {
  504. isAllowScroll2Bottom = NO ;
  505. TUIMessageCellData *lasData = [self.messageDataProvider.uiMsgs lastObject];
  506. BOOL isInBottomPage = (self.tableView.contentSize.height - self.tableView.contentOffset.y
  507. <= Screen_Height);
  508. if ([lasData.msgID isEqualToString:data.msgID] && isInBottomPage) {
  509. isAllowScroll2Bottom = YES;
  510. }
  511. }
  512. [self.messageCellConfig removeHeightCacheOfMessageCellData:data];
  513. [self reloadAndScrollToBottomOfMessage:data.innerMessage.msgID needScroll:isAllowScroll2Bottom];
  514. }
  515. if ([key isEqualToString:TUICore_TUIPluginNotify] && [subKey isEqualToString:TUICore_TUIPluginNotify_WillForwardTextSubKey]) {
  516. // Translation will be forwarded.
  517. NSString *text = param[TUICore_TUIPluginNotify_WillForwardTextSubKey_Text];
  518. if (self.delegate && [self.delegate respondsToSelector:@selector(messageController:onForwardText:)]) {
  519. [self.delegate messageController:self onForwardText:text];
  520. }
  521. }
  522. }
  523. - (void)clearAndReloadCellOfData:(TUIMessageCellData *)data {
  524. [self.messageCellConfig removeHeightCacheOfMessageCellData:data];
  525. [self reloadAndScrollToBottomOfMessage:data.innerMessage.msgID];
  526. }
  527. #pragma mark - TUIMessageProgressManagerDelegate
  528. - (void)onMessageSendingResultChanged:(TUIMessageSendingResultType)type messageID:(NSString *)msgID {
  529. // async
  530. __weak typeof(self) weakSelf = self;
  531. dispatch_async(dispatch_get_main_queue(), ^{
  532. for (TUIMessageCellData *cellData in weakSelf.messageDataProvider.uiMsgs) {
  533. if ([cellData.msgID isEqual:msgID]) {
  534. [weakSelf changeMsg:cellData status:(type == TUIMessageSendingResultTypeSucc) ? Msg_Status_Succ : Msg_Status_Fail];
  535. }
  536. }
  537. });
  538. }
  539. #pragma mark - TUIMessageBaseDataProviderDataSource
  540. + (Class)onGetCustomMessageCellDataClass:(NSString *)businessID {
  541. return [TUIMessageCellConfig_Minimalist getCustomMessageCellDataClass:businessID];
  542. }
  543. static NSMutableArray *lastMsgIndexs = nil;
  544. static NSMutableArray *reloadMsgIndexs = nil;
  545. - (BOOL)isDataSourceConsistent {
  546. NSInteger dataSourceCount = self.messageDataProvider.uiMsgs.count;
  547. NSInteger tableViewCount = [self.tableView numberOfRowsInSection:0];
  548. if (dataSourceCount != tableViewCount) {
  549. NSLog(@"Data source and UI are inconsistent: Data source count = %ld, Table view count = %ld", (long)dataSourceCount, (long)tableViewCount);
  550. return NO;
  551. }
  552. return YES;
  553. }
  554. - (void)dataProviderDataSourceWillChange:(TUIMessageDataProvider *)dataProvider {
  555. [self.tableView beginUpdates];
  556. if (lastMsgIndexs) {
  557. [lastMsgIndexs removeAllObjects];
  558. } else {
  559. lastMsgIndexs = [NSMutableArray array];
  560. }
  561. if (reloadMsgIndexs) {
  562. [reloadMsgIndexs removeAllObjects];
  563. } else {
  564. reloadMsgIndexs = [NSMutableArray array];
  565. }
  566. }
  567. - (void)dataProviderDataSourceChange:(TUIMessageDataProvider *)dataProvider
  568. withType:(TUIMessageBaseDataProviderDataSourceChangeType)type
  569. atIndex:(NSUInteger)index
  570. animation:(BOOL)animation {
  571. // insert or delete or reload current cell
  572. [reloadMsgIndexs addObject:@(index)];
  573. NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:index inSection:0] ];
  574. switch (type) {
  575. case TUIMessageBaseDataProviderDataSourceChangeTypeInsert:
  576. [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation ? UITableViewRowAnimationFade : UITableViewRowAnimationNone];
  577. break;
  578. case TUIMessageBaseDataProviderDataSourceChangeTypeDelete:
  579. [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation ? UITableViewRowAnimationFade : UITableViewRowAnimationNone];
  580. break;
  581. case TUIMessageBaseDataProviderDataSourceChangeTypeReload:
  582. [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation ? UITableViewRowAnimationFade : UITableViewRowAnimationNone];
  583. break;
  584. default:
  585. break;
  586. }
  587. // remove cache index
  588. if ([lastMsgIndexs containsObject:@(index)]) {
  589. [lastMsgIndexs removeObject:@(index)];
  590. }
  591. // reload last cell
  592. if (index >= 1 && ![reloadMsgIndexs containsObject:@(index - 1)]) {
  593. [lastMsgIndexs addObject:@(index - 1)];
  594. }
  595. }
  596. - (void)dataProviderDataSourceDidChange:(TUIMessageDataProvider *)dataProvider {
  597. for (NSNumber *number in lastMsgIndexs) {
  598. NSUInteger index = [number unsignedIntValue];
  599. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
  600. TUIMessageCellData *cellData = self.messageDataProvider.uiMsgs[indexPath.row];
  601. [self.messageCellConfig removeHeightCacheOfMessageCellData:cellData];
  602. [self.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
  603. }
  604. [lastMsgIndexs removeAllObjects];
  605. [reloadMsgIndexs removeAllObjects];
  606. [self.tableView endUpdates];
  607. }
  608. - (void)dataProvider:(TUIMessageBaseDataProvider *)dataProvider onRemoveHeightCache:(TUIMessageCellData *)cellData {
  609. if (cellData) {
  610. [self.messageCellConfig removeHeightCacheOfMessageCellData:cellData];
  611. }
  612. }
  613. - (nullable TUIMessageCellData *)dataProvider:(TUIMessageDataProvider *)dataProvider CustomCellDataFromNewIMMessage:(V2TIMMessage *)msg {
  614. if (![msg.userID isEqualToString:self.conversationData.userID] && ![msg.groupID isEqualToString:self.conversationData.groupID]) {
  615. return nil;
  616. }
  617. if (msg.status == V2TIM_MSG_STATUS_LOCAL_REVOKED) {
  618. return nil;
  619. }
  620. if ([self.delegate respondsToSelector:@selector(messageController:onNewMessage:)]) {
  621. TUIMessageCellData *customCellData = [self.delegate messageController:self onNewMessage:msg];
  622. if (customCellData) {
  623. customCellData.innerMessage = msg;
  624. return customCellData;
  625. }
  626. }
  627. return nil;
  628. }
  629. - (void)dataProvider:(TUIMessageDataProvider *)dataProvider ReceiveReadMsgWithUserID:(NSString *)userId Time:(time_t)timestamp {
  630. if (userId.length > 0 && [userId isEqualToString:self.conversationData.userID]) {
  631. for (int i = 0; i < self.messageDataProvider.uiMsgs.count; i++) {
  632. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messageDataProvider.uiMsgs.count - 1 - i inSection:0];
  633. TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  634. /**
  635. *
  636. * Determine whether the current unread needs to be changed to read by the callback timestamp
  637. */
  638. time_t msgTime = [cell.messageData.innerMessage.timestamp timeIntervalSince1970];
  639. if (msgTime <= timestamp && ![cell.readReceiptLabel.text isEqualToString:TIMCommonLocalizableString(Read)]) {
  640. cell.readReceiptLabel.text = TIMCommonLocalizableString(Read);
  641. }
  642. }
  643. }
  644. }
  645. - (void)dataProvider:(TUIMessageDataProvider *)dataProvider
  646. ReceiveReadMsgWithGroupID:(NSString *)groupID
  647. msgID:(NSString *)msgID
  648. readCount:(NSUInteger)readCount
  649. unreadCount:(NSUInteger)unreadCount {
  650. if (groupID != nil && ![groupID isEqualToString:self.conversationData.groupID]) {
  651. return;
  652. }
  653. NSInteger row = [self.messageDataProvider getIndexOfMessage:msgID];
  654. if (row < 0 || row >= self.messageDataProvider.uiMsgs.count) {
  655. return;
  656. }
  657. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
  658. TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  659. [cell updateReadLabelText];
  660. }
  661. - (void)dataProvider:(TUIMessageDataProvider *)dataProvider ReceiveNewUIMsg:(TUIMessageCellData *)uiMsg {
  662. /**
  663. * When viewing historical messages, judge whether you need to slide to the bottom according to the current contentOffset
  664. */
  665. if (self.tableView.contentSize.height - self.tableView.contentOffset.y < Screen_Height * 1.5) {
  666. [self scrollToBottom:YES];
  667. if (self.isInVC && self.isActive) {
  668. [self.messageDataProvider sendLatestMessageReadReceipt];
  669. }
  670. }
  671. [self limitReadReport];
  672. }
  673. - (void)dataProvider:(TUIMessageDataProvider *)dataProvider ReceiveRevokeUIMsg:(TUIMessageCellData *)uiMsg {
  674. return;
  675. }
  676. - (void)dataProvider:(TUIMessageBaseDataProvider *)dataProvider didChangeTranslationData:(TUIMessageCellData *)data {
  677. [self reloadAndScrollToBottomOfMessage:data.innerMessage.msgID];
  678. }
  679. #pragma mark - Private
  680. - (void)limitReadReport {
  681. static uint64_t lastTs = 0;
  682. uint64_t curTs = [[NSDate date] timeIntervalSince1970];
  683. /**
  684. * More than 1s && Not the first time, report immediately
  685. */
  686. if (curTs - lastTs >= 1 && lastTs) {
  687. lastTs = curTs;
  688. [self readReport];
  689. } else {
  690. /**
  691. * Less than 1s || First time, delay 1s and merge report
  692. */
  693. static BOOL delayReport = NO;
  694. if (delayReport) {
  695. return;
  696. }
  697. delayReport = YES;
  698. __weak typeof(self) weakSelf = self;
  699. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  700. [weakSelf readReport];
  701. delayReport = NO;
  702. });
  703. }
  704. }
  705. - (void)readReport {
  706. if (self.isInVC && self.isActive) {
  707. NSString *userID = self.conversationData.userID;
  708. if (userID.length > 0) {
  709. [TUIMessageDataProvider markC2CMessageAsRead:userID succ:nil fail:nil];
  710. }
  711. NSString *groupID = self.conversationData.groupID;
  712. if (groupID.length > 0) {
  713. [TUIMessageDataProvider markGroupMessageAsRead:groupID succ:nil fail:nil];
  714. }
  715. NSString *conversationID = @"";
  716. if (IS_NOT_EMPTY_NSSTRING(userID)) {
  717. conversationID = [NSString stringWithFormat:@"c2c_%@", userID];
  718. }
  719. if (IS_NOT_EMPTY_NSSTRING(groupID)) {
  720. conversationID = [NSString stringWithFormat:@"group_%@", groupID];
  721. }
  722. if (IS_NOT_EMPTY_NSSTRING(self.conversationData.conversationID)) {
  723. conversationID = self.conversationData.conversationID;
  724. }
  725. if (conversationID.length > 0) {
  726. [TUIMessageDataProvider markConversationAsUndead:@[ conversationID ] enableMark:NO];
  727. }
  728. }
  729. }
  730. /**
  731. * When the receiver sends a visible message read receipt:
  732. * 1. The time when messageVC is visible. You will be notified when [self viewDidAppear:] is invoked
  733. * 2. The time when scrollview scrolled to bottom by called [self scrollToBottom:] (For example, click the "x new message" tips in the lower right corner). You
  734. * will be notified when [UIScrollViewDelegate scrollViewDidEndScrollingAnimation:] is invoked.
  735. * + Note that you need to use the state of the scrollView to accurately determine whether the scrollView has really stopped sliding.
  736. * 3. The time when the user drags the scrollView continuously to view the message. You will be notified when [UIScrollViewDelegate scrollViewDidScroll:] is
  737. * invoked.
  738. * + Note here to determine whether the scrolling of the scrollView is triggered by user gestures (rather than automatic code triggers). So use the
  739. * self.scrollingTriggeredByUser flag to distinguish.
  740. * + The update logic of self.scrollingTriggeredByUser is as follows:
  741. * - Set YES when the user's finger touches the screen and starts to drag (scrollViewWillBeginDragging:);
  742. * - When the user's finger drags at a certain acceleration and leaves the screen, when the screen automatically stops sliding
  743. * (scrollViewDidEndDecelerating:), set to NO;
  744. * - No acceleration is applied after the user's finger slides, and when the user lifts the finger directly (scrollViewDidEndDragging:), set NO.
  745. * 4. When the user stays in the latest message interface and receives a new message at this time. Get notified in [self dataProvider:ReceiveNewUIMsg:] .
  746. */
  747. - (void)sendVisibleReadGroupMessages {
  748. if (self.isInVC && self.isActive) {
  749. NSRange range = [self calcVisibleCellRange];
  750. [self.messageDataProvider sendMessageReadReceiptAtIndexes:[self transferIndexFromRange:range]];
  751. }
  752. }
  753. - (NSRange)calcVisibleCellRange {
  754. NSArray *indexPaths = [self.tableView indexPathsForVisibleRows];
  755. if (indexPaths.count == 0) {
  756. return NSMakeRange(0, 0);
  757. }
  758. NSIndexPath *topmost = indexPaths.firstObject;
  759. NSIndexPath *downmost = indexPaths.lastObject;
  760. return NSMakeRange(topmost.row, downmost.row - topmost.row + 1);
  761. }
  762. - (NSArray *)transferIndexFromRange:(NSRange)range {
  763. NSMutableArray *index = [NSMutableArray array];
  764. NSInteger start = range.location;
  765. for (int i = 0; i < range.length; i++) {
  766. [index addObject:@(start + i)];
  767. }
  768. return index;
  769. }
  770. - (void)hideKeyboardIfNeeded {
  771. [self.view endEditing:YES];
  772. [TUITool.applicationKeywindow endEditing:YES];
  773. }
  774. - (CGFloat)getHeightFromMessageCellData:(TUIMessageCellData *)cellData {
  775. return [self.messageCellConfig getHeightFromMessageCellData:cellData];
  776. }
  777. #pragma mark - UITableViewDelegate
  778. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  779. return self.messageDataProvider.uiMsgs.count;
  780. }
  781. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  782. if (indexPath.row < self.messageDataProvider.uiMsgs.count) {
  783. TUIMessageCellData *cellData = self.messageDataProvider.uiMsgs[indexPath.row];
  784. return [self.messageCellConfig getHeightFromMessageCellData:cellData];
  785. } else {
  786. return 0;
  787. }
  788. }
  789. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  790. if (indexPath.row < self.messageDataProvider.uiMsgs.count) {
  791. TUIMessageCellData *cellData = self.messageDataProvider.uiMsgs[indexPath.row];
  792. return [self.messageCellConfig getEstimatedHeightFromMessageCellData:cellData];
  793. } else {
  794. return UITableViewAutomaticDimension;
  795. }
  796. }
  797. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  798. TUIMessageCellData *data = self.messageDataProvider.uiMsgs[indexPath.row];
  799. data.showCheckBox = self.showCheckBox && [self supportCheckBox:data];
  800. TUIMessageCell *cell = nil;
  801. if ([self.delegate respondsToSelector:@selector(messageController:onShowMessageData:)]) {
  802. cell = [self.delegate messageController:self onShowMessageData:data];
  803. if (cell) {
  804. cell.delegate = self;
  805. return cell;
  806. }
  807. }
  808. if (!data.reuseId) {
  809. NSAssert(NO, @"Unknow cell");
  810. return nil;
  811. }
  812. cell = [tableView dequeueReusableCellWithIdentifier:data.reuseId forIndexPath:indexPath];
  813. cell.delegate = self;
  814. [cell fillWithData:data];
  815. [cell notifyBottomContainerReadyOfData:nil];
  816. return cell;
  817. }
  818. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  819. TUIMessageCell *uiMsg = (TUIMessageCell *)cell;
  820. if ([uiMsg isKindOfClass:TUIMessageCell.class] && [self.delegate respondsToSelector:@selector(messageController:willDisplayCell:withData:)]) {
  821. [self.delegate messageController:self willDisplayCell:uiMsg withData:uiMsg.messageData];
  822. }
  823. }
  824. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  825. if (indexPath.row < self.messageDataProvider.uiMsgs.count) {
  826. TUITextMessageCellData *cellData = (TUITextMessageCellData *)self.messageDataProvider.uiMsgs[indexPath.row];
  827. // Delete after TUICallKit is connected according to the standard process
  828. if ([cellData isKindOfClass:TUITextMessageCellData.class]) {
  829. if ((cellData.isAudioCall || cellData.isVideoCall) && cellData.showUnreadPoint) {
  830. cellData.innerMessage.localCustomInt = 1;
  831. cellData.showUnreadPoint = NO;
  832. }
  833. }
  834. [TUICore notifyEvent:TUICore_TUIChatNotify
  835. subKey:TUICore_TUIChatNotify_MessageDisplayedSubKey
  836. object:cellData
  837. param:nil];
  838. }
  839. }
  840. #pragma mark - UIScrollViewDelegate
  841. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  842. if (self.scrollingTriggeredByUser) {
  843. // only if the scrollView is dragged by user's finger to scroll, we need to send read receipts.
  844. [self sendVisibleReadGroupMessages];
  845. self.isAutoScrolledToBottom = NO;
  846. }
  847. }
  848. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  849. self.scrollingTriggeredByUser = YES;
  850. [self didTapViewController];
  851. }
  852. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  853. if ([self isScrollViewEndDragging:scrollView]) {
  854. // user presses on the scrolling scrollView and forces it to stop scrolling immediately.
  855. self.scrollingTriggeredByUser = NO;
  856. }
  857. }
  858. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  859. if ([self isScrollViewEndDecelerating:scrollView]) {
  860. // user drags the scrollView with a certain acceleration and makes a flick gesture, and scrollView will stop scrolling after decelerating.
  861. self.scrollingTriggeredByUser = NO;
  862. }
  863. }
  864. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  865. if ([self isScrollViewEndDecelerating:scrollView]) {
  866. // UIScrollView automatically stops scrolling, for example triggered after calling scrollToBottom
  867. [self sendVisibleReadGroupMessages];
  868. }
  869. }
  870. - (BOOL)isScrollViewEndDecelerating:(UIScrollView *)scrollView {
  871. return scrollView.tracking == 0 && scrollView.dragging == 0 && scrollView.decelerating == 0;
  872. }
  873. - (BOOL)isScrollViewEndDragging:(UIScrollView *)scrollView {
  874. return scrollView.tracking == 1 && scrollView.dragging == 0 && scrollView.decelerating == 0;
  875. }
  876. #pragma mark - TUIMessageCellDelegate
  877. - (void)onSelectMessage:(TUIMessageCell *)cell {
  878. if (TUIChatConfig.defaultConfig.eventConfig.chatEventListener &&
  879. [TUIChatConfig.defaultConfig.eventConfig.chatEventListener respondsToSelector:@selector(onMessageClicked:messageCellData:)]) {
  880. BOOL result = [TUIChatConfig.defaultConfig.eventConfig.chatEventListener onMessageClicked:cell messageCellData:cell.messageData];
  881. if (result) {
  882. return;
  883. }
  884. }
  885. if (cell.messageData.innerMessage.hasRiskContent) {
  886. if (![cell isKindOfClass:[TUIReferenceMessageCell_Minimalist class]]) {
  887. return;
  888. }
  889. }
  890. if (self.showCheckBox && [self supportCheckBox:(TUIMessageCellData *)cell.data]) {
  891. TUIMessageCellData *data = (TUIMessageCellData *)cell.data;
  892. data.selected = !data.selected;
  893. [self.tableView reloadData];
  894. if ([self.delegate respondsToSelector:@selector(messageController:onSelectMessageWhenMultiCheckboxAppear:)]) {
  895. [self.delegate messageController:self onSelectMessageWhenMultiCheckboxAppear:data];
  896. }
  897. return;
  898. }
  899. //Hide the keyboard when tapping on the message.
  900. [self hideKeyboardIfNeeded];
  901. if ([cell isKindOfClass:[TUITextMessageCell_Minimalist class]]) {
  902. [self clickTextMessage:(TUITextMessageCell_Minimalist *)cell];
  903. } else if ([cell isKindOfClass:[TUISystemMessageCell class]]) {
  904. [self clickSystemMessage:(TUISystemMessageCell *)cell];
  905. } else if ([cell isKindOfClass:[TUIVoiceMessageCell_Minimalist class]]) {
  906. [self playVoiceMessage:(TUIVoiceMessageCell_Minimalist *)cell];
  907. } else if ([cell isKindOfClass:[TUIImageMessageCell_Minimalist class]]) {
  908. [self showImageMessage:(TUIImageMessageCell_Minimalist *)cell];
  909. } else if ([cell isKindOfClass:[TUIVideoMessageCell_Minimalist class]]) {
  910. [self showVideoMessage:(TUIVideoMessageCell_Minimalist *)cell];
  911. } else if ([cell isKindOfClass:[TUIFileMessageCell_Minimalist class]]) {
  912. [self showFileMessage:(TUIFileMessageCell_Minimalist *)cell];
  913. } else if ([cell isKindOfClass:[TUIMergeMessageCell_Minimalist class]]) {
  914. [self showRelayMessage:(TUIMergeMessageCell_Minimalist *)cell];
  915. } else if ([cell isKindOfClass:[TUILinkCell_Minimalist class]]) {
  916. [self showLinkMessage:(TUILinkCell_Minimalist *)cell];
  917. } else if ([cell isKindOfClass:TUIReplyMessageCell_Minimalist.class]) {
  918. [self showReplyMessage:(TUIReplyMessageCell_Minimalist *)cell];
  919. } else if ([cell isKindOfClass:TUIReferenceMessageCell_Minimalist.class]) {
  920. [self showReplyMessage:(TUIReplyMessageCell_Minimalist *)cell];
  921. } else if ([cell isKindOfClass:TUIOrderCell_Minimalist.class]) {
  922. [self showOrderMessage:(TUIOrderCell_Minimalist *)cell];
  923. }
  924. if ([self.delegate respondsToSelector:@selector(messageController:onSelectMessageContent:)]) {
  925. [self.delegate messageController:self onSelectMessageContent:cell];
  926. }
  927. }
  928. - (void)showContextWindow:(TUIMessageCell *)cell {
  929. CGRect frame = [UIApplication.sharedApplication.keyWindow convertRect:cell.container.frame fromView:cell];
  930. TUIChatPopContextController *alertController = [[TUIChatPopContextController alloc] init];
  931. alertController.alertViewCellData = cell.messageData;
  932. alertController.originFrame = frame;
  933. alertController.alertCellClass = cell.class;
  934. // blur effect
  935. [alertController setBlurEffectWithView:self.navigationController.view];
  936. // config
  937. [self configItems:alertController targetCell:cell];
  938. __weak typeof(cell) weakCell = cell;
  939. __weak typeof(self) weakSelf = self;
  940. alertController.viewWillShowHandler = ^(TUIMessageCell *_Nonnull alertView) {
  941. alertView.delegate = weakSelf;
  942. };
  943. alertController.dismissComplete = ^{
  944. if (weakCell) {
  945. weakCell.container.hidden = NO;
  946. }
  947. };
  948. [self.navigationController presentViewController:alertController animated:NO completion:nil];
  949. self.popAlertController = alertController;
  950. }
  951. - (void)configItems:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  952. /**
  953. * Sort priorities: copy, forward, multiselect, reference, reply, Withdraw, delete
  954. * The higher the weight, the more prioritized it is:
  955. Copy - 10000
  956. Forward - 9000
  957. Select - 8000
  958. Quote - 7000
  959. Reply - 5000
  960. Recall - 3000
  961. Details - 2000
  962. Delete - 1000
  963. */
  964. NSMutableArray *items = [NSMutableArray arrayWithCapacity:6];
  965. [self addNormalItemToItems:items cell:cell alertController:alertController];
  966. [self addExtraItemToItems:items cell:cell alertController:alertController];
  967. NSMutableArray *sortedArray = [self sortItems:items];
  968. NSMutableArray *allPageItemsArray = [self pageItems:sortedArray inAlertController:alertController];
  969. if (allPageItemsArray.count > 0) {
  970. alertController.items = allPageItemsArray[0];
  971. }
  972. }
  973. - (void)addNormalItemToItems:(NSMutableArray *)items
  974. cell:(TUIMessageCell *)cell
  975. alertController:(TUIChatPopContextController *)alertController {
  976. BOOL isPluginCustomMessage = [TUIMessageCellConfig_Minimalist isPluginCustomMessageCellData:cell.messageData];
  977. if (isPluginCustomMessage) {
  978. [self addPluginCustomMessageItemToItems:items cell:cell alertController:alertController];
  979. return;
  980. }
  981. [self addNomalMessageItemToItems:items cell:cell alertController:alertController];
  982. }
  983. - (void)addPluginCustomMessageItemToItems:(NSMutableArray *)items
  984. cell:(TUIMessageCell *)cell
  985. alertController:(TUIChatPopContextController *)alertController {
  986. V2TIMMessage *imMsg = cell.messageData.innerMessage;
  987. // Plugin build-in custom messsages, support actions: multiSelect, reference, reply, delete, recall.
  988. if ([self isAddMultiSelect:imMsg]) {
  989. [items addObject:[self setupMultiSelectAction:alertController targetCell:cell]];
  990. }
  991. if ([self isAddReply:imMsg]) {
  992. [items addObject:[self setupReplyAction:alertController targetCell:cell]];
  993. }
  994. if ([self isAddQuote:imMsg]) {
  995. [items addObject:[self setupReferenceAction:alertController targetCell:cell]];
  996. }
  997. if ([self isAddDelete]) {
  998. [items addObject:[self setupDeleteAction:alertController targetCell:cell]];
  999. }
  1000. if ([self isAddRecall:imMsg]) {
  1001. [items addObject:[self setupRecallAction:alertController targetCell:cell]];
  1002. }
  1003. }
  1004. - (void)addNomalMessageItemToItems:(NSMutableArray *)items
  1005. cell:(TUIMessageCell *)cell
  1006. alertController:(TUIChatPopContextController *)alertController {
  1007. V2TIMMessage *imMsg = cell.messageData.innerMessage;
  1008. // Normal messages.
  1009. if (imMsg.soundElem) {
  1010. [items addObject:[self setupAudioPlaybackStyleAction:alertController targetCell:cell]];
  1011. }
  1012. if ([self isAddCopy:imMsg data:cell.messageData]) {
  1013. [items addObject:[self setupCopyAction:alertController targetCell:cell]];
  1014. }
  1015. if ([self isAddForward:imMsg]) {
  1016. [items addObject:[self setupForwardAction:alertController targetCell:cell]];
  1017. }
  1018. if ([self isAddMultiSelect:imMsg]) {
  1019. [items addObject:[self setupMultiSelectAction:alertController targetCell:cell]];
  1020. }
  1021. if ([self isAddQuote:imMsg]) {
  1022. [items addObject:[self setupReferenceAction:alertController targetCell:cell]];
  1023. }
  1024. if ([self isAddReply:imMsg]) {
  1025. [items addObject:[self setupReplyAction:alertController targetCell:cell]];
  1026. }
  1027. if ([self isAddRecall:imMsg]) {
  1028. [items addObject:[self setupRecallAction:alertController targetCell:cell]];
  1029. }
  1030. if ([self isAddInfo:imMsg]) {
  1031. [items addObject:[self setupInfoAction:alertController targetCell:cell]];
  1032. }
  1033. if ([self isAddDelete]) {
  1034. [items addObject:[self setupDeleteAction:alertController targetCell:cell]];
  1035. }
  1036. if ([self isAddPin:imMsg]) {
  1037. [items addObject:[self setupGroupPinAction:alertController targetCell:cell]];
  1038. }
  1039. }
  1040. - (BOOL)isAddDelete {
  1041. return [TUIChatConfig defaultConfig].enablePopMenuDeleteAction;
  1042. }
  1043. - (BOOL)isAddCopy:(V2TIMMessage *)imMsg data:(TUIMessageCellData *)data {
  1044. BOOL isCopyShown = [TUIChatConfig defaultConfig].enablePopMenuCopyAction;
  1045. BOOL isContentModerated = imMsg.hasRiskContent;
  1046. return isCopyShown && ([data isKindOfClass:[TUITextMessageCellData class]] || [data isKindOfClass:TUIReferenceMessageCellData.class]) && !isContentModerated;
  1047. }
  1048. - (BOOL)isAddMultiSelect:(V2TIMMessage *)imMsg {
  1049. BOOL isSelectShown = [TUIChatConfig defaultConfig].enablePopMenuSelectAction;
  1050. BOOL isContentModerated = imMsg.hasRiskContent;
  1051. return isSelectShown && !isContentModerated;
  1052. }
  1053. - (BOOL)isAddReply:(V2TIMMessage *)imMsg {
  1054. BOOL isReplyShown = [TUIChatConfig defaultConfig].enablePopMenuReplyAction;
  1055. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1056. BOOL isContentModerated = imMsg.hasRiskContent;
  1057. return isReplyShown && isMsgSentSucceeded && !isContentModerated;
  1058. }
  1059. - (BOOL)isAddRecall:(V2TIMMessage *)imMsg {
  1060. BOOL isMyselfMsgSender = [imMsg isSelf];
  1061. BOOL isRecallSupported = [[NSDate date] timeIntervalSinceDate:imMsg.timestamp] < TUIChatConfig.defaultConfig.timeIntervalForMessageRecall;
  1062. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1063. BOOL isRecallShown = [TUIChatConfig defaultConfig].enablePopMenuRecallAction;
  1064. return imMsg && isMyselfMsgSender && isRecallSupported && isMsgSentSucceeded && isRecallShown;
  1065. }
  1066. - (BOOL)isAddQuote:(V2TIMMessage *)imMsg {
  1067. BOOL isQuoteShown = [TUIChatConfig defaultConfig].enablePopMenuReferenceAction;
  1068. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1069. BOOL isContentModerated = imMsg.hasRiskContent;
  1070. return isQuoteShown && isMsgSentSucceeded && !isContentModerated;
  1071. }
  1072. - (BOOL)isAddForward:(V2TIMMessage *)imMsg {
  1073. BOOL isForwardShown = [TUIChatConfig defaultConfig].enablePopMenuForwardAction;
  1074. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1075. BOOL isContentModerated = imMsg.hasRiskContent;
  1076. return isForwardShown && isMsgSentSucceeded && !isContentModerated;
  1077. }
  1078. - (BOOL)isAddPin:(V2TIMMessage *)imMsg {
  1079. BOOL isGroup = (imMsg.groupID.length > 0);
  1080. BOOL isCurrentUserSuperAdmin = [self.messageDataProvider isCurrentUserRoleSuperAdminInGroup];
  1081. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1082. BOOL isPinShown = [TUIChatConfig defaultConfig].enablePopMenuPinAction;
  1083. BOOL isContentModerated = imMsg.hasRiskContent;
  1084. return isGroup && isCurrentUserSuperAdmin && isMsgSentSucceeded && isPinShown && !isContentModerated;
  1085. }
  1086. - (BOOL)isAddInfo:(V2TIMMessage *)imMsg {
  1087. BOOL isMyselfMsgSender = [imMsg isSelf];
  1088. BOOL isMsgSentSucceeded = imMsg.status == V2TIM_MSG_STATUS_SEND_SUCC;
  1089. BOOL isInfoShown = [TUIChatConfig defaultConfig].enablePopMenuInfoAction;
  1090. return imMsg && isMyselfMsgSender && isMsgSentSucceeded && isInfoShown;
  1091. }
  1092. - (void)addExtraItemToItems:(NSMutableArray *)items
  1093. cell:(TUIMessageCell *)cell
  1094. alertController:(TUIChatPopContextController *)alertController {
  1095. NSArray<TUIExtensionInfo *> *infoArray =
  1096. [TUICore getExtensionList:TUICore_TUIChatExtension_PopMenuActionItem_MinimalistExtensionID
  1097. param:@{TUICore_TUIChatExtension_PopMenuActionItem_TargetVC : self, TUICore_TUIChatExtension_PopMenuActionItem_ClickCell : cell}];
  1098. for (TUIExtensionInfo *info in infoArray) {
  1099. if (info.text && info.icon && info.onClicked) {
  1100. TUIChatPopContextExtionItem *extension = [[TUIChatPopContextExtionItem alloc]
  1101. initWithTitle:info.text
  1102. markIcon:info.icon
  1103. weight:info.weight
  1104. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1105. [alertController blurDismissViewControllerAnimated:NO
  1106. completion:^(BOOL finished) { info.onClicked(@{});}];
  1107. }];
  1108. [items addObject:extension];
  1109. }
  1110. }
  1111. }
  1112. - (NSMutableArray *)sortItems:(NSMutableArray *)items {
  1113. NSArray *sortResultArray = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  1114. TUIChatPopContextExtionItem *per1 = obj1;
  1115. TUIChatPopContextExtionItem *per2 = obj2;
  1116. return per1.weight > per2.weight ? NSOrderedAscending : NSOrderedDescending;
  1117. }];
  1118. return [NSMutableArray arrayWithArray:sortResultArray];
  1119. }
  1120. - (NSMutableArray *)pageItems:(NSMutableArray *)items inAlertController:(TUIChatPopContextController *)alertController {
  1121. NSInteger perPageLimitedCount = 4;
  1122. NSMutableArray *allPageItemsArray = [NSMutableArray array];
  1123. NSUInteger itemsRemaining = items.count;
  1124. int j = 0;
  1125. while (itemsRemaining) {
  1126. NSRange range = NSMakeRange(j, MIN(perPageLimitedCount, itemsRemaining));
  1127. NSMutableArray *subLogArr = [NSMutableArray arrayWithArray:[items subarrayWithRange:range]];
  1128. TUIChatPopContextExtionItem *lastItem = [subLogArr lastObject];
  1129. lastItem.needBottomLine = YES;
  1130. [allPageItemsArray addObject:subLogArr];
  1131. itemsRemaining -= range.length;
  1132. j += range.length;
  1133. }
  1134. if (allPageItemsArray.count != 1) {
  1135. // more than one
  1136. NSInteger lastPagedIndex = allPageItemsArray.count - 1;
  1137. for (int pageIndex = 0; pageIndex < allPageItemsArray.count; pageIndex++) {
  1138. NSInteger nextPageIndex = 0;
  1139. if (lastPagedIndex != pageIndex) {
  1140. nextPageIndex = pageIndex + 1;
  1141. } else {
  1142. nextPageIndex = 0;
  1143. }
  1144. TUIChatPopContextExtionItem *moreItem =
  1145. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(More)
  1146. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_more")]
  1147. weight:INT_MAX
  1148. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1149. NSMutableArray *nextPageItems = allPageItemsArray[nextPageIndex];
  1150. ;
  1151. alertController.items = nextPageItems;
  1152. [alertController updateExtionView];
  1153. }];
  1154. moreItem.titleColor = [UIColor tui_colorWithHex:@"147AFF"];
  1155. NSMutableArray *pageItems = allPageItemsArray[pageIndex];
  1156. [pageItems addObject:moreItem];
  1157. }
  1158. } else {
  1159. // only one
  1160. NSMutableArray<TUIChatPopContextExtionItem *> *items = allPageItemsArray[0];
  1161. TUIChatPopContextExtionItem *lastItem = [items lastObject];
  1162. lastItem.needBottomLine = NO;
  1163. }
  1164. return allPageItemsArray;
  1165. }
  1166. - (void)onLongPressMessage:(TUIMessageCell *)cell {
  1167. if (TUIChatConfig.defaultConfig.eventConfig.chatEventListener &&
  1168. [TUIChatConfig.defaultConfig.eventConfig.chatEventListener respondsToSelector:@selector(onMessageLongClicked:messageCellData:)]) {
  1169. BOOL result = [TUIChatConfig.defaultConfig.eventConfig.chatEventListener onMessageLongClicked:cell messageCellData:cell.messageData];
  1170. if (result) {
  1171. return;
  1172. }
  1173. }
  1174. TUIMessageCellData *data = cell.messageData;
  1175. if ([data isKindOfClass:[TUISystemMessageCellData class]]) {
  1176. return;
  1177. }
  1178. self.menuUIMsg = data;
  1179. [self showContextWindow:cell];
  1180. }
  1181. - (void)onLongSelectMessageAvatar:(TUIMessageCell *)cell {
  1182. if (TUIChatConfig.defaultConfig.eventConfig.chatEventListener &&
  1183. [TUIChatConfig.defaultConfig.eventConfig.chatEventListener respondsToSelector:@selector(onUserIconLongClicked:messageCellData:)]) {
  1184. BOOL result = [TUIChatConfig.defaultConfig.eventConfig.chatEventListener onUserIconLongClicked:cell messageCellData:cell.messageData];
  1185. if (result) {
  1186. return;
  1187. }
  1188. }
  1189. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onLongSelectMessageAvatar:)]) {
  1190. [_delegate messageController:self onLongSelectMessageAvatar:cell];
  1191. }
  1192. }
  1193. - (void)onRetryMessage:(TUIMessageCell *)cell {
  1194. _reSendUIMsg = cell.messageData;
  1195. __weak typeof(self) weakSelf = self;
  1196. UIAlertController *alert = [UIAlertController alertControllerWithTitle:TIMCommonLocalizableString(TUIKitTipsConfirmResendMessage)
  1197. message:nil
  1198. preferredStyle:UIAlertControllerStyleAlert];
  1199. [alert tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(Re_send)
  1200. style:UIAlertActionStyleDefault
  1201. handler:^(UIAlertAction *_Nonnull action) {
  1202. [weakSelf sendUIMessage:weakSelf.reSendUIMsg];
  1203. }]];
  1204. [alert tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(Cancel)
  1205. style:UIAlertActionStyleCancel
  1206. handler:^(UIAlertAction *_Nonnull action){
  1207. }]];
  1208. [self.navigationController presentViewController:alert animated:YES completion:nil];
  1209. }
  1210. - (void)onSelectMessageAvatar:(TUIMessageCell *)cell {
  1211. if (TUIChatConfig.defaultConfig.eventConfig.chatEventListener &&
  1212. [TUIChatConfig.defaultConfig.eventConfig.chatEventListener respondsToSelector:@selector(onUserIconClicked:messageCellData:)]) {
  1213. BOOL result = [TUIChatConfig.defaultConfig.eventConfig.chatEventListener onUserIconClicked:cell messageCellData:cell.messageData];
  1214. if (result) {
  1215. return;
  1216. }
  1217. }
  1218. if ([self.delegate respondsToSelector:@selector(messageController:onSelectMessageAvatar:)]) {
  1219. [self.delegate messageController:self onSelectMessageAvatar:cell];
  1220. }
  1221. }
  1222. - (void)onJumpToRepliesDetailPage:(TUIMessageCellData *)data {
  1223. TUIMessageCellData *copyData = [TUIMessageDataProvider getCellData:data.innerMessage];
  1224. if (!copyData) {
  1225. return;
  1226. }
  1227. @weakify(self);
  1228. [self.messageDataProvider preProcessMessage:@[ copyData ]
  1229. callback:^{
  1230. dispatch_async(dispatch_get_main_queue(), ^{
  1231. @strongify(self);
  1232. TUIMessageCell *cell = [[TUIMessageCell alloc] init];
  1233. [cell fillWithData:copyData];
  1234. TUIRepliesDetailViewController_Minimalist *repliesDetailVC =
  1235. [[TUIRepliesDetailViewController_Minimalist alloc] initWithCellData:copyData
  1236. conversationData:self.conversationData];
  1237. repliesDetailVC.delegate = self.delegate;
  1238. repliesDetailVC.modalPresentationStyle = UIModalPresentationCustom;
  1239. [self.navigationController presentViewController:repliesDetailVC animated:YES completion:nil];
  1240. self.hasCoverPage = YES;
  1241. repliesDetailVC.parentPageDataProvider = self.messageDataProvider;
  1242. @weakify(self);
  1243. repliesDetailVC.willCloseCallback = ^() {
  1244. @strongify(self);
  1245. self.hasCoverPage = NO;
  1246. [self.tableView reloadData];
  1247. };
  1248. });
  1249. }];
  1250. }
  1251. - (void)onJumpToMessageInfoPage:(TUIMessageCellData *)data selectCell:(TUIMessageCell *)cell {
  1252. TUIMessageCellData *alertViewCellData = [TUIMessageDataProvider getCellData:cell.messageData.innerMessage];;
  1253. @weakify(self);
  1254. [self.messageDataProvider preProcessMessage:@[alertViewCellData] callback:^{
  1255. @strongify(self);
  1256. TUIMessageReadViewController_Minimalist *readViewController =
  1257. [[TUIMessageReadViewController_Minimalist alloc] initWithCellData:data
  1258. dataProvider:self.messageDataProvider
  1259. showReadStatusDisable:NO
  1260. c2cReceiverName:self.conversationData.title
  1261. c2cReceiverAvatar:self.conversationData.faceUrl];
  1262. CGRect frame = [UIApplication.sharedApplication.keyWindow convertRect:cell.frame fromView:cell];
  1263. readViewController.originFrame = frame;
  1264. readViewController.alertCellClass = cell.class;
  1265. @weakify(self);
  1266. readViewController.viewWillShowHandler = ^(TUIMessageCell *_Nonnull alertView) {
  1267. alertView.delegate = self;
  1268. };
  1269. readViewController.viewWillDismissHandler = ^(TUIMessageCell * _Nonnull alertView) {
  1270. self.hasCoverPage = NO;
  1271. };
  1272. readViewController.alertViewCellData = alertViewCellData;
  1273. self.hasCoverPage = YES;
  1274. [self.navigationController pushViewController:readViewController animated:YES];
  1275. }];
  1276. }
  1277. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  1278. if (action == @selector(onDelete:) || action == @selector(onRevoke:) || action == @selector(onReSend:) || action == @selector(onCopyMsg:) ||
  1279. action == @selector(onMulitSelect:) || action == @selector(onForward:) || action == @selector(onReply:)) {
  1280. return YES;
  1281. }
  1282. return NO;
  1283. }
  1284. - (BOOL)canBecomeFirstResponder {
  1285. return YES;
  1286. }
  1287. - (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder API_AVAILABLE(ios(13.0)) {
  1288. if (@available(iOS 16.0, *)) {
  1289. [builder removeMenuForIdentifier:UIMenuLookup];
  1290. }
  1291. [super buildMenuWithBuilder:builder];
  1292. }
  1293. - (void)menuDidHide:(NSNotification *)notification {
  1294. if (_delegate && [_delegate respondsToSelector:@selector(didHideMenuInMessageController:)]) {
  1295. [_delegate didHideMenuInMessageController:self];
  1296. }
  1297. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerDidHideMenuNotification object:nil];
  1298. }
  1299. - (void)onCopyMsg:(id)sender {
  1300. NSString *content = @"";
  1301. /**
  1302. *
  1303. * The text message should be based on the content of the message actually selected by the cursor
  1304. */
  1305. if ([sender isKindOfClass:[TUITextMessageCell_Minimalist class]]) {
  1306. TUITextMessageCell_Minimalist *txtCell = (TUITextMessageCell_Minimalist *)sender;
  1307. content = txtCell.textData.content;
  1308. }
  1309. if ([sender isKindOfClass:TUIReferenceMessageCell_Minimalist.class]) {
  1310. TUIReferenceMessageCell_Minimalist *replyMsgCell = (TUIReferenceMessageCell_Minimalist *)sender;
  1311. TUIReferenceMessageCellData *replyMsg = (TUIReferenceMessageCellData *)replyMsgCell.data;
  1312. content = replyMsg.content;
  1313. }
  1314. if (content.length > 0) {
  1315. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  1316. pasteboard.string = content;
  1317. [TUITool makeToast:TIMCommonLocalizableString(Copied)];
  1318. }
  1319. }
  1320. - (void)onRevoke:(id)sender {
  1321. @weakify(self);
  1322. [self.messageDataProvider revokeUIMsg:self.menuUIMsg
  1323. SuccBlock:^{
  1324. @strongify(self);
  1325. if (self.delegate && [self.delegate respondsToSelector:@selector(didHideMenuInMessageController:)]) {
  1326. [self.delegate didHideMenuInMessageController:self];
  1327. }
  1328. }
  1329. FailBlock:^(int code, NSString *desc) {
  1330. NSAssert(NO, desc);
  1331. }];
  1332. }
  1333. - (void)onReSend:(id)sender {
  1334. [self sendUIMessage:_menuUIMsg];
  1335. }
  1336. - (void)onMulitSelect:(id)sender {
  1337. [self enableMultiSelectedMode:YES];
  1338. if (self.menuUIMsg.innerMessage.hasRiskContent) {
  1339. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onSelectMessageMenu:withData:)]) {
  1340. [_delegate messageController:self onSelectMessageMenu:0 withData:nil];
  1341. }
  1342. return;
  1343. }
  1344. self.menuUIMsg.selected = YES;
  1345. [self.tableView beginUpdates];
  1346. NSInteger index = [self.messageDataProvider.uiMsgs indexOfObject:self.menuUIMsg];
  1347. [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ] withRowAnimation:UITableViewRowAnimationNone];
  1348. [self.tableView endUpdates];
  1349. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onSelectMessageMenu:withData:)]) {
  1350. [_delegate messageController:self onSelectMessageMenu:0 withData:_menuUIMsg];
  1351. }
  1352. }
  1353. - (void)onForward:(id)sender {
  1354. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onSelectMessageMenu:withData:)]) {
  1355. [_delegate messageController:self onSelectMessageMenu:1 withData:_menuUIMsg];
  1356. }
  1357. }
  1358. - (void)onReply:(id)sender {
  1359. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onRelyMessage:)]) {
  1360. [_delegate messageController:self onRelyMessage:self.menuUIMsg];
  1361. }
  1362. }
  1363. - (void)onReference:(id)sender {
  1364. if (_delegate && [_delegate respondsToSelector:@selector(messageController:onReferenceMessage:)]) {
  1365. [_delegate messageController:self onReferenceMessage:self.menuUIMsg];
  1366. }
  1367. }
  1368. - (BOOL)supportCheckBox:(TUIMessageCellData *)data {
  1369. if ([data isKindOfClass:TUISystemMessageCellData.class]) {
  1370. return NO;
  1371. }
  1372. return YES;
  1373. }
  1374. - (BOOL)supportRelay:(TUIMessageCellData *)data {
  1375. if ([data isKindOfClass:TUIVoiceMessageCellData.class]) {
  1376. return NO;
  1377. }
  1378. return YES;
  1379. }
  1380. - (void)enableMultiSelectedMode:(BOOL)enable {
  1381. self.showCheckBox = enable;
  1382. if (!enable) {
  1383. for (TUIMessageCellData *cellData in self.messageDataProvider.uiMsgs) {
  1384. cellData.selected = NO;
  1385. }
  1386. }
  1387. [self.tableView reloadData];
  1388. }
  1389. - (NSArray<TUIMessageCellData *> *)multiSelectedResult:(TUIMultiResultOption)option {
  1390. NSMutableArray *arrayM = [NSMutableArray array];
  1391. if (!self.showCheckBox) {
  1392. return [NSArray arrayWithArray:arrayM];
  1393. }
  1394. BOOL filterUnsupported = option & TUIMultiResultOptionFiterUnsupportRelay;
  1395. for (TUIMessageCellData *data in self.messageDataProvider.uiMsgs) {
  1396. if (data.selected) {
  1397. if (filterUnsupported && ![self supportRelay:data]) {
  1398. continue;
  1399. }
  1400. [arrayM addObject:data];
  1401. }
  1402. }
  1403. return [NSArray arrayWithArray:arrayM];
  1404. }
  1405. - (void)deleteMessages:(NSArray<TUIMessageCellData *> *)uiMsgs {
  1406. [self.messageDataProvider deleteUIMsgs:uiMsgs
  1407. SuccBlock:nil
  1408. FailBlock:^(int code, NSString *desc) {
  1409. NSLog(@"deleteMessages failed!");
  1410. NSAssert(NO, desc);
  1411. }];
  1412. }
  1413. - (void)clickTextMessage:(TUITextMessageCell_Minimalist *)cell {
  1414. V2TIMMessage *message = cell.messageData.innerMessage;
  1415. if (0 == message.userID.length) {
  1416. return;
  1417. }
  1418. [TUIMessageDataProvider.callingDataProvider redialFromMessage:message];
  1419. }
  1420. - (void)clickSystemMessage:(TUISystemMessageCell *)cell {
  1421. TUISystemMessageCellData *data = (TUISystemMessageCellData *)cell.messageData;
  1422. if (data.supportReEdit) {
  1423. if (self.delegate && [self.delegate respondsToSelector:@selector(messageController:onReEditMessage:)]) {
  1424. [self.delegate messageController:self onReEditMessage:cell.messageData];
  1425. }
  1426. }
  1427. }
  1428. - (void)playVoiceMessage:(TUIVoiceMessageCell_Minimalist *)cell {
  1429. for (TUIMessageCellData *cellData in self.messageDataProvider.uiMsgs) {
  1430. if (![cellData isKindOfClass:[TUIVoiceMessageCellData class]]) {
  1431. continue;
  1432. }
  1433. TUIVoiceMessageCellData *voiceMsg = (TUIVoiceMessageCellData *)cellData;
  1434. if (voiceMsg == cell.voiceData) {
  1435. [voiceMsg playVoiceMessage];
  1436. cell.voiceReadPoint.hidden = YES;
  1437. } else {
  1438. [voiceMsg stopVoiceMessage];
  1439. }
  1440. }
  1441. }
  1442. - (void)showImageMessage:(TUIImageMessageCell_Minimalist *)cell {
  1443. [self hideKeyboardIfNeeded];
  1444. CGRect frame = [cell.thumb convertRect:cell.thumb.bounds toView:[UIApplication sharedApplication].delegate.window];
  1445. TUIMediaView_Minimalist *mediaView = [[TUIMediaView_Minimalist alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
  1446. [mediaView setThumb:cell.thumb frame:frame];
  1447. [mediaView setCurMessage:cell.messageData.innerMessage];
  1448. __weak typeof(self) weakSelf = self;
  1449. mediaView.onClose = ^{
  1450. [weakSelf didCloseMediaMessage:cell];
  1451. };
  1452. [self willShowMediaMessage:cell];
  1453. [[UIApplication sharedApplication].keyWindow addSubview:mediaView];
  1454. }
  1455. - (void)showVideoMessage:(TUIVideoMessageCell_Minimalist *)cell {
  1456. [self hideKeyboardIfNeeded];
  1457. CGRect frame = [cell.thumb convertRect:cell.thumb.bounds toView:[UIApplication sharedApplication].delegate.window];
  1458. TUIMediaView_Minimalist *mediaView = [[TUIMediaView_Minimalist alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
  1459. [mediaView setThumb:cell.thumb frame:frame];
  1460. [mediaView setCurMessage:cell.messageData.innerMessage];
  1461. __weak typeof(self) weakSelf = self;
  1462. mediaView.onClose = ^{
  1463. [weakSelf didCloseMediaMessage:cell];
  1464. };
  1465. [self willShowMediaMessage:cell];
  1466. [[UIApplication sharedApplication].keyWindow addSubview:mediaView];
  1467. }
  1468. - (void)showFileMessage:(TUIFileMessageCell_Minimalist *)cell {
  1469. [self hideKeyboardIfNeeded];
  1470. TUIFileMessageCellData *fileData = cell.fileData;
  1471. if (![fileData isLocalExist]) {
  1472. [fileData downloadFile];
  1473. return;
  1474. }
  1475. if (self.popAlertController) {
  1476. [self.popAlertController blurDismissViewControllerAnimated:NO
  1477. completion:^(BOOL finished){
  1478. }];
  1479. }
  1480. TUIFileViewController_Minimalist *file = [[TUIFileViewController_Minimalist alloc] init];
  1481. file.data = [cell fileData];
  1482. [self.navigationController pushViewController:file animated:YES];
  1483. }
  1484. - (void)showRelayMessage:(TUIMergeMessageCell_Minimalist *)cell {
  1485. if (self.popAlertController) {
  1486. [self.popAlertController blurDismissViewControllerAnimated:NO
  1487. completion:^(BOOL finished){
  1488. }];
  1489. }
  1490. TUIMergeMessageListController_Minimalist *mergeVc = [[TUIMergeMessageListController_Minimalist alloc] init];
  1491. mergeVc.delegate = self.delegate;
  1492. mergeVc.mergerElem = cell.mergeData.mergerElem;
  1493. mergeVc.conversationData = self.conversationData;
  1494. mergeVc.parentPageDataProvider = self.messageDataProvider;
  1495. __weak typeof(self) weakSelf = self;
  1496. mergeVc.willCloseCallback = ^() {
  1497. [weakSelf.tableView reloadData];
  1498. };
  1499. [self.navigationController pushViewController:mergeVc animated:YES];
  1500. }
  1501. - (void)showLinkMessage:(TUILinkCell_Minimalist *)cell {
  1502. TUILinkCellData *cellData = cell.customData;
  1503. if (cellData.link) {
  1504. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cellData.link]];
  1505. }
  1506. }
  1507. - (void)showOrderMessage:(TUIOrderCell_Minimalist *)cell {
  1508. TUIOrderCellData *cellData = cell.customData;
  1509. if (cellData.link) {
  1510. [TUITool openLinkWithURL:[NSURL URLWithString:cellData.link]];
  1511. }
  1512. }
  1513. - (void)showReplyMessage:(TUIReplyMessageCell_Minimalist *)cell {
  1514. // subclass override
  1515. }
  1516. - (void)willShowMediaMessage:(TUIMessageCell *)cell {
  1517. // subclass override
  1518. }
  1519. - (void)didCloseMediaMessage:(TUIMessageCell *)cell {
  1520. // subclass override
  1521. }
  1522. - (void)onDelete:(TUIMessageCell *)cell {
  1523. // subclass override
  1524. }
  1525. #pragma mark - config TUIChatPopContextExtionItems
  1526. - (TUIChatPopContextExtionItem *)setupCopyAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1527. @weakify(self);
  1528. @weakify(cell);
  1529. @weakify(alertController);
  1530. TUIChatPopContextExtionItem *copyItem =
  1531. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Copy)
  1532. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_copy")]
  1533. weight:10000
  1534. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1535. @strongify(alertController);
  1536. [alertController blurDismissViewControllerAnimated:NO
  1537. completion:^(BOOL finished) {
  1538. @strongify(self);
  1539. @strongify(cell);
  1540. [self onCopyMsg:cell];
  1541. }];
  1542. }];
  1543. return copyItem;
  1544. }
  1545. - (TUIChatPopContextExtionItem *)setupForwardAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1546. @weakify(self);
  1547. @weakify(alertController);
  1548. TUIChatPopContextExtionItem *forwardItem =
  1549. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Forward)
  1550. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_forward")]
  1551. weight:9000
  1552. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1553. @strongify(alertController);
  1554. [alertController blurDismissViewControllerAnimated:NO
  1555. completion:^(BOOL finished) {
  1556. @strongify(self);
  1557. [self onForward:nil];
  1558. }];
  1559. }];
  1560. return forwardItem;
  1561. }
  1562. - (TUIChatPopContextExtionItem *)setupMultiSelectAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1563. @weakify(self);
  1564. @weakify(alertController);
  1565. TUIChatPopContextExtionItem *multiSelectItem =
  1566. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(MultiSelect)
  1567. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_multi")]
  1568. weight:8000
  1569. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1570. @strongify(alertController);
  1571. [alertController blurDismissViewControllerAnimated:NO
  1572. completion:^(BOOL finished) {
  1573. @strongify(self);
  1574. [self onMulitSelect:nil];
  1575. }];
  1576. }];
  1577. return multiSelectItem;
  1578. }
  1579. - (TUIChatPopContextExtionItem *)setupReferenceAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1580. @weakify(self);
  1581. @weakify(alertController);
  1582. TUIChatPopContextExtionItem *referenceItem =
  1583. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Quote)
  1584. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_quote")]
  1585. weight:7000
  1586. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1587. @strongify(alertController);
  1588. [alertController blurDismissViewControllerAnimated:NO
  1589. completion:^(BOOL finished) {
  1590. @strongify(self);
  1591. [self onReference:nil];
  1592. }];
  1593. }];
  1594. return referenceItem;
  1595. }
  1596. - (TUIChatPopContextExtionItem *)setupReplyAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1597. @weakify(self);
  1598. @weakify(alertController);
  1599. TUIChatPopContextExtionItem *replyItem =
  1600. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Reply)
  1601. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_reply")]
  1602. weight:5000
  1603. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1604. @strongify(alertController);
  1605. [alertController blurDismissViewControllerAnimated:NO
  1606. completion:^(BOOL finished) {
  1607. @strongify(self);
  1608. [self onReply:nil];
  1609. }];
  1610. }];
  1611. return replyItem;
  1612. }
  1613. - (TUIChatPopContextExtionItem *)setupRecallAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1614. @weakify(self);
  1615. @weakify(alertController);
  1616. TUIChatPopContextExtionItem *revocationItem =
  1617. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Recall)
  1618. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_revocation")]
  1619. weight:3000
  1620. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1621. @strongify(alertController);
  1622. [alertController blurDismissViewControllerAnimated:NO
  1623. completion:^(BOOL finished) {
  1624. @strongify(self);
  1625. [self onRevoke:nil];
  1626. }];
  1627. }];
  1628. return revocationItem;
  1629. }
  1630. - (TUIChatPopContextExtionItem *)setupInfoAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1631. @weakify(self);
  1632. @weakify(cell);
  1633. @weakify(alertController);
  1634. TUIChatPopContextExtionItem *infoItem =
  1635. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Info)
  1636. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_info")]
  1637. weight:2000
  1638. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1639. @strongify(alertController);
  1640. [alertController blurDismissViewControllerAnimated:NO
  1641. completion:^(BOOL finished) {
  1642. @strongify(self);
  1643. @strongify(cell);
  1644. [self onJumpToMessageInfoPage:cell.messageData selectCell:cell];
  1645. }];
  1646. }];
  1647. return infoItem;
  1648. }
  1649. - (TUIChatPopContextExtionItem *)setupDeleteAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1650. @weakify(self);
  1651. @weakify(cell);
  1652. @weakify(alertController);
  1653. TUIChatPopContextExtionItem *deleteItem =
  1654. [[TUIChatPopContextExtionItem alloc] initWithTitle:TIMCommonLocalizableString(Delete)
  1655. markIcon:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_delete")]
  1656. weight:1000
  1657. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1658. @strongify(alertController);
  1659. [alertController blurDismissViewControllerAnimated:NO
  1660. completion:^(BOOL finished) {
  1661. @strongify(self);
  1662. @strongify(cell);
  1663. [self onDelete:cell];
  1664. }];
  1665. }];
  1666. deleteItem.titleColor = [UIColor tui_colorWithHex:@"FF584C"];
  1667. return deleteItem;
  1668. }
  1669. - (TUIChatPopContextExtionItem *)setupAudioPlaybackStyleAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1670. @weakify(self);
  1671. @weakify(cell);
  1672. @weakify(alertController);
  1673. TUIVoiceAudioPlaybackStyle originStyle = [TUIVoiceMessageCellData getAudioplaybackStyle];
  1674. TUIChatPopContextExtionItem *styleActionItem = nil;
  1675. __weak typeof(styleActionItem) weakStyleActionItem = styleActionItem;
  1676. NSString *title = @"";
  1677. UIImage * img = nil;
  1678. if (originStyle == TUIVoiceAudioPlaybackStyleLoudspeaker) {
  1679. title = TIMCommonLocalizableString(TUIKitAudioPlaybackStyleHandset);
  1680. img = [UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_loudspeaker")];
  1681. }
  1682. else {
  1683. title = TIMCommonLocalizableString(TUIKitAudioPlaybackStyleLoudspeaker);
  1684. img = [UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_handset")];
  1685. }
  1686. styleActionItem =
  1687. [[TUIChatPopContextExtionItem alloc] initWithTitle:title
  1688. markIcon:img
  1689. weight:11000
  1690. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1691. @strongify(alertController);
  1692. [alertController blurDismissViewControllerAnimated:NO
  1693. completion:^(BOOL finished) {
  1694. if (originStyle == TUIVoiceAudioPlaybackStyleLoudspeaker) {
  1695. // Change To Handset
  1696. weakStyleActionItem.title = TIMCommonLocalizableString(TUIKitAudioPlaybackStyleLoudspeaker);
  1697. [TUITool hideToast];
  1698. [TUITool makeToast:TIMCommonLocalizableString(TUIKitAudioPlaybackStyleChange2Handset) duration:2];
  1699. } else {
  1700. weakStyleActionItem.title = TIMCommonLocalizableString(TUIKitAudioPlaybackStyleHandset);
  1701. [TUITool hideToast];
  1702. [TUITool makeToast:TIMCommonLocalizableString(TUIKitAudioPlaybackStyleChange2Loudspeaker) duration:2];
  1703. }
  1704. [TUIVoiceMessageCellData changeAudioPlaybackStyle];
  1705. }];
  1706. }];
  1707. return styleActionItem;
  1708. }
  1709. - (BOOL)isCurrentUserRoleSuperAdminInGroup {
  1710. return [self.messageDataProvider isCurrentUserRoleSuperAdminInGroup];
  1711. }
  1712. - (BOOL)isCurrentMessagePin:(NSString *)msgID {
  1713. return [self.messageDataProvider isCurrentMessagePin:msgID];
  1714. }
  1715. - (void)unPinGroupMessage:(V2TIMMessage *)innerMessage {
  1716. NSString *groupId = self.conversationData.groupID;
  1717. BOOL isPinned = [self.messageDataProvider isCurrentMessagePin:innerMessage.msgID];
  1718. BOOL pinOrUnpin = !isPinned;
  1719. [self.messageDataProvider pinGroupMessage:groupId message:innerMessage isPinned:pinOrUnpin succ:^{
  1720. } fail:^(int code, NSString *desc) {
  1721. }];
  1722. }
  1723. - (TUIChatPopContextExtionItem *)setupGroupPinAction:(TUIChatPopContextController *)alertController targetCell:(TUIMessageCell *)cell {
  1724. @weakify(self);
  1725. @weakify(cell);
  1726. @weakify(alertController);
  1727. BOOL isPinned = [self.messageDataProvider isCurrentMessagePin:self.menuUIMsg.innerMessage.msgID];
  1728. UIImage* img = isPinned ? [UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_unpin")] :
  1729. [UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_extion_pin")];
  1730. TUIChatPopContextExtionItem *groupPinAction =
  1731. [[TUIChatPopContextExtionItem alloc] initWithTitle:isPinned?
  1732. TIMCommonLocalizableString(TUIKitGroupMessageUnPin) : TIMCommonLocalizableString(TUIKitGroupMessagePin)
  1733. markIcon:img
  1734. weight:900
  1735. withActionHandler:^(TUIChatPopContextExtionItem *action) {
  1736. @strongify(alertController);
  1737. [alertController blurDismissViewControllerAnimated:NO
  1738. completion:^(BOOL finished) {
  1739. @strongify(self);
  1740. [self onGroupPin:nil
  1741. currentStatus:isPinned];
  1742. }];
  1743. }];
  1744. return groupPinAction;
  1745. }
  1746. - (void)onGroupPin:(id)sender currentStatus:(BOOL)currentStatus {
  1747. NSString *groupId = self.conversationData.groupID;
  1748. BOOL isPinned = currentStatus;
  1749. BOOL pinOrUnpin = !isPinned;
  1750. [self.messageDataProvider pinGroupMessage:groupId message:self.menuUIMsg.innerMessage isPinned:pinOrUnpin succ:^{
  1751. } fail:^(int code, NSString *desc) {
  1752. if (code == 10070) {
  1753. [TUITool makeToast:TIMCommonLocalizableString(TUIKitGroupMessagePinOverLimit)];
  1754. }
  1755. else if (code == 10004) {
  1756. if (pinOrUnpin) {
  1757. [TUITool makeToast:TIMCommonLocalizableString(TUIKitGroupMessagePinRepeatedly)];
  1758. }
  1759. else {
  1760. [TUITool makeToast:TIMCommonLocalizableString(TUIKitGroupMessageUnPinRepeatedly)];
  1761. }
  1762. }
  1763. }];
  1764. }
  1765. @end