// // IndexSearchVC.m // smartRhino // // Created by niuzhen on 2020/9/17. // Copyright © 2020 tederen. All rights reserved. // #import "IndexSearchVC.h" #import "DrawerView.h" #import "TabBarController.h" #import "SegmentViewController.h" #import "IndexAllVC.h" #import "IndexSubVC.h" #import "InfoSearchView.h" #import "SearchTypeSection.h" #import "SearchTypeCell.h" #import "SearchHistoryCell.h" #import "SearchDetailViewCell.h" #import #define PYSEARCH_MARGIN 10 @interface IndexSearchVC () @property (nonatomic, strong) InfoSearchView *topNavSearch; @property (strong, nonatomic) DrawerView *drawerView; @property (strong, nonatomic) SegmentViewController *pageView; @property (copy,nonatomic) NSString *searchStr; @property (strong, nonatomic) IndexAllVC *allVC; @property (strong, nonatomic) IndexSubVC *articeVC; @property (strong, nonatomic) IndexSubVC *bookVC; @property (strong, nonatomic) IndexSubVC *videoVC; @property (strong, nonatomic) IndexSubVC *musicVC; // 历史记录tableView @property (nonatomic, strong) TDTableView *historyTableView; // 历史记录tableViewHeaderContainer @property (nonatomic, strong) UIView *historyHeaderContainer; // 历史记录tableHeaderView @property (nonatomic, strong) UIView *historyTableHeaderView; @property (nonatomic, strong) NSMutableArray *historyArray; @property (nonatomic, strong) SearchTypeSection *historyHeaderTitleView; @property (nonatomic, strong) UIView *hisNoView; @end @implementation IndexSearchVC - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController.navigationBar setHidden:YES]; } - (DrawerView *)drawerView{ if(!_drawerView){ _drawerView = [[DrawerView alloc] init]; } return _drawerView; } - (InfoSearchView *)topNavSearch { if (!_topNavSearch) { _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault]; } return _topNavSearch; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.topNavSearch.searchBar resignFirstResponder]; } - (void)searchButtonAction:(UISearchBar *)searchBar { self.searchStr = searchBar.text; [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.pageView.view.hidden = NO; self.historyTableView.hidden = YES; // 添加历史记录,刷新historyTable [self noticeGetData]; [self.view endEditing:YES]; } - (void)noticeGetData { if (self.searchStr.length == 0) { return; } switch (self.pageView.selectIndex - 10000) { case 0: { self.allVC.searchText = self.searchStr; [self.allVC getData]; } break; case 1: { self.articeVC.searchText = self.searchStr; self.articeVC.type = MediaMediaArticeType; [self.articeVC getData]; } break; case 2: { self.bookVC.searchText = self.searchStr; self.bookVC.type = MediaBookType; [self.bookVC getData]; } break; case 3: { self.videoVC.searchText = self.searchStr; self.videoVC.type = MediaVideoType; [self.videoVC getData]; } break; default: { self.musicVC.searchText = self.searchStr; self.musicVC.type = MediaMusicType; [self.musicVC getData]; } break; } } // 文本改变回调 - (void)textDidChange:(NSString *)text { self.searchStr = text; if (ISEmptyString(text)) { // 没有文本显示历史记录cell } else { // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录 } } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.topNavSearch]; [self.view addSubview:self.historyTableView]; [self addNavSearch]; [self addPageView]; [self.historyTableView setFrame:CGRectMake(0, CGRectGetMaxY(self.topNavSearch.frame), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topNavSearch.frame))]; [self initHistoryTableHeaderView]; [self.pageView addObserver:self forKeyPath:@"selectIndex" options:NSKeyValueObservingOptionNew context:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { id new = [change objectForKey:@"new"]; id old = [change objectForKey:@"old"]; if (new != old) { [self noticeGetData]; } } - (void)addNavSearch { WS(weakSelf); self.topNavSearch.delegate = self; self.topNavSearch.backBlock = ^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }; self.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 }; } - (void)addPageView { self.pageView = [[SegmentViewController alloc] init]; NSArray * titleArray = @[@"全部",@"文章",@"书籍",@"视频",@"音频"]; self.pageView.titleArray = titleArray; NSMutableArray *controlArray = [[NSMutableArray alloc]init]; self.allVC = [[IndexAllVC alloc]init]; [controlArray addObject:self.allVC]; self.articeVC = [[IndexSubVC alloc]init]; [controlArray addObject:self.articeVC]; self.bookVC = [[IndexSubVC alloc]init]; [controlArray addObject:self.bookVC]; self.videoVC = [[IndexSubVC alloc]init]; [controlArray addObject:self.videoVC]; self.musicVC = [[IndexSubVC alloc]init]; [controlArray addObject:self.musicVC]; self.pageView.subViewControllers = controlArray; self.pageView.buttonWidth = self.view.frame.size.width / titleArray.count; self.pageView.buttonHeight = 34.f; self.pageView.bottomCount = titleArray.count; [self.pageView initSegment:0]; [self.pageView addParentController:self]; [self.pageView.view setFrame:CGRectMake(0, CGRectGetMaxY(self.topNavSearch.frame), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topNavSearch.frame))]; self.pageView.view.hidden = YES; } - (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 *)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; } - (UIView *)hisNoView { if (!_hisNoView) { _hisNoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)]; _hisNoView.hidden = YES; } return _hisNoView; } - (UIView *)historyHeaderContainer { if (!_historyHeaderContainer) { _historyHeaderContainer = [UIView new]; _historyHeaderContainer.backgroundColor = [UIColor whiteColor]; } return _historyHeaderContainer; } - (TDTableView *)historyTableView { if (!_historyTableView) { _historyTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _historyTableView.dataSource = self; _historyTableView.delegate = self; } return _historyTableView; } - (NSMutableArray *)historyArray { if (!_historyArray) { _historyArray = [_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home].mutableCopy; } return _historyArray; } - (void)resetHistoryHeaderViewHeight { CGFloat containerHeight = CGRectGetMaxY(self.historyHeaderContainer.subviews.lastObject.frame); self.historyHeaderContainer.frame = CGRectMake(0, 0, kScreenWidth, containerHeight); } - (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)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]; } - (void)tagDidCLick:(UITapGestureRecognizer *)gr { UILabel *label = (UILabel *)gr.view; self.searchStr = label.text; [self.topNavSearch setNowSearchData:self.searchStr]; self.pageView.view.hidden = NO; self.historyTableView.hidden = YES; [self noticeGetData]; } #pragma mark - UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 34; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"]; if (!searchHistoryCell) { searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchHistoryCell"]; } [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]]; return searchHistoryCell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.view endEditing:YES]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; self.searchStr = self.historyArray[indexPath.row]; [self.topNavSearch setNowSearchData:self.searchStr]; self.pageView.view.hidden = NO; self.historyTableView.hidden = YES; // 添加历史记录,刷新historyTable [self noticeGetData]; } @end