123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- //
- // 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 ()
- <UISearchBarDelegate, FKHistoryKeywordViewDelegate, UITableViewDelegate, UITableViewDataSource, FLNetworkDelegate>
- @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
|