WFCUConversationTableViewController.m 27 KB

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