省钱达人

CreatSearchManager.m 5.8KB

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