// // CreatSearchManager.m // YouHuiProject // // Created by 小花 on 2018/7/24. // Copyright © 2018年 kuxuan. All rights reserved. // #import "CreatSearchManager.h" #import "KBFindSearchResultViewController.h" #import "KBGoodDetailViewController.h" #import "PYSearch.h" @interface CreatSearchManager () { PYSearchViewController *searchVc; } @property (nonatomic, strong) NSMutableArray *everyOneSearch; @property (nonatomic, strong) NSArray *hotSearchModelArray; @end static CreatSearchManager *_searchManager; @implementation CreatSearchManager + (instancetype)shareManager { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _searchManager = [[CreatSearchManager alloc] init]; }); return _searchManager; } - (void)dealloc { } - (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]; // } self.hotSearchModelArray = [NSArray yy_modelArrayWithClass:[HotSearchModel class] json:json]; for (HotSearchModel *model in self.hotSearchModelArray) { [self.everyOneSearch addObject:model.name]; } } 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]; if (text.length > 0) { 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:^{ }]; 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 { searchViewController.hotSearchModelArray = self.hotSearchModelArray; searchViewController.hotSearches = self.everyOneSearch; } #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 { HotSearchModel *model = self.hotSearchModelArray[index]; if ([model.type isEqualToString:@"0"]) { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = model.name; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; }else { KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time]; detail.requestModel = requestModel; [searchViewController.navigationController pushViewController:detail animated:YES]; } [MobClick event:search_count label:Search_Hot_String]; } /** 点击历史 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchHistoryAtIndex:(NSInteger)index searchText:(NSString *)searchText { NSArray *searchHistory = [searchViewController getHistorySearchArray]; HotSearchModel *model = searchHistory[index]; if ([model.type isEqualToString:@"0"]) { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = model.name; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; }else { KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time]; detail.requestModel = requestModel; [searchViewController.navigationController pushViewController:detail animated:YES]; } [MobClick event:search_count label:Search_History_String]; } - (NSMutableArray *)everyOneSearch { if (!_everyOneSearch) { _everyOneSearch = [NSMutableArray array]; } return _everyOneSearch; } @end