WFCUContactListViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. //
  2. // ContactListViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/7.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUContactListViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "SDWebImage.h"
  11. #import "WFCUProfileTableViewController.h"
  12. #import "WFCUContactSelectTableViewCell.h"
  13. #import "WFCUContactTableViewCell.h"
  14. #import "pinyin.h"
  15. #import "WFCUFavGroupTableViewController.h"
  16. #import "WFCUFriendRequestViewController.h"
  17. #import "UITabBar+badge.h"
  18. #import "WFCUNewFriendTableViewCell.h"
  19. #import "WFCUAddFriendViewController.h"
  20. #import "MBProgressHUD.h"
  21. #import "WFCUFavChannelTableViewController.h"
  22. #import "WFCUConfigManager.h"
  23. #import "UIView+Toast.h"
  24. #import "UIImage+ERCategory.h"
  25. #import "UIFont+YH.h"
  26. #import "UIColor+YH.h"
  27. @interface WFCUContactListViewController () <UITableViewDataSource, UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating>
  28. @property (nonatomic, strong)UITableView *tableView;
  29. @property (nonatomic, strong)NSMutableArray<WFCCUserInfo *> *dataArray;
  30. @property (nonatomic, strong)NSMutableArray<NSString *> *selectedContacts;
  31. @property (nonatomic, strong) NSMutableArray<WFCCUserInfo *> *searchList;
  32. @property (nonatomic, strong) UISearchController *searchController;
  33. @property(nonatomic, strong) NSMutableDictionary *resultDic;
  34. @property(nonatomic, strong) NSDictionary *allFriendSectionDic;
  35. @property(nonatomic, strong) NSArray *allKeys;
  36. @property(nonatomic, assign)BOOL sorting;
  37. @property(nonatomic, assign)BOOL needSort;
  38. @property(nonatomic, strong)UIActivityIndicatorView *activityIndicator;
  39. @end
  40. static NSMutableDictionary *hanziStringDict = nil;
  41. @implementation WFCUContactListViewController
  42. - (instancetype)init {
  43. self = [super init];
  44. if (self) {
  45. [self setup];
  46. }
  47. return self;
  48. }
  49. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  50. self = [super initWithCoder:aDecoder];
  51. if (self) {
  52. [self setup];
  53. }
  54. return self;
  55. }
  56. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  57. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  58. if (self) {
  59. [self setup];
  60. }
  61. return self;
  62. }
  63. - (void)setup {
  64. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFriendRequestUpdated:) name:kFriendRequestUpdated object:nil];
  65. }
  66. - (void)viewDidLoad {
  67. [super viewDidLoad];
  68. // Do any additional setup after loading the view.
  69. CGRect frame = self.view.frame;
  70. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  71. self.tableView.delegate = self;
  72. self.tableView.dataSource = self;
  73. self.tableView.backgroundColor = [UIColor colorWithHexString:@"0xededed"];
  74. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  75. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  76. self.tableView.tableHeaderView = nil;
  77. if (self.selectContact) {
  78. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Cancel") style:UIBarButtonItemStyleDone target:self action:@selector(onLeftBarBtn:)];
  79. if(self.multiSelect) {
  80. self.selectedContacts = [[NSMutableArray alloc] init];
  81. [self updateRightBarBtn];
  82. }
  83. } else {
  84. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  85. }
  86. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  87. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onContactsUpdated:) name:kFriendListUpdated object:nil];
  88. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClearAllUnread:) name:@"kTabBarClearBadgeNotification" object:nil];
  89. _searchList = [NSMutableArray array];
  90. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  91. self.searchController.searchResultsUpdater = self;
  92. self.searchController.delegate = self;
  93. self.searchController.dimsBackgroundDuringPresentation = NO;
  94. if (@available(iOS 13, *)) {
  95. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  96. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  97. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  98. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  99. } else {
  100. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  101. }
  102. if (@available(iOS 9.1, *)) {
  103. self.searchController.obscuresBackgroundDuringPresentation = NO;
  104. }
  105. [self.searchController.searchBar setPlaceholder:WFCString(@"SearchContact")];
  106. if (@available(iOS 11.0, *)) {
  107. self.navigationItem.searchController = _searchController;
  108. _searchController.hidesNavigationBarDuringPresentation = YES;
  109. } else {
  110. self.tableView.tableHeaderView = _searchController.searchBar;
  111. }
  112. self.definesPresentationContext = YES;
  113. self.tableView.sectionIndexColor = [UIColor colorWithHexString:@"0x4e4e4e"];
  114. [self.view addSubview:self.tableView];
  115. [self.view bringSubviewToFront:self.activityIndicator];
  116. [self.tableView reloadData];
  117. }
  118. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  119. [super traitCollectionDidChange:previousTraitCollection];
  120. if (@available(iOS 13.0, *)) {
  121. if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
  122. [self.tableView reloadData];
  123. }
  124. }
  125. }
  126. - (void)updateRightBarBtn {
  127. if(self.selectedContacts.count == 0) {
  128. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Ok") style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  129. self.navigationItem.rightBarButtonItem.enabled = NO;
  130. } else {
  131. if (self.multiSelect && self.maxSelectCount > 1) {
  132. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@(%d/%d)", WFCString(@"Ok"), (int)self.selectedContacts.count, self.maxSelectCount] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  133. } else {
  134. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@(%d)", WFCString(@"Ok"), (int)self.selectedContacts.count] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  135. }
  136. }
  137. }
  138. - (void)onRightBarBtn:(UIBarButtonItem *)sender {
  139. if (self.selectContact) {
  140. if (self.selectedContacts) {
  141. [self left:^{
  142. self.selectResult(self.selectedContacts);
  143. }];
  144. }
  145. } else {
  146. UIViewController *addFriendVC = [[WFCUAddFriendViewController alloc] init];
  147. addFriendVC.hidesBottomBarWhenPushed = YES;
  148. [self.navigationController pushViewController:addFriendVC animated:YES];
  149. }
  150. }
  151. - (void)onLeftBarBtn:(UIBarButtonItem *)sender {
  152. if (self.cancelSelect) {
  153. self.cancelSelect();
  154. }
  155. [self left:nil];
  156. }
  157. - (void)left:(void (^)(void))completion {
  158. if (self.isPushed) {
  159. [self.navigationController popViewControllerAnimated:YES];
  160. } else {
  161. [self.navigationController dismissViewControllerAnimated:YES completion:completion];
  162. }
  163. }
  164. - (void)viewWillAppear:(BOOL)animated {
  165. [super viewWillAppear:animated];
  166. self.dataArray = [[NSMutableArray alloc] init];
  167. if (self.selectContact) {
  168. [self loadContact:NO];
  169. } else {
  170. [self loadContact:YES];
  171. [self updateBadgeNumber];
  172. }
  173. }
  174. - (void)loadContact:(BOOL)forceLoadFromRemote {
  175. [self.dataArray removeAllObjects];
  176. NSArray *userIdList;
  177. if (self.candidateUsers.count) {
  178. userIdList = self.candidateUsers;
  179. } else {
  180. userIdList = [[WFCCIMService sharedWFCIMService] getMyFriendList:forceLoadFromRemote];
  181. }
  182. self.dataArray = [[WFCCIMService sharedWFCIMService] getUserInfos:userIdList inGroup:nil];
  183. self.needSort = YES;
  184. }
  185. - (void)setNeedSort:(BOOL)needSort {
  186. _needSort = needSort;
  187. if (needSort && !self.sorting) {
  188. _needSort = NO;
  189. if (self.searchController.active) {
  190. [self sortAndRefreshWithList:self.searchList];
  191. } else {
  192. [self sortAndRefreshWithList:self.dataArray];
  193. }
  194. }
  195. }
  196. - (void)onUserInfoUpdated:(NSNotification *)notification {
  197. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  198. BOOL needRefresh = NO;
  199. for (WFCCUserInfo *ui in self.dataArray) {
  200. if ([ui.userId isEqualToString:userInfo.userId]) {
  201. needRefresh = YES;
  202. [ui cloneFrom:userInfo];
  203. break;
  204. }
  205. }
  206. if(needRefresh) {
  207. self.needSort = needRefresh;
  208. }
  209. }
  210. - (void)onContactsUpdated:(NSNotification *)notification {
  211. [self loadContact:NO];
  212. }
  213. - (void)sortAndRefreshWithList:(NSArray *)friendList {
  214. self.sorting = YES;
  215. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  216. self.resultDic = [WFCUContactListViewController sortedArrayWithPinYinDic:friendList];
  217. dispatch_async(dispatch_get_main_queue(), ^{
  218. self.allFriendSectionDic = self.resultDic[@"infoDic"];
  219. self.allKeys = self.resultDic[@"allKeys"];
  220. if (!self.selectContact && !self.searchController.active) {
  221. UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 48)];
  222. countLabel.textAlignment = NSTextAlignmentCenter;
  223. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0.5)];
  224. line.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:0.8];
  225. [countLabel addSubview:line];
  226. [countLabel setText:[NSString stringWithFormat:WFCString(@"NumberOfContacts"), (int)self.dataArray.count]];
  227. countLabel.font = [UIFont systemFontOfSize:14];
  228. countLabel.textColor = [UIColor grayColor];
  229. self.tableView.tableFooterView = countLabel;
  230. } else {
  231. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  232. }
  233. [self.tableView reloadData];
  234. self.sorting = NO;
  235. if (self.needSort) {
  236. self.needSort = self.needSort;
  237. }
  238. [self.activityIndicator stopAnimating];
  239. self.activityIndicator.hidden = YES;
  240. });
  241. });
  242. }
  243. - (void)didReceiveMemoryWarning {
  244. [super didReceiveMemoryWarning];
  245. // Dispose of any resources that can be recreated.
  246. }
  247. - (void)onFriendRequestUpdated:(id)sender {
  248. [self updateBadgeNumber];
  249. }
  250. - (void)onClearAllUnread:(NSNotification *)notification {
  251. if ([notification.object intValue] == 1) {
  252. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  253. [self updateBadgeNumber];
  254. }
  255. }
  256. - (void)updateBadgeNumber {
  257. int count = [[WFCCIMService sharedWFCIMService] getUnreadFriendRequestStatus];
  258. [self.tabBarController.tabBar showBadgeOnItemIndex:1 badgeValue:count];
  259. }
  260. #pragma mark - UITableViewDataSource
  261. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  262. NSArray *dataSource;
  263. if (self.searchController.active || self.selectContact) {
  264. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  265. if (section == 0) {
  266. return 1;
  267. }
  268. dataSource = self.allFriendSectionDic[self.allKeys[section-1]];
  269. } else {
  270. dataSource = self.allFriendSectionDic[self.allKeys[section]];
  271. }
  272. return dataSource.count;
  273. } else {
  274. if (section == 0) {
  275. return 3;
  276. } else {
  277. dataSource = self.allFriendSectionDic[self.allKeys[section - 1]];
  278. return dataSource.count;
  279. }
  280. }
  281. }
  282. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
  283. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
  284. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  285. UITableViewCell *cell = nil;
  286. #define REUSEIDENTIFY @"resueCell"
  287. NSArray *dataSource;
  288. if (self.searchController.active || self.selectContact) {
  289. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  290. if (indexPath.section == 0) {
  291. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"new_channel"];
  292. if (self.showCreateChannel) {
  293. cell.textLabel.text = WFCString(@"CreateChannel");
  294. } else {
  295. cell.textLabel.text = WFCString(@"MentionAll");
  296. }
  297. return cell;
  298. }
  299. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section-1]];
  300. } else {
  301. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section]];
  302. }
  303. } else {
  304. if (indexPath.section == 0) {
  305. if (indexPath.row == 0) {
  306. WFCUNewFriendTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"];
  307. if (contactCell == nil) {
  308. contactCell = [[WFCUNewFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newFriendCell"];
  309. }
  310. contactCell.nameLabel.text = WFCString(@"NewFriend");
  311. contactCell.portraitView.image = [UIImage imageNamed:@"friend_request_icon"];
  312. [contactCell refresh];
  313. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  314. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  315. return contactCell;
  316. } else if(indexPath.row == 1) {
  317. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  318. if (contactCell == nil) {
  319. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  320. }
  321. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  322. contactCell.nameLabel.text = WFCString(@"Group");
  323. contactCell.portraitView.image = [UIImage imageNamed:@"contact_group_icon"];
  324. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  325. return contactCell;
  326. } else {
  327. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  328. if (contactCell == nil) {
  329. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  330. }
  331. contactCell.nameLabel.text = WFCString(@"Channel");
  332. contactCell.portraitView.image = [UIImage imageNamed:@"contact_channel_icon"];
  333. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  334. return contactCell;
  335. }
  336. } else {
  337. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section - 1]];
  338. }
  339. }
  340. if (self.selectContact) {
  341. #define SELECT_REUSEIDENTIFY @"resueSelectCell"
  342. WFCUContactSelectTableViewCell *selectCell = [tableView dequeueReusableCellWithIdentifier:SELECT_REUSEIDENTIFY];
  343. if (selectCell == nil) {
  344. selectCell = [[WFCUContactSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SELECT_REUSEIDENTIFY];
  345. selectCell.selectionStyle = UITableViewCellSelectionStyleNone;
  346. }
  347. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  348. selectCell.friendUid = userInfo.userId;
  349. selectCell.multiSelect = self.multiSelect;
  350. if (self.multiSelect && !self.withoutCheckBox) {
  351. if ([self.selectedContacts containsObject:userInfo.userId]) {
  352. selectCell.checked = YES;
  353. } else {
  354. selectCell.checked = NO;
  355. }
  356. if ([self.disableUsers containsObject:userInfo.userId]) {
  357. selectCell.disabled = YES;
  358. if(self.disableUsersSelected) {
  359. selectCell.checked = YES;
  360. } else {
  361. selectCell.checked = NO;
  362. }
  363. } else {
  364. selectCell.disabled = NO;
  365. }
  366. } else {
  367. WFCUContactTableViewCell *selectCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  368. if (selectCell == nil) {
  369. selectCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  370. }
  371. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  372. selectCell.userId = userInfo.userId;
  373. }
  374. selectCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  375. cell = selectCell;
  376. } else {
  377. #define REUSEIDENTIFY @"resueCell"
  378. if (indexPath.section == 0 && !self.searchController.active) {
  379. if (indexPath.row == 0) {
  380. WFCUNewFriendTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"];
  381. if (contactCell == nil) {
  382. contactCell = [[WFCUNewFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newFriendCell"];
  383. }
  384. [contactCell refresh];
  385. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  386. contactCell.nameLabel.text = WFCString(@"NewFriend");
  387. contactCell.portraitView.image = [UIImage imageNamed:@"friend_request_icon"];
  388. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  389. cell = contactCell;
  390. } else {
  391. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  392. if (contactCell == nil) {
  393. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  394. }
  395. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  396. contactCell.nameLabel.text = WFCString(@"Group");
  397. contactCell.portraitView.image = [UIImage imageNamed:@"contact_group_icon"];
  398. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  399. cell = contactCell;
  400. }
  401. } else {
  402. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  403. if (contactCell == nil) {
  404. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  405. }
  406. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  407. contactCell.userId = userInfo.userId;
  408. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  409. cell = contactCell;
  410. }
  411. }
  412. if (cell == nil) {
  413. NSLog(@"error");
  414. }
  415. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  416. return cell;
  417. }
  418. -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  419. if (@available(iOS 11.0, *)) {
  420. if (self.selectContact) {
  421. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  422. NSMutableArray *indexs = [self.allKeys mutableCopy];
  423. [indexs insertObject:@"" atIndex:0];
  424. return indexs;
  425. }
  426. return self.allKeys;
  427. }
  428. if (self.searchController.active) {
  429. return self.allKeys;
  430. }
  431. NSMutableArray *indexs = [self.allKeys mutableCopy];
  432. [indexs insertObject:@"" atIndex:0];
  433. return indexs;
  434. } else {
  435. return nil;
  436. }
  437. }
  438. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  439. if (self.selectContact) {
  440. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  441. return self.allKeys.count + 1;
  442. }
  443. return self.allKeys.count;
  444. }
  445. if (self.searchController.active) {
  446. return self.allKeys.count;
  447. }
  448. return 1 + self.allKeys.count;
  449. }
  450. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  451. if (section == 0) {
  452. return 0;
  453. }
  454. return 30;
  455. }
  456. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  457. NSString *title;
  458. if (self.selectContact || self.searchController.active) {
  459. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  460. if (section == 0) {
  461. return nil;
  462. }
  463. title = self.allKeys[section-1];
  464. } else {
  465. title = self.allKeys[section];
  466. }
  467. } else {
  468. if (section == 0) {
  469. return nil;
  470. } else {
  471. title = self.allKeys[section - 1];
  472. }
  473. }
  474. if (title == nil || title.length == 0) {
  475. return nil;
  476. }
  477. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
  478. view.backgroundColor = [UIColor colorWithHexString:@"0xededed"];
  479. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, self.view.frame.size.width, 30)];
  480. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  481. label.textColor = [UIColor colorWithHexString:@"0x828282"];
  482. label.textAlignment = NSTextAlignmentLeft;
  483. label.text = [NSString stringWithFormat:@"%@", title];
  484. [view addSubview:label];
  485. return view;
  486. }
  487. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  488. return 51;
  489. }
  490. - (UIActivityIndicatorView *)activityIndicator {
  491. if (!_activityIndicator) {
  492. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  493. _activityIndicator.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
  494. [self.view addSubview:_activityIndicator];
  495. [_activityIndicator startAnimating];
  496. [self.view bringSubviewToFront:_activityIndicator];
  497. }
  498. return _activityIndicator;
  499. }
  500. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  501. if (self.selectContact) {
  502. return index;
  503. }
  504. if (self.searchController.active) {
  505. return index;
  506. }
  507. return index;
  508. }
  509. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  510. NSArray *dataSource;
  511. if (self.searchController.active || self.selectContact) {
  512. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  513. if (indexPath.section == 0) {
  514. if (self.showCreateChannel) {
  515. [self left:^{
  516. if (self.createChannel) {
  517. self.createChannel();
  518. }
  519. }];
  520. } else {
  521. [self left:^{
  522. if (self.mentionAll) {
  523. self.mentionAll();
  524. }
  525. }];
  526. }
  527. return;
  528. }
  529. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section-1]];
  530. } else {
  531. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section]];
  532. }
  533. } else {
  534. if (indexPath.section == 0) {
  535. if (indexPath.row == 0) {
  536. UIViewController *addFriendVC = [[WFCUFriendRequestViewController alloc] init];
  537. addFriendVC.hidesBottomBarWhenPushed = YES;
  538. [self.navigationController pushViewController:addFriendVC animated:YES];
  539. } else if(indexPath.row == 1) {
  540. WFCUFavGroupTableViewController *groupVC = [[WFCUFavGroupTableViewController alloc] init];;
  541. groupVC.hidesBottomBarWhenPushed = YES;
  542. [self.navigationController pushViewController:groupVC animated:YES];
  543. } else {
  544. WFCUFavChannelTableViewController *channelVC = [[WFCUFavChannelTableViewController alloc] init];;
  545. channelVC.hidesBottomBarWhenPushed = YES;
  546. [self.navigationController pushViewController:channelVC animated:YES];
  547. }
  548. return;
  549. } else {
  550. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section - 1]];
  551. }
  552. }
  553. if (self.selectContact) {
  554. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  555. if (self.multiSelect) {
  556. if ([self.disableUsers containsObject:userInfo.userId]) {
  557. return;
  558. }
  559. if ([self.selectedContacts containsObject:userInfo.userId]) {
  560. [self.selectedContacts removeObject:userInfo.userId];
  561. ((WFCUContactSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).checked = NO;
  562. } else {
  563. if (self.maxSelectCount > 0 && self.selectedContacts.count >= self.maxSelectCount) {
  564. [self.view makeToast:WFCString(@"MaxCount")];
  565. return;
  566. }
  567. [self.selectedContacts addObject:userInfo.userId];
  568. ((WFCUContactSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).checked = YES;
  569. }
  570. [self updateRightBarBtn];
  571. } else {
  572. self.selectResult([NSArray arrayWithObjects:userInfo.userId, nil]);
  573. [self left:nil];
  574. }
  575. } else {
  576. WFCUProfileTableViewController *vc = [[WFCUProfileTableViewController alloc] init];
  577. WFCCUserInfo *friend = dataSource[indexPath.row];
  578. vc.userId = friend.userId;
  579. vc.hidesBottomBarWhenPushed = YES;
  580. [self.navigationController pushViewController:vc animated:YES];
  581. }
  582. }
  583. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  584. if (self.searchController.active) {
  585. [self.searchController.searchBar resignFirstResponder];
  586. }
  587. }
  588. #pragma mark - UISearchControllerDelegate
  589. - (void)didPresentSearchController:(UISearchController *)searchController {
  590. self.tabBarController.tabBar.hidden = YES;
  591. self.extendedLayoutIncludesOpaqueBars = YES;
  592. }
  593. - (void)willDismissSearchController:(UISearchController *)searchController {
  594. self.tabBarController.tabBar.hidden = NO;
  595. self.extendedLayoutIncludesOpaqueBars = NO;
  596. }
  597. - (void)didDismissSearchController:(UISearchController *)searchController {
  598. self.needSort = YES;
  599. }
  600. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  601. if (searchController.active) {
  602. NSString *searchString = [self.searchController.searchBar text];
  603. if (self.searchList!= nil) {
  604. [self.searchList removeAllObjects];
  605. for (WFCCUserInfo *friend in self.dataArray) {
  606. if ([friend.displayName.lowercaseString containsString:searchString.lowercaseString] || [friend.friendAlias.lowercaseString containsString:searchString.lowercaseString]) {
  607. [self.searchList addObject:friend];
  608. }
  609. }
  610. }
  611. self.needSort = YES;
  612. }
  613. }
  614. + (NSMutableDictionary *)sortedArrayWithPinYinDic:(NSArray *)userList {
  615. if (!userList)
  616. return nil;
  617. NSArray *_keys = @[
  618. @"A",
  619. @"B",
  620. @"C",
  621. @"D",
  622. @"E",
  623. @"F",
  624. @"G",
  625. @"H",
  626. @"I",
  627. @"J",
  628. @"K",
  629. @"L",
  630. @"M",
  631. @"N",
  632. @"O",
  633. @"P",
  634. @"Q",
  635. @"R",
  636. @"S",
  637. @"T",
  638. @"U",
  639. @"V",
  640. @"W",
  641. @"X",
  642. @"Y",
  643. @"Z",
  644. @"#"
  645. ];
  646. NSMutableDictionary *infoDic = [NSMutableDictionary new];
  647. NSMutableArray *_tempOtherArr = [NSMutableArray new];
  648. BOOL isReturn = NO;
  649. NSMutableDictionary *firstLetterDict = [[NSMutableDictionary alloc] init];
  650. for (NSString *key in _keys) {
  651. if ([_tempOtherArr count]) {
  652. isReturn = YES;
  653. }
  654. NSMutableArray *tempArr = [NSMutableArray new];
  655. for (id user in userList) {
  656. NSString *firstLetter;
  657. WFCCUserInfo *userInfo = (WFCCUserInfo*)user;
  658. NSString *userName = userInfo.displayName;
  659. if (userInfo.friendAlias.length) {
  660. userName = userInfo.friendAlias;
  661. }
  662. if (userName.length == 0) {
  663. userInfo.displayName = [NSString stringWithFormat:@"<%@>", userInfo.userId];
  664. userName = userInfo.displayName;
  665. }
  666. firstLetter = [firstLetterDict objectForKey:userName];
  667. if (!firstLetter) {
  668. firstLetter = [self getFirstUpperLetter:userName];
  669. [firstLetterDict setObject:firstLetter forKey:userName];
  670. }
  671. if ([firstLetter isEqualToString:key]) {
  672. [tempArr addObject:user];
  673. }
  674. if (isReturn)
  675. continue;
  676. char c = [firstLetter characterAtIndex:0];
  677. if (isalpha(c) == 0) {
  678. [_tempOtherArr addObject:user];
  679. }
  680. }
  681. if (![tempArr count])
  682. continue;
  683. [infoDic setObject:tempArr forKey:key];
  684. }
  685. if ([_tempOtherArr count])
  686. [infoDic setObject:_tempOtherArr forKey:@"#"];
  687. NSArray *keys = [[infoDic allKeys]
  688. sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  689. return [obj1 compare:obj2 options:NSNumericSearch];
  690. }];
  691. NSMutableArray *allKeys = [[NSMutableArray alloc] initWithArray:keys];
  692. if ([allKeys containsObject:@"#"]) {
  693. [allKeys removeObject:@"#"];
  694. [allKeys insertObject:@"#" atIndex:allKeys.count];
  695. }
  696. NSMutableDictionary *resultDic = [NSMutableDictionary new];
  697. [resultDic setObject:infoDic forKey:@"infoDic"];
  698. [resultDic setObject:allKeys forKey:@"allKeys"];
  699. [infoDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  700. NSMutableArray *_tempOtherArr = (NSMutableArray *)obj;
  701. [_tempOtherArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  702. WFCCUserInfo *user1 = (WFCCUserInfo *)obj1;
  703. WFCCUserInfo *user2 = (WFCCUserInfo *)obj2;
  704. NSString *user1Pinyin = [WFCUContactListViewController hanZiToPinYinWithString:user1.displayName];
  705. NSString *user2Pinyin = [WFCUContactListViewController hanZiToPinYinWithString:user2.displayName];
  706. return [user1Pinyin compare:user2Pinyin];
  707. }];
  708. }];
  709. return resultDic;
  710. }
  711. + (NSString *)getFirstUpperLetter:(NSString *)hanzi {
  712. NSString *pinyin = [self hanZiToPinYinWithString:hanzi];
  713. NSString *firstUpperLetter = [[pinyin substringToIndex:1] uppercaseString];
  714. if ([firstUpperLetter compare:@"A"] != NSOrderedAscending &&
  715. [firstUpperLetter compare:@"Z"] != NSOrderedDescending) {
  716. return firstUpperLetter;
  717. } else {
  718. return @"#";
  719. }
  720. }
  721. + (NSString *)hanZiToPinYinWithString:(NSString *)hanZi {
  722. if (!hanZi) {
  723. return nil;
  724. }
  725. if (!hanziStringDict) {
  726. hanziStringDict = [[NSMutableDictionary alloc] init];
  727. }
  728. NSString *pinYinResult = [hanziStringDict objectForKey:hanZi];
  729. if (pinYinResult) {
  730. return pinYinResult;
  731. }
  732. pinYinResult = [NSString string];
  733. for (int j = 0; j < hanZi.length; j++) {
  734. NSString *singlePinyinLetter = nil;
  735. if ([self isChinese:[hanZi substringWithRange:NSMakeRange(j, 1)]]) {
  736. singlePinyinLetter = [[NSString
  737. stringWithFormat:@"%c", pinyinFirstLetter([hanZi characterAtIndex:j])]
  738. uppercaseString];
  739. }else{
  740. singlePinyinLetter = [hanZi substringWithRange:NSMakeRange(j, 1)];
  741. }
  742. pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter];
  743. }
  744. [hanziStringDict setObject:pinYinResult forKey:hanZi];
  745. return pinYinResult;
  746. }
  747. + (BOOL)isChinese:(NSString *)text
  748. {
  749. NSString *match = @"(^[\u4e00-\u9fa5]+$)";
  750. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
  751. return [predicate evaluateWithObject:text];
  752. }
  753. - (void)dealloc {
  754. [[NSNotificationCenter defaultCenter] removeObserver:self];
  755. }
  756. @end