WFCUConversationSearchTableViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. @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. [self.searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
  35. self.searchController.searchBar.placeholder = @"搜索";
  36. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  37. [self.view addSubview:self.tableView];
  38. self.tableView.delegate = self;
  39. self.tableView.dataSource = self;
  40. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  41. if (@available(iOS 11.0, *)) {
  42. self.navigationItem.searchController = _searchController;
  43. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  44. _searchController.hidesNavigationBarDuringPresentation = YES;
  45. } else {
  46. self.tableView.tableHeaderView = _searchController.searchBar;
  47. }
  48. self.definesPresentationContext = YES;
  49. }
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. self.messages = [[NSMutableArray alloc] init];
  53. [self initSearchUIAndTableView];
  54. [self.searchController.searchBar setText:self.keyword];
  55. self.searchController.active = YES;
  56. }
  57. - (void)didReceiveMemoryWarning {
  58. [super didReceiveMemoryWarning];
  59. // Dispose of any resources that can be recreated.
  60. }
  61. #pragma mark - Table view data source
  62. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  63. return 1;
  64. }
  65. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  66. return self.messages.count;
  67. }
  68. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  69. WFCUConversationSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  70. if (!cell) {
  71. cell = [[WFCUConversationSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  72. }
  73. WFCCMessage *msg = [self.messages objectAtIndex:indexPath.row];
  74. cell.keyword = self.keyword;
  75. cell.message = msg;
  76. return cell;
  77. }
  78. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  79. return 68;
  80. }
  81. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  82. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
  83. UIImageView *portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 32, 32)];
  84. portraitView.layer.cornerRadius = 3.f;
  85. portraitView.layer.masksToBounds = YES;
  86. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, self.tableView.frame.size.width, 40)];
  87. label.font = [UIFont boldSystemFontOfSize:18];
  88. label.textColor = [UIColor blackColor];
  89. label.textAlignment = NSTextAlignmentLeft;
  90. header.backgroundColor = [UIColor colorWithRed:239/255.f green:239/255.f blue:239/255.f alpha:1.0f];
  91. if (self.conversation.type == Single_Type) {
  92. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.conversation.target refresh:NO];
  93. [portraitView sd_setImageWithURL:[NSURL URLWithString:userInfo.portrait] placeholderImage:[UIImage imageNamed:@"PersonalChat"]];
  94. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", userInfo.displayName];
  95. } else if (self.conversation.type == Group_Type) {
  96. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.conversation.target refresh:NO];
  97. [portraitView sd_setImageWithURL:[NSURL URLWithString:groupInfo.portrait] placeholderImage:[UIImage imageNamed:@"GroupChatRound"]];
  98. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", groupInfo.name];
  99. } else if(self.conversation.type == Channel_Type) {
  100. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:self.conversation.target refresh:NO];
  101. [portraitView sd_setImageWithURL:[NSURL URLWithString:channelInfo.portrait] placeholderImage:[UIImage imageNamed:@"GroupChatRound"]];
  102. label.text = [NSString stringWithFormat:@"\"%@\"的聊天记录", channelInfo.name];
  103. }
  104. [header addSubview:label];
  105. [header addSubview:portraitView];
  106. return header;
  107. }
  108. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  109. return 40;
  110. }
  111. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  112. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  113. mvc.conversation = self.messages[indexPath.row].conversation;
  114. mvc.highlightMessageId = self.messages[indexPath.row].messageId;
  115. mvc.highlightText = self.keyword;
  116. [self.navigationController pushViewController:mvc animated:YES];
  117. }
  118. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  119. if (self.searchController.active) {
  120. [self.searchController.searchBar resignFirstResponder];
  121. }
  122. }
  123. - (void)dealloc {
  124. [[NSNotificationCenter defaultCenter] removeObserver:self];
  125. _searchController = nil;
  126. }
  127. #pragma mark - UISearchControllerDelegate
  128. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  129. NSString *searchString = [self.searchController.searchBar text];
  130. if (searchString.length) {
  131. self.messages = [[[WFCCIMService sharedWFCIMService] searchMessage:self.conversation keyword:searchString] mutableCopy];
  132. self.keyword = searchString;
  133. } else {
  134. [self.messages removeAllObjects];
  135. }
  136. //刷新表格
  137. [self.tableView reloadData];
  138. }
  139. @end