IndexSearchVC.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. @interface IndexSearchVC ()<InfoSearchViewDelegate>
  16. @property (nonatomic, strong) InfoSearchView *topNavSearch;
  17. @property (strong, nonatomic) DrawerView *drawerView;
  18. @property (strong, nonatomic) SegmentViewController *pageView;
  19. @property (copy,nonatomic) NSString *searchStr;
  20. @property (strong, nonatomic) IndexAllVC *allVC;
  21. @property (strong, nonatomic) IndexSubVC *articeVC;
  22. @property (strong, nonatomic) IndexSubVC *bookVC;
  23. @property (strong, nonatomic) IndexSubVC *videoVC;
  24. @property (strong, nonatomic) IndexSubVC *musicVC;
  25. @end
  26. @implementation IndexSearchVC
  27. - (void)viewWillAppear:(BOOL)animated {
  28. [super viewWillAppear:animated];
  29. [self.navigationController.navigationBar setHidden:YES];
  30. }
  31. - (DrawerView *)drawerView{
  32. if(!_drawerView){
  33. _drawerView = [[DrawerView alloc] init];
  34. }
  35. return _drawerView;
  36. }
  37. - (InfoSearchView *)topNavSearch {
  38. if (!_topNavSearch) {
  39. _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault];
  40. }
  41. return _topNavSearch;
  42. }
  43. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  44. {
  45. [self.topNavSearch.searchBar resignFirstResponder];
  46. }
  47. - (void)searchButtonAction:(UISearchBar *)searchBar {
  48. self.searchStr = searchBar.text;
  49. // 添加历史记录,刷新historyTable
  50. [self noticeGetData];
  51. [self.view endEditing:YES];
  52. }
  53. - (void)noticeGetData
  54. {
  55. if (self.searchStr.length == 0) {
  56. return;
  57. }
  58. switch (self.pageView.selectIndex - 10000) {
  59. case 0:
  60. {
  61. self.allVC.searchText = self.searchStr;
  62. [self.allVC getData];
  63. }
  64. break;
  65. case 1:
  66. {
  67. self.articeVC.searchText = self.searchStr;
  68. self.articeVC.type = MediaArticeType;
  69. [self.articeVC getData];
  70. }
  71. break;
  72. case 2:
  73. {
  74. self.bookVC.searchText = self.searchStr;
  75. self.bookVC.type = MediaBookType;
  76. [self.bookVC getData];
  77. }
  78. break;
  79. case 3:
  80. {
  81. self.videoVC.searchText = self.searchStr;
  82. self.videoVC.type = MediaVideoType;
  83. [self.videoVC getData];
  84. }
  85. break;
  86. default:
  87. {
  88. self.musicVC.searchText = self.searchStr;
  89. self.musicVC.type = MediaMusicType;
  90. [self.musicVC getData];
  91. }
  92. break;
  93. }
  94. }
  95. // 文本改变回调
  96. - (void)textDidChange:(NSString *)text {
  97. self.searchStr = text;
  98. if (ISEmptyString(text)) {
  99. // 没有文本显示历史记录cell
  100. } else {
  101. // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录
  102. }
  103. }
  104. - (void)viewDidLoad {
  105. [super viewDidLoad];
  106. self.view.backgroundColor = [UIColor whiteColor];
  107. [self.view addSubview:self.topNavSearch];
  108. [self addNavSearch];
  109. [self addPageView];
  110. [self.pageView addObserver:self forKeyPath:@"selectIndex" options:NSKeyValueObservingOptionNew context:nil];
  111. }
  112. - (void)dealloc
  113. {
  114. [[NSNotificationCenter defaultCenter] removeObserver:self];
  115. }
  116. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
  117. {
  118. id new = [change objectForKey:@"new"];
  119. id old = [change objectForKey:@"old"];
  120. if (new != old) {
  121. [self noticeGetData];
  122. }
  123. }
  124. - (void)addNavSearch
  125. {
  126. WS(weakSelf);
  127. self.topNavSearch.delegate = self;
  128. self.topNavSearch.backBlock = ^{
  129. [weakSelf.navigationController popViewControllerAnimated:YES];
  130. };
  131. self.topNavSearch.menuBlock = ^{
  132. [weakSelf.view endEditing:YES];
  133. [[UtilsTools getWindow] addSubview:weakSelf.drawerView];
  134. weakSelf.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) {
  135. if (![weakSelf.tabBarController isKindOfClass:[TabBarController class]]) {
  136. [TabBarController sharedTabBarController].tabBar.hidden = NO;
  137. [weakSelf.tabBarController.navigationController popViewControllerAnimated:NO];
  138. }
  139. if ([weakSelf.navigationController.viewControllers count] > 1) {
  140. [weakSelf.navigationController popToRootViewControllerAnimated:NO];
  141. }
  142. switch (indexPath.section) {
  143. case 0:
  144. {
  145. [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row];
  146. }
  147. break;
  148. case 1:
  149. {
  150. switch (indexPath.row) {
  151. case 0:
  152. {
  153. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  154. }
  155. break;
  156. case 1:
  157. {
  158. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  159. }
  160. break;
  161. default:
  162. {
  163. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  164. }
  165. break;
  166. }
  167. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  168. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}];
  169. });
  170. }
  171. break;
  172. default:
  173. {
  174. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  175. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  176. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(4 + indexPath.row)}];
  177. });
  178. }
  179. break;
  180. }
  181. };
  182. weakSelf.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);// - kNavigationHeight
  183. };
  184. }
  185. - (void)addPageView
  186. {
  187. self.pageView = [[SegmentViewController alloc] init];
  188. NSArray * titleArray = @[@"全部",@"文章",@"书籍",@"视频",@"音频"];
  189. self.pageView.titleArray = titleArray;
  190. NSMutableArray *controlArray = [[NSMutableArray alloc]init];
  191. self.allVC = [[IndexAllVC alloc]init];
  192. [controlArray addObject:self.allVC];
  193. self.articeVC = [[IndexSubVC alloc]init];
  194. [controlArray addObject:self.articeVC];
  195. self.bookVC = [[IndexSubVC alloc]init];
  196. [controlArray addObject:self.bookVC];
  197. self.videoVC = [[IndexSubVC alloc]init];
  198. [controlArray addObject:self.videoVC];
  199. self.musicVC = [[IndexSubVC alloc]init];
  200. [controlArray addObject:self.musicVC];
  201. self.pageView.subViewControllers = controlArray;
  202. self.pageView.buttonWidth = self.view.frame.size.width / titleArray.count;
  203. self.pageView.buttonHeight = 34.f;
  204. self.pageView.bottomCount = titleArray.count;
  205. [self.pageView initSegment:0];
  206. [self.pageView addParentController:self];
  207. [self.pageView.view setFrame:CGRectMake(0, CGRectGetMaxY(self.topNavSearch.frame), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topNavSearch.frame))];
  208. }
  209. @end