// // CreatSearchManager.m // YouHuiProject // // Created by 小花 on 2018/7/24. // Copyright © 2018年 kuxuan. All rights reserved. // #import "CreatSearchManager.h" #import "KBFindSearchResultViewController.h" #import "PYSearch.h" @interface CreatSearchManager () { PYSearchViewController *searchVc; } @property (nonatomic, strong) NSMutableArray *everyOneSearch; @end @implementation CreatSearchManager - (void)loadHotText { NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/goods/everyoneSearch", BaseURL]; [KBCacheHttp get:urlString params:nil success:^(id json, BOOL isCache) { [self.everyOneSearch removeAllObjects]; for (NSDictionary *dic in json) { [self.everyOneSearch addObject:dic]; } } failure:^(NSError *error) { }]; } - (void)createSearchViewControllerWith:(NSString *)text viewController:(UIViewController *)vc{ [self loadHotText]; PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:text]; searchVc = searchViewController; // searchViewController.swapHotSeachWithSearchHistory = YES; searchViewController.searchHistoryStyle = PYSearchHistoryStyleARCBorderTag; searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag; searchViewController.searchTextField.font = [UIFont systemFontOfSize:13]; searchViewController.searchTextField.enablesReturnKeyAutomatically = NO; searchViewController.delegate = self; [self getHotSearchToSearchViewController:searchViewController]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController]; KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchBar = searchViewController.searchBar; resultVC.searchName = text; searchViewController.searchBar.text = text; searchViewController.searchSuggestions = nil; [nav pushViewController:resultVC animated:NO]; [vc presentViewController:nav animated:NO completion:^{ for (int i = 0; i < searchViewController.hotSearchTags.count; i++) { UILabel *tag = searchViewController.hotSearchTags[i]; NSDictionary *dic = self.everyOneSearch[i]; if ([dic[@"color"] boolValue]) { tag.layer.borderColor = [UIColor homeRedColor].CGColor; tag.textColor = [UIColor homeRedColor]; } } }]; searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) { //处理搜索点击事件 [MobClick event:search_count label:Search_Normal]; KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchBar = searchBar; resultVC.searchName = searchText; if (searchText.length == 0) { searchBar.text = searchBar.placeholder; resultVC.searchName = searchBar.placeholder; } searchViewController.searchSuggestions = nil; [nav pushViewController:resultVC animated:NO]; }; } /** 加载热门搜索 */ - (void)getHotSearchToSearchViewController:(PYSearchViewController *)searchViewController { NSMutableArray *hotSearch = [NSMutableArray array]; for (NSDictionary *dic in self.everyOneSearch) { [hotSearch addObject:dic[@"name"]]; } searchViewController.hotSearches = hotSearch; } #pragma mark - PYSearchViewControllerDelegate /** 输入完成时触发 */ - (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText { if (searchText.length) { [KBHttp get:SearchAdvice params:@{@"name":searchText} success:^(id json) { NSArray *list = json[@"data"]; searchViewController.searchSuggestions = list; } failure:^(NSError *error) { }]; } } /** 点击建议 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; } /** 点击热搜 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectHotSearchAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; [MobClick event:search_count label:Search_Hot_String]; } /** 点击历史 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchHistoryAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; [MobClick event:search_count label:Search_History_String]; } - (NSMutableArray *)everyOneSearch { if (!_everyOneSearch) { _everyOneSearch = [NSMutableArray array]; } return _everyOneSearch; } @end