WFCUContactListViewController.m 31 KB

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