EMAtGroupMembersViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // EMAtGroupMembersViewController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/19.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMAtGroupMembersViewController.h"
  9. #import "EMRealtimeSearch.h"
  10. #import "EMSearchBar.h"
  11. #import "EMAvatarNameCell.h"
  12. @interface EMAtGroupMembersViewController ()<EMSearchBarDelegate, EMAvatarNameCellDelegate>
  13. @property (nonatomic, strong) EMGroup *group;
  14. @property (nonatomic, strong) NSString *cursor;
  15. @end
  16. @implementation EMAtGroupMembersViewController
  17. - (instancetype)initWithGroup:(EMGroup *)aGroup
  18. {
  19. self = [super init];
  20. if (self) {
  21. self.group = aGroup;
  22. }
  23. return self;
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view.
  28. [self _setupSubviews];
  29. [self _fetchGroupMembersWithIsHeader:YES isShowHUD:YES];
  30. }
  31. #pragma mark - Subviews
  32. - (void)_setupSubviews
  33. {
  34. [self addPopBackLeftItemWithTarget:self action:@selector(backAction)];
  35. self.title = @"群组成员列表";
  36. self.showRefreshHeader = YES;
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. self.tableView.rowHeight = 60;
  39. }
  40. #pragma mark - Table view data source
  41. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  42. return 1;
  43. }
  44. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  45. if (tableView == self.tableView) {
  46. return [self.dataArray count];
  47. } else {
  48. return [self.searchResults count];
  49. }
  50. }
  51. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  52. EMAvatarNameCell *cell = (EMAvatarNameCell *)[tableView dequeueReusableCellWithIdentifier:@"UITableViewCellOwner"];
  53. // Configure the cell...
  54. if (cell == nil || !cell) {
  55. cell = [[EMAvatarNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellOwner"];
  56. cell.delegate = self;
  57. }
  58. NSString *name = nil;
  59. if (tableView == self.tableView) {
  60. name = [self.dataArray objectAtIndex:indexPath.row];
  61. } else {
  62. name = [self.searchResults objectAtIndex:indexPath.row];
  63. }
  64. cell.nameLabel.text = name;
  65. cell.avatarView.image = [UIImage imageNamed:@"user_avatar_blue"];
  66. cell.indexPath = indexPath;
  67. return cell;
  68. }
  69. #pragma mark - Table view delegate
  70. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72. [self.view endEditing:YES];
  73. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  74. NSString *name = nil;
  75. if (tableView == self.tableView) {
  76. name = [self.dataArray objectAtIndex:indexPath.row];
  77. } else {
  78. name = [self.searchResults objectAtIndex:indexPath.row];
  79. }
  80. if (self.selectedCompletion) {
  81. self.selectedCompletion(name);
  82. }
  83. [self backAction];
  84. }
  85. #pragma mark - EMSearchBarDelegate
  86. - (void)searchTextDidChangeWithString:(NSString *)aString
  87. {
  88. __weak typeof(self) weakself = self;
  89. [[EMRealtimeSearch shared] realtimeSearchWithSource:self.dataArray searchText:aString collationStringSelector:nil resultBlock:^(NSArray *results) {
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. [weakself.searchResults removeAllObjects];
  92. [weakself.searchResults addObjectsFromArray:results];
  93. [weakself.searchResultTableView reloadData];
  94. });
  95. }];
  96. }
  97. #pragma mark - Data
  98. - (void)_fetchGroupMembersWithIsHeader:(BOOL)aIsHeader
  99. isShowHUD:(BOOL)aIsShowHUD
  100. {
  101. if (aIsShowHUD) {
  102. [self showHudInView:self.view hint:@"获取群组成员..."];
  103. }
  104. __weak typeof(self) weakself = self;
  105. void (^errorBlock)(EMError *aError) = ^(EMError *aError) {
  106. if (aIsShowHUD) {
  107. [weakself hideHud];
  108. }
  109. [weakself tableViewDidFinishTriggerHeader:aIsHeader reload:NO];
  110. [EMAlertController showErrorAlert:aError.errorDescription];
  111. };
  112. void (^fetchMembersBlock) (void) = ^(void) {
  113. [[EMClient sharedClient].groupManager getGroupMemberListFromServerWithId:weakself.group.groupId cursor:weakself.cursor pageSize:50 completion:^(EMCursorResult *aResult, EMError *aError) {
  114. if (aError) {
  115. errorBlock(aError);
  116. return ;
  117. }
  118. if (aIsShowHUD) {
  119. [weakself hideHud];
  120. }
  121. weakself.cursor = aResult.cursor;
  122. [weakself.dataArray addObjectsFromArray:aResult.list];
  123. [weakself.dataArray removeObject:[EMClient sharedClient].currentUsername];
  124. if ([aResult.list count] == 0 || [aResult.cursor length] == 0) {
  125. weakself.showRefreshFooter = NO;
  126. } else {
  127. weakself.showRefreshFooter = YES;
  128. }
  129. [weakself.tableView reloadData];
  130. [weakself tableViewDidFinishTriggerHeader:aIsHeader reload:NO];
  131. }];
  132. };
  133. if (aIsHeader) {
  134. [[EMClient sharedClient].groupManager getGroupSpecificationFromServerWithId:self.group.groupId completion:^(EMGroup *aGroup, EMError *aError) {
  135. if (aError) {
  136. errorBlock(aError);
  137. return ;
  138. }
  139. weakself.group = aGroup;
  140. [weakself.dataArray removeAllObjects];
  141. [weakself.dataArray addObjectsFromArray:aGroup.adminList];
  142. fetchMembersBlock();
  143. }];
  144. } else {
  145. fetchMembersBlock();
  146. }
  147. }
  148. - (void)tableViewDidTriggerHeaderRefresh
  149. {
  150. self.cursor = nil;
  151. [self _fetchGroupMembersWithIsHeader:YES isShowHUD:NO];
  152. }
  153. - (void)tableViewDidTriggerFooterRefresh
  154. {
  155. [self _fetchGroupMembersWithIsHeader:NO isShowHUD:NO];
  156. }
  157. #pragma mark - Action
  158. - (void)backAction
  159. {
  160. [[EMRealtimeSearch shared] realtimeSearchStop];
  161. [self.navigationController popViewControllerAnimated:YES];
  162. }
  163. @end