WFCUAddFriendViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #import "WFCUImage.h"
  16. #import "WFCUGeneralImageTextTableViewCell.h"
  17. #import "WFCUDomainTableViewController.h"
  18. #define CELL_HEIGHT 56
  19. @interface WFCUAddFriendViewController () <UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UISearchBarDelegate>
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) UISearchController *searchController;
  22. @property (nonatomic, strong) NSArray *searchList;
  23. @property (nonatomic, assign) BOOL texting;
  24. @property (nonatomic, strong) UITextView *noUserView;
  25. @property(nonatomic, assign)BOOL meshEnabled;
  26. @end
  27. @implementation WFCUAddFriendViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self initSearchUIAndData];
  31. self.extendedLayoutIncludesOpaqueBars = YES;
  32. }
  33. - (void)viewWillDisappear:(BOOL)animated {
  34. [super viewWillDisappear:animated];
  35. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  36. }
  37. - (void)initSearchUIAndData {
  38. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  39. self.navigationItem.title = WFCString(@"AddFriend");
  40. self.meshEnabled = [[WFCCIMService sharedWFCIMService] isMeshEnabled];
  41. _searchList = [NSMutableArray array];
  42. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  43. self.searchController.searchResultsUpdater = self;
  44. self.searchController.dimsBackgroundDuringPresentation = NO;
  45. if (@available(iOS 9.1, *)) {
  46. self.searchController.obscuresBackgroundDuringPresentation = NO;
  47. }
  48. if (@available(iOS 13, *)) {
  49. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  50. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  51. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  52. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  53. } else {
  54. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  55. }
  56. self.searchController.searchBar.placeholder = WFCString(@"SearchUserHint");
  57. self.searchController.searchBar.delegate = self;
  58. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  59. [self.view addSubview:self.tableView];
  60. self.tableView.delegate = self;
  61. self.tableView.dataSource = self;
  62. if (@available(iOS 15, *)) {
  63. self.tableView.sectionHeaderTopPadding = 0;
  64. }
  65. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  66. if (@available(iOS 11.0, *)) {
  67. self.navigationItem.searchController = _searchController;
  68. self.navigationItem.hidesSearchBarWhenScrolling = false;
  69. _searchController.hidesNavigationBarDuringPresentation = YES;
  70. } else {
  71. self.tableView.tableHeaderView = _searchController.searchBar;
  72. }
  73. self.definesPresentationContext = YES;
  74. [self.view addSubview:_tableView];
  75. self.noUserView = [[UITextView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 400)];
  76. self.noUserView.textAlignment = NSTextAlignmentCenter;
  77. self.noUserView.text = @"用户不存在";
  78. self.noUserView.font = [UIFont systemFontOfSize:18];
  79. self.noUserView.hidden = YES;
  80. [self.view addSubview:self.noUserView];
  81. self.domainId = _domainId;
  82. }
  83. - (void)setDomainId:(NSString *)domainId {
  84. _domainId = domainId;
  85. if(self.meshEnabled) {
  86. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 24)];
  87. if(self.domainId) {
  88. WFCCDomainInfo *domainInfo = [[WFCCIMService sharedWFCIMService] getDomainInfo:self.domainId refresh:NO];
  89. label.text = [NSString stringWithFormat:@"在单位 %@ 中搜索用户", domainInfo.name];
  90. } else {
  91. label.text = @"在本单位搜索用户";
  92. }
  93. label.userInteractionEnabled = YES;
  94. label.textAlignment = NSTextAlignmentLeft;
  95. label.font = [UIFont systemFontOfSize:14];
  96. UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectDomain:)];
  97. [label addGestureRecognizer:gestureRecognizer];
  98. self.tableView.tableHeaderView = label;
  99. }
  100. }
  101. - (void)onSelectDomain:(id)sender {
  102. WFCUDomainTableViewController *vc = [[WFCUDomainTableViewController alloc] init];
  103. vc.onSelect = ^(NSString *domainId) {
  104. self.domainId = domainId;
  105. };
  106. vc.isPresent = YES;
  107. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  108. [self.navigationController presentViewController:nav animated:YES completion:nil];
  109. }
  110. - (void)viewWillAppear:(BOOL)animated {
  111. [super viewWillAppear:animated];
  112. self.tabBarController.tabBar.hidden = YES;
  113. }
  114. - (void)viewDidAppear:(BOOL)animated {
  115. [super viewDidAppear:animated];
  116. self.searchController.active = YES;
  117. [self.searchController.searchBar becomeFirstResponder];
  118. }
  119. - (void)setTexting:(BOOL)texting {
  120. _texting = texting;
  121. [self.tableView reloadData];
  122. }
  123. #pragma mark - UISearchBarDelegate
  124. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  125. [self onSearch:searchBar];
  126. }
  127. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  128. self.texting = YES;
  129. }
  130. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  131. [self.navigationController popViewControllerAnimated:YES];
  132. }
  133. #pragma mark - UISearchControllerDelegate
  134. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  135. NSLog(@"updateSearchResultsForSearchController");
  136. }
  137. #pragma mark - UITableViewDataSource
  138. //table 返回的行数
  139. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  140. self.noUserView.hidden = YES;
  141. if (self.searchController.active) {
  142. if(self.texting) {
  143. return self.searchController.searchBar.text.length?1:0;
  144. }
  145. if(![self.searchList count]) {
  146. self.noUserView.hidden = NO;
  147. }
  148. return [self.searchList count];
  149. } else {
  150. return 0;
  151. }
  152. }
  153. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  154. if (self.searchController.active) {
  155. if(self.texting) {
  156. [self onSearch:nil];
  157. return;
  158. }
  159. WFCCUserInfo *userInfo = self.searchList[indexPath.row];
  160. WFCUProfileTableViewController *pvc = [[WFCUProfileTableViewController alloc] init];
  161. pvc.userId = userInfo.userId;
  162. pvc.sourceType = FriendSource_Search;
  163. [self.navigationController pushViewController:pvc animated:YES];
  164. }
  165. }
  166. //返回单元格内容
  167. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. static NSString *flag = @"cell";
  169. WFCUGeneralImageTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag];
  170. if (cell == nil) {
  171. cell = [[WFCUGeneralImageTextTableViewCell alloc] initWithReuseIdentifier:flag cellHeight:CELL_HEIGHT];
  172. }
  173. if(self.texting) {
  174. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"搜索:"];
  175. [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:self.searchController.searchBar.text attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}]];
  176. cell.titleLable.attributedText = attStr;
  177. cell.portraitIV.image = [WFCUImage imageNamed:@"search_icon"];
  178. } else {
  179. WFCCUserInfo *userInfo = self.searchList[indexPath.row];
  180. [cell.titleLable setText:userInfo.displayName];
  181. [cell.portraitIV sd_setImageWithURL:[NSURL URLWithString:[userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
  182. }
  183. return cell;
  184. }
  185. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  186. return CELL_HEIGHT;
  187. }
  188. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  189. if (self.searchController.active) {
  190. [self.searchController.searchBar resignFirstResponder];
  191. }
  192. }
  193. - (void)onSearch:(id)sender {
  194. __weak typeof(self) ws = self;
  195. NSString *searchString = [ws.searchController.searchBar text];
  196. if (searchString.length) {
  197. [[WFCCIMService sharedWFCIMService] searchUser:searchString
  198. domain:self.domainId
  199. searchType:SearchUserType_Name_Mobile
  200. page:0
  201. success:^(NSArray<WFCCUserInfo *> *machedUsers) {
  202. dispatch_async(dispatch_get_main_queue(), ^{
  203. if([machedUsers count] == 1) {
  204. ws.texting = YES;
  205. WFCCUserInfo *userInfo = machedUsers[0];
  206. WFCUProfileTableViewController *pvc = [[WFCUProfileTableViewController alloc] init];
  207. pvc.userId = userInfo.userId;
  208. pvc.sourceType = FriendSource_Search;
  209. [ws.navigationController pushViewController:pvc animated:YES];
  210. } else {
  211. ws.texting = NO;
  212. ws.searchList = machedUsers;
  213. [ws.tableView reloadData];
  214. }
  215. });
  216. }
  217. error:^(int errorCode) {
  218. dispatch_async(dispatch_get_main_queue(), ^{
  219. ws.searchList = nil;
  220. [ws.tableView reloadData];
  221. });
  222. NSLog(@"Search failed, errorCode(%d)", errorCode);
  223. }];
  224. } else {
  225. ws.searchList = nil;
  226. [ws.tableView reloadData];
  227. }
  228. }
  229. - (void)dealloc {
  230. _tableView = nil;
  231. _searchController = nil;
  232. _searchList = nil;
  233. }
  234. @end