IndexSearchVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // IndexSearchVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/9/17.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "IndexSearchVC.h"
  9. #import "DrawerView.h"
  10. #import "TabBarController.h"
  11. #import "SegmentViewController.h"
  12. #import "IndexAllVC.h"
  13. #import "IndexSubVC.h"
  14. #import "InfoSearchView.h"
  15. #import "SearchTypeSection.h"
  16. #import "SearchTypeCell.h"
  17. #import "SearchHistoryCell.h"
  18. #import "SearchDetailViewCell.h"
  19. #import <YYModel/YYModel.h>
  20. #define PYSEARCH_MARGIN 10
  21. @interface IndexSearchVC ()<InfoSearchViewDelegate, UITableViewDataSource, UITableViewDelegate>
  22. @property (nonatomic, strong) InfoSearchView *topNavSearch;
  23. @property (strong, nonatomic) DrawerView *drawerView;
  24. @property (strong, nonatomic) SegmentViewController *pageView;
  25. @property (copy,nonatomic) NSString *searchStr;
  26. @property (strong, nonatomic) IndexAllVC *allVC;
  27. @property (strong, nonatomic) IndexSubVC *articeVC;
  28. @property (strong, nonatomic) IndexSubVC *bookVC;
  29. @property (strong, nonatomic) IndexSubVC *videoVC;
  30. @property (strong, nonatomic) IndexSubVC *musicVC;
  31. // 历史记录tableView
  32. @property (nonatomic, strong) TDTableView *historyTableView;
  33. // 历史记录tableViewHeaderContainer
  34. @property (nonatomic, strong) UIView *historyHeaderContainer;
  35. // 历史记录tableHeaderView
  36. @property (nonatomic, strong) UIView *historyTableHeaderView;
  37. @property (nonatomic, strong) NSMutableArray *historyArray;
  38. @property (nonatomic, strong) SearchTypeSection *historyHeaderTitleView;
  39. @property (nonatomic, strong) UIView *hisNoView;
  40. @end
  41. @implementation IndexSearchVC
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. [self.navigationController.navigationBar setHidden:YES];
  45. }
  46. - (DrawerView *)drawerView{
  47. if(!_drawerView){
  48. _drawerView = [[DrawerView alloc] init];
  49. }
  50. return _drawerView;
  51. }
  52. - (InfoSearchView *)topNavSearch {
  53. if (!_topNavSearch) {
  54. _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault];
  55. }
  56. return _topNavSearch;
  57. }
  58. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  59. {
  60. [self.topNavSearch.searchBar resignFirstResponder];
  61. }
  62. - (void)searchButtonAction:(UISearchBar *)searchBar {
  63. self.searchStr = searchBar.text;
  64. [self insertHistorySearchStr:self.searchStr];
  65. // 更新本地历史记录
  66. self.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]];
  67. // 更新tableViewHeader的高度
  68. if (self.historyArray.count == 0) {
  69. self.hisNoView.hidden = NO;
  70. }else{
  71. self.hisNoView.hidden = YES;
  72. }
  73. [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray];
  74. [self resetHistoryHeaderViewHeight];
  75. [self.historyTableView reloadData];
  76. self.pageView.view.hidden = NO;
  77. self.historyTableView.hidden = YES;
  78. // 添加历史记录,刷新historyTable
  79. [self noticeGetData];
  80. [self.view endEditing:YES];
  81. }
  82. - (void)noticeGetData
  83. {
  84. if (self.searchStr.length == 0) {
  85. return;
  86. }
  87. switch (self.pageView.selectIndex - 10000) {
  88. case 0:
  89. {
  90. self.allVC.searchText = self.searchStr;
  91. [self.allVC getData];
  92. }
  93. break;
  94. case 1:
  95. {
  96. self.articeVC.searchText = self.searchStr;
  97. self.articeVC.type = MediaMediaArticeType;
  98. [self.articeVC getData];
  99. }
  100. break;
  101. case 2:
  102. {
  103. self.bookVC.searchText = self.searchStr;
  104. self.bookVC.type = MediaBookType;
  105. [self.bookVC getData];
  106. }
  107. break;
  108. case 3:
  109. {
  110. self.videoVC.searchText = self.searchStr;
  111. self.videoVC.type = MediaVideoType;
  112. [self.videoVC getData];
  113. }
  114. break;
  115. default:
  116. {
  117. self.musicVC.searchText = self.searchStr;
  118. self.musicVC.type = MediaMusicType;
  119. [self.musicVC getData];
  120. }
  121. break;
  122. }
  123. }
  124. // 文本改变回调
  125. - (void)textDidChange:(NSString *)text {
  126. self.searchStr = text;
  127. if (ISEmptyString(text)) {
  128. // 没有文本显示历史记录cell
  129. } else {
  130. // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录
  131. }
  132. }
  133. - (void)viewDidLoad {
  134. [super viewDidLoad];
  135. self.view.backgroundColor = [UIColor whiteColor];
  136. [self.view addSubview:self.topNavSearch];
  137. [self.view addSubview:self.historyTableView];
  138. [self addNavSearch];
  139. [self addPageView];
  140. [self.historyTableView setFrame:CGRectMake(0, CGRectGetMaxY(self.topNavSearch.frame), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topNavSearch.frame))];
  141. [self initHistoryTableHeaderView];
  142. [self.pageView addObserver:self forKeyPath:@"selectIndex" options:NSKeyValueObservingOptionNew context:nil];
  143. }
  144. - (void)dealloc
  145. {
  146. [[NSNotificationCenter defaultCenter] removeObserver:self];
  147. }
  148. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
  149. {
  150. id new = [change objectForKey:@"new"];
  151. id old = [change objectForKey:@"old"];
  152. if (new != old) {
  153. [self noticeGetData];
  154. }
  155. }
  156. - (void)addNavSearch
  157. {
  158. WS(weakSelf);
  159. self.topNavSearch.delegate = self;
  160. self.topNavSearch.backBlock = ^{
  161. [weakSelf.navigationController popViewControllerAnimated:YES];
  162. };
  163. self.topNavSearch.menuBlock = ^{
  164. [weakSelf.view endEditing:YES];
  165. [[UtilsTools getWindow] addSubview:weakSelf.drawerView];
  166. weakSelf.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) {
  167. if (![weakSelf.tabBarController isKindOfClass:[TabBarController class]]) {
  168. [TabBarController sharedTabBarController].tabBar.hidden = NO;
  169. [weakSelf.tabBarController.navigationController popViewControllerAnimated:NO];
  170. }
  171. if ([weakSelf.navigationController.viewControllers count] > 1) {
  172. [weakSelf.navigationController popToRootViewControllerAnimated:NO];
  173. }
  174. switch (indexPath.section) {
  175. case 0:
  176. {
  177. [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row];
  178. }
  179. break;
  180. case 1:
  181. {
  182. switch (indexPath.row) {
  183. case 0:
  184. {
  185. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  186. }
  187. break;
  188. case 1:
  189. {
  190. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  191. }
  192. break;
  193. default:
  194. {
  195. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  196. }
  197. break;
  198. }
  199. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  200. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}];
  201. });
  202. }
  203. break;
  204. default:
  205. {
  206. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  207. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  208. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(4 + indexPath.row)}];
  209. });
  210. }
  211. break;
  212. }
  213. };
  214. weakSelf.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);// - kNavigationHeight
  215. };
  216. }
  217. - (void)addPageView
  218. {
  219. self.pageView = [[SegmentViewController alloc] init];
  220. NSArray * titleArray = @[@"全部",@"文章",@"书籍",@"视频",@"音频"];
  221. self.pageView.titleArray = titleArray;
  222. NSMutableArray *controlArray = [[NSMutableArray alloc]init];
  223. self.allVC = [[IndexAllVC alloc]init];
  224. [controlArray addObject:self.allVC];
  225. self.articeVC = [[IndexSubVC alloc]init];
  226. [controlArray addObject:self.articeVC];
  227. self.bookVC = [[IndexSubVC alloc]init];
  228. [controlArray addObject:self.bookVC];
  229. self.videoVC = [[IndexSubVC alloc]init];
  230. [controlArray addObject:self.videoVC];
  231. self.musicVC = [[IndexSubVC alloc]init];
  232. [controlArray addObject:self.musicVC];
  233. self.pageView.subViewControllers = controlArray;
  234. self.pageView.buttonWidth = self.view.frame.size.width / titleArray.count;
  235. self.pageView.buttonHeight = 34.f;
  236. self.pageView.bottomCount = titleArray.count;
  237. [self.pageView initSegment:0];
  238. [self.pageView addParentController:self];
  239. [self.pageView.view setFrame:CGRectMake(0, CGRectGetMaxY(self.topNavSearch.frame), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topNavSearch.frame))];
  240. self.pageView.view.hidden = YES;
  241. }
  242. - (void)initHistoryTableHeaderView {
  243. [self.historyHeaderTitleView setSectionTitle:@"最近搜索"];
  244. WEAKSELF
  245. self.historyHeaderTitleView.rightAction = ^{
  246. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  247. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  248. // 清空历史记录
  249. [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]];
  250. weakSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]];
  251. [weakSelf addAndLayoutTagsWithTagsContentView:weakSelf.historyTableHeaderView tagTexts:weakSelf.historyArray];
  252. [weakSelf resetHistoryHeaderViewHeight];
  253. if (weakSelf.historyArray.count == 0) {
  254. weakSelf.hisNoView.hidden = NO;
  255. }else{
  256. weakSelf.hisNoView.hidden = YES;
  257. }
  258. [weakSelf.historyTableView reloadData];
  259. }];
  260. UIAlertController *tmpVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除最近搜索?" preferredStyle:UIAlertControllerStyleAlert];
  261. [tmpVc addAction:action1];
  262. [tmpVc addAction:action2];
  263. [weakSelf presentViewController:tmpVc animated:YES completion:nil];
  264. };
  265. [self.historyHeaderContainer addSubview:self.historyHeaderTitleView];
  266. self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, 0);
  267. [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray];
  268. CGFloat height = CGRectGetMaxY(self.historyTableHeaderView.subviews.lastObject.frame);
  269. self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, height);
  270. [self.historyHeaderContainer addSubview:self.historyTableHeaderView];
  271. [self resetHistoryHeaderViewHeight];
  272. self.historyTableView.tableHeaderView = self.historyHeaderContainer;
  273. self.historyTableView.tableFooterView = self.hisNoView;
  274. if (self.historyArray.count == 0) {
  275. self.hisNoView.hidden = NO;
  276. }else{
  277. self.hisNoView.hidden = YES;
  278. }
  279. UILabel * label = [UILabel new];
  280. label.text = @"暂无搜索记录!";
  281. label.font = [UIFont systemFontOfSize:14];
  282. label.textColor = UIColorHex(0x999999);
  283. [self.hisNoView addSubview:label];
  284. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  285. make.center.mas_equalTo(self.hisNoView);
  286. }];
  287. }
  288. - (UIView *)historyTableHeaderView {
  289. if (!_historyTableHeaderView) {
  290. _historyTableHeaderView = [UIView new];
  291. _historyTableHeaderView.backgroundColor = [UIColor whiteColor];;
  292. }
  293. return _historyTableHeaderView;
  294. }
  295. - (SearchTypeSection *)historyHeaderTitleView {
  296. if (!_historyHeaderTitleView) {
  297. _historyHeaderTitleView = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 0, kGXScreenWidth, 44)];
  298. }
  299. return _historyHeaderTitleView;
  300. }
  301. - (UIView *)hisNoView
  302. {
  303. if (!_hisNoView) {
  304. _hisNoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
  305. _hisNoView.hidden = YES;
  306. }
  307. return _hisNoView;
  308. }
  309. - (UIView *)historyHeaderContainer {
  310. if (!_historyHeaderContainer) {
  311. _historyHeaderContainer = [UIView new];
  312. _historyHeaderContainer.backgroundColor = [UIColor whiteColor];
  313. }
  314. return _historyHeaderContainer;
  315. }
  316. - (TDTableView *)historyTableView {
  317. if (!_historyTableView) {
  318. _historyTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  319. _historyTableView.dataSource = self;
  320. _historyTableView.delegate = self;
  321. }
  322. return _historyTableView;
  323. }
  324. - (NSMutableArray *)historyArray {
  325. if (!_historyArray) {
  326. _historyArray = [_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home].mutableCopy;
  327. }
  328. return _historyArray;
  329. }
  330. - (void)resetHistoryHeaderViewHeight {
  331. CGFloat containerHeight = CGRectGetMaxY(self.historyHeaderContainer.subviews.lastObject.frame);
  332. self.historyHeaderContainer.frame = CGRectMake(0, 0, kScreenWidth, containerHeight);
  333. }
  334. - (void)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts:(NSArray<NSString *> *)tagTexts;
  335. {
  336. [contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  337. NSMutableArray *tagsM = [NSMutableArray array];
  338. for (int i = 0; i < tagTexts.count; i++) {
  339. UILabel *label = [self labelWithTitle:tagTexts[i]];
  340. [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagDidCLick:)]];
  341. [contentView addSubview:label];
  342. [tagsM addObject:label];
  343. }
  344. CGFloat currentX = 0;
  345. CGFloat currentY = 0;
  346. CGFloat countRow = 0;
  347. CGFloat countCol = 0;
  348. CGFloat leftMargin = 12;
  349. for (int i = 0; i < contentView.subviews.count; i++) {
  350. UILabel *subView = contentView.subviews[i];
  351. // When the number of search words is too large, the width is width of the contentView
  352. if (subView.width > contentView.width) {
  353. subView.width = contentView.width;
  354. }
  355. if (leftMargin + currentX + subView.width + PYSEARCH_MARGIN * countRow > contentView.width) {
  356. subView.x = leftMargin;
  357. subView.y = (currentY += subView.height) + PYSEARCH_MARGIN * ++countCol;
  358. currentX = subView.width;
  359. countRow = 1;
  360. } else {
  361. subView.x = leftMargin + (currentX += subView.width) - subView.width + PYSEARCH_MARGIN * countRow;
  362. subView.y = currentY + PYSEARCH_MARGIN * countCol;
  363. countRow ++;
  364. }
  365. }
  366. }
  367. // 创建label
  368. - (UILabel *)labelWithTitle:(NSString *)title
  369. {
  370. UILabel *label = [[UILabel alloc] init];
  371. label.userInteractionEnabled = YES;
  372. label.font = [UIFont systemFontOfSize:14];
  373. label.text = title;
  374. label.textColor = [UIColor grayColor];
  375. label.backgroundColor = kColorFromRGB(0xfafafa);
  376. label.textAlignment = NSTextAlignmentCenter;
  377. [label sizeToFit];
  378. label.width += 20;
  379. label.height += 14;
  380. label.layer.cornerRadius = label.height / 2;
  381. label.clipsToBounds = YES;
  382. return label;
  383. }
  384. - (void)insertHistorySearchStr:(NSString *)string {
  385. for (NSString *cache in _historyArray) {
  386. // 查重
  387. if ([cache isEqualToString:string]) {
  388. return;
  389. }
  390. }
  391. [_historyArray insertObject:string atIndex:0];
  392. if (_historyArray.count > 10) {
  393. NSArray *orderArray = [_historyArray subarrayWithRange:NSMakeRange(0, 10)];
  394. _historyArray = orderArray.mutableCopy;
  395. }
  396. [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:_historyArray];
  397. }
  398. - (void)tagDidCLick:(UITapGestureRecognizer *)gr
  399. {
  400. UILabel *label = (UILabel *)gr.view;
  401. self.searchStr = label.text;
  402. [self.topNavSearch setNowSearchData:self.searchStr];
  403. self.pageView.view.hidden = NO;
  404. self.historyTableView.hidden = YES;
  405. [self noticeGetData];
  406. }
  407. #pragma mark - UITableViewDelegate
  408. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  409. return 1;
  410. }
  411. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  412. return 0;
  413. }
  414. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  415. return 34;
  416. }
  417. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  418. SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"];
  419. if (!searchHistoryCell) {
  420. searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchHistoryCell"];
  421. }
  422. [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]];
  423. return searchHistoryCell;
  424. }
  425. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  426. [self.view endEditing:YES];
  427. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  428. self.searchStr = self.historyArray[indexPath.row];
  429. [self.topNavSearch setNowSearchData:self.searchStr];
  430. self.pageView.view.hidden = NO;
  431. self.historyTableView.hidden = YES;
  432. // 添加历史记录,刷新historyTable
  433. [self noticeGetData];
  434. }
  435. @end