WFCUAddFriendViewController.m 7.0 KB

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