WFCUConversationSearchTableViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // ConversationSearchTableViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/8/29.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUConversationSearchTableViewController.h"
  9. #import "WFCUContactListViewController.h"
  10. #import "WFCUCreateGroupViewController.h"
  11. #import "WFCUFriendRequestViewController.h"
  12. #import "WFCUMessageListViewController.h"
  13. #import "SDWebImage.h"
  14. #import "WFCUUtilities.h"
  15. #import "UITabBar+badge.h"
  16. #import "KxMenu.h"
  17. #import "UIImage+ERCategory.h"
  18. #import "MBProgressHUD.h"
  19. #import "WFCUConversationSearchTableViewCell.h"
  20. #import "WFCUConfigManager.h"
  21. @interface WFCUConversationSearchTableViewController () <UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>
  22. @property (nonatomic, strong)NSMutableArray<WFCCMessage* > *messages;
  23. @property (nonatomic, strong) UISearchController *searchController;
  24. @property (nonatomic, strong) UITableView *tableView;
  25. @property (nonatomic, strong) UIView *searchViewContainer;
  26. @end
  27. @implementation WFCUConversationSearchTableViewController
  28. - (void)initSearchUIAndTableView {
  29. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  30. self.searchController.searchResultsUpdater = self;
  31. self.searchController.dimsBackgroundDuringPresentation = NO;
  32. if (@available(iOS 9.1, *)) {
  33. self.searchController.obscuresBackgroundDuringPresentation = NO;
  34. }
  35. if (@available(iOS 13, *)) {
  36. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  37. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  38. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  39. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  40. } else {
  41. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  42. }
  43. self.searchController.searchBar.placeholder = WFCString(@"Search");
  44. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  45. [self.view addSubview:self.tableView];
  46. self.tableView.delegate = self;
  47. self.tableView.dataSource = self;
  48. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  49. if (@available(iOS 11.0, *)) {
  50. self.navigationItem.searchController = _searchController;
  51. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  52. _searchController.hidesNavigationBarDuringPresentation = YES;
  53. } else {
  54. self.tableView.tableHeaderView = _searchController.searchBar;
  55. }
  56. self.definesPresentationContext = YES;
  57. }
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. self.messages = [[NSMutableArray alloc] init];
  61. [self initSearchUIAndTableView];
  62. self.extendedLayoutIncludesOpaqueBars = YES;
  63. [self.searchController.searchBar setText:self.keyword];
  64. self.searchController.active = YES;
  65. }
  66. - (void)didReceiveMemoryWarning {
  67. [super didReceiveMemoryWarning];
  68. // Dispose of any resources that can be recreated.
  69. }
  70. #pragma mark - Table view data source
  71. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  72. return 1;
  73. }
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  75. return self.messages.count;
  76. }
  77. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  78. WFCUConversationSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  79. if (!cell) {
  80. cell = [[WFCUConversationSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  81. }
  82. WFCCMessage *msg = [self.messages objectAtIndex:indexPath.row];
  83. cell.keyword = self.keyword;
  84. cell.message = msg;
  85. return cell;
  86. }
  87. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. return 68;
  89. }
  90. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  91. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
  92. UIImageView *portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 32, 32)];
  93. portraitView.layer.cornerRadius = 3.f;
  94. portraitView.layer.masksToBounds = YES;
  95. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, self.tableView.frame.size.width, 40)];
  96. label.font = [UIFont boldSystemFontOfSize:18];
  97. label.textColor = [UIColor blackColor];
  98. label.textAlignment = NSTextAlignmentLeft;
  99. header.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  100. if (self.conversation.type == Single_Type) {
  101. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.conversation.target refresh:NO];
  102. [portraitView sd_setImageWithURL:[NSURL URLWithString:[userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"PersonalChat"]];
  103. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", userInfo.displayName];
  104. } else if (self.conversation.type == Group_Type) {
  105. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.conversation.target refresh:NO];
  106. [portraitView sd_setImageWithURL:[NSURL URLWithString:[groupInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"GroupChatRound"]];
  107. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", groupInfo.name];
  108. } else if(self.conversation.type == Channel_Type) {
  109. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:self.conversation.target refresh:NO];
  110. [portraitView sd_setImageWithURL:[NSURL URLWithString:[channelInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"GroupChatRound"]];
  111. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", channelInfo.name];
  112. }
  113. [header addSubview:label];
  114. [header addSubview:portraitView];
  115. return header;
  116. }
  117. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  118. return 40;
  119. }
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  121. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  122. mvc.conversation = self.messages[indexPath.row].conversation;
  123. mvc.highlightMessageId = self.messages[indexPath.row].messageId;
  124. mvc.highlightText = self.keyword;
  125. mvc.multiSelecting = self.messageSelecting;
  126. mvc.selectedMessageIds = self.selectedMessageIds;
  127. [self.navigationController pushViewController:mvc animated:YES];
  128. }
  129. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  130. if (self.searchController.active) {
  131. [self.searchController.searchBar resignFirstResponder];
  132. }
  133. }
  134. - (void)dealloc {
  135. [[NSNotificationCenter defaultCenter] removeObserver:self];
  136. _searchController = nil;
  137. }
  138. #pragma mark - UISearchControllerDelegate
  139. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  140. NSString *searchString = [self.searchController.searchBar text];
  141. if (searchString.length) {
  142. self.messages = [[[WFCCIMService sharedWFCIMService] searchMessage:self.conversation keyword:searchString] mutableCopy];
  143. self.keyword = searchString;
  144. } else {
  145. [self.messages removeAllObjects];
  146. }
  147. //刷新表格
  148. [self.tableView reloadData];
  149. }
  150. @end