// // FKExploreKeywordController.m // FirstLink // // Created by ascii on 16/3/11. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKExploreKeywordController.h" #import "FKHistoryKeywordView.h" #import "FKQueryResultCell.h" #import "FKExploreConditionItem.h" #import "FKExploreKeywordRequest.h" #import "FKExploreKeywordReform.h" #import "FLExploreKeywordViewModel.h" #import "FKRemoteConfigManager.h" #import "SchemaManager.h" static NSInteger const ExploreQueryList = 3001; static NSInteger const ExploreQueryBrand = 3002; @interface FKExploreKeywordController () @property (nonatomic, strong) UIView *searchBarContainer; @property (nonatomic, strong) UISearchBar *searchBar; @property (nonatomic, strong) UIButton *cancelButton; @property (nonatomic, assign) FKExploreControllerType controllerType; @property (nonatomic, strong) FLExploreKeywordViewModel *viewModel; @property (nonatomic, strong) FKHistoryKeywordView *hisKeywordView; @property (nonatomic, strong) UITableView *queryResultView; @property (nonatomic, strong) NSArray *dataSource; @end static NSInteger const SEARCHBAR_NORMAL_STATE_LEFT_MARGIN = 12; static NSInteger const SEARCHBAR_NORMAL_STATE_RIGHT_MARGIN = 50; @implementation FKExploreKeywordController - (instancetype)initWithKeyword:(NSString *)keyword type:(FKExploreControllerType)controllerType { self = [super init]; if (self) { self.controllerType = controllerType; self.searchBar.text = keyword; if (controllerType == FKExploreControllerTypeLocalBrand) { self.queryResultView.hidden = NO; self.searchBar.placeholder = @"搜索你想要的品牌"; } else { self.searchBar.placeholder = [FKRemoteConfigManager searchPlaceholder]; } } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self addAllSubviews]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.searchBar becomeFirstResponder]; } #pragma mark - Response - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header { } - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header userInfo:(NSDictionary *)userInfo { if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == ExploreQueryList) { self.dataSource = [FKExploreKeywordReform parserQueryArray:response]; [self.queryResultView reloadData]; } else if (identify == ExploreQueryBrand) { self.dataSource = [FKExploreKeywordReform parserQueryArray:response]; [self.queryResultView reloadData]; } } } #pragma mark - Insert Brands - (NSInteger)updateLocalBrands:(NSArray *)brands { return 0; return [self.viewModel insertIntoBrandTable:brands]; } #pragma mark - UISearchBarDelegate - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { NSString *keyword = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; FKExploreConditionType conditionType = FKExploreConditionTypeKeyword; switch (self.controllerType) { case FKExploreControllerTypeLocalKeyword: case FKExploreControllerTypeRemoteKeyword: { keyword = [self singleSpaceSeparateString:keyword]; break; } case FKExploreControllerTypeLocalBrand: { conditionType = FKExploreConditionTypeBrand; break; } default: break; } if (keyword.length > 0) { [self.hisKeywordView insertHistoryKeyword:keyword]; [self dismissController]; self.executeSearchHandler(conditionType, nil, keyword); } else { if ([FKRemoteConfigManager defaultSearchTargetURL].length > 0) { [self dismissController]; NSURL *url = [NSURL URLWithString:[FKRemoteConfigManager defaultSearchTargetURL]]; [[SchemaManager sharedManager] parserURL:url shouldCache:NO]; } else if ([FKRemoteConfigManager defaultSearchKeyword].length > 0) { [self dismissController]; self.executeSearchHandler(FKExploreConditionTypeKeyword, nil, [FKRemoteConfigManager defaultSearchKeyword]); } else { [FLProgressHUDHelper showText:@"请输入搜索关键字" inView:self.view]; } } return NO; } return YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSString *keyword = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; [self executeQueryWithText:keyword]; } - (NSString *)singleSpaceSeparateString:(NSString *)keyword { if (keyword.length > 0) { NSString *nospaceWord; NSMutableArray *array = [NSMutableArray array]; for (NSString *word in [keyword componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) { nospaceWord = [word stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (nospaceWord.length > 0) { [array addObject:nospaceWord]; } } return [array componentsJoinedByString:@" "]; } return nil; } - (void)executeQueryWithText:(NSString *)text { if (self.controllerType == FKExploreControllerTypeLocalBrand) { self.queryResultView.hidden = NO; [FKExploreKeywordRequest requestQueryList:@"1" query:text identify:ExploreQueryBrand delegate:self]; } else if (self.controllerType == FKExploreControllerTypeRemoteKeyword) { if (text.length == 0) { self.queryResultView.hidden = YES; } else { self.queryResultView.hidden = NO; [FKExploreKeywordRequest requestQueryList:@"2" query:text identify:ExploreQueryList delegate:self]; } } else { self.queryResultView.hidden = YES; } } #pragma mark - UITableViewDelegate, UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FKQueryResultCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKQueryResultCell class])]; FKExploreConditionItem *item = [self itemAtIndex:indexPath.row]; if (item) { cell.titleLabel.text = item.name; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.queryResultView) { FKExploreConditionItem *item = [self itemAtIndex:indexPath.row]; if (item) { [self dismissController]; [self.hisKeywordView insertHistoryKeyword:item.name]; self.executeSearchHandler(FKExploreConditionTypeBrand, nil, item.name); } } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.searchBar resignFirstResponder]; } #pragma mark - FKHistoryKeywordViewDelegate - (void)historyKeywordViewDidScroll:(FKHistoryKeywordView *)hkView { [self.searchBar resignFirstResponder]; } - (void)historyKeywordView:(FKHistoryKeywordView *)hkView didSelectKeyword:(NSString *)keyword source:(FKKeywordSource)source { // if (source == FKKeywordSourceNormal) { [self.hisKeywordView insertHistoryKeyword:keyword]; // } [self dismissController]; self.executeSearchHandler(FKExploreConditionTypeKeyword, nil, keyword); } #pragma mark - Layout - (void)addAllSubviews { [self.view addSubview:self.searchBarContainer]; [self.searchBarContainer mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(SEARCHBAR_NORMAL_STATE_LEFT_MARGIN); make.top.equalTo(self.view).offset(IS_IPHONE_X ? 50 : 26); make.right.equalTo(self.view).offset(-SEARCHBAR_NORMAL_STATE_RIGHT_MARGIN); make.height.mas_equalTo(32); }]; [self.searchBarContainer addSubview:self.searchBar]; [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.searchBarContainer); make.top.equalTo(self.searchBarContainer).offset(1); make.height.mas_equalTo(30); }]; [self.view addSubview:self.cancelButton]; [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view).offset(-6); make.centerY.equalTo(self.searchBarContainer).offset(1); make.width.mas_equalTo(44); make.height.mas_equalTo(40); }]; [self.view addSubview:self.hisKeywordView]; [self.hisKeywordView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.top.equalTo(self.searchBarContainer.mas_bottom).offset(12); }]; [self.view addSubview:self.queryResultView]; [self.queryResultView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.top.equalTo(self.searchBarContainer.mas_bottom).offset(12); }]; } #pragma mark - Action - (IBAction)clickCancelButton:(id)sender { [self dismissController]; } - (void)dismissController { [self.searchBar resignFirstResponder]; [self dismissViewControllerAnimated:YES completion:^{}]; } #pragma mark - Method - (FKExploreConditionItem *)itemAtIndex:(NSInteger)index { if (index< self.dataSource.count) { return self.dataSource[index]; } return nil; } #pragma mark - Property - (FLExploreKeywordViewModel *)viewModel { if (!_viewModel) { _viewModel = [FLExploreKeywordViewModel new]; } return _viewModel; } - (UIView *)searchBarContainer { if (!_searchBarContainer) { _searchBarContainer = [UIView new]; _searchBarContainer.layer.cornerRadius = 6; _searchBarContainer.clipsToBounds = YES; _searchBarContainer.backgroundColor = UIColorFromRGB(0xf1f1f1); } return _searchBarContainer; } - (UISearchBar *)searchBar { if (!_searchBar) { _searchBar = [UISearchBar new]; _searchBar.delegate = self; _searchBar.returnKeyType = UIReturnKeySearch; _searchBar.searchBarStyle = UISearchBarStyleMinimal; _searchBar.enablesReturnKeyAutomatically = NO; _searchBar.backgroundColor = UIColorFromRGB(0xf1f1f1); [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBarBackground"] forState:UIControlStateNormal]; } return _searchBar; } - (FKHistoryKeywordView *)hisKeywordView { if (!_hisKeywordView) { _hisKeywordView = [FKHistoryKeywordView new]; _hisKeywordView.delegate = self; _hisKeywordView.viewModel = self.viewModel; } return _hisKeywordView; } - (UITableView *)queryResultView { if (!_queryResultView) { _queryResultView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _queryResultView.backgroundColor = UIColorFromRGB(0xf4f4f4); _queryResultView.hidden = YES; _queryResultView.dataSource = self; _queryResultView.delegate = self; _queryResultView.separatorStyle = UITableViewCellSeparatorStyleNone; [_queryResultView registerClass:[FKQueryResultCell class] forCellReuseIdentifier:NSStringFromClass([FKQueryResultCell class])]; } return _queryResultView; } - (UIButton *)cancelButton { if (!_cancelButton) { _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]]; [_cancelButton addTarget:self action:@selector(clickCancelButton:) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } @end