2
0

WFCUContactListViewController.m 27 KB

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