// // MyTDTopicGroupManageVC.m // smartRhino // // Created by niuzhen on 2019/12/18. // Copyright © 2019 tederen. All rights reserved. // #import "MyTDTopicGroupManageVC.h" #import "TopicGroupManageCell.h" #import "TopicGroupManageModel.h" #import "YCMenuView.h" #import "ShowNewGroupAlert.h" #import "MyTDTopicGroupUserVC.h" @interface MyTDTopicGroupManageVC () @property (weak, nonatomic) IBOutlet UIButton *AddBtn; @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UILabel *titleL; @property (weak, nonatomic) IBOutlet UIView *cusBarV; @property (weak, nonatomic) IBOutlet UIButton *menuBtn; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, strong) NSMutableArray *selectArray; @property (nonatomic, copy) NSArray *addListArray; @property (assign,nonatomic) OperationStateEnum operationStateEnum; @property (strong,nonatomic) UIView *operationBottomBgView; @property (weak, nonatomic) IBOutlet UIButton *operationAllSelectButton; @property (nonatomic, strong) UIButton *leftCloseBtn; @property (nonatomic, strong) UIButton *rightMoveBtn; @property (nonatomic, assign) BOOL iSViewEditor; @property (nonatomic, assign) BOOL isAllSelect; @end @implementation MyTDTopicGroupManageVC +(MyTDTopicGroupManageVC *)initMyTDTopicGroupManageVC; { MyTDTopicGroupManageVC *controller = [StoryboardManager.shared.myTDTopic instantiateViewControllerWithIdentifier:@"MyTDTopicGroupManageVC"]; return controller; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self getData]; } - (void)viewDidLoad { [super viewDidLoad]; self.addListArray = @[@"新建分组",@"批量编辑"]; self.tableView.delegate = self; self.tableView.dataSource = self; self.iSViewEditor = NO; self.fd_prefersNavigationBarHidden = YES; [self initBottomBgView]; [self changeToOperation:NO]; WS(weakSelf); [self.AddBtn setAction:^{ NSMutableArray *menuDataSourceArray = [weakSelf getMenuDataSource:weakSelf.addListArray]; 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 = -12; [view show]; }]; [self.operationAllSelectButton setAction:^{ for (TopicGroupManageModel *model in weakSelf.dataArray) { model.isSelect = !weakSelf.isAllSelect; } [weakSelf.tableView reloadData]; }]; [self.tableView setAllowsSelectionDuringEditing:YES]; } -(NSMutableArray *)getMenuDataSource:(NSArray *)titleArray{ NSMutableArray *menuDataSourceArray = [[NSMutableArray alloc] init]; for(int i=0;i= __IPHONE_11_0 - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){// delete action WS(weakSelf); TopicGroupManageModel * model = [self.dataArray 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)) { [[ShowNewGroupAlert initShowNewGroupAlertWithTitle:@"重命名" changeStr:model.Name confirm:^(NSString * _Nonnull groupName) { NSLog(@"%@",groupName); [weakSelf reChangeName:model name:groupName]; } cancle:^{ }] show]; }]; action2.backgroundColor = UIColorHex(#FF923A); UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1,action2]]; actions.performsFirstActionWithFullSwipe = NO; return actions; } #else - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row]; UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { 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); UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title: @"重命名" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [[ShowNewGroupAlert initShowNewGroupAlertWithTitle:@"重命名" changeStr:model.Name confirm:^(NSString * _Nonnull groupName) { NSLog(@"%@",groupName); [weakSelf reChangeName:model name:groupName]; } cancle:^{ }] show]; }]; action2.backgroundColor = UIColorHex(#FF923A); return @[action1, action2]; } #endif - (void)deleteFind:(NSArray *)array { WS(weakSelf); [[HttpManager sharedHttpManager] DeleteUrl:Host(APP_Topic_Topic_Delete_Group) parameters:@{@"Ids":array} responseStyle:DATA success:^(id _Nonnull responseObject) { [weakSelf getData]; } failure:^(NSError * _Nonnull error) { }]; } - (void)deleteFind:(NSArray *)array index:(NSArray *)indexArray { WS(weakSelf); [[HttpManager sharedHttpManager] DeleteUrl:Host(APP_Topic_Topic_Delete_Group) parameters:@{@"Ids":array} responseStyle:DATA success:^(id _Nonnull responseObject) { [weakSelf.dataArray removeObjectsInArray:indexArray]; [weakSelf.selectArray removeAllObjects]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf getData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)reChangeName:(TopicGroupManageModel *)model name:(NSString *)name { NSDictionary * paraDict = @{@"Id":@(model.Id), @"UserId":@([AppUserModel sharedAppUserModel].Id), @"Name":name }; WS(weakSelf); SHOWLOADING [[HttpManager sharedHttpManager] PUTUrl:Host(APP_Topic_Update_Group) parameters:paraDict success:^(id _Nonnull responseObject) { REMOVESHOW [weakSelf getData]; } failure:^(NSError * _Nonnull error) { REMOVESHOW }]; } -(UIView *)operationBottomBgView{ if(!_operationBottomBgView){ _operationBottomBgView = [[UIView alloc] init]; CGFloat bottomBgViewH = self.tabBarController.tabBar.height; _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.operationBackButton.hidden = NO; 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.AddBtn.hidden = NO; self.iSViewEditor = NO; self.menuBtn.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 setImage:[UIImage imageNamed:@"chatmsg_right_add_icon"] forState:UIControlStateNormal]; [self.AddBtn setAction:^{ NSMutableArray *menuDataSourceArray = [weakSelf getMenuDataSource:weakSelf.addListArray]; 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]; }]; }else{ [self.AddBtn setImage:[UIImage imageNamed:@"chatmsg_right_fileadd_icon"] forState:UIControlStateNormal]; [self.AddBtn setAction:^{ [ShowtipTool showErrorWithStatus:@"未做"]; }]; } } - (void)backAction:(id)sender{ if (self.iSViewEditor) { self.tableView.editing = NO; [self getData]; self.titleL.text = @"分组管理"; [self changeToOperation:NO]; }else{ [self.navigationController popViewControllerAnimated:true]; } } - (void)menuAction:(id)sender{ [super menuAction:sender]; } - (void)getData { [self.dataArray removeAllObjects]; WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Topic_Group) parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); NSArray * array = responseObject; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSDictionary * dict = (NSDictionary *)obj; TopicGroupManageModel * model = [TopicGroupManageModel modelWithDictionary:dict]; [weakSelf.dataArray addObject:model]; }]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)getDataNoRefresh { [self.dataArray removeAllObjects]; WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Topic_Group) parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); NSArray * array = responseObject; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSDictionary * dict = (NSDictionary *)obj; TopicGroupManageModel * model = [TopicGroupManageModel modelWithDictionary:dict]; [weakSelf.dataArray addObject:model]; }]; } failure:^(NSError * _Nonnull error) { }]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50.f; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row]; switch (self.operationStateEnum) { case OperationStateEnum0: { TopicGroupManageCell * cell = [TopicGroupManageCell configCell0:tableView indexPath:indexPath]; cell.nameL.text = model.Name; cell.countL.text = [NSString stringWithFormat:@"%ld",(long)model.IncludeCount]; return cell; } break; default: { TopicGroupManageCell * cell = [TopicGroupManageCell configCell1:tableView indexPath:indexPath]; cell.cell1NameL.text = model.Name; cell.cell1SelectBtn.selected = model.isSelect; self.isAllSelect = [self isCheckAllData]; if (self.isAllSelect) { [self.operationAllSelectButton setTitle:@"取消全选" forState:UIControlStateNormal]; }else{ [self.operationAllSelectButton setTitle:@"全选" forState:UIControlStateNormal]; } self.rightMoveBtn.enabled = [self isMoveCheckData]; return cell; } break; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row]; switch (self.operationStateEnum) { case OperationStateEnum1:{ NSLog(@"编辑状态的cell"); model.isSelect = !model.isSelect; [tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationAutomatic]; }break; default:{ NSLog(@"正常状态的cell"); MyTDTopicGroupUserVC * vc = [MyTDTopicGroupUserVC initMyTDTopicGroupUserVC]; vc.GroupId = model.Id; vc.titleStr = model.Name; [self.navigationController pushViewController:vc animated:YES]; }break; } } -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { TopicGroupManageModel * firstModel = [self.dataArray objectAtIndex:sourceIndexPath.row]; TopicGroupManageModel * secondModel = [self.dataArray objectAtIndex:destinationIndexPath.row]; [self.dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]; WS(weakSelf); [[HttpManager sharedHttpManager] PUTUrl:Host(API_APP_Topic_Sort) parameters:@{@"FirstId": @(firstModel.Id), @"SecondId": @(secondModel.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) { [weakSelf getDataNoRefresh]; } failure:^(NSError * _Nonnull error) { }]; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (self.iSViewEditor) { UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)]; view.backgroundColor = UIColorHex(#F0EFF4); UILabel * label = [UILabel new]; [view addSubview:label]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont fontWithName:@"PingFang SC" size: 13]; label.text = @"长按右侧三横,拖动可进行排序"; label.textColor = UIColorHex(#666666); [label mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(view); }]; return view; }else{ return nil; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (self.iSViewEditor) { return 35.f; }else{ return 0.01f; } } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { return NO;} - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath { return NO;} - (BOOL)isCheckAllData { for (TopicGroupManageModel * model in self.dataArray) { if (!model.isSelect) { return NO; } } return YES; } - (BOOL)isMoveCheckData { BOOL isSelect = NO; for (TopicGroupManageModel * model in self.dataArray) { if (model.isSelect) { isSelect = YES; } } if (!isSelect) { return NO; } return YES; } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (NSMutableArray *)selectArray { if (!_selectArray) { _selectArray = [NSMutableArray array]; } return _selectArray; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.RefreshListBlock) { self.RefreshListBlock(); } } @end