WFCUConversationSearchTableViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "WFCUFriendRequestViewController.h"
  11. #import "WFCUMessageListViewController.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "WFCUUtilities.h"
  14. #import "UITabBar+badge.h"
  15. #import "KxMenu.h"
  16. #import "UIImage+ERCategory.h"
  17. #import "MBProgressHUD.h"
  18. #import "WFCUConversationSearchTableViewCell.h"
  19. #import "WFCUConfigManager.h"
  20. #import "WFCUImage.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 15, *)) {
  50. self.tableView.sectionHeaderTopPadding = 0;
  51. }
  52. if (@available(iOS 11.0, *)) {
  53. self.navigationItem.searchController = _searchController;
  54. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  55. _searchController.hidesNavigationBarDuringPresentation = YES;
  56. } else {
  57. self.tableView.tableHeaderView = _searchController.searchBar;
  58. }
  59. self.definesPresentationContext = YES;
  60. }
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. self.messages = [[NSMutableArray alloc] init];
  64. [self initSearchUIAndTableView];
  65. self.extendedLayoutIncludesOpaqueBars = YES;
  66. [self.searchController.searchBar setText:self.keyword];
  67. self.searchController.active = YES;
  68. }
  69. - (void)didReceiveMemoryWarning {
  70. [super didReceiveMemoryWarning];
  71. // Dispose of any resources that can be recreated.
  72. }
  73. #pragma mark - Table view data source
  74. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  75. return 1;
  76. }
  77. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  78. return self.messages.count;
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  81. WFCUConversationSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  82. if (!cell) {
  83. cell = [[WFCUConversationSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  84. }
  85. WFCCMessage *msg = [self.messages objectAtIndex:indexPath.row];
  86. cell.keyword = self.keyword;
  87. cell.message = msg;
  88. return cell;
  89. }
  90. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  91. return 68;
  92. }
  93. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  94. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
  95. UIImageView *portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 32, 32)];
  96. portraitView.layer.cornerRadius = 3.f;
  97. portraitView.layer.masksToBounds = YES;
  98. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, self.tableView.frame.size.width, 40)];
  99. label.font = [UIFont boldSystemFontOfSize:18];
  100. label.textColor = [UIColor blackColor];
  101. label.textAlignment = NSTextAlignmentLeft;
  102. header.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  103. if (self.conversation.type == Single_Type) {
  104. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.conversation.target refresh:NO];
  105. [portraitView sd_setImageWithURL:[NSURL URLWithString:[userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  106. if(userInfo.displayName.length) {
  107. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", userInfo.displayName];
  108. } else {
  109. label.text = @"用户聊天记录";
  110. }
  111. } else if (self.conversation.type == Group_Type) {
  112. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.conversation.target refresh:NO];
  113. [portraitView sd_setImageWithURL:[NSURL URLWithString:[groupInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"GroupChatRound"]];
  114. if(groupInfo.displayName.length) {
  115. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", groupInfo.displayName];
  116. } else {
  117. label.text = @"群组聊天记录";
  118. }
  119. } else if(self.conversation.type == Channel_Type) {
  120. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:self.conversation.target refresh:NO];
  121. [portraitView sd_setImageWithURL:[NSURL URLWithString:[channelInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"GroupChatRound"]];
  122. if(channelInfo.name.length) {
  123. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", channelInfo.name];
  124. } else {
  125. label.text = @"频道聊天记录";
  126. }
  127. } else if(self.conversation.type == SecretChat_Type) {
  128. NSString *userId = [[WFCCIMService sharedWFCIMService] getSecretChatInfo:self.conversation.target].userId;
  129. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId refresh:NO];
  130. [portraitView sd_setImageWithURL:[NSURL URLWithString:[userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  131. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", userInfo.displayName];
  132. }
  133. [header addSubview:label];
  134. [header addSubview:portraitView];
  135. return header;
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  138. return 40;
  139. }
  140. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  141. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  142. mvc.conversation = self.messages[indexPath.row].conversation;
  143. mvc.highlightMessageId = self.messages[indexPath.row].messageId;
  144. mvc.highlightText = self.keyword;
  145. mvc.multiSelecting = self.messageSelecting;
  146. mvc.selectedMessageIds = self.selectedMessageIds;
  147. [self.navigationController pushViewController:mvc animated:YES];
  148. }
  149. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  150. if (self.searchController.active) {
  151. [self.searchController.searchBar resignFirstResponder];
  152. }
  153. }
  154. - (void)dealloc {
  155. [[NSNotificationCenter defaultCenter] removeObserver:self];
  156. _searchController = nil;
  157. }
  158. #pragma mark - UISearchControllerDelegate
  159. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  160. NSString *searchString = [self.searchController.searchBar text];
  161. if (searchString.length) {
  162. self.messages = [[[WFCCIMService sharedWFCIMService] searchMessage:self.conversation keyword:searchString order:YES limit:100 offset:0 withUser:nil] mutableCopy];
  163. self.keyword = searchString;
  164. } else {
  165. [self.messages removeAllObjects];
  166. }
  167. //刷新表格
  168. [self.tableView reloadData];
  169. }
  170. @end