WFCUConversationTableViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 "WFCUCreateGroupViewController.h"
  12. #import "WFCUFriendRequestViewController.h"
  13. #import "WFCUSearchGroupTableViewCell.h"
  14. #import "WFCUConversationSearchTableViewController.h"
  15. #import "WFCUSearchChannelViewController.h"
  16. #import "WFCUCreateChannelViewController.h"
  17. #import "WFCUMessageListViewController.h"
  18. #import <WFChatClient/WFCChatClient.h>
  19. #import "WFCUUtilities.h"
  20. #import "UITabBar+badge.h"
  21. #import "KxMenu.h"
  22. #import "UIImage+ERCategory.h"
  23. #import "MBProgressHUD.h"
  24. #import "WFCUContactTableViewCell.h"
  25. #import "QrCodeHelper.h"
  26. #import "WFCUConfigManager.h"
  27. @interface WFCUConversationTableViewController () <UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>
  28. @property (nonatomic, strong)NSMutableArray<WFCCConversationInfo *> *conversations;
  29. @property (nonatomic, strong) UISearchController *searchController;
  30. @property (nonatomic, strong) NSArray<WFCCConversationSearchInfo *> *searchConversationList;
  31. @property (nonatomic, strong) NSArray<WFCCUserInfo *> *searchFriendList;
  32. @property (nonatomic, strong) NSArray<WFCCGroupSearchInfo *> *searchGroupList;
  33. @property (nonatomic, strong) UITableView *tableView;
  34. @property (nonatomic, strong) UIView *searchViewContainer;
  35. @property (nonatomic, assign) BOOL firstAppear;
  36. @property (nonatomic, strong) UIView *pcSessionView;
  37. @end
  38. @implementation WFCUConversationTableViewController
  39. - (void)initSearchUIAndTableView {
  40. _searchConversationList = [NSMutableArray array];
  41. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  42. self.searchController.searchResultsUpdater = self;
  43. self.searchController.delegate = self;
  44. self.searchController.dimsBackgroundDuringPresentation = NO;
  45. if (@available(iOS 13, *)) {
  46. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  47. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  48. } else {
  49. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  50. }
  51. if (@available(iOS 9.1, *)) {
  52. self.searchController.obscuresBackgroundDuringPresentation = NO;
  53. }
  54. self.searchController.searchBar.placeholder = WFCString(@"Search");
  55. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  56. [self.view addSubview:self.tableView];
  57. self.tableView.delegate = self;
  58. self.tableView.dataSource = self;
  59. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  60. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  61. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  62. if (@available(iOS 11.0, *)) {
  63. self.navigationItem.searchController = _searchController;
  64. } else {
  65. self.tableView.tableHeaderView = _searchController.searchBar;
  66. }
  67. self.definesPresentationContext = YES;
  68. [self updatePcSession];
  69. }
  70. - (void)onUserInfoUpdated:(NSNotification *)notification {
  71. if (self.searchController.active) {
  72. [self.tableView reloadData];
  73. } else {
  74. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  75. NSArray *dataSource = self.conversations;
  76. for (int i = 0; i < dataSource.count; i++) {
  77. WFCCConversationInfo *conv = dataSource[i];
  78. if (conv.conversation.type == Single_Type && [conv.conversation.target isEqualToString:userInfo.userId]) {
  79. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  80. }
  81. }
  82. }
  83. }
  84. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  85. if (self.searchController.active) {
  86. [self.tableView reloadData];
  87. } else {
  88. WFCCGroupInfo *groupInfo = notification.userInfo[@"groupInfo"];
  89. NSArray *dataSource = self.conversations;
  90. for (int i = 0; i < dataSource.count; i++) {
  91. WFCCConversationInfo *conv = dataSource[i];
  92. if (conv.conversation.type == Group_Type && [conv.conversation.target isEqualToString:groupInfo.target]) {
  93. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  94. }
  95. }
  96. }
  97. }
  98. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  99. if (self.searchController.active) {
  100. [self.tableView reloadData];
  101. } else {
  102. WFCCChannelInfo *channelInfo = notification.userInfo[@"groupInfo"];
  103. NSArray *dataSource = self.conversations;
  104. for (int i = 0; i < dataSource.count; i++) {
  105. WFCCConversationInfo *conv = dataSource[i];
  106. if (conv.conversation.type == Channel_Type && [conv.conversation.target isEqualToString:channelInfo.channelId]) {
  107. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  108. }
  109. }
  110. }
  111. }
  112. - (void)onSendingMessageStatusUpdated:(NSNotification *)notification {
  113. if (self.searchController.active) {
  114. [self.tableView reloadData];
  115. } else {
  116. long messageId = [notification.object longValue];
  117. NSArray *dataSource = self.conversations;
  118. if (messageId == 0) {
  119. return;
  120. }
  121. for (int i = 0; i < dataSource.count; i++) {
  122. WFCCConversationInfo *conv = dataSource[i];
  123. if (conv.lastMessage && conv.lastMessage.direction == MessageDirection_Send && conv.lastMessage.messageId == messageId) {
  124. conv.lastMessage = [[WFCCIMService sharedWFCIMService] getMessage:messageId];
  125. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  126. }
  127. }
  128. }
  129. }
  130. - (void)onRightBarBtn:(UIBarButtonItem *)sender {
  131. CGFloat searchExtra = 0;
  132. if (@available(iOS 11.0, *)) {
  133. if (self.searchController.searchBar.bounds.size.height > 0) {
  134. searchExtra = 52;
  135. }
  136. }
  137. [KxMenu showMenuInView:self.view
  138. fromRect:CGRectMake(self.view.bounds.size.width - 56, kStatusBarAndNavigationBarHeight + searchExtra, 48, 5)
  139. menuItems:@[
  140. [KxMenuItem menuItem:WFCString(@"StartChat")
  141. image:[UIImage imageNamed:@"menu_start_chat"]
  142. target:self
  143. action:@selector(startChatAction:)],
  144. [KxMenuItem menuItem:WFCString(@"AddFriend")
  145. image:[UIImage imageNamed:@"menu_add_friends"]
  146. target:self
  147. action:@selector(addFriendsAction:)],
  148. [KxMenuItem menuItem:WFCString(@"SubscribeChannel")
  149. image:[UIImage imageNamed:@"menu_listen_channel"]
  150. target:self
  151. action:@selector(listenChannelAction:)],
  152. [KxMenuItem menuItem:WFCString(@"ScanQRCode")
  153. image:[UIImage imageNamed:@"menu_scan_qr"]
  154. target:self
  155. action:@selector(scanQrCodeAction:)]
  156. ]];
  157. }
  158. - (void)startChatAction:(id)sender {
  159. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  160. pvc.selectContact = YES;
  161. pvc.multiSelect = YES;
  162. pvc.showCreateChannel = YES;
  163. __weak typeof(self)ws = self;
  164. pvc.createChannel = ^(void) {
  165. WFCUCreateChannelViewController *vc = [[WFCUCreateChannelViewController alloc] init];
  166. vc.hidesBottomBarWhenPushed = YES;
  167. [self.navigationController pushViewController:vc animated:YES];
  168. };
  169. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  170. if (contacts.count == 1) {
  171. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  172. mvc.conversation = [WFCCConversation conversationWithType:Single_Type target:contacts[0] line:0];
  173. mvc.hidesBottomBarWhenPushed = YES;
  174. [ws.navigationController pushViewController:mvc animated:YES];
  175. } else {
  176. WFCUCreateGroupViewController *vc = [[WFCUCreateGroupViewController alloc] init];
  177. vc.memberIds = [contacts mutableCopy];
  178. if (![vc.memberIds containsObject:[WFCCNetworkService sharedInstance].userId]) {
  179. [vc.memberIds insertObject:[WFCCNetworkService sharedInstance].userId atIndex:0];
  180. }
  181. vc.hidesBottomBarWhenPushed = YES;
  182. [ws.navigationController pushViewController:vc animated:YES];
  183. }
  184. };
  185. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  186. [self.navigationController presentViewController:navi animated:YES completion:nil];
  187. }
  188. - (void)addFriendsAction:(id)sender {
  189. UIViewController *addFriendVC = [[WFCUFriendRequestViewController alloc] init];
  190. addFriendVC.hidesBottomBarWhenPushed = YES;
  191. [self.navigationController pushViewController:addFriendVC animated:YES];
  192. }
  193. - (void)listenChannelAction:(id)sender {
  194. UIViewController *searchChannelVC = [[WFCUSearchChannelViewController alloc] init];
  195. searchChannelVC.hidesBottomBarWhenPushed = YES;
  196. [self.navigationController pushViewController:searchChannelVC animated:YES];
  197. }
  198. - (void)scanQrCodeAction:(id)sender {
  199. if (gQrCodeDelegate) {
  200. [gQrCodeDelegate scanQrCode:self.navigationController];
  201. }
  202. }
  203. - (void)viewDidLoad {
  204. [super viewDidLoad];
  205. self.conversations = [[NSMutableArray alloc] init];
  206. [self initSearchUIAndTableView];
  207. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  208. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClearAllUnread:) name:@"kTabBarClearBadgeNotification" object:nil];
  209. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  210. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  211. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:nil];
  212. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSendingMessageStatusUpdated:) name:kSendingMessageStatusUpdated object:nil];
  213. self.firstAppear = YES;
  214. }
  215. - (void)updateConnectionStatus:(ConnectionStatus)status {
  216. UIView *title;
  217. if (status != kConnectionStatusConnecting && status != kConnectionStatusReceiving) {
  218. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 40, 0, 80, 44)];
  219. switch (status) {
  220. case kConnectionStatusLogout:
  221. navLabel.text = WFCString(@"NotLogin");
  222. break;
  223. case kConnectionStatusUnconnected:
  224. navLabel.text = WFCString(@"NotConnect");
  225. break;
  226. case kConnectionStatusConnected:
  227. navLabel.text = WFCString(@"Message");
  228. break;
  229. default:
  230. break;
  231. }
  232. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  233. navLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
  234. navLabel.textAlignment = NSTextAlignmentCenter;
  235. title = navLabel;
  236. } else {
  237. UIView *continer = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 60, 0, 120, 44)];
  238. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 80, 40)];
  239. if (status == kConnectionStatusConnecting) {
  240. navLabel.text = WFCString(@"Connecting");
  241. } else {
  242. navLabel.text = WFCString(@"Synching");
  243. }
  244. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  245. navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
  246. [continer addSubview:navLabel];
  247. UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  248. indicatorView.center = CGPointMake(20, 21);
  249. [indicatorView startAnimating];
  250. indicatorView.color = [WFCUConfigManager globalManager].naviTextColor;
  251. [continer addSubview:indicatorView];
  252. title = continer;
  253. }
  254. self.navigationItem.titleView = title;
  255. }
  256. - (void)onConnectionStatusChanged:(NSNotification *)notification {
  257. ConnectionStatus status = [notification.object intValue];
  258. [self updateConnectionStatus:status];
  259. }
  260. - (void)onReceiveMessages:(NSNotification *)notification {
  261. NSArray<WFCCMessage *> *messages = notification.object;
  262. if ([messages count]) {
  263. [self refreshList];
  264. [self refreshLeftButton];
  265. }
  266. }
  267. - (void)onSettingUpdated:(NSNotification *)notification {
  268. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  269. [self refreshList];
  270. [self refreshLeftButton];
  271. [self updatePcSession];
  272. });
  273. }
  274. - (void)onRecallMessages:(NSNotification *)notification {
  275. [self refreshList];
  276. [self refreshLeftButton];
  277. }
  278. - (void)onClearAllUnread:(NSNotification *)notification {
  279. if ([notification.object intValue] == 0) {
  280. [[WFCCIMService sharedWFCIMService] clearAllUnreadStatus];
  281. [self refreshList];
  282. [self refreshLeftButton];
  283. }
  284. }
  285. - (void)refreshList {
  286. self.conversations = [[[WFCCIMService sharedWFCIMService] getConversationInfos:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]] mutableCopy];
  287. [self updateBadgeNumber];
  288. [self.tableView reloadData];
  289. }
  290. - (void)updateBadgeNumber {
  291. int count = 0;
  292. for (WFCCConversationInfo *info in self.conversations) {
  293. if (!info.isSilent) {
  294. count += info.unreadCount.unread;
  295. }
  296. }
  297. [self.tabBarController.tabBar showBadgeOnItemIndex:0 badgeValue:count];
  298. }
  299. - (void)updatePcSession {
  300. NSString *pcOnline = [[WFCCIMService sharedWFCIMService] getUserSetting:UserSettingScope_PC_Online key:@""];
  301. if (@available(iOS 11.0, *)) {
  302. if ([pcOnline isEqualToString:@"1"]) {
  303. self.tableView.tableHeaderView = self.pcSessionView;
  304. } else {
  305. self.tableView.tableHeaderView = nil;
  306. }
  307. } else {
  308. }
  309. }
  310. - (void)didReceiveMemoryWarning {
  311. [super didReceiveMemoryWarning];
  312. }
  313. -(void)viewDidDisappear:(BOOL)animated {
  314. [super viewDidDisappear:animated];
  315. [self refreshLeftButton];
  316. }
  317. - (void)viewWillAppear:(BOOL)animated {
  318. [super viewWillAppear:animated];
  319. if (self.firstAppear) {
  320. self.firstAppear = NO;
  321. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectionStatusChanged:) name:kConnectionStatusChanged object:nil];
  322. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessages:) name:kReceiveMessages object:nil];
  323. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRecallMessages:) name:kRecallMessages object:nil];
  324. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingUpdated:) name:kSettingUpdated object:nil];
  325. }
  326. [self updateConnectionStatus:[WFCCNetworkService sharedInstance].currentConnectionStatus];
  327. [self refreshList];
  328. [self refreshLeftButton];
  329. }
  330. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  331. [super traitCollectionDidChange:previousTraitCollection];
  332. if (@available(iOS 13.0, *)) {
  333. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  334. [self.tableView reloadData];
  335. }
  336. }
  337. }
  338. - (void)viewDidAppear:(BOOL)animated {
  339. [super viewDidAppear:animated];
  340. if (self.searchController.isActive) {
  341. self.tabBarController.tabBar.hidden = YES;
  342. }
  343. }
  344. - (void)refreshLeftButton {
  345. dispatch_async(dispatch_get_main_queue(), ^{
  346. WFCCUnreadCount *unreadCount = [[WFCCIMService sharedWFCIMService] getUnreadCount:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
  347. NSUInteger count = unreadCount.unread;
  348. NSString *title = nil;
  349. if (count > 0 && count < 1000) {
  350. title = [NSString stringWithFormat:WFCString(@"BackNumber"), count];
  351. } else if (count >= 1000) {
  352. title = WFCString(@"BackMore");
  353. } else {
  354. title = WFCString(@"Back");
  355. }
  356. UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
  357. item.title = title;
  358. self.navigationItem.backBarButtonItem = item;
  359. });
  360. }
  361. - (UIView *)pcSessionView {
  362. if (!_pcSessionView) {
  363. _pcSessionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
  364. [_pcSessionView setBackgroundColor:[UIColor grayColor]];
  365. UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 32, 32)];
  366. iv.image = [UIImage imageNamed:@"pc_session"];
  367. [_pcSessionView addSubview:iv];
  368. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(68, 10, 100, 20)];
  369. label.text = WFCString(@"PCLogined");
  370. [_pcSessionView addSubview:label];
  371. }
  372. return _pcSessionView;
  373. }
  374. #pragma mark - Table view data source
  375. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  376. int sec = 0;
  377. if (self.searchFriendList.count) {
  378. sec++;
  379. }
  380. if (self.searchGroupList.count) {
  381. sec++;
  382. }
  383. if (self.searchConversationList.count) {
  384. sec++;
  385. }
  386. if (sec == 0) {
  387. sec = 1;
  388. }
  389. return sec;
  390. }
  391. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  392. if (self.searchController.active) {
  393. int sec = 0;
  394. if (self.searchFriendList.count) {
  395. sec++;
  396. if (section == sec-1) {
  397. return self.searchFriendList.count;
  398. }
  399. }
  400. if (self.searchGroupList.count) {
  401. sec++;
  402. if (section == sec-1) {
  403. return self.searchGroupList.count;
  404. }
  405. }
  406. if (self.searchConversationList.count) {
  407. sec++;
  408. if (sec-1 == section) {
  409. return self.searchConversationList.count;
  410. }
  411. }
  412. return 0;
  413. } else {
  414. return self.conversations.count;
  415. }
  416. }
  417. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  418. if (self.searchController.active) {
  419. int sec = 0;
  420. if (self.searchFriendList.count) {
  421. sec++;
  422. if (indexPath.section == sec-1) {
  423. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  424. if (cell == nil) {
  425. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  426. cell.big = YES;
  427. }
  428. cell.userId = self.searchFriendList[indexPath.row].userId;
  429. return cell;
  430. }
  431. }
  432. if (self.searchGroupList.count) {
  433. sec++;
  434. if (indexPath.section == sec-1) {
  435. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  436. if (cell == nil) {
  437. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  438. }
  439. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  440. return cell;
  441. }
  442. }
  443. if (self.searchConversationList.count) {
  444. sec++;
  445. if (sec-1 == indexPath.section) {
  446. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  447. if (cell == nil) {
  448. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  449. }
  450. cell.searchInfo = self.searchConversationList[indexPath.row];
  451. return cell;
  452. }
  453. }
  454. return nil;
  455. } else {
  456. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  457. if (cell == nil) {
  458. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  459. }
  460. cell.info = self.conversations[indexPath.row];
  461. return cell;
  462. }
  463. }
  464. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  465. return 72;
  466. }
  467. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  468. {
  469. if ([cell respondsToSelector:@selector(setSeparatorInset:)])
  470. {
  471. [cell setSeparatorInset:UIEdgeInsetsMake(0, 76, 0, 0)];
  472. }
  473. if ([cell respondsToSelector:@selector(setLayoutMargins:)])
  474. {
  475. [cell setLayoutMargins:UIEdgeInsetsMake(0, 76, 0, 0)];
  476. }
  477. }
  478. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  479. if (self.searchController.isActive) {
  480. if (self.searchConversationList.count + self.searchGroupList.count + self.searchFriendList.count > 0) {
  481. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)];
  482. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)];
  483. label.font = [UIFont systemFontOfSize:13];
  484. label.textColor = [UIColor grayColor];
  485. label.textAlignment = NSTextAlignmentLeft;
  486. label.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  487. int sec = 0;
  488. if (self.searchFriendList.count) {
  489. sec++;
  490. if (section == sec-1) {
  491. label.text = WFCString(@"Contact");
  492. }
  493. }
  494. if (self.searchGroupList.count) {
  495. sec++;
  496. if (section == sec-1) {
  497. label.text = WFCString(@"Group");
  498. }
  499. }
  500. if (self.searchConversationList.count) {
  501. sec++;
  502. if (sec-1 == section) {
  503. label.text = WFCString(@"Message");
  504. }
  505. }
  506. [header addSubview:label];
  507. return header;
  508. } else {
  509. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
  510. return header;
  511. }
  512. } else {
  513. return nil;
  514. }
  515. }
  516. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  517. if (self.searchController.isActive) {
  518. return 20;
  519. }
  520. return 0;
  521. }
  522. // Override to support conditional editing of the table view.
  523. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  524. // Return NO if you do not want the specified item to be editable.
  525. if (self.searchController.active) {
  526. return NO;
  527. }
  528. return YES;
  529. }
  530. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  531. __weak typeof(self) ws = self;
  532. UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  533. [[WFCCIMService sharedWFCIMService] clearUnreadStatus:ws.conversations[indexPath.row].conversation];
  534. [[WFCCIMService sharedWFCIMService] removeConversation:ws.conversations[indexPath.row].conversation clearMessage:YES];
  535. [ws.conversations removeObjectAtIndex:indexPath.row];
  536. [ws updateBadgeNumber];
  537. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  538. }];
  539. UITableViewRowAction *setTop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Pinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  540. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:YES success:^{
  541. [ws refreshList];
  542. } error:^(int error_code) {
  543. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  544. hud.label.text = WFCString(@"UpdateFailure");
  545. hud.mode = MBProgressHUDModeText;
  546. hud.removeFromSuperViewOnHide = YES;
  547. [hud hideAnimated:NO afterDelay:1.5];
  548. }];
  549. }];
  550. UITableViewRowAction *setUntop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Unpinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  551. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:NO success:^{
  552. [ws refreshList];
  553. } error:^(int error_code) {
  554. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  555. hud.label.text = WFCString(@"UpdateFailure");
  556. hud.mode = MBProgressHUDModeText;
  557. hud.removeFromSuperViewOnHide = YES;
  558. [hud hideAnimated:NO afterDelay:1.5];
  559. }];
  560. [self refreshList];
  561. }];
  562. setTop.backgroundColor = [UIColor purpleColor];
  563. setUntop.backgroundColor = [UIColor orangeColor];
  564. if (self.conversations[indexPath.row].isTop) {
  565. return @[delete, setUntop ];
  566. } else {
  567. return @[delete, setTop];
  568. }
  569. };
  570. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  571. if (self.searchController.active) {
  572. [self.searchController.searchBar resignFirstResponder];
  573. }
  574. }
  575. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  576. if (self.searchController.active) {
  577. int sec = 0;
  578. if (self.searchFriendList.count) {
  579. sec++;
  580. if (indexPath.section == sec-1) {
  581. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  582. WFCCUserInfo *info = self.searchFriendList[indexPath.row];
  583. mvc.conversation = [[WFCCConversation alloc] init];
  584. mvc.conversation.type = Single_Type;
  585. mvc.conversation.target = info.userId;
  586. mvc.conversation.line = 0;
  587. mvc.hidesBottomBarWhenPushed = YES;
  588. [self.navigationController pushViewController:mvc animated:YES];
  589. }
  590. }
  591. if (self.searchGroupList.count) {
  592. sec++;
  593. if (indexPath.section == sec-1) {
  594. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  595. WFCCGroupSearchInfo *info = self.searchGroupList[indexPath.row];
  596. mvc.conversation = [[WFCCConversation alloc] init];
  597. mvc.conversation.type = Group_Type;
  598. mvc.conversation.target = info.groupInfo.target;
  599. mvc.conversation.line = 0;
  600. mvc.hidesBottomBarWhenPushed = YES;
  601. [self.navigationController pushViewController:mvc animated:YES];
  602. }
  603. }
  604. if (self.searchConversationList.count) {
  605. sec++;
  606. if (sec-1 == indexPath.section) {
  607. WFCCConversationSearchInfo *info = self.searchConversationList[indexPath.row];
  608. if (info.marchedCount == 1) {
  609. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  610. mvc.conversation = info.conversation;
  611. mvc.highlightMessageId = info.marchedMessage.messageId;
  612. mvc.highlightText = info.keyword;
  613. mvc.hidesBottomBarWhenPushed = YES;
  614. [self.navigationController pushViewController:mvc animated:YES];
  615. } else {
  616. WFCUConversationSearchTableViewController *mvc = [[WFCUConversationSearchTableViewController alloc] init];
  617. mvc.conversation = info.conversation;
  618. mvc.keyword = info.keyword;
  619. mvc.hidesBottomBarWhenPushed = YES;
  620. [self.navigationController pushViewController:mvc animated:YES];
  621. }
  622. }
  623. }
  624. } else {
  625. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  626. WFCCConversationInfo *info = self.conversations[indexPath.row];
  627. mvc.conversation = info.conversation;
  628. mvc.hidesBottomBarWhenPushed = YES;
  629. [self.navigationController pushViewController:mvc animated:YES];
  630. }
  631. }
  632. - (void)dealloc {
  633. [[NSNotificationCenter defaultCenter] removeObserver:self];
  634. _searchController = nil;
  635. _searchConversationList = nil;
  636. }
  637. #pragma mark - UISearchControllerDelegate
  638. - (void)didPresentSearchController:(UISearchController *)searchController {
  639. self.tabBarController.tabBar.hidden = YES;
  640. }
  641. - (void)willDismissSearchController:(UISearchController *)searchController {
  642. self.tabBarController.tabBar.hidden = NO;
  643. }
  644. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  645. NSString *searchString = [self.searchController.searchBar text];
  646. if (searchString.length) {
  647. self.searchConversationList = [[WFCCIMService sharedWFCIMService] searchConversation:searchString inConversation:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
  648. self.searchFriendList = [[WFCCIMService sharedWFCIMService] searchFriends:searchString];
  649. self.searchGroupList = [[WFCCIMService sharedWFCIMService] searchGroups:searchString];
  650. } else {
  651. self.searchConversationList = nil;
  652. self.searchFriendList = nil;
  653. self.searchGroupList = nil;
  654. }
  655. [self.tableView reloadData];
  656. }
  657. @end