EaseUsersListViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseUsersListViewController.h"
  13. #import "UIViewController+HUD.h"
  14. #import "EaseMessageViewController.h"
  15. @interface EaseUsersListViewController ()
  16. @property (strong, nonatomic) UISearchBar *searchBar;
  17. @end
  18. @implementation EaseUsersListViewController
  19. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  20. {
  21. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  22. if (self) {
  23. }
  24. return self;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. [self tableViewDidTriggerHeaderRefresh];
  30. }
  31. - (void)didReceiveMemoryWarning {
  32. [super didReceiveMemoryWarning];
  33. // Dispose of any resources that can be recreated.
  34. }
  35. #pragma mark - setter
  36. - (void)setShowSearchBar:(BOOL)showSearchBar
  37. {
  38. if (_showSearchBar != showSearchBar) {
  39. _showSearchBar = showSearchBar;
  40. }
  41. }
  42. #pragma mark - Table view data source
  43. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  44. {
  45. // Return the number of sections.
  46. return 2;
  47. }
  48. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  49. {
  50. // Return the number of rows in the section.
  51. if (section == 0) {
  52. if ([_dataSource respondsToSelector:@selector(numberOfRowInUserListViewController:)]) {
  53. return [_dataSource numberOfRowInUserListViewController:self];
  54. }
  55. return 0;
  56. }
  57. return [self.dataArray count];
  58. }
  59. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  60. {
  61. NSString *CellIdentifier = [EaseUserCell cellIdentifierWithModel:nil];
  62. EaseUserCell *cell = (EaseUserCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  63. // Configure the cell...
  64. if (cell == nil) {
  65. cell = [[EaseUserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  66. }
  67. if (indexPath.section == 0) {
  68. return nil;
  69. } else {
  70. id<IUserModel> model = nil;
  71. if ([_dataSource respondsToSelector:@selector(userListViewController:userModelForIndexPath:)]) {
  72. model = [_dataSource userListViewController:self userModelForIndexPath:indexPath];
  73. }
  74. else {
  75. model = [self.dataArray objectAtIndex:indexPath.row];
  76. }
  77. if (model) {
  78. cell.model = model;
  79. }
  80. return cell;
  81. }
  82. }
  83. #pragma mark - Table view delegate
  84. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. return [EaseUserCell cellHeightWithModel:nil];
  87. }
  88. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  89. {
  90. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  91. id<IUserModel> model = nil;
  92. if (_dataSource && [_dataSource respondsToSelector:@selector(userListViewController:userModelForIndexPath:)]) {
  93. model = [_dataSource userListViewController:self userModelForIndexPath:indexPath];
  94. }
  95. else {
  96. model = [self.dataArray objectAtIndex:indexPath.row];
  97. }
  98. if (model) {
  99. if (_delegate && [_delegate respondsToSelector:@selector(userListViewController:didSelectUserModel:)]) {
  100. [_delegate userListViewController:self didSelectUserModel:model];
  101. } else {
  102. EaseMessageViewController *viewController = [[EaseMessageViewController alloc] initWithConversationChatter:model.buddy conversationType:EMConversationTypeChat];
  103. viewController.title = model.nickname;
  104. [self.navigationController pushViewController:viewController animated:YES];
  105. }
  106. }}
  107. #pragma mark - data
  108. - (void)tableViewDidTriggerHeaderRefresh
  109. {
  110. __weak typeof(self) weakself = self;
  111. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  112. EMError *error = nil;
  113. NSArray *buddyList = [[EMClient sharedClient].contactManager getContactsFromServerWithError:&error];
  114. if (!error) {
  115. NSMutableArray *contactsSource = [NSMutableArray arrayWithArray:buddyList];
  116. NSMutableArray *tempDataArray = [NSMutableArray array];
  117. // remove the contact that is currently in the black list
  118. NSArray *blockList = [[EMClient sharedClient].contactManager getBlackList];
  119. for (NSInteger i = 0; i < buddyList.count; i++) {
  120. NSString *buddy = [buddyList objectAtIndex:i];
  121. if (![blockList containsObject:buddy]) {
  122. [contactsSource addObject:buddy];
  123. id<IUserModel> model = nil;
  124. if (weakself.dataSource && [weakself.dataSource respondsToSelector:@selector(userListViewController:modelForBuddy:)]) {
  125. model = [weakself.dataSource userListViewController:self modelForBuddy:buddy];
  126. }
  127. else{
  128. model = [[EaseUserModel alloc] initWithBuddy:buddy];
  129. }
  130. if(model){
  131. [tempDataArray addObject:model];
  132. }
  133. }
  134. }
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. [weakself.dataArray removeAllObjects];
  137. [weakself.dataArray addObjectsFromArray:tempDataArray];
  138. [weakself.tableView reloadData];
  139. });
  140. }
  141. [weakself tableViewDidFinishTriggerHeader:YES reload:NO];
  142. });
  143. }
  144. - (void)dealloc
  145. {
  146. }
  147. @end