2
0

WFCUGroupMemberCollectionViewController.m 11 KB

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