// // GroupSquareVC.m // smartRhino // // Created by niuzhen on 2020/5/6. // Copyright © 2020 tederen. All rights reserved. // #import "GroupSquareVC.h" #import "GroupSquareModel.h" #import "GroupSquareSubModel.h" #import "GroupSquareCell.h" #import "MyTDGroupView.h" #import "GroupSynopsisVC.h" #import "GroupSquareSearchVC.h" #import "TDGroupInfoListVC.h" #import "GroupVerifyVC.h" @interface GroupSquareVC () @property (weak, nonatomic) IBOutlet UITableView *leftTable; @property (weak, nonatomic) IBOutlet UITableView *rightTable; @property (weak, nonatomic) IBOutlet UIView *HeadView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *height; @property (strong, nonatomic) MyTDGroupView *SearchView; @property (copy, nonatomic) NSMutableArray *leftArray; @property (copy, nonatomic) NSMutableArray *rightArray; @property (assign, nonatomic) NSInteger leftId; @end @implementation GroupSquareVC +(GroupSquareVC *)initGroupSquareVC{ GroupSquareVC *controller = [StoryboardManager.shared.Source instantiateViewControllerWithIdentifier:@"GroupSquareVC"]; return controller; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; [self.HeadView addSubview:self.SearchView]; [self.SearchView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(6); make.left.right.mas_equalTo(self.HeadView); make.height.mas_offset(36); }]; WS(weakSelf); [self.SearchView.button setAction:^{ GroupSquareSearchVC * vc = [[GroupSquareSearchVC alloc] init]; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; self.leftTable.delegate = self; self.leftTable.dataSource = self; self.leftTable.showsHorizontalScrollIndicator = NO; self.leftTable.showsVerticalScrollIndicator = NO; self.rightTable.delegate = self; self.rightTable.dataSource = self; self.leftTable.estimatedRowHeight = 0; self.leftTable.estimatedSectionHeaderHeight = 0; self.leftTable.estimatedSectionFooterHeight = 0; self.rightTable.estimatedRowHeight = 0; self.rightTable.estimatedSectionHeaderHeight = 0; self.rightTable.estimatedSectionFooterHeight = 0; [self getData]; } - (void)getData { WS(weakSelf); [self.leftArray removeAllObjects]; NSString * url = [NSString stringWithFormat:@"%@%@",Host(API_APP_GROUP_Group_Category),@"0"]; [[HttpManager sharedHttpManager] GET:url parameters:@{} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@",responseObject); if ([responseObject isKindOfClass:[NSArray class]]) { NSArray * array = responseObject; for (NSInteger i = 0; i < array.count; i ++) { GroupSquareModel * model = [GroupSquareModel modelWithDictionary:array[i]]; model.isSelect = i == 0 ? YES : NO; [weakSelf.leftArray addObject:model]; } GroupSquareModel * fmodel = weakSelf.leftArray.firstObject; weakSelf.leftId = fmodel.Id; [weakSelf getSubData:fmodel.Id]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } - (void)getSubData:(NSInteger)listID { WS(weakSelf); [self.rightArray removeAllObjects]; NSString * url = [NSString stringWithFormat:@"%@%ld",Host(API_APP_GROUP_Category_list),listID]; [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) { if ([responseObject isKindOfClass:[NSArray class]]) { for (NSDictionary * dict in responseObject) { GroupSquareSubModel * model = [GroupSquareSubModel modelWithDictionary:dict]; [weakSelf.rightArray addObject:model]; } } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (void)reloadData { [self.leftTable reloadData]; [self.rightTable reloadData]; } #pragma mark -UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.leftTable) { return self.leftArray.count; }else{ return self.rightArray.count; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.leftTable) { return [GroupSquareCell configCell0Height]; }else{ return [GroupSquareCell configCell1Height]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.leftTable) { GroupSquareModel * model = [self.leftArray objectAtIndex:indexPath.row]; GroupSquareCell * cell = [GroupSquareCell configCell0:tableView indexPath:indexPath]; [cell setLeftDataModel:model]; return cell; }else{ GroupSquareSubModel * model = [self.rightArray objectAtIndex:indexPath.row]; GroupSquareCell * cell = [GroupSquareCell configCell1:tableView indexPath:indexPath]; [cell setRightDataSubModel:model]; WS(weakSelf); [cell.JoinBtn setAction:^{ [weakSelf JoinGroupIndex:indexPath withModel:model]; }]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (tableView == self.leftTable) { GroupSquareModel * model = [self.leftArray objectAtIndex:indexPath.row]; if (!model.isSelect) { [self.leftArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { GroupSquareModel * smodel = (GroupSquareModel *)obj; smodel.isSelect = NO; }]; model.isSelect = !model.isSelect; if (indexPath.row == 0) { self.height.constant = 0.f; }else{ self.height.constant = 19.f; } self.leftId = model.Id; [self getSubData:model.Id]; } }else{ WS(weakSelf); GroupSquareSubModel * model = [self.rightArray objectAtIndex:indexPath.row]; if (model.IsUser) { TDGroupInfoListVC * vc = [TDGroupInfoListVC initTDGroupInfoListVC]; vc.GroupId = model.Id; vc.titleStr = model.Name; [self.navigationController pushViewController:vc animated:YES]; }else{ GroupSynopsisVC * vc = [GroupSynopsisVC initGroupSynopsisVC]; vc.GroupModel = model; vc.JoinBlock = ^{ model.IsUser = YES; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.rightTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); }; vc.SendBlock = ^{ model.IsApply = YES; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.rightTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); }; [self.navigationController pushViewController:vc animated:YES]; } } } - (void)JoinGroupIndex:(NSIndexPath *)indexPath withModel:(GroupSquareSubModel *)model { WS(weakSelf); if (model.IsAdmin) { GroupVerifyVC * vc = [GroupVerifyVC initGroupVerifyVC]; vc.Id = model.Id; vc.SendBlock = ^{ model.IsApply = YES; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf showMessage]; [weakSelf.rightTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); }; [self.navigationController pushViewController:vc animated:YES]; }else{ NSDictionary * paraDict = @{@"GroupId":@(model.Id), @"UserIds":@[@([AppUserModel sharedAppUserModel].Id)], @"UserId":@([AppUserModel sharedAppUserModel].Id), @"SourceType":@(1)///申请 }; [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Insert_User) parameters:paraDict responseStyle:JOSN success:^(id _Nonnull responseObject) { model.IsUser = YES; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.rightTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); TDGroupInfoListVC * vc = [TDGroupInfoListVC initTDGroupInfoListVC]; vc.GroupId = model.Id; vc.titleStr = model.Name; [weakSelf.navigationController pushViewController:vc animated:YES]; } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]); }]; } } - (void)showMessage { WS(weakSelf); UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的申请已提交,请耐心等候!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *noOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [noOk setValue:UIColorHex(0x0F7FD9) forKey:@"_titleTextColor"]; [alertVC addAction:noOk]; [weakSelf presentViewController:alertVC animated:YES completion:nil]; } #pragma mark - Load On Demand - (NSMutableArray *)leftArray { if (!_leftArray) { _leftArray = [NSMutableArray array]; } return _leftArray; } - (NSMutableArray *)rightArray { if (!_rightArray) { _rightArray = [NSMutableArray array]; } return _rightArray; } - (MyTDGroupView *)SearchView { if (!_SearchView) { _SearchView = [[MyTDGroupView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)]; } return _SearchView; } @end