WFCUContactListViewController.m 33 KB

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