WFCUContactListViewController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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/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. static NSString *wfcstar = @"☆";
  42. @implementation WFCUContactListViewController
  43. - (instancetype)init {
  44. self = [super init];
  45. if (self) {
  46. [self setup];
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  51. self = [super initWithCoder:aDecoder];
  52. if (self) {
  53. [self setup];
  54. }
  55. return self;
  56. }
  57. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  58. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  59. if (self) {
  60. [self setup];
  61. }
  62. return self;
  63. }
  64. - (void)setup {
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFriendRequestUpdated:) name:kFriendRequestUpdated object:nil];
  66. }
  67. - (void)viewDidLoad {
  68. [super viewDidLoad];
  69. // Do any additional setup after loading the view.
  70. CGRect frame = self.view.frame;
  71. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  72. self.tableView.delegate = self;
  73. self.tableView.dataSource = self;
  74. self.tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  75. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  76. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  77. self.tableView.tableHeaderView = nil;
  78. if (self.selectContact) {
  79. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Cancel") style:UIBarButtonItemStyleDone target:self action:@selector(onLeftBarBtn:)];
  80. if(self.multiSelect) {
  81. self.selectedContacts = [[NSMutableArray alloc] init];
  82. [self updateRightBarBtn];
  83. }
  84. } else {
  85. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  86. }
  87. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  88. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onContactsUpdated:) name:kFriendListUpdated object:nil];
  89. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClearAllUnread:) name:@"kTabBarClearBadgeNotification" object:nil];
  90. _searchList = [NSMutableArray array];
  91. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  92. self.searchController.searchResultsUpdater = self;
  93. self.searchController.delegate = self;
  94. self.searchController.dimsBackgroundDuringPresentation = NO;
  95. if (@available(iOS 13, *)) {
  96. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  97. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  98. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  99. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  100. } else {
  101. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  102. }
  103. if (@available(iOS 9.1, *)) {
  104. self.searchController.obscuresBackgroundDuringPresentation = NO;
  105. }
  106. [self.searchController.searchBar setPlaceholder:WFCString(@"SearchContact")];
  107. if (@available(iOS 11.0, *)) {
  108. self.navigationItem.searchController = _searchController;
  109. _searchController.hidesNavigationBarDuringPresentation = YES;
  110. } else {
  111. self.tableView.tableHeaderView = _searchController.searchBar;
  112. }
  113. self.definesPresentationContext = YES;
  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] mutableCopy];
  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. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  316. return contactCell;
  317. } else if(indexPath.row == 1) {
  318. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  319. if (contactCell == nil) {
  320. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  321. }
  322. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  323. contactCell.nameLabel.text = WFCString(@"Group");
  324. contactCell.portraitView.image = [UIImage imageNamed:@"contact_group_icon"];
  325. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  326. return contactCell;
  327. } else {
  328. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  329. if (contactCell == nil) {
  330. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  331. }
  332. contactCell.nameLabel.text = WFCString(@"Channel");
  333. contactCell.portraitView.image = [UIImage imageNamed:@"contact_channel_icon"];
  334. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  335. return contactCell;
  336. }
  337. } else {
  338. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section - 1]];
  339. }
  340. }
  341. if (self.selectContact) {
  342. #define SELECT_REUSEIDENTIFY @"resueSelectCell"
  343. if (self.multiSelect && !self.withoutCheckBox) {
  344. WFCUContactSelectTableViewCell *selectCell = [tableView dequeueReusableCellWithIdentifier:SELECT_REUSEIDENTIFY];
  345. if (selectCell == nil) {
  346. selectCell = [[WFCUContactSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SELECT_REUSEIDENTIFY];
  347. selectCell.selectionStyle = UITableViewCellSelectionStyleNone;
  348. }
  349. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  350. selectCell.friendUid = userInfo.userId;
  351. selectCell.multiSelect = self.multiSelect;
  352. if ([self.selectedContacts containsObject:userInfo.userId]) {
  353. selectCell.checked = YES;
  354. } else {
  355. selectCell.checked = NO;
  356. }
  357. if ([self.disableUsers containsObject:userInfo.userId]) {
  358. selectCell.disabled = YES;
  359. if(self.disableUsersSelected) {
  360. selectCell.checked = YES;
  361. } else {
  362. selectCell.checked = NO;
  363. }
  364. } else {
  365. selectCell.disabled = NO;
  366. }
  367. selectCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  368. cell = selectCell;
  369. } else {
  370. WFCUContactTableViewCell *selectCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  371. if (selectCell == nil) {
  372. selectCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  373. }
  374. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  375. selectCell.userId = userInfo.userId;
  376. selectCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  377. cell = selectCell;
  378. }
  379. } else {
  380. #define REUSEIDENTIFY @"resueCell"
  381. if (indexPath.section == 0 && !self.searchController.active) {
  382. if (indexPath.row == 0) {
  383. WFCUNewFriendTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"];
  384. if (contactCell == nil) {
  385. contactCell = [[WFCUNewFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newFriendCell"];
  386. }
  387. [contactCell refresh];
  388. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  389. contactCell.nameLabel.text = WFCString(@"NewFriend");
  390. contactCell.portraitView.image = [UIImage imageNamed:@"friend_request_icon"];
  391. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  392. cell = contactCell;
  393. } else {
  394. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  395. if (contactCell == nil) {
  396. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  397. }
  398. contactCell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  399. contactCell.nameLabel.text = WFCString(@"Group");
  400. contactCell.portraitView.image = [UIImage imageNamed:@"contact_group_icon"];
  401. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  402. cell = contactCell;
  403. }
  404. } else {
  405. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  406. if (contactCell == nil) {
  407. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  408. }
  409. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  410. contactCell.userId = userInfo.userId;
  411. contactCell.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
  412. cell = contactCell;
  413. }
  414. }
  415. if (cell == nil) {
  416. NSLog(@"error");
  417. }
  418. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0);
  419. return cell;
  420. }
  421. -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  422. if (@available(iOS 11.0, *)) {
  423. if (self.selectContact) {
  424. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  425. NSMutableArray *indexs = [self.allKeys mutableCopy];
  426. [indexs insertObject:@"" atIndex:0];
  427. return indexs;
  428. }
  429. return self.allKeys;
  430. }
  431. if (self.searchController.active) {
  432. return self.allKeys;
  433. }
  434. NSMutableArray *indexs = [self.allKeys mutableCopy];
  435. [indexs insertObject:@"" atIndex:0];
  436. return indexs;
  437. } else {
  438. return nil;
  439. }
  440. }
  441. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  442. if (self.selectContact) {
  443. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  444. return self.allKeys.count + 1;
  445. }
  446. return self.allKeys.count;
  447. }
  448. if (self.searchController.active) {
  449. return self.allKeys.count;
  450. }
  451. return 1 + self.allKeys.count;
  452. }
  453. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  454. if (section == 0) {
  455. return 0;
  456. }
  457. return 30;
  458. }
  459. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  460. NSString *title;
  461. if (self.selectContact || self.searchController.active) {
  462. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  463. if (section == 0) {
  464. return nil;
  465. }
  466. title = self.allKeys[section-1];
  467. } else {
  468. title = self.allKeys[section];
  469. }
  470. } else {
  471. if (section == 0) {
  472. return nil;
  473. } else {
  474. title = self.allKeys[section - 1];
  475. }
  476. }
  477. if (title == nil || title.length == 0) {
  478. return nil;
  479. }
  480. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
  481. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, self.view.frame.size.width, 30)];
  482. label.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:13];
  483. label.textAlignment = NSTextAlignmentLeft;
  484. if ([title isEqualToString:wfcstar]) {
  485. title = @"☆ 星标好友";
  486. }
  487. label.text = [NSString stringWithFormat:@"%@", title];
  488. [view addSubview:label];
  489. return view;
  490. }
  491. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  492. return 51;
  493. }
  494. - (UIActivityIndicatorView *)activityIndicator {
  495. if (!_activityIndicator) {
  496. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  497. _activityIndicator.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
  498. [self.view addSubview:_activityIndicator];
  499. [_activityIndicator startAnimating];
  500. [self.view bringSubviewToFront:_activityIndicator];
  501. }
  502. return _activityIndicator;
  503. }
  504. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  505. if (self.selectContact) {
  506. return index;
  507. }
  508. if (self.searchController.active) {
  509. return index;
  510. }
  511. return index;
  512. }
  513. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  514. view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  515. }
  516. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  517. NSArray *dataSource;
  518. if (self.searchController.active || self.selectContact) {
  519. if ((self.showCreateChannel || self.showMentionAll) && !self.searchController.active) {
  520. if (indexPath.section == 0) {
  521. if (self.showCreateChannel) {
  522. [self left:^{
  523. if (self.createChannel) {
  524. self.createChannel();
  525. }
  526. }];
  527. } else {
  528. [self left:^{
  529. if (self.mentionAll) {
  530. self.mentionAll();
  531. }
  532. }];
  533. }
  534. return;
  535. }
  536. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section-1]];
  537. } else {
  538. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section]];
  539. }
  540. } else {
  541. if (indexPath.section == 0) {
  542. if (indexPath.row == 0) {
  543. UIViewController *addFriendVC = [[WFCUFriendRequestViewController alloc] init];
  544. addFriendVC.hidesBottomBarWhenPushed = YES;
  545. [self.navigationController pushViewController:addFriendVC animated:YES];
  546. } else if(indexPath.row == 1) {
  547. WFCUFavGroupTableViewController *groupVC = [[WFCUFavGroupTableViewController alloc] init];;
  548. groupVC.hidesBottomBarWhenPushed = YES;
  549. [self.navigationController pushViewController:groupVC animated:YES];
  550. } else {
  551. WFCUFavChannelTableViewController *channelVC = [[WFCUFavChannelTableViewController alloc] init];;
  552. channelVC.hidesBottomBarWhenPushed = YES;
  553. [self.navigationController pushViewController:channelVC animated:YES];
  554. }
  555. return;
  556. } else {
  557. dataSource = self.allFriendSectionDic[self.allKeys[indexPath.section - 1]];
  558. }
  559. }
  560. if (self.selectContact) {
  561. WFCCUserInfo *userInfo = dataSource[indexPath.row];
  562. if (self.multiSelect) {
  563. if ([self.disableUsers containsObject:userInfo.userId]) {
  564. return;
  565. }
  566. if ([self.selectedContacts containsObject:userInfo.userId]) {
  567. [self.selectedContacts removeObject:userInfo.userId];
  568. ((WFCUContactSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).checked = NO;
  569. } else {
  570. if (self.maxSelectCount > 0 && self.selectedContacts.count >= self.maxSelectCount) {
  571. [self.view makeToast:WFCString(@"MaxCount")];
  572. return;
  573. }
  574. [self.selectedContacts addObject:userInfo.userId];
  575. ((WFCUContactSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]).checked = YES;
  576. }
  577. [self updateRightBarBtn];
  578. } else {
  579. self.selectResult([NSArray arrayWithObjects:userInfo.userId, nil]);
  580. [self left:nil];
  581. }
  582. } else {
  583. WFCUProfileTableViewController *vc = [[WFCUProfileTableViewController alloc] init];
  584. WFCCUserInfo *friend = dataSource[indexPath.row];
  585. vc.userId = friend.userId;
  586. vc.hidesBottomBarWhenPushed = YES;
  587. [self.navigationController pushViewController:vc animated:YES];
  588. }
  589. }
  590. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  591. if (self.searchController.active) {
  592. [self.searchController.searchBar resignFirstResponder];
  593. }
  594. }
  595. #pragma mark - UISearchControllerDelegate
  596. - (void)didPresentSearchController:(UISearchController *)searchController {
  597. self.tabBarController.tabBar.hidden = YES;
  598. self.extendedLayoutIncludesOpaqueBars = YES;
  599. }
  600. - (void)willDismissSearchController:(UISearchController *)searchController {
  601. self.tabBarController.tabBar.hidden = NO;
  602. self.extendedLayoutIncludesOpaqueBars = NO;
  603. }
  604. - (void)didDismissSearchController:(UISearchController *)searchController {
  605. self.needSort = YES;
  606. }
  607. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  608. if (searchController.active) {
  609. NSString *searchString = [self.searchController.searchBar text];
  610. if (self.searchList!= nil) {
  611. [self.searchList removeAllObjects];
  612. for (WFCCUserInfo *friend in self.dataArray) {
  613. if ([friend.displayName.lowercaseString containsString:searchString.lowercaseString] || [friend.friendAlias.lowercaseString containsString:searchString.lowercaseString]) {
  614. [self.searchList addObject:friend];
  615. }
  616. }
  617. }
  618. self.needSort = YES;
  619. }
  620. }
  621. + (NSMutableDictionary *)sortedArrayWithPinYinDic:(NSArray *)userList {
  622. if (!userList)
  623. return nil;
  624. NSArray *_keys = @[
  625. wfcstar,
  626. @"A",
  627. @"B",
  628. @"C",
  629. @"D",
  630. @"E",
  631. @"F",
  632. @"G",
  633. @"H",
  634. @"I",
  635. @"J",
  636. @"K",
  637. @"L",
  638. @"M",
  639. @"N",
  640. @"O",
  641. @"P",
  642. @"Q",
  643. @"R",
  644. @"S",
  645. @"T",
  646. @"U",
  647. @"V",
  648. @"W",
  649. @"X",
  650. @"Y",
  651. @"Z",
  652. @"#"
  653. ];
  654. NSMutableDictionary *infoDic = [NSMutableDictionary new];
  655. NSMutableArray *_tempOtherArr = [NSMutableArray new];
  656. BOOL isReturn = NO;
  657. NSMutableDictionary *firstLetterDict = [[NSMutableDictionary alloc] init];
  658. NSArray<NSString *> *favUsers = [[WFCCIMService sharedWFCIMService] getFavUsers];
  659. NSMutableArray *favArrays = [[NSMutableArray alloc] init];
  660. for (NSString *favUser in favUsers) {
  661. for (WFCCUserInfo *userInfo in userList) {
  662. if ([userInfo.userId isEqualToString:favUser]) {
  663. [favArrays addObject:userInfo];
  664. break;
  665. }
  666. }
  667. }
  668. if (favArrays.count) {
  669. [infoDic setObject:favArrays forKey:wfcstar];
  670. }
  671. for (NSString *key in _keys) {
  672. if ([key isEqualToString:wfcstar]) {
  673. continue;
  674. }
  675. if ([_tempOtherArr count]) {
  676. isReturn = YES;
  677. }
  678. NSMutableArray *tempArr = [NSMutableArray new];
  679. for (id user in userList) {
  680. NSString *firstLetter;
  681. WFCCUserInfo *userInfo = (WFCCUserInfo*)user;
  682. NSString *userName = userInfo.displayName;
  683. if (userInfo.friendAlias.length) {
  684. userName = userInfo.friendAlias;
  685. }
  686. if (userName.length == 0) {
  687. userInfo.displayName = [NSString stringWithFormat:@"<%@>", userInfo.userId];
  688. userName = userInfo.displayName;
  689. }
  690. firstLetter = [firstLetterDict objectForKey:userName];
  691. if (!firstLetter) {
  692. firstLetter = [self getFirstUpperLetter:userName];
  693. [firstLetterDict setObject:firstLetter forKey:userName];
  694. }
  695. if ([firstLetter isEqualToString:key]) {
  696. [tempArr addObject:user];
  697. }
  698. if (isReturn)
  699. continue;
  700. char c = [firstLetter characterAtIndex:0];
  701. if (isalpha(c) == 0) {
  702. [_tempOtherArr addObject:user];
  703. }
  704. }
  705. if (![tempArr count])
  706. continue;
  707. [infoDic setObject:tempArr forKey:key];
  708. }
  709. if ([_tempOtherArr count])
  710. [infoDic setObject:_tempOtherArr forKey:@"#"];
  711. NSArray *keys = [[infoDic allKeys]
  712. sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  713. return [obj1 compare:obj2 options:NSNumericSearch];
  714. }];
  715. NSMutableArray *allKeys = [[NSMutableArray alloc] initWithArray:keys];
  716. if ([allKeys containsObject:@"#"]) {
  717. [allKeys removeObject:@"#"];
  718. [allKeys insertObject:@"#" atIndex:allKeys.count];
  719. }
  720. if ([allKeys containsObject:wfcstar]) {
  721. [allKeys removeObject:wfcstar];
  722. [allKeys insertObject:wfcstar atIndex:0];
  723. }
  724. NSMutableDictionary *resultDic = [NSMutableDictionary new];
  725. [resultDic setObject:infoDic forKey:@"infoDic"];
  726. [resultDic setObject:allKeys forKey:@"allKeys"];
  727. [infoDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  728. NSMutableArray *_tempOtherArr = (NSMutableArray *)obj;
  729. [_tempOtherArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  730. WFCCUserInfo *user1 = (WFCCUserInfo *)obj1;
  731. WFCCUserInfo *user2 = (WFCCUserInfo *)obj2;
  732. NSString *user1Pinyin = [WFCUContactListViewController hanZiToPinYinWithString:user1.displayName];
  733. NSString *user2Pinyin = [WFCUContactListViewController hanZiToPinYinWithString:user2.displayName];
  734. return [user1Pinyin compare:user2Pinyin];
  735. }];
  736. }];
  737. return resultDic;
  738. }
  739. + (NSString *)getFirstUpperLetter:(NSString *)hanzi {
  740. NSString *pinyin = [self hanZiToPinYinWithString:hanzi];
  741. NSString *firstUpperLetter = [[pinyin substringToIndex:1] uppercaseString];
  742. if ([firstUpperLetter compare:@"A"] != NSOrderedAscending &&
  743. [firstUpperLetter compare:@"Z"] != NSOrderedDescending) {
  744. return firstUpperLetter;
  745. } else {
  746. return @"#";
  747. }
  748. }
  749. + (NSString *)hanZiToPinYinWithString:(NSString *)hanZi {
  750. if (!hanZi) {
  751. return nil;
  752. }
  753. if (!hanziStringDict) {
  754. hanziStringDict = [[NSMutableDictionary alloc] init];
  755. }
  756. NSString *pinYinResult = [hanziStringDict objectForKey:hanZi];
  757. if (pinYinResult) {
  758. return pinYinResult;
  759. }
  760. pinYinResult = [NSString string];
  761. for (int j = 0; j < hanZi.length; j++) {
  762. NSString *singlePinyinLetter = nil;
  763. if ([self isChinese:[hanZi substringWithRange:NSMakeRange(j, 1)]]) {
  764. singlePinyinLetter = [[NSString
  765. stringWithFormat:@"%c", pinyinFirstLetter([hanZi characterAtIndex:j])]
  766. uppercaseString];
  767. }else{
  768. singlePinyinLetter = [hanZi substringWithRange:NSMakeRange(j, 1)];
  769. }
  770. pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter];
  771. }
  772. [hanziStringDict setObject:pinYinResult forKey:hanZi];
  773. return pinYinResult;
  774. }
  775. + (BOOL)isChinese:(NSString *)text
  776. {
  777. NSString *match = @"(^[\u4e00-\u9fa5]+$)";
  778. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
  779. return [predicate evaluateWithObject:text];
  780. }
  781. - (void)dealloc {
  782. [[NSNotificationCenter defaultCenter] removeObserver:self];
  783. }
  784. @end