WFCUConversationTableViewController.m 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. #import "UIImage+ERCategory.h"
  28. #import "UIFont+YH.h"
  29. #import "UIColor+YH.h"
  30. #import "WFCUSeletedUserViewController.h"
  31. @interface WFCUConversationTableViewController () <UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>
  32. @property (nonatomic, strong)NSMutableArray<WFCCConversationInfo *> *conversations;
  33. @property (nonatomic, strong) UISearchController *searchController;
  34. @property (nonatomic, strong) NSArray<WFCCConversationSearchInfo *> *searchConversationList;
  35. @property (nonatomic, strong) NSArray<WFCCUserInfo *> *searchFriendList;
  36. @property (nonatomic, strong) NSArray<WFCCGroupSearchInfo *> *searchGroupList;
  37. @property (nonatomic ,assign) BOOL isSearchConversationListExpansion;
  38. @property (nonatomic ,assign) BOOL isSearchFriendListExpansion;
  39. @property (nonatomic ,assign) BOOL isSearchGroupListExpansion;
  40. @property (nonatomic, strong) UITableView *tableView;
  41. @property (nonatomic, strong) UIView *searchViewContainer;
  42. @property (nonatomic, assign) BOOL firstAppear;
  43. @property (nonatomic, strong) UIView *pcSessionView;
  44. @end
  45. @implementation WFCUConversationTableViewController
  46. - (void)initSearchUIAndTableView {
  47. _searchConversationList = [NSMutableArray array];
  48. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  49. self.searchController.searchResultsUpdater = self;
  50. self.searchController.delegate = self;
  51. self.searchController.dimsBackgroundDuringPresentation = NO;
  52. if (@available(iOS 13, *)) {
  53. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  54. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  55. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  56. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  57. } else {
  58. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  59. }
  60. if (@available(iOS 9.1, *)) {
  61. self.searchController.obscuresBackgroundDuringPresentation = NO;
  62. }
  63. self.searchController.searchBar.placeholder = WFCString(@"Search");
  64. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  65. [self.view addSubview:self.tableView];
  66. self.tableView.delegate = self;
  67. self.tableView.dataSource = self;
  68. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  69. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  70. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"expansion"];
  71. if (@available(iOS 11.0, *)) {
  72. self.navigationItem.searchController = _searchController;
  73. } else {
  74. self.tableView.tableHeaderView = _searchController.searchBar;
  75. }
  76. self.definesPresentationContext = YES;
  77. [self updatePcSession];
  78. }
  79. - (void)onUserInfoUpdated:(NSNotification *)notification {
  80. if (self.searchController.active) {
  81. [self.tableView reloadData];
  82. } else {
  83. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  84. NSArray *dataSource = self.conversations;
  85. for (int i = 0; i < dataSource.count; i++) {
  86. WFCCConversationInfo *conv = dataSource[i];
  87. if (conv.conversation.type == Single_Type && [conv.conversation.target isEqualToString:userInfo.userId]) {
  88. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  89. } else if ([conv.lastMessage.fromUser isEqualToString:userInfo.userId]) {
  90. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  91. }
  92. }
  93. }
  94. }
  95. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  96. if (self.searchController.active) {
  97. [self.tableView reloadData];
  98. } else {
  99. WFCCGroupInfo *groupInfo = notification.userInfo[@"groupInfo"];
  100. NSArray *dataSource = self.conversations;
  101. for (int i = 0; i < dataSource.count; i++) {
  102. WFCCConversationInfo *conv = dataSource[i];
  103. if (conv.conversation.type == Group_Type && [conv.conversation.target isEqualToString:groupInfo.target]) {
  104. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  105. }
  106. }
  107. }
  108. }
  109. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  110. if (self.searchController.active) {
  111. [self.tableView reloadData];
  112. } else {
  113. WFCCChannelInfo *channelInfo = notification.userInfo[@"groupInfo"];
  114. NSArray *dataSource = self.conversations;
  115. for (int i = 0; i < dataSource.count; i++) {
  116. WFCCConversationInfo *conv = dataSource[i];
  117. if (conv.conversation.type == Channel_Type && [conv.conversation.target isEqualToString:channelInfo.channelId]) {
  118. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  119. }
  120. }
  121. }
  122. }
  123. - (void)onSendingMessageStatusUpdated:(NSNotification *)notification {
  124. if (self.searchController.active) {
  125. [self.tableView reloadData];
  126. } else {
  127. long messageId = [notification.object longValue];
  128. NSArray *dataSource = self.conversations;
  129. if (messageId == 0) {
  130. return;
  131. }
  132. for (int i = 0; i < dataSource.count; i++) {
  133. WFCCConversationInfo *conv = dataSource[i];
  134. if (conv.lastMessage && conv.lastMessage.direction == MessageDirection_Send && conv.lastMessage.messageId == messageId) {
  135. conv.lastMessage = [[WFCCIMService sharedWFCIMService] getMessage:messageId];
  136. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  137. }
  138. }
  139. }
  140. }
  141. - (void)onRightBarBtn:(UIBarButtonItem *)sender {
  142. CGFloat searchExtra = 0;
  143. if ([KxMenu isShowing]) {
  144. [KxMenu dismissMenu];
  145. return;
  146. }
  147. [KxMenu showMenuInView:self.navigationController.view
  148. fromRect:CGRectMake(self.view.bounds.size.width - 56, kStatusBarAndNavigationBarHeight + searchExtra, 48, 5)
  149. menuItems:@[
  150. [KxMenuItem menuItem:WFCString(@"StartChat")
  151. image:[UIImage imageNamed:@"menu_start_chat"]
  152. target:self
  153. action:@selector(startChatAction:)],
  154. [KxMenuItem menuItem:WFCString(@"AddFriend")
  155. image:[UIImage imageNamed:@"menu_add_friends"]
  156. target:self
  157. action:@selector(addFriendsAction:)],
  158. [KxMenuItem menuItem:WFCString(@"SubscribeChannel")
  159. image:[UIImage imageNamed:@"menu_listen_channel"]
  160. target:self
  161. action:@selector(listenChannelAction:)],
  162. [KxMenuItem menuItem:WFCString(@"ScanQRCode")
  163. image:[UIImage imageNamed:@"menu_scan_qr"]
  164. target:self
  165. action:@selector(scanQrCodeAction:)]
  166. ]];
  167. }
  168. - (void)startChatAction:(id)sender {
  169. WFCUSeletedUserViewController *pvc = [[WFCUSeletedUserViewController alloc] init];
  170. pvc.type = Horizontal;
  171. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  172. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  173. __weak typeof(self)ws = self;
  174. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  175. [navi dismissViewControllerAnimated:NO completion:nil];
  176. if (contacts.count == 1) {
  177. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  178. mvc.conversation = [WFCCConversation conversationWithType:Single_Type target:contacts[0] line:0];
  179. mvc.hidesBottomBarWhenPushed = YES;
  180. [ws.navigationController pushViewController:mvc animated:YES];
  181. } else {
  182. WFCUCreateGroupViewController *vc = [[WFCUCreateGroupViewController alloc] init];
  183. vc.memberIds = [contacts mutableCopy];
  184. if (![vc.memberIds containsObject:[WFCCNetworkService sharedInstance].userId]) {
  185. [vc.memberIds insertObject:[WFCCNetworkService sharedInstance].userId atIndex:0];
  186. }
  187. vc.hidesBottomBarWhenPushed = YES;
  188. [ws.navigationController pushViewController:vc animated:YES];
  189. }
  190. };
  191. [self.navigationController presentViewController:navi animated:YES completion:nil];
  192. }
  193. - (void)addFriendsAction:(id)sender {
  194. UIViewController *addFriendVC = [[WFCUFriendRequestViewController alloc] init];
  195. addFriendVC.hidesBottomBarWhenPushed = YES;
  196. [self.navigationController pushViewController:addFriendVC animated:YES];
  197. }
  198. - (void)listenChannelAction:(id)sender {
  199. UIViewController *searchChannelVC = [[WFCUSearchChannelViewController alloc] init];
  200. searchChannelVC.hidesBottomBarWhenPushed = YES;
  201. [self.navigationController pushViewController:searchChannelVC animated:YES];
  202. }
  203. - (void)scanQrCodeAction:(id)sender {
  204. if (gQrCodeDelegate) {
  205. [gQrCodeDelegate scanQrCode:self.navigationController];
  206. }
  207. }
  208. - (void)viewDidLoad {
  209. [super viewDidLoad];
  210. self.conversations = [[NSMutableArray alloc] init];
  211. [self initSearchUIAndTableView];
  212. self.definesPresentationContext = YES;
  213. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  214. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClearAllUnread:) name:@"kTabBarClearBadgeNotification" object:nil];
  215. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  216. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  217. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:nil];
  218. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSendingMessageStatusUpdated:) name:kSendingMessageStatusUpdated object:nil];
  219. self.firstAppear = YES;
  220. }
  221. - (void)updateConnectionStatus:(ConnectionStatus)status {
  222. UIView *title;
  223. if (status != kConnectionStatusConnecting && status != kConnectionStatusReceiving) {
  224. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 40, 0, 80, 44)];
  225. switch (status) {
  226. case kConnectionStatusLogout:
  227. navLabel.text = WFCString(@"NotLogin");
  228. break;
  229. case kConnectionStatusUnconnected:
  230. navLabel.text = WFCString(@"NotConnect");
  231. break;
  232. case kConnectionStatusConnected:
  233. navLabel.text = WFCString(@"Message");
  234. break;
  235. default:
  236. break;
  237. }
  238. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  239. navLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
  240. navLabel.textAlignment = NSTextAlignmentCenter;
  241. title = navLabel;
  242. } else {
  243. UIView *continer = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 60, 0, 120, 44)];
  244. UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 80, 40)];
  245. if (status == kConnectionStatusConnecting) {
  246. navLabel.text = WFCString(@"Connecting");
  247. } else {
  248. navLabel.text = WFCString(@"Synching");
  249. }
  250. navLabel.textColor = [WFCUConfigManager globalManager].naviTextColor;
  251. navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
  252. [continer addSubview:navLabel];
  253. UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  254. indicatorView.center = CGPointMake(20, 21);
  255. [indicatorView startAnimating];
  256. indicatorView.color = [WFCUConfigManager globalManager].naviTextColor;
  257. [continer addSubview:indicatorView];
  258. title = continer;
  259. }
  260. self.navigationItem.titleView = title;
  261. }
  262. - (void)onConnectionStatusChanged:(NSNotification *)notification {
  263. ConnectionStatus status = [notification.object intValue];
  264. [self updateConnectionStatus:status];
  265. }
  266. - (void)onReceiveMessages:(NSNotification *)notification {
  267. NSArray<WFCCMessage *> *messages = notification.object;
  268. if ([messages count]) {
  269. [self refreshList];
  270. [self refreshLeftButton];
  271. }
  272. }
  273. - (void)onSettingUpdated:(NSNotification *)notification {
  274. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  275. [self refreshList];
  276. [self refreshLeftButton];
  277. [self updatePcSession];
  278. });
  279. }
  280. - (void)onRecallMessages:(NSNotification *)notification {
  281. [self refreshList];
  282. [self refreshLeftButton];
  283. }
  284. - (void)onClearAllUnread:(NSNotification *)notification {
  285. if ([notification.object intValue] == 0) {
  286. [[WFCCIMService sharedWFCIMService] clearAllUnreadStatus];
  287. [self refreshList];
  288. [self refreshLeftButton];
  289. }
  290. }
  291. - (void)refreshList {
  292. self.conversations = [[[WFCCIMService sharedWFCIMService] getConversationInfos:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]] mutableCopy];
  293. [self updateBadgeNumber];
  294. [self.tableView reloadData];
  295. }
  296. - (void)updateBadgeNumber {
  297. int count = 0;
  298. for (WFCCConversationInfo *info in self.conversations) {
  299. if (!info.isSilent) {
  300. count += info.unreadCount.unread;
  301. }
  302. }
  303. [self.tabBarController.tabBar showBadgeOnItemIndex:0 badgeValue:count];
  304. }
  305. - (void)updatePcSession {
  306. NSArray<WFCCPCOnlineInfo *> *onlines = [[WFCCIMService sharedWFCIMService] getPCOnlineInfos];
  307. if (@available(iOS 11.0, *)) {
  308. if (onlines.count) {
  309. self.tableView.tableHeaderView = self.pcSessionView;
  310. } else {
  311. self.tableView.tableHeaderView = nil;
  312. }
  313. } else {
  314. }
  315. }
  316. - (void)didReceiveMemoryWarning {
  317. [super didReceiveMemoryWarning];
  318. }
  319. -(void)viewDidDisappear:(BOOL)animated {
  320. [super viewDidDisappear:animated];
  321. [self refreshLeftButton];
  322. if ([KxMenu isShowing]) {
  323. [KxMenu dismissMenu];
  324. }
  325. }
  326. - (void)viewWillAppear:(BOOL)animated {
  327. [super viewWillAppear:animated];
  328. if (self.firstAppear) {
  329. self.firstAppear = NO;
  330. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectionStatusChanged:) name:kConnectionStatusChanged object:nil];
  331. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessages:) name:kReceiveMessages object:nil];
  332. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRecallMessages:) name:kRecallMessages object:nil];
  333. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingUpdated:) name:kSettingUpdated object:nil];
  334. }
  335. [self updateConnectionStatus:[WFCCNetworkService sharedInstance].currentConnectionStatus];
  336. [self refreshList];
  337. [self refreshLeftButton];
  338. }
  339. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  340. [super traitCollectionDidChange:previousTraitCollection];
  341. if (@available(iOS 13.0, *)) {
  342. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  343. [self.tableView reloadData];
  344. }
  345. }
  346. }
  347. - (void)viewDidAppear:(BOOL)animated {
  348. [super viewDidAppear:animated];
  349. if (self.searchController.isActive) {
  350. self.tabBarController.tabBar.hidden = YES;
  351. }
  352. }
  353. - (void)refreshLeftButton {
  354. dispatch_async(dispatch_get_main_queue(), ^{
  355. WFCCUnreadCount *unreadCount = [[WFCCIMService sharedWFCIMService] getUnreadCount:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
  356. NSUInteger count = unreadCount.unread;
  357. NSString *title = nil;
  358. if (count > 0 && count < 1000) {
  359. title = [NSString stringWithFormat:WFCString(@"BackNumber"), count];
  360. } else if (count >= 1000) {
  361. title = WFCString(@"BackMore");
  362. } else {
  363. title = WFCString(@"Back");
  364. }
  365. UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
  366. item.title = title;
  367. self.navigationItem.backBarButtonItem = item;
  368. });
  369. }
  370. - (UIView *)pcSessionView {
  371. if (!_pcSessionView) {
  372. BOOL darkMode = NO;
  373. if (@available(iOS 13.0, *)) {
  374. if(UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  375. darkMode = YES;
  376. }
  377. }
  378. UIColor *bgColor;
  379. if (darkMode) {
  380. bgColor = [WFCUConfigManager globalManager].backgroudColor;
  381. } else {
  382. bgColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  383. }
  384. _pcSessionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
  385. [_pcSessionView setBackgroundColor:bgColor];
  386. UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 32, 32)];
  387. iv.image = [UIImage imageNamed:@"pc_session"];
  388. [_pcSessionView addSubview:iv];
  389. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(68, 10, 100, 20)];
  390. label.text = WFCString(@"PCLogined");
  391. [_pcSessionView addSubview:label];
  392. }
  393. return _pcSessionView;
  394. }
  395. #pragma mark - Table view data source
  396. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  397. int sec = 0;
  398. if (self.searchFriendList.count) {
  399. sec++;
  400. }
  401. if (self.searchGroupList.count) {
  402. sec++;
  403. }
  404. if (self.searchConversationList.count) {
  405. sec++;
  406. }
  407. if (sec == 0) {
  408. sec = 1;
  409. }
  410. return sec;
  411. }
  412. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  413. if (self.searchController.active) {
  414. int sec = 0;
  415. if (self.searchFriendList.count) {
  416. sec++;
  417. if (section == sec-1) {
  418. if (self.isSearchFriendListExpansion) {
  419. return self.searchFriendList.count;
  420. } else {
  421. if (self.searchFriendList.count > 2) {
  422. return 3;
  423. } else {
  424. return self.searchFriendList.count;
  425. }
  426. }
  427. }
  428. }
  429. if (self.searchGroupList.count) {
  430. sec++;
  431. if (section == sec-1) {
  432. if (self.isSearchGroupListExpansion) {
  433. return self.searchGroupList.count;
  434. } else {
  435. if (self.searchGroupList.count > 2) {
  436. return 3;
  437. } else {
  438. return self.searchGroupList.count;
  439. }
  440. }
  441. }
  442. }
  443. if (self.searchConversationList.count) {
  444. sec++;
  445. if (sec-1 == section) {
  446. if (self.isSearchConversationListExpansion) {
  447. return self.searchConversationList.count;
  448. } else {
  449. if (self.searchConversationList.count > 2) {
  450. return 3;
  451. } else {
  452. return self.searchConversationList.count;
  453. }
  454. }
  455. }
  456. }
  457. return 0;
  458. } else {
  459. return self.conversations.count;
  460. }
  461. }
  462. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  463. if (self.searchController.active) {
  464. int sec = 0;
  465. if (self.searchFriendList.count) {
  466. sec++;
  467. if (indexPath.section == sec-1) {
  468. if (self.isSearchFriendListExpansion) {
  469. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  470. if (cell == nil) {
  471. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  472. }
  473. cell.big = NO;
  474. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  475. cell.userId = self.searchFriendList[indexPath.row].userId;
  476. return cell;
  477. } else {
  478. if (indexPath.row == 2) {
  479. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  480. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  481. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchFriendList.count - 2];
  482. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  483. return cell;
  484. } else {
  485. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  486. if (cell == nil) {
  487. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  488. }
  489. cell.big = NO;
  490. if (indexPath.row == 1) {
  491. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  492. } else {
  493. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  494. }
  495. cell.userId = self.searchFriendList[indexPath.row].userId;
  496. return cell;
  497. }
  498. }
  499. }
  500. }
  501. if (self.searchGroupList.count) {
  502. sec++;
  503. if (indexPath.section == sec-1) {
  504. if (self.isSearchGroupListExpansion) {
  505. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  506. if (cell == nil) {
  507. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  508. }
  509. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  510. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  511. return cell;
  512. } else {
  513. if (indexPath.row == 2) {
  514. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  515. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  516. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchGroupList.count - 2];
  517. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  518. return cell;
  519. } else {
  520. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  521. if (cell == nil) {
  522. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  523. }
  524. if (indexPath.row == 1) {
  525. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  526. } else {
  527. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  528. }
  529. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  530. return cell;
  531. }
  532. }
  533. }
  534. }
  535. if (self.searchConversationList.count) {
  536. sec++;
  537. if (sec-1 == indexPath.section) {
  538. if (self.isSearchConversationListExpansion) {
  539. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  540. if (cell == nil) {
  541. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  542. }
  543. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  544. cell.big = NO;
  545. cell.searchInfo = self.searchConversationList[indexPath.row];
  546. return cell;
  547. } else {
  548. if (indexPath.row == 2) {
  549. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expansion" forIndexPath:indexPath];
  550. cell.textLabel.textColor = [UIColor colorWithHexString:@"5b6e8e"];
  551. cell.textLabel.text = [NSString stringWithFormat:@"点击展开剩余%lu项", self.searchConversationList.count - 2];
  552. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:12];
  553. return cell;
  554. } else {
  555. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  556. if (cell == nil) {
  557. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  558. }
  559. if (indexPath.row == 1) {
  560. cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  561. } else {
  562. cell.separatorInset = UIEdgeInsetsMake(0, 68, 0, 0);
  563. } cell.big = NO;
  564. cell.searchInfo = self.searchConversationList[indexPath.row];
  565. return cell;
  566. }
  567. }
  568. }
  569. }
  570. return nil;
  571. } else {
  572. WFCUConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell"];
  573. if (cell == nil) {
  574. cell = [[WFCUConversationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conversationCell"];
  575. }
  576. cell.big = YES;
  577. cell.separatorInset = UIEdgeInsetsMake(0, 76, 0, 0);
  578. cell.info = self.conversations[indexPath.row];
  579. return cell;
  580. }
  581. }
  582. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  583. if (self.searchController.active) {
  584. int sec = 0;
  585. if (self.searchFriendList.count) {
  586. sec++;
  587. if (indexPath.section == sec-1) {
  588. if (self.isSearchFriendListExpansion) {
  589. return 60;
  590. } else {
  591. if (indexPath.row == 2) {
  592. return 40;
  593. } else {
  594. return 60;
  595. }
  596. }
  597. }
  598. }
  599. if (self.searchGroupList.count) {
  600. sec++;
  601. if (indexPath.section == sec-1) {
  602. if (self.isSearchGroupListExpansion) {
  603. return 60;
  604. } else {
  605. if (indexPath.row == 2) {
  606. return 40;
  607. } else {
  608. return 60;
  609. }
  610. }
  611. }
  612. }
  613. if (self.searchConversationList.count) {
  614. sec++;
  615. if (sec-1 == indexPath.section ) {
  616. if (self.isSearchConversationListExpansion) {
  617. return 60;
  618. } else {
  619. if (indexPath.row == 2) {
  620. return 40;
  621. } else {
  622. return 60;
  623. }
  624. }
  625. }
  626. }
  627. return 60;
  628. } else {
  629. return 72;
  630. }
  631. }
  632. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  633. if (self.searchController.isActive) {
  634. if (self.searchConversationList.count + self.searchGroupList.count + self.searchFriendList.count > 0) {
  635. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 32)];
  636. header.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  637. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, self.tableView.frame.size.width, 32)];
  638. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  639. label.textColor = [UIColor colorWithHexString:@"0x828282"];
  640. label.textAlignment = NSTextAlignmentLeft;
  641. int sec = 0;
  642. if (self.searchFriendList.count) {
  643. sec++;
  644. if (section == sec-1) {
  645. label.text = WFCString(@"Contact");
  646. }
  647. }
  648. if (self.searchGroupList.count) {
  649. sec++;
  650. if (section == sec-1) {
  651. label.text = WFCString(@"Group");
  652. }
  653. }
  654. if (self.searchConversationList.count) {
  655. sec++;
  656. if (sec-1 == section) {
  657. label.text = WFCString(@"Message");
  658. }
  659. }
  660. [header addSubview:label];
  661. return header;
  662. } else {
  663. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
  664. return header;
  665. }
  666. } else {
  667. return nil;
  668. }
  669. }
  670. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  671. if (self.searchController.isActive) {
  672. return 32;
  673. }
  674. return 0;
  675. }
  676. // Override to support conditional editing of the table view.
  677. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  678. // Return NO if you do not want the specified item to be editable.
  679. if (self.searchController.active) {
  680. return NO;
  681. }
  682. return YES;
  683. }
  684. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  685. __weak typeof(self) ws = self;
  686. UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  687. [[WFCCIMService sharedWFCIMService] clearUnreadStatus:ws.conversations[indexPath.row].conversation];
  688. [[WFCCIMService sharedWFCIMService] removeConversation:ws.conversations[indexPath.row].conversation clearMessage:YES];
  689. [ws.conversations removeObjectAtIndex:indexPath.row];
  690. [ws updateBadgeNumber];
  691. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  692. }];
  693. UITableViewRowAction *setTop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Pinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  694. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:YES success:^{
  695. [ws refreshList];
  696. } error:^(int error_code) {
  697. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  698. hud.label.text = WFCString(@"UpdateFailure");
  699. hud.mode = MBProgressHUDModeText;
  700. hud.removeFromSuperViewOnHide = YES;
  701. [hud hideAnimated:NO afterDelay:1.5];
  702. }];
  703. }];
  704. UITableViewRowAction *setUntop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Unpinned") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  705. [[WFCCIMService sharedWFCIMService] setConversation:ws.conversations[indexPath.row].conversation top:NO success:^{
  706. [ws refreshList];
  707. } error:^(int error_code) {
  708. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:ws.view animated:NO];
  709. hud.label.text = WFCString(@"UpdateFailure");
  710. hud.mode = MBProgressHUDModeText;
  711. hud.removeFromSuperViewOnHide = YES;
  712. [hud hideAnimated:NO afterDelay:1.5];
  713. }];
  714. [self refreshList];
  715. }];
  716. setTop.backgroundColor = [UIColor purpleColor];
  717. setUntop.backgroundColor = [UIColor orangeColor];
  718. if (self.conversations[indexPath.row].isTop) {
  719. return @[delete, setUntop ];
  720. } else {
  721. return @[delete, setTop];
  722. }
  723. };
  724. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  725. if (self.searchController.active) {
  726. [self.searchController.searchBar resignFirstResponder];
  727. }
  728. }
  729. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  730. if (self.searchController.active) {
  731. int sec = 0;
  732. if (self.searchFriendList.count) {
  733. sec++;
  734. if (indexPath.section == sec-1) {
  735. if (!self.isSearchFriendListExpansion && indexPath.row == 2) {
  736. self.isSearchFriendListExpansion = YES;
  737. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  738. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  739. } else {
  740. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  741. WFCCUserInfo *info = self.searchFriendList[indexPath.row];
  742. mvc.conversation = [[WFCCConversation alloc] init];
  743. mvc.conversation.type = Single_Type;
  744. mvc.conversation.target = info.userId;
  745. mvc.conversation.line = 0;
  746. mvc.hidesBottomBarWhenPushed = YES;
  747. [self.navigationController pushViewController:mvc animated:YES];
  748. }
  749. }
  750. }
  751. if (self.searchGroupList.count) {
  752. sec++;
  753. if (indexPath.section == sec-1) {
  754. if (!self.isSearchGroupListExpansion && indexPath.row == 2) {
  755. self.isSearchGroupListExpansion = YES;
  756. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  757. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  758. } else {
  759. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  760. WFCCGroupSearchInfo *info = self.searchGroupList[indexPath.row];
  761. mvc.conversation = [[WFCCConversation alloc] init];
  762. mvc.conversation.type = Group_Type;
  763. mvc.conversation.target = info.groupInfo.target;
  764. mvc.conversation.line = 0;
  765. mvc.hidesBottomBarWhenPushed = YES;
  766. [self.navigationController pushViewController:mvc animated:YES];
  767. }
  768. }
  769. }
  770. if (self.searchConversationList.count) {
  771. sec++;
  772. if (sec-1 == indexPath.section) {
  773. if (!self.isSearchConversationListExpansion && indexPath.row == 2) {
  774. self.isSearchConversationListExpansion = YES;
  775. NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
  776. [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
  777. } else {
  778. WFCCConversationSearchInfo *info = self.searchConversationList[indexPath.row];
  779. if (info.marchedCount == 1) {
  780. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  781. mvc.conversation = info.conversation;
  782. mvc.highlightMessageId = info.marchedMessage.messageId;
  783. mvc.highlightText = info.keyword;
  784. mvc.hidesBottomBarWhenPushed = YES;
  785. [self.navigationController pushViewController:mvc animated:YES];
  786. } else {
  787. WFCUConversationSearchTableViewController *mvc = [[WFCUConversationSearchTableViewController alloc] init];
  788. mvc.conversation = info.conversation;
  789. mvc.keyword = info.keyword;
  790. mvc.hidesBottomBarWhenPushed = YES;
  791. [self.navigationController pushViewController:mvc animated:YES];
  792. }
  793. }
  794. }
  795. }
  796. } else {
  797. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  798. WFCCConversationInfo *info = self.conversations[indexPath.row];
  799. mvc.conversation = info.conversation;
  800. mvc.hidesBottomBarWhenPushed = YES;
  801. [self.navigationController pushViewController:mvc animated:YES];
  802. }
  803. }
  804. - (void)dealloc {
  805. [[NSNotificationCenter defaultCenter] removeObserver:self];
  806. _searchController = nil;
  807. _searchConversationList = nil;
  808. }
  809. #pragma mark - UISearchControllerDelegate
  810. - (void)didPresentSearchController:(UISearchController *)searchController {
  811. self.searchController.view.frame = self.view.bounds;
  812. self.isSearchFriendListExpansion = NO;
  813. self.isSearchConversationListExpansion = NO;
  814. self.isSearchGroupListExpansion = NO;
  815. self.tabBarController.tabBar.hidden = YES;
  816. self.extendedLayoutIncludesOpaqueBars = YES;
  817. }
  818. - (void)willDismissSearchController:(UISearchController *)searchController {
  819. self.tabBarController.tabBar.hidden = NO;
  820. self.extendedLayoutIncludesOpaqueBars = NO;
  821. }
  822. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  823. NSString *searchString = [self.searchController.searchBar text];
  824. if (searchString.length) {
  825. self.searchConversationList = [[WFCCIMService sharedWFCIMService] searchConversation:searchString inConversation:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
  826. self.searchFriendList = [[WFCCIMService sharedWFCIMService] searchFriends:searchString];
  827. self.searchGroupList = [[WFCCIMService sharedWFCIMService] searchGroups:searchString];
  828. } else {
  829. self.searchConversationList = nil;
  830. self.searchFriendList = nil;
  831. self.searchGroupList = nil;
  832. }
  833. [self.tableView reloadData];
  834. }
  835. @end