WFCUSearchChannelViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // WFCUAddFriendViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/7.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSearchChannelViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUChannelProfileViewController.h"
  11. #import <SDWebImage/SDWebImage.h>
  12. #import "MBProgressHUD.h"
  13. #import "WFCUConfigManager.h"
  14. #import "UIImage+ERCategory.h"
  15. #import "WFCUImage.h"
  16. @interface WFCUSearchChannelViewController () <UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate>
  17. @property (nonatomic, strong) UITableView *tableView;
  18. @property (nonatomic, strong) UISearchController *searchController;
  19. @property (nonatomic, strong) NSArray *searchList;
  20. @property (nonatomic, strong) NSTimer *timer;
  21. @end
  22. @implementation WFCUSearchChannelViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self initSearchUIAndData];
  26. self.extendedLayoutIncludesOpaqueBars = YES;
  27. }
  28. - (void)viewWillDisappear:(BOOL)animated {
  29. [super viewWillDisappear:animated];
  30. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  31. }
  32. - (void)initSearchUIAndData {
  33. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  34. self.navigationItem.title = WFCString(@"SubscribeChannel");
  35. _searchList = [NSMutableArray array];
  36. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  37. self.searchController.searchResultsUpdater = self;
  38. self.searchController.dimsBackgroundDuringPresentation = NO;
  39. if (@available(iOS 9.1, *)) {
  40. self.searchController.obscuresBackgroundDuringPresentation = NO;
  41. }
  42. if (@available(iOS 13, *)) {
  43. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  44. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  45. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  46. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  47. } else {
  48. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  49. }
  50. self.searchController.searchBar.placeholder = WFCString(@"SearchChannels");
  51. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  52. [self.view addSubview:self.tableView];
  53. self.tableView.delegate = self;
  54. self.tableView.dataSource = self;
  55. if (@available(iOS 15, *)) {
  56. self.tableView.sectionHeaderTopPadding = 0;
  57. }
  58. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  59. if (@available(iOS 11.0, *)) {
  60. self.navigationItem.searchController = _searchController;
  61. self.navigationItem.hidesSearchBarWhenScrolling = false;
  62. _searchController.hidesNavigationBarDuringPresentation = YES;
  63. } else {
  64. self.tableView.tableHeaderView = _searchController.searchBar;
  65. }
  66. self.definesPresentationContext = YES;
  67. [self.view addSubview:_tableView];
  68. }
  69. - (void)viewWillAppear:(BOOL)animated {
  70. [super viewWillAppear:animated];
  71. self.tabBarController.tabBar.hidden = YES;
  72. }
  73. #pragma mark - UITableViewDataSource
  74. //table 返回的行数
  75. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  76. if (self.searchController.active) {
  77. return [self.searchList count];
  78. } else {
  79. return 0;
  80. }
  81. }
  82. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  83. if (self.searchController.active) {
  84. WFCCChannelInfo *channelInfo = self.searchList[indexPath.row];
  85. WFCUChannelProfileViewController *pvc = [[WFCUChannelProfileViewController alloc] init];
  86. pvc.channelInfo = channelInfo;
  87. [self.navigationController pushViewController:pvc animated:YES];
  88. }
  89. }
  90. //返回单元格内容
  91. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  92. static NSString *flag = @"cell";
  93. if (self.searchController.active) {
  94. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag];
  95. if (cell == nil) {
  96. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
  97. }
  98. WFCCChannelInfo *channelInfo = self.searchList[indexPath.row];
  99. [cell.textLabel setText:channelInfo.name];
  100. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[channelInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  101. cell.userInteractionEnabled = YES;
  102. return cell;
  103. }
  104. else//如果没有搜索
  105. {
  106. return nil;
  107. }
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return 56;
  111. }
  112. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  113. if (self.searchController.active) {
  114. [self.searchController.searchBar resignFirstResponder];
  115. }
  116. }
  117. #pragma mark - UISearchControllerDelegate
  118. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  119. if (self.timer.valid) {
  120. [self.timer invalidate];
  121. self.timer = nil;
  122. }
  123. self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(onSearch:) userInfo:nil repeats:NO];
  124. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  125. }
  126. - (void)onSearch:(id)sender {
  127. __weak typeof(self) ws = self;
  128. NSString *searchString = [ws.searchController.searchBar text];
  129. if (searchString.length) {
  130. [[WFCCIMService sharedWFCIMService] searchChannel:searchString
  131. success:^(NSArray<WFCCChannelInfo *> *machedChannels) {
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. ws.searchList = machedChannels;
  134. [ws.tableView reloadData];
  135. });
  136. }
  137. error:^(int errorCode) {
  138. dispatch_async(dispatch_get_main_queue(), ^{
  139. ws.searchList = nil;
  140. [ws.tableView reloadData];
  141. });
  142. NSLog(@"Search failed, errorCode(%d)", errorCode);
  143. }];
  144. } else {
  145. ws.searchList = nil;
  146. [ws.tableView reloadData];
  147. }
  148. }
  149. - (void)dealloc {
  150. _tableView = nil;
  151. _searchController = nil;
  152. _searchList = nil;
  153. }
  154. @end