ManagerTableViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // ManagerTableViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by heavyrain lee on 2019/6/26.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "ManagerTableViewController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import "WFCUContactListViewController.h"
  11. #import "WFCUImage.h"
  12. #import "UIView+Toast.h"
  13. @interface ManagerTableViewController () <UITableViewDelegate, UITableViewDataSource>
  14. @property(nonatomic, strong)UITableView *tableView;
  15. @property(nonatomic, strong)NSMutableArray<WFCCGroupMember *> *managerList;
  16. @end
  17. @implementation ManagerTableViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.title = WFCString(@"ManagerSetting");
  21. [self loadManagerList];
  22. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
  23. if (@available(iOS 15, *)) {
  24. self.tableView.sectionHeaderTopPadding = 0;
  25. }
  26. self.tableView.delegate = self;
  27. self.tableView.dataSource = self;
  28. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  29. [self.tableView reloadData];
  30. [self.view addSubview:self.tableView];
  31. __weak typeof(self)ws = self;
  32. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupMemberUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  33. if ([ws.groupInfo.target isEqualToString:note.object]) {
  34. [ws loadManagerList];
  35. [ws.tableView reloadData];
  36. }
  37. }];
  38. }
  39. - (void)loadManagerList {
  40. NSArray *memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupInfo.target forceUpdate:YES];
  41. self.managerList = [[NSMutableArray alloc] init];
  42. for (WFCCGroupMember *member in memberList) {
  43. if (member.type == Member_Type_Manager) {
  44. [self.managerList addObject:member];
  45. }
  46. }
  47. }
  48. - (void)selectMemberToAdd {
  49. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  50. pvc.selectContact = YES;
  51. pvc.multiSelect = YES;
  52. __weak typeof(self)ws = self;
  53. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  54. [[WFCCIMService sharedWFCIMService] setGroupManager:self.groupInfo.target isSet:YES memberIds:contacts notifyLines:@[@(0)] notifyContent:nil success:^{
  55. for (NSString *memberId in contacts) {
  56. WFCCGroupMember *member = [[WFCCIMService sharedWFCIMService] getGroupMember:ws.groupInfo.target memberId:memberId];
  57. if (member) {
  58. member.type = Member_Type_Manager;
  59. [ws.managerList addObject:member];
  60. }
  61. }
  62. if (contacts.count) {
  63. [ws.tableView reloadData];
  64. }
  65. } error:^(int error_code) {
  66. }];
  67. };
  68. NSMutableArray *candidateUsers = [[NSMutableArray alloc] init];
  69. NSArray *memberList = [[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupInfo.target forceUpdate:NO];
  70. for (WFCCGroupMember *member in memberList) {
  71. if ((member.type == Member_Type_Normal || member.type == Member_Type_Muted || member.type == Member_Type_Allowed) && ![member.memberId isEqualToString:self.groupInfo.owner]) {
  72. [candidateUsers addObject:member.memberId];
  73. }
  74. }
  75. if([candidateUsers count]) {
  76. pvc.candidateUsers = candidateUsers;
  77. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:pvc];
  78. [self.navigationController presentViewController:navi animated:YES completion:nil];
  79. } else {
  80. [self.view makeToast:@"成员已全部添加"];
  81. }
  82. }
  83. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  84. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  85. if (cell == nil) {
  86. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  87. }
  88. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  89. cell.accessoryView = nil;
  90. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  91. if (indexPath.section == 0) {
  92. WFCCUserInfo *owner = [[WFCCIMService sharedWFCIMService] getUserInfo:self.groupInfo.owner refresh:NO];
  93. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[owner.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
  94. cell.textLabel.text = owner.displayName;
  95. } else if(indexPath.section == 1) {
  96. if (indexPath.row == 0) {
  97. cell.imageView.image = [WFCUImage imageNamed:@"plus"];
  98. cell.textLabel.text = WFCString(@"AddManager");
  99. } else {
  100. WFCCUserInfo *manager = [[WFCCIMService sharedWFCIMService] getUserInfo:[self.managerList objectAtIndex:indexPath.row-1].memberId refresh:NO];
  101. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[manager.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"]];
  102. cell.textLabel.text = manager.displayName;
  103. }
  104. }
  105. return cell;
  106. }
  107. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  108. if (indexPath.section == 0 || (indexPath.section == 1 && indexPath.row == 0)) {
  109. return NO;
  110. }
  111. return YES;
  112. }
  113. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:WFCString(@"RemoveManager") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  115. __weak typeof(self)ws = self;
  116. [[WFCCIMService sharedWFCIMService] setGroupManager:self.groupInfo.target isSet:NO memberIds:@[[self.managerList objectAtIndex:indexPath.row-1].memberId] notifyLines:@[@(0)] notifyContent:nil success:^{
  117. for (WFCCGroupMember *member in ws.managerList) {
  118. if ([member.memberId isEqualToString:[ws.managerList objectAtIndex:indexPath.row-1].memberId]) {
  119. [ws.managerList removeObject:member];
  120. [ws.tableView reloadData];
  121. break;
  122. }
  123. }
  124. } error:^(int error_code) {
  125. }];
  126. }];
  127. UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Cancel") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  128. NSLog(@"点击了编辑");
  129. }];
  130. editAction.backgroundColor = [UIColor grayColor];
  131. return @[deleteAction, editAction];
  132. }
  133. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  134. if (section == 0) {
  135. return 1;
  136. } else if(section == 1) {
  137. return self.managerList.count+1;
  138. }
  139. return 0;
  140. }
  141. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  142. if (section == 0) {
  143. return WFCString(@"GroupOwner");
  144. } else if(section == 1) {
  145. return WFCString(@"Manager");
  146. }
  147. return nil;
  148. }
  149. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  150. return 30.f;
  151. }
  152. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  153. return 0.f;
  154. }
  155. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  156. return 2; //成员管理,加群设置
  157. }
  158. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  159. if (indexPath.section == 0) {
  160. } else if(indexPath.section == 1) {
  161. if (indexPath.row == 0) {
  162. [self selectMemberToAdd];
  163. } else {
  164. }
  165. }
  166. }
  167. - (void)dealloc {
  168. [[NSNotificationCenter defaultCenter] removeObserver:self];
  169. }
  170. @end