WFCUConversationSearchTableViewController.m 7.7 KB

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