WFCUSeletedUserSearchResultViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // WFCUSeletedUserSearchResultViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by Zack Zhang on 2020/4/4.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSeletedUserSearchResultViewController.h"
  9. #import "WFCUSelectedUserTableViewCell.h"
  10. #import "WFCUUserSectionKeySupport.h"
  11. #import "UIFont+YH.h"
  12. #import "UIColor+YH.h"
  13. #import "WFCUConfigManager.h"
  14. @interface WFCUSeletedUserSearchResultViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
  15. @property (nonatomic, strong)UISearchBar *searchBar;
  16. @property (nonatomic, strong)NSMutableArray *results;
  17. @end
  18. @implementation WFCUSeletedUserSearchResultViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.results = [NSMutableArray new];
  22. self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width - 16 * 2,44)];
  23. self.searchBar.backgroundColor = [UIColor clearColor];
  24. self.searchBar.placeholder = @"搜索";
  25. if (self.needSection) {
  26. self.view.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  27. self.tableView.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  28. } else {
  29. self.view.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  30. self.tableView.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  31. }
  32. for (UIView *sView in self.searchBar.subviews[0].subviews) {
  33. if([sView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
  34. [sView removeFromSuperview];
  35. }
  36. }
  37. self.searchBar.delegate = self;
  38. [self.searchBar becomeFirstResponder];
  39. self.searchBar.showsCancelButton = YES;
  40. self.navigationItem.titleView = self.searchBar;
  41. [self.view addSubview:self.tableView];
  42. }
  43. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  44. NSMutableArray <WFCUSelectedUserInfo *>*searchList = [NSMutableArray new];
  45. for (WFCUSelectedUserInfo *friend in self.dataSource) {
  46. if ([friend.displayName.lowercaseString containsString:searchBar.text.lowercaseString] || [friend.friendAlias.lowercaseString containsString:searchBar.text.lowercaseString]) {
  47. [searchList addObject:friend];
  48. }
  49. }
  50. self.results = searchList;
  51. [self sortAndRefreshWithList:searchList];
  52. }
  53. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  54. [self.navigationController dismissViewControllerAnimated:NO completion:nil];
  55. }
  56. - (void)sortAndRefreshWithList:(NSArray *)friendList {
  57. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  58. NSMutableDictionary *resultDic = [WFCUUserSectionKeySupport userSectionKeys:friendList];
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. self.sectionDictionary = resultDic[@"infoDic"];
  61. self.sectionKeys = resultDic[@"allKeys"];
  62. [self.tableView reloadData];
  63. });
  64. });
  65. }
  66. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  67. if (self.needSection) {
  68. return self.sectionKeys.count;
  69. } else {
  70. return 1;
  71. }
  72. }
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  74. if (self.needSection) {
  75. NSString *key = self.sectionKeys[section];
  76. NSArray *users = self.sectionDictionary[key];
  77. return users.count;
  78. } else {
  79. return self.results.count;
  80. }
  81. }
  82. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  83. WFCUSelectedUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectedUserT"];
  84. if (self.needSection) {
  85. NSString *key = self.sectionKeys[indexPath.section];
  86. NSArray *users = self.sectionDictionary[key];
  87. cell.selectedUserInfo = users[indexPath.row];
  88. } else {
  89. cell.selectedUserInfo = self.results[indexPath.row];
  90. }
  91. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  92. if (self.needSection) {
  93. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  94. } else {
  95. cell.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
  96. cell.backgroundColor = [UIColor colorWithHexString:@"0x1f2026"];
  97. cell.nameLabel.textColor = [UIColor whiteColor];
  98. }
  99. return cell;
  100. }
  101. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. return 51;
  103. }
  104. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  105. if (self.needSection) {
  106. if (section == 0) {
  107. return 0;
  108. }
  109. return 30;
  110. } else {
  111. return 0;
  112. }
  113. }
  114. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  115. if (self.needSection) {
  116. NSString *title = self.sectionKeys[section];
  117. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
  118. view.backgroundColor = [UIColor colorWithHexString:@"0xededed"];
  119. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, self.view.frame.size.width, 30)];
  120. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  121. label.textColor = [UIColor colorWithHexString:@"0x828282"];
  122. label.textAlignment = NSTextAlignmentLeft;
  123. label.text = [NSString stringWithFormat:@"%@", title];
  124. [view addSubview:label];
  125. return view;
  126. } else {
  127. return nil;
  128. }
  129. }
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  131. if (!self.needSection) {
  132. WFCUSelectedUserInfo *user = nil;
  133. user = self.results[indexPath.row];
  134. self.selectedUser(user);
  135. } else {
  136. NSString *key = self.sectionKeys[indexPath.section];
  137. NSArray *users = self.sectionDictionary[key];
  138. WFCUSelectedUserInfo *user = users[indexPath.row];
  139. self.selectedUser(user);
  140. }
  141. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  142. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  143. });
  144. }
  145. - (UITableView *)tableView {
  146. if (!_tableView) {
  147. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  148. _tableView.dataSource = self;
  149. _tableView.delegate = self;
  150. [_tableView registerClass:[WFCUSelectedUserTableViewCell class] forCellReuseIdentifier:@"selectedUserT"];
  151. _tableView.tableFooterView = [UIView new];
  152. }
  153. return _tableView;
  154. }
  155. @end