// // MyTDTopicGroupSearchVC.m // smartRhino // // Created by niuzhen on 2019/12/19. // Copyright © 2019 tederen. All rights reserved. // #import "MyTDTopicGroupSearchVC.h" #import "TopicGroupManageModel.h" #import "TopicGroupManageCell.h" #import "MyTDTopicGroupUserVC.h" #import "ShowNewGroupAlert.h" @interface MyTDTopicGroupSearchVC () @property (strong, nonatomic) NSMutableArray * dataArray; @end @implementation MyTDTopicGroupSearchVC - (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_TopicGroupManager; [self addObserver:self forKeyPath:@"searchText" options:NSKeyValueObservingOptionNew context:nil]; } - (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:text]; } } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (void)getData:(NSString *)text { [self.dataArray removeAllObjects]; WS(weakSelf); [[HttpManager sharedHttpManager] GETUrl:Host(APP_Topic_List_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]; if ([model.Name containsString:text]) { [weakSelf.dataArray addObject:model]; } }]; dispatch_async(dispatch_get_main_queue(), ^{ if (weakSelf.dataArray.count > 0) { weakSelf.collectionView.hidden = YES; weakSelf.noDataView.hidden = YES; weakSelf.tableView.hidden = NO; [weakSelf.tableView reloadData]; }else{ weakSelf.noDataView.hidden = NO; weakSelf.collectionView.hidden = YES; weakSelf.tableView.hidden = YES; weakSelf.noDataL.text = @"暂无该分组"; } [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } #pragma mark - UITableViewDelegate - (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]; TopicGroupManageCell * cell = [TopicGroupManageCell configCell0:tableView indexPath:indexPath]; cell.nameL.text = model.Name; cell.countL.text = [NSString stringWithFormat:@"%ld",(long)model.IncludeCount]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row]; MyTDTopicGroupUserVC * vc = [MyTDTopicGroupUserVC initMyTDTopicGroupUserVC]; vc.GroupId = model.Id; vc.titleStr = model.Name; [self.navigationController pushViewController:vc animated:YES]; } - (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 index:indexPath.row]; }]; 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; } - (void)deleteFind:(NSInteger)Id index:(NSInteger)row { WS(weakSelf); [[HttpManager sharedHttpManager] DeleteUrl:Host(APP_Topic_Topic_Delete_Group) parameters:@{@"Ids":@[@(Id)]} responseStyle:DATA success:^(id _Nonnull responseObject) { [weakSelf.dataArray removeObjectAtIndex:row]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } 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:weakSelf.searchText]; } failure:^(NSError * _Nonnull error) { REMOVESHOW }]; } @end