// // GroupSquareSearchVC.m // smartRhino // // Created by niuzhen on 2020/1/17. // Copyright © 2020 tederen. All rights reserved. // #import "GroupSquareSearchVC.h" #import "GroupSquareModel.h" #import "GroupSquareSubModel.h" #import "GroupSquareCell.h" #import "GroupSynopsisVC.h" #import "TDGroupInfoListVC.h" #import "GroupVerifyVC.h" @interface GroupSquareSearchVC () @property (nonatomic, strong) NSMutableArray *listArray; @end @implementation GroupSquareSearchVC - (NSMutableArray *)listArray { if (!_listArray) { _listArray = [NSMutableArray array]; } return _listArray; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = RGB(255, 255, 255); self.tableView.delegate = self; self.tableView.dataSource = self; self.historySearchType = HistorySearchType_Square; [self addObserver:self forKeyPath:@"searchText" options:NSKeyValueObservingOptionNew context:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { id obj = [change objectForKey:@"new"]; NSString * text = @""; if ([obj isKindOfClass:[NSString class]]) { text = obj; }else{ text = [obj stringValue]; } if (text.length > 0) { [self getData]; } } - (void)getData { [self.listArray removeAllObjects]; WS(weakSelf); [[HttpManager sharedHttpManager] GETUrl:Host(API_APP_GROUP_Search) parameters:@{@"keyWord":self.searchText} responseStyle:JOSN success:^(id _Nonnull responseObject) { if ([responseObject isKindOfClass:[NSArray class]]) { for (NSDictionary * dict in responseObject) { GroupSquareSubModel * model = [GroupSquareSubModel modelWithDictionary:dict]; [weakSelf.listArray addObject:model]; } } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } #pragma mark - UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.listArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [GroupSquareCell configCell1Height]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { GroupSquareSubModel * model = [self.listArray objectAtIndex:indexPath.row]; GroupSquareCell * cell = [GroupSquareCell configCell1:tableView indexPath:indexPath]; [cell setRightDataSubModel:model withSearchText:self.searchText]; WS(weakSelf); [cell.JoinBtn setAction:^{ [weakSelf JoinGroupIndex:indexPath withModel:model]; }]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); [tableView deselectRowAtIndexPath:indexPath animated:YES]; GroupSquareSubModel * model = [self.listArray 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.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); }; vc.SendBlock = ^{ model.IsApply = YES; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView 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.tableView 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.tableView 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]; } @end