WFCUConversationTableViewController.m 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. //
  2. // ConversationTableViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/8/29.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUConversationTableViewController.h"
  9. #import "WFCUConversationTableViewCell.h"
  10. #import "WFCUContactListViewController.h"
  11. #import "WFCUFriendRequestViewController.h"
  12. #import "WFCUSearchGroupTableViewCell.h"
  13. #import "WFCUConversationSearchTableViewController.h"
  14. #import "WFCUSearchChannelViewController.h"
  15. #import "WFCUCreateChannelViewController.h"
  16. #import "WFCUMessageListViewController.h"
  17. #import <WFChatClient/WFCChatClient.h>
  18. #import "WFCUUtilities.h"
  19. #import "UITabBar+badge.h"
  20. #import "KxMenu.h"
  21. #import "UIImage+ERCategory.h"
  22. #import "MBProgressHUD.h"
  23. #import "WFCUPinyinUtility.h"
  24. #import "WFCUContactTableViewCell.h"
  25. #import "QrCodeHelper.h"
  26. #import "WFCUConfigManager.h"
  27. #import "UIImage+ERCategory.h"
  28. #import "UIFont+YH.h"
  29. #import "UIColor+YH.h"
  30. #import "UIView+Toast.h"
  31. #import "WFCUSeletedUserViewController.h"
  32. #import "WFCUEnum.h"
  33. #import "WFCUImage.h"
  34. @interface WFCUConversationTableViewController () <UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>
  35. @property (nonatomic, strong)NSMutableArray<WFCCConversationInfo *> *conversations;
  36. @property (nonatomic, strong) UISearchController *searchController;
  37. @property (nonatomic, strong) NSArray<WFCCConversationSearchInfo *> *searchConversationList;
  38. @property (nonatomic, strong) NSArray<WFCCUserInfo *> *searchFriendList;
  39. @property (nonatomic, strong) NSArray<WFCCGroupSearchInfo *> *searchGroupList;
  40. @property (nonatomic ,assign) BOOL isSearchConversationListExpansion;
  41. @property (nonatomic ,assign) BOOL isSearchFriendListExpansion;
  42. @property (nonatomic ,assign) BOOL isSearchGroupListExpansion;
  43. @property (nonatomic, strong) UITableView *tableView;
  44. @property (nonatomic, strong) UIView *searchViewContainer;
  45. @property (nonatomic, assign) BOOL firstAppear;
  46. @property (nonatomic, strong) UIView *pcSessionView;
  47. @property (nonatomic, strong) UILabel *pcSessionLabel;
  48. @end
  49. @implementation WFCUConversationTableViewController
  50. - (void)initSearchUIAndTableView {
  51. _searchConversationList = [NSMutableArray array];
  52. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  53. self.searchController.searchResultsUpdater = self;
  54. self.searchController.delegate = self;
  55. self.searchController.dimsBackgroundDuringPresentation = NO;
  56. if (@available(iOS 13, *)) {
  57. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  58. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  59. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  60. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  61. } else {
  62. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  63. }
  64. if (@available(iOS 9.1, *)) {
  65. self.searchController.obscuresBackgroundDuringPresentation = NO;
  66. }
  67. self.searchController.searchBar.placeholder = WFCString(@"Search");
  68. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  69. if (@available(iOS 15, *)) {
  70. self.tableView.sectionHeaderTopPadding = 0;
  71. }
  72. [self.view addSubview:self.tableView];
  73. self.tableView.delegate = self;
  74. self.tableView.dataSource = self;
  75. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  76. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  77. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"expansion"];
  78. if (@available(iOS 11.0, *)) {
  79. self.navigationItem.searchController = _searchController;
  80. } else {
  81. self.tableView.tableHeaderView = _searchController.searchBar;
  82. }
  83. self.definesPresentationContext = YES;
  84. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  85. }
  86. - (void)onUserInfoUpdated:(NSNotification *)notification {
  87. if (self.searchController.active) {
  88. [self.tableView reloadData];
  89. }
  90. }
  91. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  92. if (self.searchController.active) {
  93. [self.tableView reloadData];
  94. }
  95. }
  96. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  97. if (self.searchController.active) {
  98. [self.tableView reloadData];
  99. }
  100. }
  101. - (void)onSendingMessageStatusUpdated:(NSNotification *)notification {
  102. if (self.searchController.active) {
  103. [self.tableView reloadData];
  104. } else {
  105. long messageId = [notification.object longValue];
  106. NSArray *dataSource = self.conversations;
  107. if (messageId == 0) {
  108. return;
  109. }
  110. for (int i = 0; i < dataSource.count; i++) {
  111. WFCCConversationInfo *conv = dataSource[i];
  112. if (conv.lastMessage && conv.lastMessage.direction == MessageDirection_Send && conv.lastMessage.messageId == messageId) {
  113. conv.lastMessage = [[WFCCIMService sharedWFCIMService] getMessage:messageId];
  114. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  115. }
  116. }
  117. }
  118. }
  119. - (void)onSecretChatStateChanged:(NSNotification *)notification {
  120. [self refreshList];
  121. [self refreshLeftButton];
  122. }
  123. - (void)onSecretMessageBurned:(NSNotification *)notification {
  124. [self refreshList];
  125. [self refreshLeftButton];
  126. }
  127. - (void)onRightBarBtn:(UIBarButtonItem *)sender {
  128. CGFloat searchExtra = 0;
  129. if ([KxMenu isShowing]) {
  130. [KxMenu dismissMenu];
  131. return;
  132. }
  133. NSArray *menuItems;
  134. if ([[WFCCIMService sharedWFCIMService] isEnableSecretChat] && [[WFCCIMService sharedWFCIMService] isUserEnableSecretChat]) {
  135. menuItems = @[
  136. [KxMenuItem menuItem:WFCString(@"StartChat")
  137. image:[WFCUImage imageNamed:@"menu_start_chat"]
  138. target:self
  139. action:@selector(startChatAction:)],
  140. [KxMenuItem menuItem:WFCString(@"StartSecretChat")
  141. image:[WFCUImage imageNamed:@"menu_start_chat"]
  142. target:self
  143. action:@selector(startSecretChatAction:)],
  144. [KxMenuItem menuItem:WFCString(@"AddFriend")
  145. image:[WFCUImage imageNamed:@"menu_add_friends"]
  146. target:self
  147. action:@selector(addFriendsAction:)],
  148. [KxMenuItem menuItem:WFCString(@"SubscribeChannel")
  149. image:[WFCUImage imageNamed:@"menu_listen_channel"]
  150. target:self
  151. action:@selector(listenChannelAction:)],
  152. [KxMenuItem menuItem:WFCString(@"ScanQRCode")
  153. image:[WFCUImage imageNamed:@"menu_scan_qr"]
  154. target:self
  155. action:@selector(scanQrCodeAction:)]
  156. ];
  157. } else {
  158. menuItems = @[
  159. [KxMenuItem menuItem:WFCString(@"StartChat")
  160. image:[WFCUImage imageNamed:@"menu_start_chat"]
  161. target:self
  162. action:@selector(startChatAction:)],
  163. [KxMenuItem menuItem:WFCString(@"AddFriend")
  164. image:[WFCUImage imageNamed:@"menu_add_friends"]
  165. target:self
  166. action:@selector(addFriendsAction:)],
  167. [KxMenuItem menuItem:WFCString(@"SubscribeChannel")
  168. image:[WFCUImage imageNamed:@"menu_listen_channel"]
  169. target:self
  170. action:@selector(listenChannelAction:)],
  171. [KxMenuItem menuItem:WFCString(@"ScanQRCode")
  172. image:[WFCUImage imageNamed:@"menu_scan_qr"]
  173. target:self
  174. action:@selector(scanQrCodeAction:)]
  175. ];
  176. }
  177. [KxMenu showMenuInView:self.navigationController.view
  178. fromRect:CGRectMake(self.view.bounds.size.width - 56, [WFCUUtilities wf_navigationFullHeight] + searchExtra, 48, 5)
  179. menuItems:menuItems];
  180. }
  181. - (void)startChatAction:(id)sender {
  182. WFCUSeletedUserViewController *pvc = [[WFCUSeletedUserViewController alloc] init];
  183. pvc.type = Horizontal;
  184. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  185. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  186. __weak typeof(self)ws = self;
  187. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  188. [navi dismissViewControllerAnimated:NO completion:nil];
  189. if (contacts.count == 1) {
  190. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  191. mvc.conversation = [WFCCConversation conversationWithType:Single_Type target:contacts[0] line:0];
  192. mvc.hidesBottomBarWhenPushed = YES;
  193. [ws.navigationController pushViewController:mvc animated:YES];
  194. } else {
  195. [self createGroup:contacts];
  196. }
  197. };
  198. [self.navigationController presentViewController:navi animated:YES completion:nil];
  199. }
  200. - (void)startSecretChatAction:(id)sender {
  201. WFCUSeletedUserViewController *pvc = [[WFCUSeletedUserViewController alloc] init];
  202. pvc.type = Horizontal;
  203. pvc.maxSelectCount = 1;
  204. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  205. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  206. __weak typeof(self)ws = self;
  207. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  208. [navi dismissViewControllerAnimated:NO completion:nil];
  209. if (contacts.count == 1) {
  210. [[WFCCIMService sharedWFCIMService] createSecretChat:contacts[0] success:^(NSString *targetId, int line) {
  211. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  212. mvc.conversation = [WFCCConversation conversationWithType:SecretChat_Type target:targetId line:line];
  213. mvc.hidesBottomBarWhenPushed = YES;
  214. [ws.navigationController pushViewController:mvc animated:YES];
  215. } error:^(int error_code) {
  216. }];
  217. }
  218. };
  219. [self.navigationController presentViewController:navi animated:YES completion:nil];
  220. }
  221. - (void)createGroup:(NSArray<NSString *> *)contacts {
  222. __weak typeof(self) ws = self;
  223. NSMutableArray<NSString *> *memberIds = [contacts mutableCopy];
  224. if (![memberIds containsObject:[WFCCNetworkService sharedInstance].userId]) {
  225. [memberIds insertObject:[WFCCNetworkService sharedInstance].userId atIndex:0];
  226. }
  227. NSString *name;
  228. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[memberIds objectAtIndex:0] refresh:NO];
  229. name = userInfo.displayName;
  230. for (int i = 1; i < MIN(8, memberIds.count); i++) {
  231. userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[memberIds objectAtIndex:i] refresh:NO];
  232. if (userInfo.displayName.length > 0) {
  233. if (name.length + userInfo.displayName.length + 1 > 16) {
  234. name = [name stringByAppendingString:WFCString(@"Etc")];
  235. break;
  236. }
  237. name = [name stringByAppendingFormat:@",%@", userInfo.displayName];
  238. }
  239. }
  240. if (name.length == 0) {
  241. name = WFCString(@"GroupChat");
  242. }
  243. NSString *extraStr = [WFCCUtilities getGroupMemberExtra:GroupMemberSource_Invite sourceTargetId:[WFCCNetworkService sharedInstance].userId];
  244. [[WFCCIMService sharedWFCIMService] createGroup:nil name:name portrait:nil type:GroupType_Restricted groupExtra:nil members:memberIds memberExtra:extraStr notifyLines:@[@(0)] notifyContent:nil success:^(NSString *groupId) {
  245. NSLog(@"create group success");
  246. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  247. mvc.conversation = [[WFCCConversation alloc] init];
  248. mvc.conversation.type = Group_Type;
  249. mvc.conversation.target = groupId;
  250. mvc.conversation.line = 0;
  251. mvc.hidesBottomBarWhenPushed = YES;
  252. [ws.navigationController pushViewController:mvc animated:YES];
  253. } error:^(int error_code) {
  254. NSLog(@"create group failure");
  255. [ws.view makeToast:WFCString(@"CreateGroupFailure")
  256. duration:2
  257. position:CSToastPositionCenter];
  258. }];
  259. }
  260. - (void)addFriendsAction:(id)sender {
  261. UIViewController *addFriendVC = [[WFCUFriendRequestViewController alloc] init];
  262. addFriendVC.hidesBottomBarWhenPushed = YES;
  263. [self.navigationController pushViewController:addFriendVC animated:YES];
  264. }
  265. - (void)listenChannelAction:(id)sender {
  266. UIViewController *searchChannelVC = [[WFCUSearchChannelViewController alloc] init];
  267. searchChannelVC.hidesBottomBarWhenPushed = YES;
  268. [self.navigationController pushViewController:searchChannelVC animated:YES];
  269. }
  270. - (void)scanQrCodeAction:(id)sender {
  271. if (gQrCodeDelegate) {
  272. [gQrCodeDelegate scanQrCode:self.navigationController];
  273. }
  274. }
  275. - (void)viewDidLoad {
  276. [super viewDidLoad];
  277. self.conversations = [[NSMutableArray alloc] init];
  278. [self initSearchUIAndTableView];
  279. self.definesPresentationContext = YES;
  280. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[WFCUImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  281. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClearAllUnread:) name:@"kTabBarClearBadgeNotification" object:nil];
  282. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  283. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  284. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:nil];
  285. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSendingMessageStatusUpdated:) name:kSendingMessageStatusUpdated object:nil];
  286. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageUpdated:) name:kMessageUpdated object:nil];
  287. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSecretChatStateChanged:) name:kSecretChatStateUpdated object:nil];
  288. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSecretMessageBurned:) name:kSecretMessageBurned object:nil];
  289. self.firstAppear = YES;
  290. }
  291. - (void)updateConnectionStatus:(ConnectionStatus)status {
  292. [self updateTitle];
  293. }
  294. - (void)updateTitle {
  295. UIView *title;
  296. ConnectionStatus status = [WFCCNetworkService sharedInstance].currentConnectionStatus;
  297. if (status != kConnectionStatusConnecting && status != kConnectionStatusReceiving) {
  298. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 40, 0, 80, 44)];
  299. switch (status) {
  300. case kConnectionStatusLogout:
  301. navLabel.text = WFCString(@"NotLogin");
  302. break;
  303. case kConnectionStatusConnected: {
  304. int count = 0;
  305. for (WFCCConversationInfo *info in self.conversations) {
  306. if (!info.isSilent) {
  307. count += info.unreadCount.unread;
  308. }
  309. }
  310. if (count) {
  311. navLabel.text = [NSString stringWithFormat:WFCString(@"NumberOfMessage"), count];
  312. } else {
  313. navLabel.text = WFCString(@"Message");
  314. }
  315. }
  316. break;
  317. default:
  318. case kConnectionStatusUnconnected:
  319. navLabel.text = WFCString(@"NotConnect");
  320. break;
  321. }
  322. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  323. navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
  324. navLabel.textAlignment = NSTextAlignmentCenter;
  325. title = navLabel;
  326. } else {
  327. UIView *continer = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 60, 0, 120, 44)];
  328. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 80, 40)];
  329. if (status == kConnectionStatusConnecting) {
  330. navLabel.text = WFCString(@"Connecting");
  331. } else {
  332. navLabel.text = WFCString(@"Synching");
  333. }
  334. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  335. navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
  336. [continer addSubview:navLabel];
  337. UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  338. indicatorView.center = CGPointMake(20, 21);
  339. [indicatorView startAnimating];
  340. indicatorView.color = [WFCUConfigManager globalManager].naviTextColor;
  341. [continer addSubview:indicatorView];
  342. title = continer;
  343. }
  344. self.navigationItem.titleView = title;
  345. }
  346. - (void)onConnectionStatusChanged:(NSNotification *)notification {
  347. ConnectionStatus status = [notification.object intValue];
  348. [self updateConnectionStatus:status];
  349. [self updatePcSession];
  350. }
  351. - (void)onReceiveMessages:(NSNotification *)notification {
  352. NSArray<WFCCMessage *> *messages = notification.object;
  353. if ([messages count]) {
  354. [self refreshList];
  355. [self refreshLeftButton];
  356. }
  357. }
  358. - (void)onMessageUpdated:(NSNotification *)notification {
  359. [self refreshList];
  360. [self refreshLeftButton];
  361. }
  362. - (void)onSettingUpdated:(NSNotification *)notification {
  363. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  364. [self refreshList];
  365. [self refreshLeftButton];
  366. [self updatePcSession];
  367. });
  368. }
  369. - (void)onRecallMessages:(NSNotification *)notification {
  370. [self refreshList];
  371. [self refreshLeftButton];
  372. }
  373. - (void)onDeleteMessages:(NSNotification *)notification {
  374. [self refreshList];
  375. [self refreshLeftButton];
  376. }
  377. - (void)onClearAllUnread:(NSNotification *)notification {
  378. if ([notification.object intValue] == 0) {
  379. [[WFCCIMService sharedWFCIMService] clearAllUnreadStatus];
  380. [self refreshList];
  381. [self refreshLeftButton];
  382. }
  383. }
  384. - (void)refreshList {
  385. self.conversations = [[[WFCCIMService sharedWFCIMService] getConversationInfos:@[@(Single_Type), @(Group_Type), @(Channel_Type), @(SecretChat_Type)] lines:@[@(0), @(5)]] mutableCopy];
  386. [self updateBadgeNumber];
  387. [self.tableView reloadData];
  388. }
  389. - (void)updateBadgeNumber {
  390. int count = 0;
  391. for (WFCCConversationInfo *info in self.conversations) {
  392. if (!info.isSilent) {
  393. count += info.unreadCount.unread;
  394. }
  395. }
  396. [self.tabBarController.tabBar showBadgeOnItemIndex:0 badgeValue:count];
  397. [self updateTitle];
  398. }
  399. - (void)updatePcSession {
  400. NSArray<WFCCPCOnlineInfo *> *onlines = [[WFCCIMService sharedWFCIMService] getPCOnlineInfos];
  401. if (@available(iOS 11.0, *)) {
  402. if (onlines.count && [WFCCNetworkService sharedInstance].currentConnectionStatus == kConnectionStatusConnected) {
  403. self.tableView.tableHeaderView = self.pcSessionView;
  404. if (![[NSUserDefaults standardUserDefaults] boolForKey:@"wfc_uikit_had_pc_session"]) {
  405. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"wfc_uikit_had_pc_session"];
  406. [[NSUserDefaults standardUserDefaults] synchronize];
  407. }
  408. } else {
  409. self.tableView.tableHeaderView = nil;
  410. }
  411. } else {
  412. }
  413. }
  414. - (void)didReceiveMemoryWarning {
  415. [super didReceiveMemoryWarning];
  416. }
  417. -(void)viewDidDisappear:(BOOL)animated {
  418. [super viewDidDisappear:animated];
  419. [self refreshLeftButton];
  420. if ([KxMenu isShowing]) {
  421. [KxMenu dismissMenu];
  422. }
  423. }
  424. - (void)viewWillAppear:(BOOL)animated {
  425. [super viewWillAppear:animated];
  426. if (self.firstAppear) {
  427. self.firstAppear = NO;
  428. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectionStatusChanged:) name:kConnectionStatusChanged object:nil];
  429. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessages:) name:kReceiveMessages object:nil];
  430. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRecallMessages:) name:kRecallMessages object:nil];
  431. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeleteMessages:) name:kDeleteMessages object:nil];
  432. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingUpdated:) name:kSettingUpdated object:nil];
  433. }
  434. [self updateConnectionStatus:[WFCCNetworkService sharedInstance].currentConnectionStatus];
  435. [self refreshList];
  436. [self refreshLeftButton];
  437. [self updatePcSession];
  438. }
  439. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  440. [super traitCollectionDidChange:previousTraitCollection];
  441. if (@available(iOS 13.0, *)) {
  442. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  443. [self.tableView reloadData];
  444. }
  445. }
  446. }
  447. - (void)viewDidAppear:(BOOL)animated {
  448. [super viewDidAppear:animated];
  449. if (self.searchController.isActive) {
  450. self.tabBarController.tabBar.hidden = YES;
  451. }
  452. }
  453. - (void)refreshLeftButton {
  454. dispatch_async(dispatch_get_main_queue(), ^{
  455. WFCCUnreadCount *unreadCount = [[WFCCIMService sharedWFCIMService] getUnreadCount:@[@(Single_Type), @(Group_Type), @(Channel_Type), @(SecretChat_Type)] lines:@[@(0)]];
  456. NSUInteger count = unreadCount.unread;
  457. NSString *title = nil;
  458. if (count > 0 && count < 1000) {
  459. title = [NSString stringWithFormat:WFCString(@"BackNumber"), count];
  460. } else if (count >= 1000) {
  461. title = WFCString(@"BackMore");
  462. } else {
  463. title = WFCString(@"Back");
  464. }
  465. UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
  466. item.title = title;
  467. self.navigationItem.backBarButtonItem = item;
  468. });
  469. }
  470. - (UIView *)pcSessionView {
  471. if (!_pcSessionView) {
  472. BOOL darkMode = NO;
  473. if (@available(iOS 13.0, *)) {
  474. if(UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  475. darkMode = YES;
  476. }
  477. }
  478. UIColor *bgColor;
  479. if (darkMode) {
  480. bgColor = [WFCUConfigManager globalManager].backgroudColor;
  481. } else {
  482. bgColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  483. }
  484. _pcSessionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
  485. [_pcSessionView setBackgroundColor:bgColor];
  486. UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 32, 32)];
  487. iv.image = [WFCUImage imageNamed:@"pc_session"];
  488. [_pcSessionView addSubview:iv];
  489. self.pcSessionLabel = [[UILabel alloc] initWithFrame:CGRectMake(68, 10, self.view.bounds.size.width - 68 - 16, 20)];
  490. self.pcSessionLabel.font = [UIFont systemFontOfSize:16];
  491. [_pcSessionView addSubview:self.pcSessionLabel];
  492. _pcSessionView.userInteractionEnabled = YES;
  493. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapPCBar:)];
  494. [_pcSessionView addGestureRecognizer:tap];
  495. }
  496. NSArray<WFCCPCOnlineInfo *> *infos = [[WFCCIMService sharedWFCIMService] getPCOnlineInfos];
  497. self.pcSessionLabel.text = nil;
  498. if (infos.count) {
  499. if (infos[0].platform == PlatformType_Windows) {
  500. self.pcSessionLabel.text = [NSString stringWithFormat:@"Windows %@", WFCString(@"LoggedIn")];
  501. } else if(infos[0].platform == PlatformType_OSX) {
  502. self.pcSessionLabel.text = [NSString stringWithFormat:@"Mac %@", WFCString(@"LoggedIn")];
  503. } else if(infos[0].platform == PlatformType_Linux) {
  504. self.pcSessionLabel.text = [NSString stringWithFormat:@"Linux %@", WFCString(@"LoggedIn")];
  505. } else if(infos[0].platform == PlatformType_HarmonyPC) {
  506. self.pcSessionLabel.text = [NSString stringWithFormat:@"鸿蒙电脑 %@", WFCString(@"LoggedIn")];
  507. } else if(infos[0].platform == PlatformType_WEB) {
  508. self.pcSessionLabel.text = [NSString stringWithFormat:@"Web %@", WFCString(@"LoggedIn")];
  509. } else if(infos[0].platform == PlatformType_WX) {
  510. self.pcSessionLabel.text = [NSString stringWithFormat:WFCString(@"%@LoggedIn"), WFCString(@"MicroApp")];
  511. } else if(infos[0].platform == PlatformType_iPad) {
  512. self.pcSessionLabel.text = [NSString stringWithFormat:@"iPad %@", WFCString(@"LoggedIn")];
  513. } else if(infos[0].platform == PlatformType_APad) {
  514. self.pcSessionLabel.text = [NSString stringWithFormat:WFCString(@"%@LoggedIn"), WFCString(@"AndroidPad")];
  515. } else if(infos[0].platform == PlatformType_HarmonyPad) {
  516. self.pcSessionLabel.text = [NSString stringWithFormat:@"鸿蒙平板 %@", WFCString(@"LoggedIn")];
  517. }
  518. if(self.pcSessionLabel.text.length && [[WFCCIMService sharedWFCIMService] isMuteNotificationWhenPcOnline]) {
  519. self.pcSessionLabel.text = [self.pcSessionLabel.text stringByAppendingFormat:@",%@", WFCString(@"MobileNoNotification")];
  520. }
  521. }
  522. return _pcSessionView;
  523. }
  524. - (void)onTapPCBar:(id)sender {
  525. NSArray<WFCCPCOnlineInfo *> *onlines = [[WFCCIMService sharedWFCIMService] getPCOnlineInfos];
  526. if ([[WFCUConfigManager globalManager].appServiceProvider respondsToSelector:@selector(showPCSessionViewController:pcClient:)]) {
  527. [[WFCUConfigManager globalManager].appServiceProvider showPCSessionViewController:self pcClient:[onlines objectAtIndex:0]];
  528. }
  529. }
  530. #pragma mark - Table view data source
  531. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  532. int sec = 0;
  533. if (self.searchFriendList.count) {
  534. sec++;
  535. }
  536. if (self.searchGroupList.count) {
  537. sec++;
  538. }
  539. if (self.searchConversationList.count) {
  540. sec++;
  541. }
  542. if (sec == 0) {
  543. sec = 1;
  544. }
  545. return sec;
  546. }
  547. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  548. if (self.searchController.active) {
  549. int sec = 0;
  550. if (self.searchFriendList.count) {
  551. sec++;
  552. if (section == sec-1) {
  553. if (self.isSearchFriendListExpansion) {
  554. return self.searchFriendList.count;
  555. } else {
  556. if (self.searchFriendList.count > 2) {
  557. return 3;
  558. } else {
  559. return self.searchFriendList.count;
  560. }
  561. }
  562. }
  563. }
  564. if (self.searchGroupList.count) {
  565. sec++;
  566. if (section == sec-1) {
  567. if (self.isSearchGroupListExpansion) {
  568. return self.searchGroupList.count;
  569. } else {
  570. if (self.searchGroupList.count > 2) {
  571. return 3;
  572. } else {
  573. return self.searchGroupList.count;
  574. }
  575. }
  576. }
  577. }
  578. if (self.searchConversationList.count) {
  579. sec++;
  580. if (sec-1 == section) {
  581. if (self.isSearchConversationListExpansion) {
  582. return self.searchConversationList.count;
  583. } else {
  584. if (self.searchConversationList.count > 2) {
  585. return 3;
  586. } else {
  587. return self.searchConversationList.count;
  588. }
  589. }
  590. }
  591. }
  592. return 0;
  593. } else {
  594. return self.conversations.count;
  595. }
  596. }
  597. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  598. if (self.searchController.active) {
  599. int sec = 0;
  600. if (self.searchFriendList.count) {
  601. sec++;
  602. if (indexPath.section == sec-1) {
  603. if (self.isSearchFriendListExpansion) {
  604. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  605. if (cell == nil) {
  606. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  607. }
  608. cell.big = NO;
  609. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  610. [cell setUserId:self.searchFriendList[indexPath.row].userId groupId:nil];
  611. return cell;
  612. } else {
  613. if (indexPath.row == 2) {
  614. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  615. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  616. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchFriendList.count - 2];
  617. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  618. return cell;
  619. } else {
  620. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  621. if (cell == nil) {
  622. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  623. }
  624. cell.big = NO;
  625. if (indexPath.row == 1) {
  626. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  627. } else {
  628. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  629. }
  630. [cell setUserId:self.searchFriendList[indexPath.row].userId groupId:nil];
  631. return cell;
  632. }
  633. }
  634. }
  635. }
  636. if (self.searchGroupList.count) {
  637. sec++;
  638. if (indexPath.section == sec-1) {
  639. if (self.isSearchGroupListExpansion) {
  640. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  641. if (cell == nil) {
  642. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  643. }
  644. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  645. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  646. return cell;
  647. } else {
  648. if (indexPath.row == 2) {
  649. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  650. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  651. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchGroupList.count - 2];
  652. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  653. return cell;
  654. } else {
  655. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  656. if (cell == nil) {
  657. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  658. }
  659. if (indexPath.row == 1) {
  660. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  661. } else {
  662. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  663. }
  664. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  665. return cell;
  666. }
  667. }
  668. }
  669. }
  670. if (self.searchConversationList.count) {
  671. sec++;
  672. if (sec-1 == indexPath.section) {
  673. if (self.isSearchConversationListExpansion) {
  674. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchConversationCell"];
  675. if (cell == nil) {
  676. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchConversationCell"];
  677. }
  678. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  679. cell.big = NO;
  680. cell.searchInfo = self.searchConversationList[indexPath.row];
  681. return cell;
  682. } else {
  683. if (indexPath.row == 2) {
  684. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  685. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  686. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchConversationList.count - 2];
  687. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  688. return cell;
  689. } else {
  690. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchConversationCell"];
  691. if (cell == nil) {
  692. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchConversationCell"];
  693. }
  694. if (indexPath.row == 1) {
  695. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  696. } else {
  697. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  698. } cell.big = NO;
  699. cell.searchInfo = self.searchConversationList[indexPath.row];
  700. return cell;
  701. }
  702. }
  703. }
  704. }
  705. return nil;
  706. } else {
  707. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  708. if (cell == nil) {
  709. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  710. }
  711. cell.big = YES;
  712. cell.separatorInset = UIEdgeInsetsMake(0, 76, 0, 0);
  713. cell.info = self.conversations[indexPath.row];
  714. return cell;
  715. }
  716. }
  717. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  718. if (self.searchController.active) {
  719. int sec = 0;
  720. if (self.searchFriendList.count) {
  721. sec++;
  722. if (indexPath.section == sec-1) {
  723. if (self.isSearchFriendListExpansion) {
  724. return 60;
  725. } else {
  726. if (indexPath.row == 2) {
  727. return 40;
  728. } else {
  729. return 60;
  730. }
  731. }
  732. }
  733. }
  734. if (self.searchGroupList.count) {
  735. sec++;
  736. if (indexPath.section == sec-1) {
  737. if (self.isSearchGroupListExpansion) {
  738. return 60;
  739. } else {
  740. if (indexPath.row == 2) {
  741. return 40;
  742. } else {
  743. return 60;
  744. }
  745. }
  746. }
  747. }
  748. if (self.searchConversationList.count) {
  749. sec++;
  750. if (sec-1 == indexPath.section ) {
  751. if (self.isSearchConversationListExpansion) {
  752. return 60;
  753. } else {
  754. if (indexPath.row == 2) {
  755. return 40;
  756. } else {
  757. return 60;
  758. }
  759. }
  760. }
  761. }
  762. return 60;
  763. } else {
  764. return 72;
  765. }
  766. }
  767. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  768. if (self.searchController.isActive) {
  769. if (self.searchConversationList.count + self.searchGroupList.count + self.searchFriendList.count > 0) {
  770. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 32)];
  771. header.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  772. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, self.tableView.frame.size.width, 32)];
  773. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  774. label.textColor = [UIColor colorWithHexString:@"0x828282"];
  775. label.textAlignment = NSTextAlignmentLeft;
  776. int sec = 0;
  777. if (self.searchFriendList.count) {
  778. sec++;
  779. if (section == sec-1) {
  780. label.text = WFCString(@"Contact");
  781. }
  782. }
  783. if (self.searchGroupList.count) {
  784. sec++;
  785. if (section == sec-1) {
  786. label.text = WFCString(@"Group");
  787. }
  788. }
  789. if (self.searchConversationList.count) {
  790. sec++;
  791. if (sec-1 == section) {
  792. label.text = WFCString(@"Message");
  793. }
  794. }
  795. [header addSubview:label];
  796. return header;
  797. } else {
  798. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
  799. return header;
  800. }
  801. } else {
  802. return nil;
  803. }
  804. }
  805. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  806. if (self.searchController.isActive) {
  807. return 32;
  808. }
  809. return 0;
  810. }
  811. // Override to support conditional editing of the table view.
  812. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  813. // Return NO if you do not want the specified item to be editable.
  814. if (self.searchController.active) {
  815. return NO;
  816. }
  817. return YES;
  818. }
  819. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  820. __weak typeof(self) ws = self;
  821. UITableViewRowAction *markAsUnread = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"MarkAsUnread") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  822. [[WFCCIMService sharedWFCIMService] markAsUnRead:ws.conversations[indexPath.row].conversation syncToOtherClient:YES];
  823. [ws refreshList];
  824. }];
  825. UITableViewRowAction *clearUnread = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"MarkAsRead") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  826. [[WFCCIMService sharedWFCIMService] clearUnreadStatus:ws.conversations[indexPath.row].conversation];
  827. [ws refreshList];
  828. }];
  829. UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  830. [[WFCCIMService sharedWFCIMService] clearUnreadStatus:ws.conversations[indexPath.row].conversation];
  831. [[WFCCIMService sharedWFCIMService] removeConversation:ws.conversations[indexPath.row].conversation clearMessage:YES];
  832. [ws.conversations removeObjectAtIndex:indexPath.row];
  833. [ws updateBadgeNumber];
  834. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  835. }];
  836. UITableViewRowAction *setTop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Pinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  837. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:1 success:^{
  838. [ws refreshList];
  839. } error:^(int error_code) {
  840. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  841. hud.label.text = WFCString(@"UpdateFailure");
  842. hud.mode = MBProgressHUDModeText;
  843. hud.removeFromSuperViewOnHide = YES;
  844. [hud hideAnimated:NO afterDelay:1.5];
  845. }];
  846. }];
  847. UITableViewRowAction *setUntop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Unpinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  848. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:0 success:^{
  849. [ws refreshList];
  850. } error:^(int error_code) {
  851. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  852. hud.label.text = WFCString(@"UpdateFailure");
  853. hud.mode = MBProgressHUDModeText;
  854. hud.removeFromSuperViewOnHide = YES;
  855. [hud hideAnimated:NO afterDelay:1.5];
  856. }];
  857. [self refreshList];
  858. }];
  859. setTop.backgroundColor = [UIColor purpleColor];
  860. setUntop.backgroundColor = [UIColor orangeColor];
  861. clearUnread.backgroundColor = [UIColor blueColor];
  862. markAsUnread.backgroundColor = [UIColor blueColor];
  863. if(self.conversations[indexPath.row].unreadCount.unread) {
  864. if (self.conversations[indexPath.row].isTop) {
  865. return @[delete, setUntop, clearUnread];
  866. } else {
  867. return @[delete, setTop, clearUnread];
  868. }
  869. } else {
  870. NSArray<WFCCMessage *> *readedMsgs = [[WFCCIMService sharedWFCIMService] getMessages:self.conversations[indexPath.row].conversation messageStatus:@[@(Message_Status_Readed), @(Message_Status_Played)] from:0 count:1 withUser:nil];
  871. if(readedMsgs.count) {
  872. if (self.conversations[indexPath.row].isTop) {
  873. return @[delete, setUntop, markAsUnread];
  874. } else {
  875. return @[delete, setTop, markAsUnread];
  876. }
  877. } else {
  878. if (self.conversations[indexPath.row].isTop) {
  879. return @[delete, setUntop];
  880. } else {
  881. return @[delete, setTop];
  882. }
  883. }
  884. }
  885. };
  886. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  887. if (self.searchController.active) {
  888. [self.searchController.searchBar resignFirstResponder];
  889. }
  890. }
  891. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  892. if (self.searchController.active) {
  893. int sec = 0;
  894. if (self.searchFriendList.count) {
  895. sec++;
  896. if (indexPath.section == sec-1) {
  897. if (!self.isSearchFriendListExpansion && indexPath.row == 2) {
  898. self.isSearchFriendListExpansion = YES;
  899. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  900. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  901. } else {
  902. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  903. WFCCUserInfo *info = self.searchFriendList[indexPath.row];
  904. mvc.conversation = [[WFCCConversation alloc] init];
  905. mvc.conversation.type = Single_Type;
  906. mvc.conversation.target = info.userId;
  907. mvc.conversation.line = 0;
  908. mvc.hidesBottomBarWhenPushed = YES;
  909. [self.navigationController pushViewController:mvc animated:YES];
  910. }
  911. }
  912. }
  913. if (self.searchGroupList.count) {
  914. sec++;
  915. if (indexPath.section == sec-1) {
  916. if (!self.isSearchGroupListExpansion && indexPath.row == 2) {
  917. self.isSearchGroupListExpansion = YES;
  918. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  919. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  920. } else {
  921. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  922. WFCCGroupSearchInfo *info = self.searchGroupList[indexPath.row];
  923. mvc.conversation = [[WFCCConversation alloc] init];
  924. mvc.conversation.type = Group_Type;
  925. mvc.conversation.target = info.groupInfo.target;
  926. mvc.conversation.line = 0;
  927. mvc.hidesBottomBarWhenPushed = YES;
  928. [self.navigationController pushViewController:mvc animated:YES];
  929. }
  930. }
  931. }
  932. if (self.searchConversationList.count) {
  933. sec++;
  934. if (sec-1 == indexPath.section) {
  935. if (!self.isSearchConversationListExpansion && indexPath.row == 2) {
  936. self.isSearchConversationListExpansion = YES;
  937. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  938. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  939. } else {
  940. WFCCConversationSearchInfo *info = self.searchConversationList[indexPath.row];
  941. if (info.marchedCount == 1) {
  942. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  943. mvc.conversation = info.conversation;
  944. mvc.highlightMessageId = info.marchedMessage.messageId;
  945. mvc.highlightText = info.keyword;
  946. mvc.hidesBottomBarWhenPushed = YES;
  947. [self.navigationController pushViewController:mvc animated:YES];
  948. } else {
  949. WFCUConversationSearchTableViewController *mvc = [[WFCUConversationSearchTableViewController alloc] init];
  950. mvc.conversation = info.conversation;
  951. mvc.keyword = info.keyword;
  952. mvc.hidesBottomBarWhenPushed = YES;
  953. [self.navigationController pushViewController:mvc animated:YES];
  954. }
  955. }
  956. }
  957. }
  958. } else {
  959. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  960. WFCCConversationInfo *info = self.conversations[indexPath.row];
  961. mvc.conversation = info.conversation;
  962. mvc.hidesBottomBarWhenPushed = YES;
  963. [self.navigationController pushViewController:mvc animated:YES];
  964. }
  965. }
  966. - (void)dealloc {
  967. [[NSNotificationCenter defaultCenter] removeObserver:self];
  968. _searchController = nil;
  969. _searchConversationList = nil;
  970. }
  971. #pragma mark - UISearchControllerDelegate
  972. - (void)didPresentSearchController:(UISearchController *)searchController {
  973. self.searchController.view.frame = self.view.bounds;
  974. self.isSearchFriendListExpansion = NO;
  975. self.isSearchConversationListExpansion = NO;
  976. self.isSearchGroupListExpansion = NO;
  977. self.tabBarController.tabBar.hidden = YES;
  978. self.extendedLayoutIncludesOpaqueBars = YES;
  979. }
  980. - (void)willDismissSearchController:(UISearchController *)searchController {
  981. self.tabBarController.tabBar.hidden = NO;
  982. self.extendedLayoutIncludesOpaqueBars = NO;
  983. }
  984. - (NSArray<WFCCUserInfo *> *)searchFriends:(NSString *)searchString {
  985. NSMutableArray<WFCCUserInfo *> *result = [[NSMutableArray alloc] init];
  986. if(searchString.length) {
  987. WFCUPinyinUtility *pu = [[WFCUPinyinUtility alloc] init];
  988. NSArray<WFCCUserInfo *> *dataArray = [[WFCCIMService sharedWFCIMService] getUserInfos:[[WFCCIMService sharedWFCIMService] getMyFriendList:NO] inGroup:nil];
  989. BOOL isChinese = [pu isChinese:searchString];
  990. for (WFCCUserInfo *friend in dataArray) {
  991. if ([friend.displayName.lowercaseString containsString:searchString.lowercaseString] || [friend.friendAlias.lowercaseString containsString:searchString.lowercaseString]) {
  992. [result addObject:friend];
  993. } else if(!isChinese) {
  994. if([pu isMatch:friend.displayName ofPinYin:searchString] || [pu isMatch:friend.friendAlias ofPinYin:searchString]) {
  995. [result addObject:friend];
  996. }
  997. }
  998. }
  999. }
  1000. return result;
  1001. }
  1002. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  1003. NSString *searchString = [self.searchController.searchBar text];
  1004. if (searchString.length) {
  1005. self.searchConversationList = [[WFCCIMService sharedWFCIMService] searchConversation:searchString inConversation:@[@(Single_Type), @(Group_Type), @(Channel_Type), @(SecretChat_Type)] lines:@[@(0)]];
  1006. self.searchFriendList = [self searchFriends:searchString];
  1007. self.searchGroupList = [[WFCCIMService sharedWFCIMService] searchGroups:searchString];
  1008. } else {
  1009. self.searchConversationList = nil;
  1010. self.searchFriendList = nil;
  1011. self.searchGroupList = nil;
  1012. }
  1013. [self.tableView reloadData];
  1014. }
  1015. @end