// // MyTDTopicGroupUserVC.m // smartRhino // // Created by niuzhen on 2019/12/18. // Copyright © 2019 tederen. All rights reserved. // #import "MyTDTopicGroupUserVC.h" #import "MyTDGroupView.h" #import "YCMenuView.h" #import "MailListCell.h" #import "MailListByZuCell.h" #import "ChineseString.h" #import "AddLookUpUserSecondVC.h" #import "MyTDTopicUserMoveVC.h" #import "MyTDTopicUserSearchVC.h" @interface MyTDTopicGroupUserVC () @property (weak, nonatomic) IBOutlet UIButton *addBtn; @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UILabel *titleL; @property (weak, nonatomic) IBOutlet UIButton *operationAllSelectButton; @property (weak, nonatomic) IBOutlet UIButton *menuBtn; @property (assign,nonatomic) OperationStateEnum operationStateEnum; @property (strong,nonatomic) UIView *operationBottomBgView; @property (nonatomic, strong) UIButton *leftCloseBtn; @property (nonatomic, strong) UIButton *rightMoveBtn; @property (nonatomic, assign) BOOL iSViewEditor; @property (nonatomic, assign) BOOL isNeedSelelct; @property (strong,nonatomic) NSMutableArray *taskActionArray; @property (strong,nonatomic) NSMutableArray *pinyinArray; @property (strong,nonatomic) NSMutableArray *nameArray; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, strong) NSMutableArray *dataSortArray; @property (nonatomic, strong) NSMutableArray *selectArray; @property (nonatomic, strong) NSMutableArray *selectIdArray; @property (nonatomic, strong) NSMutableArray *listArray; @end @implementation MyTDTopicGroupUserVC +(MyTDTopicGroupUserVC *)initMyTDTopicGroupUserVC{ MyTDTopicGroupUserVC *controller = [StoryboardManager.shared.myTDTopic instantiateViewControllerWithIdentifier:@"MyTDTopicGroupUserVC"]; return controller; } - (void)viewDidLoad { [super viewDidLoad]; self.titleL.text = self.titleStr; [self getData]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recviveChaoSongRen:) name:NOTIFI_ChaoSongRen object:nil]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = RGB(255, 255, 255); if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor clearColor]; self.iSViewEditor = NO; [self initBottomBgView]; [self changeToOperation:NO]; WS(weakSelf); [self.operationAllSelectButton setAction:^{ for (NSArray * array in weakSelf.dataSortArray) { for (SelectModel * model in array) { model.hadSelected = weakSelf.isNeedSelelct; } } [weakSelf.tableView reloadData]; }]; self.isNeedSelelct = YES; [self.addBtn setAction:^{ NSMutableArray *menuDataSourceArray = [weakSelf getMenuDataSource:weakSelf.taskActionArray]; YCMenuView *view = [YCMenuView menuWithActions:menuDataSourceArray width:106 relyonView:weakSelf.addBtn]; view.menuColor = RGB(255, 255, 255); view.separatorColor = RGB(234, 234, 234); view.textColor = RGB(102, 102, 102); view.textFont = [UIFont systemFontOfSize:16.0]; view.menuCellHeight = 43.5; view.maxDisplayCount = 10; view.offset = -6; [view show]; }]; } -(void)initBottomBgView{ self.leftCloseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.leftCloseBtn setTitle:@"删除" forState:UIControlStateNormal]; [self.leftCloseBtn setTitleColor:RGB(255, 82, 82) forState:UIControlStateNormal]; self.leftCloseBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [self.operationBottomBgView addSubview:self.leftCloseBtn]; self.leftCloseBtn.frame = CGRectMake(0, 10, SCREEN_WIDTH/2, self.operationBottomBgView.height - 10); self.self.rightMoveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.rightMoveBtn setTitle:@"移动" forState:UIControlStateNormal]; [self.rightMoveBtn setTitleColor:RGB(57, 121, 211) forState:UIControlStateNormal]; [self.rightMoveBtn setTitleColor:UIColorHex(#BBBBBB) forState:UIControlStateDisabled]; self.rightMoveBtn.enabled = NO; self.rightMoveBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [self.operationBottomBgView addSubview:self.rightMoveBtn]; self.rightMoveBtn.frame = CGRectMake(SCREEN_WIDTH/2, 10, SCREEN_WIDTH/2, self.operationBottomBgView.height - 10); UIView *line = [[UIView alloc] init]; line.backgroundColor = RGB(240, 239, 244); [self.operationBottomBgView addSubview:line]; line.frame = CGRectMake(SCREEN_WIDTH/2, 0, .5, self.operationBottomBgView.height); UIView *topLine = [[UIView alloc] init]; topLine.backgroundColor = RGB(240, 239, 244); [self.operationBottomBgView addSubview:topLine]; topLine.frame = CGRectMake(0, 0, SCREEN_WIDTH, 10); WS(weakSelf); [self.leftCloseBtn setAction:^{ NSMutableArray *selectIDArray = [NSMutableArray array]; for (NSArray * array in weakSelf.dataSortArray) { for (SelectModel * model in array) { if (model.hadSelected) { [selectIDArray addObject:@(model.Id)]; } } } if (selectIDArray.count == 0) { SHOWERROR(@"请选择要删除的通知"); return ; }else{ [weakSelf deleteFind:selectIDArray]; } }]; [self.rightMoveBtn setAction:^{ NSMutableArray *selectIDArray = [NSMutableArray array]; for (NSArray * array in weakSelf.dataSortArray) { for (SelectModel * model in array) { if (model.hadSelected) { [selectIDArray addObject:@(model.Id)]; } } } if (selectIDArray.count == 0) { SHOWERROR(@"请选择要移动的通知或者文件夹"); return ; } [weakSelf launchMoveVC:selectIDArray]; }]; [[UtilsTools getWindow] addSubview:self.operationBottomBgView]; } #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); SelectModel * model = [[self.dataSortArray objectAtIndex:indexPath.section-1] objectAtIndex:indexPath.row]; 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.Id)]]; }]; 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); UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"移动" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) { [weakSelf launchMoveVC:@[@(model.Id)]]; }]; action2.backgroundColor = RGB(61, 156, 248); UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1,action2]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } #else - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); SelectModel * model = [[self.dataSortArray objectAtIndex:indexPath.section-1] objectAtIndex:indexPath.row]; UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakSelf deleteFind:@[@(model.Id)]]; }]; action1.backgroundColor = UIColorHex(#F64A33); UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"移动" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { SelectModel * model = [[weakSelf.dataSortArray objectAtIndex:indexPath.section-1] objectAtIndex:indexPath.row]; [weakSelf launchMoveVC:@[@(model.Id)]]; }]; action2.backgroundColor = RGB(61, 156, 248); return @[action1,action2]; } #endif - (void)deleteFind:(NSArray *)array { WS(weakSelf); [[HttpManager sharedHttpManager] DeleteUrl:Host(APP_Topic_Group_User) parameters:@{@"Ids":array} responseStyle:DATA success:^(id _Nonnull responseObject) { [weakSelf getData]; } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]) }]; } - (void)launchMoveVC:(NSArray *)selectIDArray{ MyTDTopicUserMoveVC * vc = [MyTDTopicUserMoveVC initMyTDTopicUserMoveVC]; vc.selectIdArray = selectIDArray; [self.navigationController pushViewController:vc animated:YES]; [self changeToOperation:NO]; } -(UIView *)operationBottomBgView{ if(!_operationBottomBgView){ _operationBottomBgView = [[UIView alloc] init]; CGFloat bottomBgViewH = self.tabBarController.tabBar.height + 10; _operationBottomBgView.frame = CGRectMake(0, SCREEN_HEIGHT - bottomBgViewH, SCREEN_WIDTH, bottomBgViewH); _operationBottomBgView.backgroundColor = RGB(255, 255, 255); _operationBottomBgView.hidden = YES; } return _operationBottomBgView; } #pragma mark 批量操作与正常操作界面切换 -(void)changeToOperation:(BOOL)operation{ if(operation){ self.iSViewEditor = YES; //默认操作状态为批量编辑状态 self.addBtn.hidden = YES; self.menuBtn.hidden = YES; self.operationStateEnum = OperationStateEnum1; self.operationAllSelectButton.hidden = NO; self.tabBarController.tabBar.hidden = YES; self.operationBottomBgView.hidden = NO; self.view.height -= self.tabBarController.tabBar.height; self.tableView.height -= self.tabBarController.tabBar.height; [self.view layoutIfNeeded]; [self.tableView layoutIfNeeded]; }else{ self.iSViewEditor = NO; self.menuBtn.hidden = NO; self.addBtn.hidden = NO; //默认操作状态为无状态 self.operationStateEnum = OperationStateEnum0; self.operationAllSelectButton.hidden = YES; self.tabBarController.tabBar.hidden = YES; self.tableView.height += self.tabBarController.tabBar.height; self.view.height += self.tabBarController.tabBar.height; [self.view layoutIfNeeded]; [self.tableView layoutIfNeeded]; self.operationBottomBgView.hidden = YES; } [self.tableView reloadData]; } #pragma mark 移动 isEdit:当前是否为编辑状态 -(void)userDidMove:(BOOL)move isEdit:(BOOL)isEdit{ //移动 if(move){ //默认操作状态为移动状态 self.operationStateEnum = OperationStateEnum2; self.addBtn.hidden = NO; [self resetRightBtnAction:NO]; //隐藏tabBar - 并将TableView至底端 self.tabBarController.tabBar.hidden = YES; self.view.height += self.tabBarController.tabBar.height; self.tableView.height += self.tabBarController.tabBar.height; [self.view layoutIfNeeded]; [self.tableView layoutIfNeeded]; //隐藏operationBottomBgView self.operationBottomBgView.hidden = YES; } else{ //隐藏tabBar - 并将TableView至底端有tabBar.height的高 self.tabBarController.tabBar.hidden = YES; self.view.height -= self.tabBarController.tabBar.height; self.tableView.height -= self.tabBarController.tabBar.height; [self.view layoutIfNeeded]; [self.tableView layoutIfNeeded]; //显示operationBottomBgView self.operationBottomBgView.hidden = NO; if(isEdit){ self.operationStateEnum = OperationStateEnum1; self.addBtn.hidden = YES; [self resetRightBtnAction:YES]; }else{ [self changeToOperation:NO]; } } [self.tableView reloadData]; } -(void)resetRightBtnAction:(BOOL)isShowMenu{ WS(weakSelf); if(isShowMenu){ [self.addBtn setAction:^{ NSMutableArray *menuDataSourceArray = [weakSelf getMenuDataSource:weakSelf.taskActionArray]; YCMenuView *view = [YCMenuView menuWithActions:menuDataSourceArray width:106 relyonView:weakSelf.addBtn]; view.menuColor = RGB(255, 255, 255); view.separatorColor = RGB(234, 234, 234); view.textColor = RGB(102, 102, 102); view.textFont = [UIFont systemFontOfSize:16.0]; view.menuCellHeight = 43.5; view.maxDisplayCount = 10; view.offset = -6; [view show]; }]; } } - (void)backAction:(id)sender{ if (self.iSViewEditor) { [self changeToOperation:NO]; }else{ [self.navigationController popViewControllerAnimated:true]; } } - (void)menuAction:(id)sender{ [super menuAction:sender]; } -(NSMutableArray *)getMenuDataSource:(NSMutableArray *)titleArray{ NSMutableArray *menuDataSourceArray = [[NSMutableArray alloc] init]; for(int i=0;i 0) { NSArray * array = self.dataSortArray[section - 1]; return array.count; }else{ return 0; } }break; } } //section右侧index数组 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return self.pinyinArray; } //点击右侧索引表项时调用 索引与section的对应关系 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return index; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *headerView = [[UIView alloc]init]; if(section > 0){ [headerView setBackgroundColor:RGBA(238, 238, 238, 1)]; NSString *returnStr = [self.pinyinArray objectAtIndex:section - 1]; UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 30)]; [headerView addSubview:headerLabel]; headerLabel.font = [UIFont systemFontOfSize:14.f]; headerLabel.text = returnStr; headerLabel.textColor = RGB(153, 153, 153); }else{ [headerView setBackgroundColor:[UIColor whiteColor]]; [headerView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)]; MyTDGroupView * view = [[MyTDGroupView alloc] init]; [headerView addSubview:view]; WS(weakSelf); UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) { MyTDTopicUserSearchVC * vc = [[MyTDTopicUserSearchVC alloc] init]; vc.listArray = [NSMutableArray arrayWithArray:weakSelf.listArray]; vc.upDateBlock = ^{ [weakSelf getData]; }; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; [headerView addGestureRecognizer:tap]; } return headerView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if(section > 0){ return 30; } return 50.f; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01f; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 65.f; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ SelectModel * model = [[self.dataSortArray objectAtIndex:indexPath.section-1] objectAtIndex:indexPath.row]; switch (self.operationStateEnum) { case OperationStateEnum0: { MailListByZuCell *cell = [MailListByZuCell configCell0:tableView indexPath:indexPath]; cell.cell0TitleLabel.text = model.UserName; [cell.cell0ImgView sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; return cell; } break; default: { MailListByZuCell *cell = [MailListByZuCell configCell1:tableView indexPath:indexPath]; cell.cell0TitleLabel.text = model.UserName; [cell.cell0ImgView sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; cell.selectBtn.selected = model.hadSelected; self.rightMoveBtn.enabled = [self isMoveCheckData]; if ([self isAllSelect]) { self.isNeedSelelct = NO; [self.operationAllSelectButton setTitle:@"取消全选" forState:UIControlStateNormal]; }else{ self.isNeedSelelct = YES; [self.operationAllSelectButton setTitle:@"全选" forState:UIControlStateNormal]; } return cell; } break; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { NSLog(@"safdasfasdf"); SelectModel * model = [[self.dataSortArray objectAtIndex:indexPath.section-1] objectAtIndex:indexPath.row]; switch (self.operationStateEnum) { case OperationStateEnum1:{ model.hadSelected = !model.hadSelected; [tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone]; }break; default:{ }break; } } - (BOOL)isAllSelect { for (NSArray * array in self.dataSortArray) { for (SelectModel * model in array) { if (!model.hadSelected) { return NO; } } } return YES; } - (BOOL)isMoveCheckData { BOOL isSelect = NO; for (NSArray * array in self.dataSortArray) { for (SelectModel * model in array) { if (model.hadSelected) { isSelect = YES; } } } if (!isSelect) { return NO; } return YES; } - (void)sortData { [self.dataSortArray removeAllObjects]; self.pinyinArray = [[NSMutableArray alloc] init]; self.nameArray = [[NSMutableArray alloc] init]; NSMutableArray *stringsToSort = [[NSMutableArray alloc] init]; for(int i = 0;i < [self.dataArray count];i++){ SelectModel *model = [self.dataArray objectAtIndex:i]; NSString * str = [NSString stringWithFormat:@"%@UserId%ld",model.UserName,(long)model.UserId]; [stringsToSort addObject:str]; } self.pinyinArray = [ChineseString IndexArray:stringsToSort]; self.nameArray = [ChineseString LetterSortArray:stringsToSort]; for (NSArray * array in self.nameArray) { NSMutableArray * subAddArray = [NSMutableArray array]; for (NSString * string in array) { NSInteger flag = 0; BOOL isAdd = NO; for (NSInteger i = 0; i < self.dataArray.count; i ++) { SelectModel * model = self.dataArray[i]; if ([string containsString:[NSString stringWithFormat:@"UserId%ld",(long)model.UserId]]) { [subAddArray addObject:model]; isAdd = YES; flag = i; } } if (isAdd) { [self.dataArray removeObjectAtIndex:flag]; } } if (subAddArray.count > 0) { [self.dataSortArray addObject:subAddArray]; } } WS(weakSelf); dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } - (void)getData{ [self.listArray removeAllObjects]; [self.dataArray removeAllObjects]; WS(weakSelf); SHOWLOADING NSString * url = [NSString stringWithFormat:@"%@%ld",Host(APP_Topic_User_Get),(long)self.GroupId]; [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); REMOVESHOW for (NSDictionary * dict in responseObject[@"Items"]) { SelectModel * model = [SelectModel modelWithDictionary:dict]; model.UserName = model.Name; [weakSelf.dataArray addObject:model]; [weakSelf.listArray addObject:model]; } [weakSelf sortData]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } #pragma mark - Notification - (void)recviveChaoSongRen:(NSNotification *)notification{ NSMutableArray * array = notification.userInfo[@"selectPeople"]; NSMutableArray * array0 = self.selectArray; NSMutableArray * array1 = notification.userInfo[@"selectPeople"]; if (array0.count == 0) { [self.selectArray addObjectsFromArray:array]; }else{ for (NSInteger x = 0; x < array.count; x++) { for (NSInteger y = 0 ; y < array0.count; y++) { if (array[x].UserId == array0[y].UserId) { [array1 removeObjectAtIndex:x]; } } } [self.selectArray addObjectsFromArray:array1]; } if (self.selectArray.count > 0) { for (NSInteger i = 0; i < self.selectArray.count; i ++) { SelectModel * model = self.selectArray[i]; [self.selectIdArray addObject:@(model.UserId)]; } [self addUser:self.selectIdArray]; } } - (void)addUser:(NSArray *)array { NSLog(@"=============%@",array); NSDictionary * paraDict = @{@"UserIds":array, @"TopicGroupId":@(self.GroupId) }; WS(weakSelf); SHOWLOADING [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Topic_Add_Group_User) parameters:paraDict responseStyle:DATA success:^(id _Nonnull responseObject) { REMOVESHOW NSLog(@"添加人员成功"); [weakSelf getData]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } - (NSMutableArray *)selectArray { if (!_selectArray) { _selectArray = [NSMutableArray array]; } return _selectArray; } - (NSMutableArray *)selectIdArray { if (!_selectIdArray) { _selectIdArray = [NSMutableArray array]; } return _selectIdArray; } - (NSMutableArray *)nameArray{ if(!_nameArray) { _nameArray = [NSMutableArray array]; } return _nameArray; } - (NSMutableArray *)dataArray{ if(!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (NSMutableArray *)listArray{ if(!_listArray) { _listArray = [NSMutableArray array]; } return _listArray; } - (NSMutableArray *)dataSortArray{ if(!_dataSortArray) { _dataSortArray = [NSMutableArray array]; } return _dataSortArray; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self changeToOperation:NO]; } @end