口袋优选

CreatSearchManager.m 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // CreatSearchManager.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/24.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "CreatSearchManager.h"
  9. #import "KBFindSearchResultViewController.h"
  10. #import "KBGoodDetailViewController.h"
  11. #import "PYSearch.h"
  12. @interface CreatSearchManager ()<PYSearchViewControllerDelegate, PYSearchViewControllerDataSource>
  13. {
  14. PYSearchViewController *searchVc;
  15. }
  16. @property (nonatomic, strong) NSMutableArray *everyOneSearch;
  17. @property (nonatomic, strong) NSArray *hotSearchModelArray;
  18. @end
  19. static CreatSearchManager *_searchManager;
  20. @implementation CreatSearchManager
  21. + (instancetype)shareManager {
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. _searchManager = [[CreatSearchManager alloc] init];
  25. });
  26. return _searchManager;
  27. }
  28. - (void)dealloc {
  29. }
  30. - (void)loadHotText {
  31. NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/goods/everyoneSearch", BaseURL];
  32. [KBCacheHttp get:urlString params:nil success:^(id json, BOOL isCache) {
  33. [self.everyOneSearch removeAllObjects];
  34. // for (NSDictionary *dic in json) {
  35. // [self.everyOneSearch addObject:dic];
  36. // }
  37. self.hotSearchModelArray = [NSArray yy_modelArrayWithClass:[HotSearchModel class] json:json];
  38. for (HotSearchModel *model in self.hotSearchModelArray) {
  39. [self.everyOneSearch addObject:model.name];
  40. }
  41. } failure:^(NSError *error) {
  42. }];
  43. }
  44. - (void)createSearchViewControllerWith:(NSString *)text viewController:(UIViewController *)vc{
  45. [self loadHotText];
  46. PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:text];
  47. searchVc = searchViewController;
  48. // searchViewController.swapHotSeachWithSearchHistory = YES;
  49. searchViewController.searchHistoryStyle = PYSearchHistoryStyleARCBorderTag;
  50. searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag;
  51. searchViewController.searchTextField.font = [UIFont systemFontOfSize:13];
  52. searchViewController.searchTextField.enablesReturnKeyAutomatically = NO;
  53. searchViewController.delegate = self;
  54. [self getHotSearchToSearchViewController:searchViewController];
  55. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
  56. if (text.length > 0) {
  57. KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init];
  58. resultVC.searchBar = searchViewController.searchBar;
  59. resultVC.searchName = text;
  60. searchViewController.searchBar.text = text;
  61. searchViewController.searchSuggestions = nil;
  62. [nav pushViewController:resultVC animated:NO];
  63. }
  64. [vc presentViewController:nav animated:NO completion:^{
  65. }];
  66. searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
  67. //处理搜索点击事件
  68. [MobClick event:search_count label:Search_Normal];
  69. KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init];
  70. resultVC.searchBar = searchBar;
  71. resultVC.searchName = searchText;
  72. if (searchText.length == 0) {
  73. searchBar.text = searchBar.placeholder;
  74. resultVC.searchName = searchBar.placeholder;
  75. }
  76. searchViewController.searchSuggestions = nil;
  77. [nav pushViewController:resultVC animated:NO];
  78. };
  79. }
  80. /**
  81. 加载热门搜索
  82. */
  83. - (void)getHotSearchToSearchViewController:(PYSearchViewController *)searchViewController {
  84. searchViewController.hotSearchModelArray = self.hotSearchModelArray;
  85. searchViewController.hotSearches = self.everyOneSearch;
  86. }
  87. #pragma mark - PYSearchViewControllerDelegate
  88. /**
  89. 输入完成时触发
  90. */
  91. - (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
  92. {
  93. if (searchText.length) {
  94. [KBHttp get:SearchAdvice params:@{@"name":searchText} success:^(id json) {
  95. NSArray *list = json[@"data"];
  96. searchViewController.searchSuggestions = list;
  97. } failure:^(NSError *error) {
  98. }];
  99. }
  100. }
  101. /**
  102. 点击建议
  103. */
  104. - (void)searchViewController:(PYSearchViewController *)searchViewController
  105. didSelectSearchSuggestionAtIndex:(NSInteger)index
  106. searchText:(NSString *)searchText {
  107. KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init];
  108. resultVC.searchName = searchText;
  109. resultVC.searchBar = searchViewController.searchBar;
  110. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  111. }
  112. /**
  113. 点击热搜
  114. */
  115. - (void)searchViewController:(PYSearchViewController *)searchViewController
  116. didSelectHotSearchAtIndex:(NSInteger)index
  117. searchText:(NSString *)searchText {
  118. HotSearchModel *model = self.hotSearchModelArray[index];
  119. if ([model.type isEqualToString:@"0"]) {
  120. KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init];
  121. resultVC.searchName = model.name;
  122. resultVC.searchBar = searchViewController.searchBar;
  123. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  124. }else {
  125. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  126. DetailRequestModel *requestModel = [[DetailRequestModel alloc]
  127. initWithId:model.goods_id
  128. is_coupon:model.is_coupon
  129. coupon_price:model.coupon_price
  130. price:model.price
  131. discount_price:model.discount_price
  132. commission_rate:model.commission_rate
  133. coupon_start_time:model.coupon_start_time
  134. coupon_end_time:model.coupon_end_time];
  135. detail.requestModel = requestModel;
  136. [searchViewController.navigationController pushViewController:detail animated:YES];
  137. }
  138. [MobClick event:search_count label:Search_Hot_String];
  139. }
  140. /**
  141. 点击历史
  142. */
  143. - (void)searchViewController:(PYSearchViewController *)searchViewController
  144. didSelectSearchHistoryAtIndex:(NSInteger)index
  145. searchText:(NSString *)searchText {
  146. NSArray *searchHistory = [searchViewController getHistorySearchArray];
  147. HotSearchModel *model = searchHistory[index];
  148. if ([model.type isEqualToString:@"0"]) {
  149. KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init];
  150. resultVC.searchName = model.name;
  151. resultVC.searchBar = searchViewController.searchBar;
  152. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  153. }else {
  154. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  155. DetailRequestModel *requestModel = [[DetailRequestModel alloc]
  156. initWithId:model.goods_id
  157. is_coupon:model.is_coupon
  158. coupon_price:model.coupon_price
  159. price:model.price
  160. discount_price:model.discount_price
  161. commission_rate:model.commission_rate
  162. coupon_start_time:model.coupon_start_time
  163. coupon_end_time:model.coupon_end_time];
  164. detail.requestModel = requestModel;
  165. [searchViewController.navigationController pushViewController:detail animated:YES];
  166. }
  167. [MobClick event:search_count label:Search_History_String];
  168. }
  169. - (NSMutableArray *)everyOneSearch {
  170. if (!_everyOneSearch) {
  171. _everyOneSearch = [NSMutableArray array];
  172. }
  173. return _everyOneSearch;
  174. }
  175. @end