WFCUAddFriendViewController.m 6.6 KB

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