WFCUGroupMemberCollectionViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // WFCUGroupMemberCollectionViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by heavyrain lee on 2019/8/18.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUGroupMemberCollectionViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUConversationSettingMemberCollectionViewLayout.h"
  11. #import "WFCUConversationSettingMemberCell.h"
  12. #import "WFCUContactListViewController.h"
  13. #import "WFCUProfileTableViewController.h"
  14. #import "WFCUConfigManager.h"
  15. #import "UIView+Toast.h"
  16. @interface WFCUGroupMemberCollectionViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
  17. @property (nonatomic, strong)UICollectionView *memberCollectionView;
  18. @property (nonatomic, strong)WFCUConversationSettingMemberCollectionViewLayout *memberCollectionViewLayout;
  19. @property (nonatomic, strong)NSArray<WFCCGroupMember *> *memberList;
  20. @property (nonatomic, strong)WFCCGroupInfo *groupInfo;
  21. @end
  22. #define Group_Member_Cell_Reuese_ID @"cell"
  23. @implementation WFCUGroupMemberCollectionViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupId forceUpdate:YES];
  27. self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.groupId refresh:YES];
  28. int memberCollectionCount;
  29. if ([self isGroupManager]) {
  30. memberCollectionCount = (int)self.memberList.count + 2;
  31. } else if(self.groupInfo.type == GroupType_Restricted) {
  32. if (self.groupInfo.joinType == 1 || self.groupInfo.joinType == 0) {
  33. memberCollectionCount = (int)self.memberList.count + 1;
  34. } else {
  35. memberCollectionCount = (int)self.memberList.count;
  36. }
  37. } else {
  38. memberCollectionCount = (int)self.memberList.count + 1;
  39. }
  40. self.memberCollectionViewLayout = [[WFCUConversationSettingMemberCollectionViewLayout alloc] initWithItemMargin:8];
  41. self.memberCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.memberCollectionViewLayout];
  42. self.memberCollectionView.delegate = self;
  43. self.memberCollectionView.dataSource = self;
  44. self.memberCollectionView.backgroundColor = [UIColor whiteColor];
  45. [self.memberCollectionView registerClass:[WFCUConversationSettingMemberCell class] forCellWithReuseIdentifier:Group_Member_Cell_Reuese_ID];
  46. [self.view addSubview:self.memberCollectionView];
  47. __weak typeof(self)ws = self;
  48. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupMemberUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  49. if ([ws.groupId isEqualToString:note.object]) {
  50. ws.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:ws.groupId refresh:NO];
  51. ws.memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:NO];
  52. [ws.memberCollectionView reloadData];
  53. }
  54. }];
  55. }
  56. - (BOOL)isGroupOwner {
  57. return [self.groupInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId];
  58. }
  59. - (BOOL)isGroupManager {
  60. if ([self isGroupOwner]) {
  61. return YES;
  62. }
  63. __block BOOL isManager = false;
  64. [self.memberList enumerateObjectsUsingBlock:^(WFCCGroupMember * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  65. if ([obj.memberId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  66. if (obj.type == Member_Type_Manager || obj.type == Member_Type_Owner) {
  67. isManager = YES;
  68. }
  69. *stop = YES;
  70. }
  71. }];
  72. return isManager;
  73. }
  74. #pragma mark - UICollectionViewDataSource
  75. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  76. if([self isGroupManager]) {
  77. return self.memberList.count + 2;
  78. } else {
  79. if (self.groupInfo.type == GroupType_Restricted && self.groupInfo.joinType != 1 && self.groupInfo.joinType != 0) {
  80. return self.memberList.count;
  81. }
  82. return self.memberList.count + 1;
  83. }
  84. return 0;
  85. }
  86. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  87. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  88. WFCUConversationSettingMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Group_Member_Cell_Reuese_ID forIndexPath:indexPath];
  89. if (indexPath.row < self.memberList.count) {
  90. WFCCGroupMember *member = self.memberList[indexPath.row];
  91. [cell setModel:member withType:Group_Type];
  92. } else {
  93. if (indexPath.row == self.memberList.count) {
  94. [cell.headerImageView setImage:[UIImage imageNamed:@"addmember"]];
  95. cell.nameLabel.text = nil;
  96. cell.nameLabel.hidden = YES;
  97. } else {
  98. [cell.headerImageView setImage:[UIImage imageNamed:@"removemember"]];
  99. cell.nameLabel.text = nil;
  100. cell.nameLabel.hidden = YES;
  101. }
  102. }
  103. return cell;
  104. }
  105. #pragma mark - UICollectionViewDelegate
  106. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  107. __weak typeof(self)ws = self;
  108. if (indexPath.row == self.memberList.count) {
  109. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  110. pvc.selectContact = YES;
  111. pvc.multiSelect = YES;
  112. NSMutableArray *disabledUser = [[NSMutableArray alloc] init];
  113. for (WFCCGroupMember *member in self.memberList) {
  114. [disabledUser addObject:member.memberId];
  115. }
  116. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  117. [[WFCCIMService sharedWFCIMService] addMembers:contacts toGroup:ws.groupId notifyLines:@[@(0)] notifyContent:nil success:^{
  118. [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:YES];
  119. } error:^(int error_code) {
  120. }];
  121. };
  122. pvc.disableUsersSelected = YES;
  123. pvc.disableUsers = disabledUser;
  124. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  125. [self.navigationController presentViewController:navi animated:YES completion:nil];
  126. } else if(indexPath.row == self.memberList.count + 1) {
  127. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  128. pvc.selectContact = YES;
  129. pvc.multiSelect = YES;
  130. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  131. [[WFCCIMService sharedWFCIMService] kickoffMembers:contacts fromGroup:self.groupId notifyLines:@[@(0)] notifyContent:nil success:^{
  132. [[WFCCIMService sharedWFCIMService] getGroupMembers:ws.groupId forceUpdate:YES];
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. NSMutableArray *tmpArray = [self.memberList mutableCopy];
  135. NSMutableArray *removeArray = [[NSMutableArray alloc] init];
  136. [tmpArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  137. WFCCGroupMember *member = obj;
  138. if([contacts containsObject:member.memberId]) {
  139. [removeArray addObject:member];
  140. }
  141. }];
  142. [tmpArray removeObjectsInArray:removeArray];
  143. self.memberList = [tmpArray mutableCopy];
  144. [self.memberCollectionView reloadData];
  145. });
  146. } error:^(int error_code) {
  147. }];
  148. };
  149. NSMutableArray *candidateUsers = [[NSMutableArray alloc] init];
  150. NSMutableArray *disableUsers = [[NSMutableArray alloc] init];
  151. BOOL isOwner = [self isGroupOwner];
  152. for (WFCCGroupMember *member in self.memberList) {
  153. [candidateUsers addObject:member.memberId];
  154. if (!isOwner && (member.type == Member_Type_Manager || [self.groupInfo.owner isEqualToString:member.memberId])) {
  155. [disableUsers addObject:member.memberId];
  156. }
  157. }
  158. [disableUsers addObject:[WFCCNetworkService sharedInstance].userId];
  159. pvc.candidateUsers = candidateUsers;
  160. pvc.disableUsers = [disableUsers copy];
  161. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  162. [self.navigationController presentViewController:navi animated:YES completion:nil];
  163. } else {
  164. WFCCGroupMember *member = [self.memberList objectAtIndex:indexPath.row];
  165. NSString *userId = member.memberId;
  166. if (self.groupInfo.privateChat) {
  167. if (![self.groupInfo.owner isEqualToString:userId] && ![self.groupInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  168. WFCCGroupMember *gm = [[WFCCIMService sharedWFCIMService] getGroupMember:self.groupId memberId:[WFCCNetworkService sharedInstance].userId];
  169. if (gm.type != Member_Type_Manager) {
  170. WFCCGroupMember *gm = [[WFCCIMService sharedWFCIMService] getGroupMember:self.groupId memberId:userId];
  171. if (gm.type != Member_Type_Manager) {
  172. [self.view makeToast:WFCString(@"NotAllowTemporarySession") duration:1 position:CSToastPositionCenter];
  173. return;
  174. }
  175. }
  176. }
  177. }
  178. WFCUProfileTableViewController *vc = [[WFCUProfileTableViewController alloc] init];
  179. vc.userId = userId;
  180. vc.hidesBottomBarWhenPushed = YES;
  181. [self.navigationController pushViewController:vc animated:YES];
  182. }
  183. }
  184. - (void)dealloc {
  185. [[NSNotificationCenter defaultCenter] removeObserver:self];
  186. }
  187. @end