// // ReplayMeSearchVC.m // TheoryNetwork // // Created by tederen on 2019/9/20. // Copyright © 2019 tederen. All rights reserved. // #import "ReplayMeSearchVC.h" #import "InfoSearchView.h" #import "SearchTypeSection.h" #import "SearchTypeCell.h" #import "SearchHistoryCell.h" #import "ReplayMeCell.h" #import "HomeDetailController.h" #import "MyApprovalBCell.h" #import #import "MyApprovalPageDetail.h" #import "DrawerView.h" #import "TabBarController.h" #define PYSEARCH_MARGIN 10 static NSString * SearchDetailViewCellID = @"SearchDetailViewCellId"; static NSString * MyApprovalBCellID = @"MyApprovalBCell"; @interface ReplayMeSearchVC () @property (nonatomic, strong) InfoSearchView *topNavSearch; @property (nonatomic, strong) UIView *hisNoView; @property (strong,nonatomic) DrawerView *drawerView; @property (nonatomic, copy) NSArray *warmArray; @property (nonatomic, copy) NSArray *coldArray; // 历史记录tableView @property (nonatomic, strong) TDTableView *historyTableView; // 历史记录tableViewHeaderContainer @property (nonatomic, strong) UIView *historyHeaderContainer; // 历史记录tableHeaderView @property (nonatomic, strong) UIView *historyTableHeaderView; @property (nonatomic, strong)SearchTypeSection *historyHeaderTitleView; // 搜索结果tableView @property (nonatomic, strong) UITableView *resultTableView; // 历史记录 @property (nonatomic, strong) NSMutableArray *historyArray; // 搜索结果 @property (nonatomic, strong) NSMutableArray *resultArray; @property (nonatomic, copy) NSString *searchStr; @property (nonatomic, strong) UIView *noDataView; @property (nonatomic, strong) UILabel *noDataTipLabel; @end @implementation ReplayMeSearchVC -(DrawerView *)drawerView{ if(!_drawerView){ _drawerView = [[DrawerView alloc] init]; } return _drawerView; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //TODO:测试数据 _warmArray = @[@"信仰的力量", @"牛顿改革"]; _coldArray = @[@"科技", @"国家组织", @"泰德仁和"]; [self.view addSubview:self.topNavSearch]; [self.view addSubview:self.historyTableView]; [self initHistoryTableHeaderView]; // [self.view addSubview:self.thinkTableView]; [self.view addSubview:self.resultTableView]; [self.resultTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.topNavSearch.mas_bottom); if (@available(iOS 11.0, *)) { make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom); } else { make.bottom.equalTo(self.view.mas_bottom); } }]; [self.view addSubview:self.noDataView]; self.noDataView.frame = self.historyTableView.frame; [self.noDataView addSubview:self.noDataTipLabel]; [self.noDataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.noDataView); make.top.mas_offset(32); }]; [self.noDataView setHidden:YES]; [self.resultTableView registerNib:[UINib nibWithNibName:@"MyApprovalBCell" bundle:nil] forCellReuseIdentifier:MyApprovalBCellID]; [self.topNavSearch.searchBar becomeFirstResponder]; [self.historyTableView setHidden:NO]; [self.resultTableView setHidden:YES]; } // 请求列表 - (void)getListData:(NSString *)keyword IsClickSearchAction:(BOOL)isClickSearchAction{ [self.topNavSearch.searchBar resignFirstResponder]; [self.historyTableView setHidden:YES]; [self.resultTableView reloadData]; [self.resultTableView setHidden:NO]; // WEAKSELF // if (isClickSearchAction) { // SHOWLOADING // [weakSelf.noDataView setHidden:YES]; // [self.resultTableView setHidden:NO]; // [self.historyTableView setHidden:YES]; // } // [self.resultArray removeAllObjects]; // // [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: @{@"page":@1,@"perPage":@100,@"keyword":keyword} responseStyle:JOSN success:^(id _Nonnull responseObject) { // REMOVESHOW // NSDictionary *dic = (NSDictionary *)responseObject; // NSLog(@"首页搜索列表数据%@=====================================",dic[@"total"]); // HomeArticleModel *model = [HomeArticleModel yy_modelWithDictionary:dic]; // weakSelf.resultArray = [NSMutableArray arrayWithArray: model.Items]; // // // [weakSelf.noDataView setHidden:YES]; // if (ISEmptyString(self.searchStr)) { // //显示历史记录 // [weakSelf.historyTableView setHidden:NO]; // [weakSelf.resultTableView reloadData]; // [weakSelf.resultTableView setHidden:YES]; // } else { // [weakSelf.historyTableView setHidden:YES]; // [weakSelf.resultTableView reloadData]; // [weakSelf.resultTableView setHidden:NO]; // // 点击搜索按钮 //// if (!model.Items.count) { //// // 文章列表为空,显示无结果 //// [self.noDataView setHidden:NO]; //// } else { //// // 文章列表不为空 //// [weakSelf.historyTableView setHidden:YES]; //// [weakSelf.resultTableView reloadData]; //// [weakSelf.resultTableView setHidden:NO]; //// } // } // } failure:^(NSError * _Nonnull error) { // SHOWERROR([ZYCTool handerResultData:error]) // }]; } - (BOOL)hidesBottomBarWhenPushed { return YES; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController.navigationBar setHidden:YES]; } #pragma mark - InfoSearchViewDelegate // 点击搜索按钮 - (void)searchButtonAction:(UISearchBar *)searchBar { self.searchStr = searchBar.text; // 添加历史记录,刷新historyTable [self insertHistorySearchStr:self.searchStr]; // 更新本地历史记录 self.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]]; // 更新tableViewHeader的高度 if (self.historyArray.count == 0) { self.hisNoView.hidden = NO; }else{ self.hisNoView.hidden = YES; } [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray]; [self resetHistoryHeaderViewHeight]; [self.historyTableView reloadData]; // 网络请求 [self getListData:self.searchStr IsClickSearchAction:YES]; [self.view endEditing:YES]; } // 文本改变回调 - (void)textDidChange:(NSString *)text { self.searchStr = text; if (ISEmptyString(text)) { // 没有文本显示历史记录cell [self.noDataView setHidden:YES]; [self.historyTableView setHidden:NO]; // [self.thinkTableView setHidden:YES]; [self.resultTableView setHidden:YES]; } else { // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录 [self getListData:text IsClickSearchAction:NO]; } } #pragma mark - UITableViewDataSource && UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([tableView isEqual:self.historyTableView]) { return 0; }else { return 4; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView isEqual:self.historyTableView]) { return 34; }else { return UITableViewAutomaticDimension; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView isEqual:self.historyTableView]) { SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"]; if (!searchHistoryCell) { searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchHistoryCell"]; } [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]]; return searchHistoryCell; } else { ReplayMeCell * cell = [ReplayMeCell configCell:tableView indexPath:indexPath]; [cell setDataWithText:@"会议"]; if (indexPath.row >= 3) { cell.redV.hidden = YES; } return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.view endEditing:YES]; if ([tableView isEqual:self.historyTableView]) { self.searchStr = self.historyArray[indexPath.row]; [self.topNavSearch setNowSearchData:self.searchStr]; [self getListData:self.searchStr IsClickSearchAction:YES]; } else { } } #pragma mark - 查重 - (void)insertHistorySearchStr:(NSString *)string { for (NSString *cache in _historyArray) { // 查重 if ([cache isEqualToString:string]) { return; } } [_historyArray insertObject:string atIndex:0]; if (_historyArray.count > 10) { NSArray *orderArray = [_historyArray subarrayWithRange:NSMakeRange(0, 10)]; _historyArray = orderArray.mutableCopy; } [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:_historyArray]; } #pragma mark - getter - (InfoSearchView *)topNavSearch { if (!_topNavSearch) { _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault]; _topNavSearch.delegate = self; WEAKSELF _topNavSearch.backBlock = ^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }; _topNavSearch.menuBlock = ^{ [weakSelf.view endEditing:YES]; [[UtilsTools getWindow] addSubview:weakSelf.drawerView]; weakSelf.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) { if (![weakSelf.tabBarController isKindOfClass:[TabBarController class]]) { [TabBarController sharedTabBarController].tabBar.hidden = NO; [weakSelf.tabBarController.navigationController popViewControllerAnimated:NO]; } if ([weakSelf.navigationController.viewControllers count] > 1) { [weakSelf.navigationController popToRootViewControllerAnimated:NO]; } switch (indexPath.section) { case 0: { [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row]; } break; case 1: { switch (indexPath.row) { case 0: { [[TabBarController sharedTabBarController] setSelectedIndex:2]; } break; case 1: { [[TabBarController sharedTabBarController] setSelectedIndex:2]; } break; default: { [[TabBarController sharedTabBarController] setSelectedIndex:3]; } break; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}]; }); } break; default: { [[TabBarController sharedTabBarController] setSelectedIndex:3]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(4 + indexPath.row)}]; }); } break; } }; weakSelf.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);// - kNavigationHeight }; } return _topNavSearch; } - (TDTableView *)historyTableView { if (!_historyTableView) { _historyTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _historyTableView.dataSource = self; _historyTableView.delegate = self; _historyTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45); } return _historyTableView; } - (UITableView *)resultTableView { if (!_resultTableView) { _resultTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _resultTableView.dataSource = self; _resultTableView.delegate = self; _resultTableView.separatorStyle = UITableViewCellSeparatorStyleNone; } return _resultTableView; } - (NSMutableArray *)historyArray { if (!_historyArray) { _historyArray = [_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_XinYuan].mutableCopy; } return _historyArray; } - (NSMutableArray *)resultArray { if (!_resultArray) { _resultArray = [NSMutableArray array]; } return _resultArray; } - (UIView *)noDataView { if (!_noDataView) { _noDataView = [UIView new]; _noDataView.backgroundColor = [UIColor whiteColor]; } return _noDataView; } - (UILabel *)noDataTipLabel { if (!_noDataTipLabel) { _noDataTipLabel = [UILabel new]; _noDataTipLabel.text = @"暂无搜索结果"; _noDataTipLabel.font = Kfont(15); _noDataTipLabel.textColor = kColorFromRGB(0x999999); _noDataTipLabel.textAlignment = NSTextAlignmentCenter; } return _noDataTipLabel; } - (UIView *)historyTableHeaderView { if (!_historyTableHeaderView) { _historyTableHeaderView = [UIView new]; _historyTableHeaderView.backgroundColor = [UIColor whiteColor];; } return _historyTableHeaderView; } - (SearchTypeSection *)historyHeaderTitleView { if (!_historyHeaderTitleView) { _historyHeaderTitleView = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 0, kGXScreenWidth, 44)]; } return _historyHeaderTitleView; } #pragma mark - historyHeaderView // 添加tagView - (void)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts:(NSArray *)tagTexts; { [contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; NSMutableArray *tagsM = [NSMutableArray array]; for (int i = 0; i < tagTexts.count; i++) { UILabel *label = [self labelWithTitle:tagTexts[i]]; [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagDidCLick:)]]; [contentView addSubview:label]; [tagsM addObject:label]; } CGFloat currentX = 0; CGFloat currentY = 0; CGFloat countRow = 0; CGFloat countCol = 0; CGFloat leftMargin = 12; for (int i = 0; i < contentView.subviews.count; i++) { UILabel *subView = contentView.subviews[i]; // When the number of search words is too large, the width is width of the contentView if (subView.width > contentView.width) { subView.width = contentView.width; } if (leftMargin + currentX + subView.width + PYSEARCH_MARGIN * countRow > contentView.width) { subView.x = leftMargin; subView.y = (currentY += subView.height) + PYSEARCH_MARGIN * ++countCol; currentX = subView.width; countRow = 1; } else { subView.x = leftMargin + (currentX += subView.width) - subView.width + PYSEARCH_MARGIN * countRow; subView.y = currentY + PYSEARCH_MARGIN * countCol; countRow ++; } } } // 创建label - (UILabel *)labelWithTitle:(NSString *)title { UILabel *label = [[UILabel alloc] init]; label.userInteractionEnabled = YES; label.font = [UIFont systemFontOfSize:14]; label.text = title; label.textColor = [UIColor grayColor]; label.backgroundColor = kColorFromRGB(0xfafafa); label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; label.width += 20; label.height += 14; label.layer.cornerRadius = label.height / 2; label.clipsToBounds = YES; return label; } - (void)tagDidCLick:(UITapGestureRecognizer *)gr { UILabel *label = (UILabel *)gr.view; self.searchStr = label.text; [self.topNavSearch setNowSearchData:self.searchStr]; [self getListData:self.searchStr IsClickSearchAction:YES]; } - (UIView *)historyHeaderContainer { if (!_historyHeaderContainer) { _historyHeaderContainer = [UIView new]; _historyHeaderContainer.backgroundColor = [UIColor whiteColor]; } return _historyHeaderContainer; } // 创建historyTableView的headerView - (void)initHistoryTableHeaderView { [self.historyHeaderTitleView setSectionTitle:@"最近搜索"]; WEAKSELF self.historyHeaderTitleView.rightAction = ^{ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 清空历史记录 [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]]; weakSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]]; [weakSelf addAndLayoutTagsWithTagsContentView:weakSelf.historyTableHeaderView tagTexts:weakSelf.historyArray]; [weakSelf resetHistoryHeaderViewHeight]; if (weakSelf.historyArray.count == 0) { weakSelf.hisNoView.hidden = NO; }else{ weakSelf.hisNoView.hidden = YES; } [weakSelf.historyTableView reloadData]; }]; UIAlertController *tmpVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除最近搜索?" preferredStyle:UIAlertControllerStyleAlert]; [tmpVc addAction:action1]; [tmpVc addAction:action2]; [weakSelf presentViewController:tmpVc animated:YES completion:nil]; }; [self.historyHeaderContainer addSubview:self.historyHeaderTitleView]; self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, 0); [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray]; CGFloat height = CGRectGetMaxY(self.historyTableHeaderView.subviews.lastObject.frame); self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, height); [self.historyHeaderContainer addSubview:self.historyTableHeaderView]; [self resetHistoryHeaderViewHeight]; self.historyTableView.tableHeaderView = self.historyHeaderContainer; self.historyTableView.tableFooterView = self.hisNoView; if (self.historyArray.count == 0) { self.hisNoView.hidden = NO; }else{ self.hisNoView.hidden = YES; } UILabel * label = [UILabel new]; label.text = @"暂无搜索记录!"; label.font = [UIFont systemFontOfSize:14]; label.textColor = UIColorHex(0x999999); [self.hisNoView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(self.hisNoView); }]; } - (UIView *)hisNoView { if (!_hisNoView) { _hisNoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)]; _hisNoView.hidden = YES; } return _hisNoView; } // 更新header高度 - (void)resetHistoryHeaderViewHeight { CGFloat containerHeight = CGRectGetMaxY(self.historyHeaderContainer.subviews.lastObject.frame); self.historyHeaderContainer.frame = CGRectMake(0, 0, kScreenWidth, containerHeight); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.topNavSearch.searchBar resignFirstResponder]; } @end