// // TDGroupInfoDetailVC.m // smartRhino // // Created by niuzhen on 2019/12/13. // Copyright © 2019 tederen. All rights reserved. // #import "TDGroupInfoDetailVC.h" #import "TDGroupInfoDetailCell.h" #import "createSearchBarView.h" #import "TDGroupInfoListModel.h" #import "TDGroupQrcodeVC.h" #import "TDGroupInfoChangeVc.h" #import "ChatGroupAddVC.h" #import "MailListDetailVC.h" @interface TDGroupInfoDetailVC () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UILabel *titleL; @property (strong, nonatomic)NSMutableArray * dataArray; @property (strong, nonatomic)TDGroupInfoListModel * listModel; @end @implementation TDGroupInfoDetailVC +(TDGroupInfoDetailVC *)initTDGroupInfoDetailVC{ TDGroupInfoDetailVC *controller = [StoryboardManager.shared.TDGroup instantiateViewControllerWithIdentifier:@"TDGroupInfoDetailVC"]; return controller; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self getData]; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.tableView.delegate = self; self.tableView.dataSource = self; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMessage) name:GROUPADDMESSAGE object:nil]; } - (void)getData { [self.dataArray removeAllObjects]; WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Detail) parameters:@{@"GroupId":@(self.GroupId)} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); weakSelf.listModel = [TDGroupInfoListModel modelWithDictionary:responseObject]; [weakSelf.dataArray addObjectsFromArray: weakSelf.listModel.GroupUserListResult]; if (weakSelf.upDateTitleBolck) { weakSelf.upDateTitleBolck(weakSelf.listModel.Name); } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } #pragma mark - UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { switch (self.listModel.GroupUserRoleId) { case 1: return 6; break; case 2: return 4; break; default: return 3; break; } }else{ return self.dataArray.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); if (indexPath.section == 0) { switch (self.listModel.GroupUserRoleId) { case 1: { switch (indexPath.row) { case 0: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell0:tableView indexPath:indexPath]; [cell.cell0ImgV sd_setImageWithURL:[NSURL URLWithString:self.listModel.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; cell.cell0TitleL.text = self.listModel.Name; return cell; } break; case 1: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell1:tableView indexPath:indexPath]; cell.cell1TitleL.text = @"小组二维码"; return cell; } break; case 2: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell2:tableView indexPath:indexPath]; cell.cell2TitleL.text = @"加入小组需要管理员同意"; [cell.cell2Switch setBlockForControlEvents:UIControlEventValueChanged block:^(id _Nonnull sender) { [weakSelf NeedAgree]; }]; [cell.cell2Switch setOn:self.listModel.IsAdmin]; return cell; } break; case 3: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell2:tableView indexPath:indexPath]; cell.cell2TitleL.text = @"只允许管理员发话题(禁言)"; [cell.cell2Switch setBlockForControlEvents:UIControlEventValueChanged block:^(id _Nonnull sender) { [weakSelf AdminTalk]; }]; [cell.cell2Switch setOn:self.listModel.IsWords]; return cell; } break; case 4: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell2:tableView indexPath:indexPath]; cell.cell2TitleL.text = @"消息免打扰"; [cell.cell2Switch setBlockForControlEvents:UIControlEventValueChanged block:^(id _Nonnull sender) { [weakSelf ChatEnabelChange]; }]; [cell.cell2Switch setOn:self.listModel.IsUDisturb]; return cell; } break; default: { ChatGroupAddVC *vc = [[ChatGroupAddVC alloc] init]; TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell3:tableView indexPath:indexPath]; cell.cell3TitleL.text = @"添加成员"; [cell.cell3AddBtn setAction:^{ Department *defaltGroupSetion = [[Department alloc]init]; defaltGroupSetion.Name = @"单位通讯录"; defaltGroupSetion.Id = 0; Department *myGroupSetion = [[Department alloc]init]; myGroupSetion.Name = @"自建通讯录"; defaltGroupSetion.Id = 0; vc.departSourceDataArray = @[defaltGroupSetion,myGroupSetion].mutableCopy; vc.isDefalutPage = 1; vc.type = AddGroupUser; vc.Id = weakSelf.listModel.Id; NSMutableArray * array = [NSMutableArray array]; for (TDGroupInfoModel * model in self.dataArray) { SelectModel * smodel = [[SelectModel alloc] init]; smodel.UserId = model.UserId; smodel.UserName = model.Name; [array addObject:smodel]; } vc.grouperArray = array; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; return cell; } break; } } break; case 2: { switch (indexPath.row) { case 0: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell0:tableView indexPath:indexPath]; [cell.cell0ImgV sd_setImageWithURL:[NSURL URLWithString:self.listModel.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; cell.cell0TitleL.text = self.listModel.Name; return cell; } break; case 1: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell1:tableView indexPath:indexPath]; cell.cell1TitleL.text = @"小组二维码"; return cell; } break; case 2: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell2:tableView indexPath:indexPath]; cell.cell2TitleL.text = @"消息免打扰"; [cell.cell2Switch setBlockForControlEvents:UIControlEventValueChanged block:^(id _Nonnull sender) { [weakSelf ChatEnabelChange]; }]; [cell.cell2Switch setOn:self.listModel.IsUDisturb]; return cell; } break; default: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell3:tableView indexPath:indexPath]; cell.cell3TitleL.text = @"添加成员"; [cell.cell3AddBtn setAction:^{ Department *defaltGroupSetion = [[Department alloc]init]; defaltGroupSetion.Name = @"单位通讯录"; defaltGroupSetion.Id = 0; Department *myGroupSetion = [[Department alloc]init]; myGroupSetion.Name = @"自建通讯录"; defaltGroupSetion.Id = 0; ChatGroupAddVC *vc = [[ChatGroupAddVC alloc] init]; vc.departSourceDataArray = @[defaltGroupSetion,myGroupSetion].mutableCopy; vc.isDefalutPage = 1; vc.type = AddGroupUser; vc.Id = weakSelf.listModel.Id; NSMutableArray * array = [NSMutableArray array]; for (TDGroupInfoModel * model in self.dataArray) { SelectModel * smodel = [[SelectModel alloc] init]; smodel.UserId = model.UserId; smodel.UserName = model.Name; [array addObject:smodel]; } vc.grouperArray = array; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; return cell; } break; } } break; default: { switch (indexPath.row) { case 0: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell0:tableView indexPath:indexPath]; [cell.cell0ImgV sd_setImageWithURL:[NSURL URLWithString:self.listModel.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; cell.cell0TitleL.text = self.listModel.Name; return cell; } break; case 1: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell1:tableView indexPath:indexPath]; cell.cell1TitleL.text = @"小组二维码"; return cell; } break; default: { TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell2:tableView indexPath:indexPath]; cell.cell2TitleL.text = @"消息免打扰"; [cell.cell2Switch setBlockForControlEvents:UIControlEventValueChanged block:^(id _Nonnull sender) { [weakSelf ChatEnabelChange]; }]; [cell.cell2Switch setOn:self.listModel.IsUDisturb]; return cell; } break; } } break; } }else{ TDGroupInfoModel * model = [self.dataArray objectAtIndex:indexPath.row]; TDGroupInfoDetailCell *cell = [TDGroupInfoDetailCell configCell4:tableView indexPath:indexPath]; [cell.cell4imgV sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; cell.cell4NameL.text = model.Name; switch (model.GroupUserRoleId) { case 1: { cell.cell4LevelL.hidden = NO; cell.cell4LevelL.text = @"创建者"; cell.cell4LevelL.backgroundColor = UIColorHex(#3979D3); } break; case 2: { cell.cell4LevelL.hidden = NO; cell.cell4LevelL.text = @"管理员"; cell.cell4LevelL.backgroundColor = UIColorHex(#FF9503); } break; default: { cell.cell4LevelL.hidden = YES; } break; } cell.cell4subL.text = model.DepartmentName; cell.cell4TimeL.text = @"2019-11-14"; return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65.f; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 50.f; } return 35.f; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { return [[createSearchBarView alloc] init]; }else{ UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)]; view.backgroundColor = UIColorHex(#F0EFF4); UILabel * label = [UILabel new]; NSString * str = [NSString stringWithFormat:@"成员:%ld人,话题:%ld",(long)self.listModel.GroupUserNumber,(long)self.listModel.GroupTopicNumber]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 13],NSForegroundColorAttributeName : UIColorHex(#666666)}]; label.attributedText = string; [view addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_offset(@15); make.centerY.mas_equalTo(view); }]; return view; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { switch (indexPath.row) { case 0: { if (self.listModel.GroupUserRoleId == 1) { TDGroupInfoChangeVc * vc = [TDGroupInfoChangeVc initTDGroupInfoChangeVc]; vc.model = self.listModel; [self.navigationController pushViewController:vc animated:YES]; } } break; case 1: { TDGroupQrcodeVC * vc= [TDGroupQrcodeVC initTDGroupQrcodeVC]; vc.isCreate = NO; vc.model = self.listModel; [self.navigationController pushViewController:vc animated:YES]; } break; default: break; } }else{ TDGroupInfoModel * model = [self.dataArray objectAtIndex:indexPath.row]; MailListDetailVC *vc = [MailListDetailVC initMailListDetailVC]; vc.indexId = model.UserId; [self.navigationController pushViewController:vc animated:YES]; } } #pragma mark - UISwitch - event - (void)NeedAgree { WS(weakSelf); NSString * url = [NSString stringWithFormat:@"%@%ld",Host(APP_Group_Set_Admin),(long)self.listModel.Id]; [[HttpManager sharedHttpManager] PUTUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) { dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)AdminTalk { WS(weakSelf); NSString * url = [NSString stringWithFormat:@"%@%ld",Host(APP_Group_Set_Words),(long)self.listModel.Id]; [[HttpManager sharedHttpManager] PUTUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) { dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)ChatEnabelChange { WS(weakSelf); NSString * url = [NSString stringWithFormat:@"%@%ld",Host(APP_Group_Set_Disturb),(long)self.listModel.Id]; [[HttpManager sharedHttpManager] PUTUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) { dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } #pragma Mark 左滑按钮 iOS8以上 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0 - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){// delete action WS(weakSelf); if (indexPath.section > 0) { TDGroupInfoModel *model = [self.dataArray objectAtIndex:indexPath.row]; switch (self.listModel.GroupUserRoleId) { case 1:{ if (model.GroupUserRoleId != 1) { UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [weakSelf deleteFind:model]; }]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:sureAction]; [alert addAction:cancelAction]; [weakSelf presentViewController:alert animated:YES completion:^{ }]; }]; action1.backgroundColor = UIColorHex(#F64A33); NSString * isAdmin = model.GroupUserRoleId == 2 ? @"取消管理员" : @"设为管理员"; UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:isAdmin handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) { [weakSelf setRoleLevel:model]; }]; action2.backgroundColor = UIColorHex(#FF9503); UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1,action2]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } }break; case 2:{ if (model.GroupUserRoleId == 3) { UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [weakSelf deleteFind:@[model].mutableCopy]; }]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:sureAction]; [alert addAction:cancelAction]; [weakSelf presentViewController:alert animated:YES completion:^{ }]; }]; action1.backgroundColor = UIColorHex(#F64A33); UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } }break; default:{ UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } } } UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } #else - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { WEAKSELF if (indexPath.section > 0) { TDGroupInfoModel *model = [self.dataArray objectAtIndex:indexPath.row]; switch (self.listModel.GroupUserRoleId) { case 1:{ if (model.GroupUserRoleId != 1) { UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakSelf deleteFind:model]; }]; action1.backgroundColor = UIColorHex(#FF3B2F); NSString * isAdmin = model.GroupUserRoleId == 2 ? @"取消管理员" : @"设为管理员"; UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:isAdmin handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakSelf setRoleLevel:model]; }]; action2.backgroundColor = UIColorHex(#FF9503); return @[action1, action2]; }else{ return @[]; } }break; case 2:{ if (model.GroupUserRoleId == 3) { UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakSelf deleteFind:@[model].mutableCopy]; }]; action1.backgroundColor = UIColorHex(#FF3B2F); return @[action1]; }else{ return @[]; } }break; default:{ return @[]; } } }else{ return @[]; } } #endif - (void)deleteFind:(TDGroupInfoModel *)model { WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_DELETE_User) parameters:@{@"GroupUserIds":@[@(model.UserId)],@"GroupId":@(self.listModel.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) { SHOWSUCCESS(@"删除成功") dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)setRoleLevel:(TDGroupInfoModel *)model { NSInteger level = model.GroupUserRoleId == 2 ? 3 : 2; WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Update_User) parameters:@{@"RoleId":@(level),@"Id":@(model.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) { SHOWSUCCESS(@"设置成功") dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } /// 退出小组 - (IBAction)qiutGroupAction:(id)sender { SHOWLOADING WEAKSELF NSString * url = [NSString stringWithFormat:@"%@%ld",Host(SaoYiSao3_Post),(long)self.GroupId]; [[HttpManager sharedHttpManager] DeleteUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) { REMOVESHOW for ( int i = 0 ;i< weakSelf.navigationController.viewControllers.count;i++) { if ([weakSelf.navigationController.viewControllers[i] isKindOfClass:NSClassFromString(@"MyTDGroupViewController")]) { [weakSelf.navigationController popToViewController:weakSelf.navigationController.viewControllers[i] animated:YES]; return; } } } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]); }]; } - (IBAction)backAction:(id)sender { [super backAction:sender]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore]; [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray * __nonnull records) { for (WKWebsiteDataRecord *record in records) { [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes forDataRecords:@[record] completionHandler:^{ NSLog(@"Cookies for %@ deleted successfully",record.displayName); }]; } }]; } - (void)showMessage { UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"需等待对方验证同意加入本小组" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } @end