GroupMuteTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // GroupMuteTableViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by heavyrain lee on 2019/6/26.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "GroupMuteTableViewController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import "WFCUContactListViewController.h"
  11. #import "WFCUGeneralSwitchTableViewCell.h"
  12. #import "WFCUContactListViewController.h"
  13. #import "WFCUImage.h"
  14. #import "UIView+Toast.h"
  15. @interface GroupMuteTableViewController () <UITableViewDelegate, UITableViewDataSource>
  16. @property(nonatomic, strong)UITableView *tableView;
  17. @property(nonatomic, strong)NSMutableArray<WFCCGroupMember *> *mutedMemberList;
  18. @property(nonatomic, strong)NSMutableArray<WFCCGroupMember *> *allowedMemberList;
  19. @end
  20. @implementation GroupMuteTableViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.title = WFCString(@"GroupMuteSetting");
  24. [self loadMemberList];
  25. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
  26. if (@available(iOS 15, *)) {
  27. self.tableView.sectionHeaderTopPadding = 0;
  28. }
  29. self.tableView.delegate = self;
  30. self.tableView.dataSource = self;
  31. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  32. [self.tableView reloadData];
  33. [self.view addSubview:self.tableView];
  34. __weak typeof(self)ws = self;
  35. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupMemberUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  36. if ([ws.groupInfo.target isEqualToString:note.object]) {
  37. [ws loadMemberList];
  38. [ws.tableView reloadData];
  39. }
  40. }];
  41. }
  42. - (void)loadMemberList {
  43. NSArray *memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupInfo.target forceUpdate:YES];
  44. self.mutedMemberList = [[NSMutableArray alloc] init];
  45. self.allowedMemberList = [[NSMutableArray alloc] init];
  46. for (WFCCGroupMember *member in memberList) {
  47. if (member.type == Member_Type_Muted) {
  48. [self.mutedMemberList addObject:member];
  49. } else if (member.type == Member_Type_Allowed) {
  50. [self.allowedMemberList addObject:member];
  51. }
  52. }
  53. }
  54. - (void)selectMemberToAdd:(BOOL)isAllow {
  55. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  56. pvc.selectContact = YES;
  57. pvc.multiSelect = YES;
  58. __weak typeof(self)ws = self;
  59. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  60. if (isAllow) {
  61. [[WFCCIMService sharedWFCIMService] allowGroupMember:self.groupInfo.target isSet:YES memberIds:contacts notifyLines:@[@(0)] notifyContent:nil success:^{
  62. [ws loadMemberList];
  63. [ws.tableView reloadData];
  64. } error:^(int error_code) {
  65. }];
  66. } else {
  67. [[WFCCIMService sharedWFCIMService] muteGroupMember:self.groupInfo.target isSet:YES memberIds:contacts notifyLines:@[@(0)] notifyContent:nil success:^{
  68. [ws loadMemberList];
  69. [ws.tableView reloadData];
  70. } error:^(int error_code) {
  71. }];
  72. }
  73. };
  74. NSMutableArray *candidateUsers = [[NSMutableArray alloc] init];
  75. NSArray *memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupInfo.target forceUpdate:NO];
  76. for (WFCCGroupMember *member in memberList) {
  77. if ((member.type == Member_Type_Normal || (isAllow && member.type == Member_Type_Muted) || (!isAllow && member.type == Member_Type_Allowed)) && ![member.memberId isEqualToString:self.groupInfo.owner]) {
  78. [candidateUsers addObject:member.memberId];
  79. }
  80. }
  81. if([candidateUsers count]) {
  82. pvc.candidateUsers = candidateUsers;
  83. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  84. [self.navigationController presentViewController:navi animated:YES completion:nil];
  85. } else {
  86. [self.view makeToast:@"成员已全部添加"];
  87. }
  88. }
  89. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  90. __weak typeof(self)ws = self;
  91. if (indexPath.section == 0) {
  92. WFCUGeneralSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switchcell"];
  93. if (cell == nil) {
  94. cell = [[WFCUGeneralSwitchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"switchcell"];
  95. cell.textLabel.text = WFCString(@"MuteAll");
  96. cell.onSwitch = ^(BOOL value, int type, void (^onDone)(BOOL success)) {
  97. [[WFCCIMService sharedWFCIMService] modifyGroupInfo:self.groupInfo.target type:Modify_Group_Mute newValue:value?@"1":@"0" notifyLines:@[@(0)] notifyContent:nil success:^{
  98. ws.groupInfo.mute = value;
  99. onDone(YES);
  100. } error:^(int error_code) {
  101. onDone(NO);
  102. }];
  103. };
  104. }
  105. cell.on = self.groupInfo.mute;
  106. return cell;
  107. } else {
  108. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  109. if (cell == nil) {
  110. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  111. }
  112. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  113. cell.accessoryView = nil;
  114. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  115. if(indexPath.section == 1) {
  116. if (indexPath.row == 0) {
  117. cell.imageView.image = [WFCUImage imageNamed:@"plus"];
  118. cell.textLabel.text = WFCString(@"MuteMember");
  119. } else {
  120. WFCCUserInfo *member = [[WFCCIMService sharedWFCIMService] getUserInfo:[self.mutedMemberList objectAtIndex:indexPath.row-1].memberId refresh:NO];
  121. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[member.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"]];
  122. cell.textLabel.text = member.displayName;
  123. }
  124. } else if(indexPath.section == 2) {
  125. if (indexPath.row == 0) {
  126. cell.imageView.image = [WFCUImage imageNamed:@"plus"];
  127. cell.textLabel.text = WFCString(@"AllowMember");
  128. } else {
  129. WFCCUserInfo *member = [[WFCCIMService sharedWFCIMService] getUserInfo:[self.allowedMemberList objectAtIndex:indexPath.row-1].memberId refresh:NO];
  130. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[member.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"]];
  131. cell.textLabel.text = member.displayName;
  132. }
  133. }
  134. return cell;
  135. }
  136. }
  137. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  138. if (indexPath.section == 0 || (indexPath.section == 1 && indexPath.row == 0)) {
  139. return NO;
  140. }
  141. return YES;
  142. }
  143. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  144. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:WFCString(@"Unmute") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  145. __weak typeof(self)ws = self;
  146. if (indexPath.section == 1) {
  147. [[WFCCIMService sharedWFCIMService] muteGroupMember:self.groupInfo.target isSet:NO memberIds:@[[self.mutedMemberList objectAtIndex:indexPath.row-1].memberId] notifyLines:@[@(0)] notifyContent:nil success:^{
  148. for (WFCCGroupMember *member in ws.mutedMemberList) {
  149. if ([member.memberId isEqualToString:[ws.mutedMemberList objectAtIndex:indexPath.row-1].memberId]) {
  150. [ws.mutedMemberList removeObject:member];
  151. [ws.tableView reloadData];
  152. break;
  153. }
  154. }
  155. } error:^(int error_code) {
  156. }];
  157. } else if(indexPath.section == 2) {
  158. [[WFCCIMService sharedWFCIMService] allowGroupMember:self.groupInfo.target isSet:NO memberIds:@[[self.allowedMemberList objectAtIndex:indexPath.row-1].memberId] notifyLines:@[@(0)] notifyContent:nil success:^{
  159. for (WFCCGroupMember *member in ws.allowedMemberList) {
  160. if ([member.memberId isEqualToString:[ws.allowedMemberList objectAtIndex:indexPath.row-1].memberId]) {
  161. [ws.allowedMemberList removeObject:member];
  162. [ws.tableView reloadData];
  163. break;
  164. }
  165. }
  166. } error:^(int error_code) {
  167. }];
  168. }
  169. }];
  170. UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Cancel") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  171. }];
  172. editAction.backgroundColor = [UIColor grayColor];
  173. return @[deleteAction, editAction];
  174. }
  175. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  176. if (section == 0) {
  177. return 1;
  178. } else if(section == 1) {
  179. return self.mutedMemberList.count+1;
  180. } else if(section == 2) {
  181. return self.allowedMemberList.count+1;
  182. }
  183. return 0;
  184. }
  185. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  186. if (section == 0) {
  187. return WFCString(@"MuteAll");
  188. } else if(section == 1) {
  189. return WFCString(@"MutedList");
  190. } else if(section == 2) {
  191. return WFCString(@"AllowList");
  192. }
  193. return nil;
  194. }
  195. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  196. return 30.f;
  197. }
  198. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  199. return 0.f;
  200. }
  201. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  202. return 3; //全员禁言,群成员禁言,允许发言成员
  203. }
  204. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  205. if (indexPath.section == 0) {
  206. } else if(indexPath.section == 1) {
  207. if (indexPath.row == 0) {
  208. [self selectMemberToAdd:NO];
  209. } else {
  210. }
  211. } else if(indexPath.section == 2) {
  212. if (indexPath.row == 0) {
  213. [self selectMemberToAdd:YES];
  214. } else {
  215. }
  216. }
  217. }
  218. - (void)dealloc {
  219. [[NSNotificationCenter defaultCenter] removeObserver:self];
  220. }
  221. @end