猎豆优选

CreatSearchManager.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "LDFindSearchResultViewController.h"
  10. #import "PYSearch.h"
  11. @interface CreatSearchManager ()<PYSearchViewControllerDelegate, PYSearchViewControllerDataSource>
  12. {
  13. PYSearchViewController *searchVc;
  14. }
  15. @property (nonatomic, strong) NSMutableArray *everyOneSearch;
  16. @property (nonatomic, strong) NSArray *hotSearchModelArray;
  17. @end
  18. @implementation CreatSearchManager
  19. - (void)loadHotText {
  20. NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/goods/everyoneSearch", BaseURL];
  21. [LDCacheHttp get:urlString params:nil success:^(id json, BOOL isCache) {
  22. [self.everyOneSearch removeAllObjects];
  23. // for (NSDictionary *dic in json) {
  24. // [self.everyOneSearch addObject:dic];
  25. // }
  26. self.hotSearchModelArray = [NSArray yy_modelArrayWithClass:[HotSearchModel class] json:json];
  27. for (HotSearchModel *model in self.hotSearchModelArray) {
  28. [self.everyOneSearch addObject:model.name];
  29. }
  30. } failure:^(NSError *error) {
  31. }];
  32. }
  33. - (void)createSearchViewControllerWith:(NSString *)text viewController:(UIViewController *)vc{
  34. [self loadHotText];
  35. PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:text];
  36. searchVc = searchViewController;
  37. // searchViewController.swapHotSeachWithSearchHistory = YES;
  38. searchViewController.searchHistoryStyle = PYSearchHistoryStyleARCBorderTag;
  39. searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag;
  40. searchViewController.searchTextField.font = [UIFont systemFontOfSize:13];
  41. searchViewController.searchTextField.enablesReturnKeyAutomatically = NO;
  42. searchViewController.delegate = self;
  43. [self getHotSearchToSearchViewController:searchViewController];
  44. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
  45. LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController alloc] init];
  46. resultVC.searchBar = searchViewController.searchBar;
  47. resultVC.searchName = text;
  48. searchViewController.searchBar.text = text;
  49. searchViewController.searchSuggestions = nil;
  50. [nav pushViewController:resultVC animated:NO];
  51. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  52. [vc presentViewController:nav animated:NO completion:^{
  53. }];
  54. searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
  55. //处理搜索点击事件
  56. [MobClick event:search_count label:Search_Normal];
  57. LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController alloc] init];
  58. resultVC.searchBar = searchBar;
  59. resultVC.searchName = searchText;
  60. if (searchText.length == 0) {
  61. searchBar.text = searchBar.placeholder;
  62. resultVC.searchName = searchBar.placeholder;
  63. }
  64. searchViewController.searchSuggestions = nil;
  65. [nav pushViewController:resultVC animated:NO];
  66. };
  67. }
  68. /**
  69. 加载热门搜索
  70. */
  71. - (void)getHotSearchToSearchViewController:(PYSearchViewController *)searchViewController {
  72. searchViewController.hotSearchModelArray = self.hotSearchModelArray;
  73. searchViewController.hotSearches = self.everyOneSearch;
  74. }
  75. #pragma mark - PYSearchViewControllerDelegate
  76. /**
  77. 输入完成时触发
  78. */
  79. - (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
  80. {
  81. if (searchText.length) {
  82. [LDHttp get:SearchAdvice params:@{@"name":searchText} success:^(id json) {
  83. NSArray *list = json[@"data"];
  84. searchViewController.searchSuggestions = list;
  85. } failure:^(NSError *error) {
  86. }];
  87. }
  88. }
  89. /**
  90. 点击建议
  91. */
  92. - (void)searchViewController:(PYSearchViewController *)searchViewController
  93. didSelectSearchSuggestionAtIndex:(NSInteger)index
  94. searchText:(NSString *)searchText {
  95. LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController alloc] init];
  96. resultVC.searchName = searchText;
  97. resultVC.searchBar = searchViewController.searchBar;
  98. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  99. }
  100. /**
  101. 点击热搜
  102. */
  103. - (void)searchViewController:(PYSearchViewController *)searchViewController
  104. didSelectHotSearchAtIndex:(NSInteger)index
  105. searchText:(NSString *)searchText {
  106. LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController alloc] init];
  107. resultVC.searchName = searchText;
  108. resultVC.searchBar = searchViewController.searchBar;
  109. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  110. [MobClick event:search_count label:Search_Hot_String];
  111. }
  112. /**
  113. 点击历史
  114. */
  115. - (void)searchViewController:(PYSearchViewController *)searchViewController
  116. didSelectSearchHistoryAtIndex:(NSInteger)index
  117. searchText:(NSString *)searchText {
  118. LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController alloc] init];
  119. resultVC.searchName = searchText;
  120. resultVC.searchBar = searchViewController.searchBar;
  121. [searchViewController.navigationController pushViewController:resultVC animated:NO];
  122. [MobClick event:search_count label:Search_History_String];
  123. }
  124. - (NSMutableArray *)everyOneSearch {
  125. if (!_everyOneSearch) {
  126. _everyOneSearch = [NSMutableArray array];
  127. }
  128. return _everyOneSearch;
  129. }
  130. @end