Ei kuvausta

FKExploreKeywordController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // FKExploreKeywordController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/3/11.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKExploreKeywordController.h"
  9. #import "FKHistoryKeywordView.h"
  10. #import "FKQueryResultCell.h"
  11. #import "FKExploreConditionItem.h"
  12. #import "FKExploreKeywordRequest.h"
  13. #import "FKExploreKeywordReform.h"
  14. #import "FLExploreKeywordViewModel.h"
  15. #import "FKRemoteConfigManager.h"
  16. #import "SchemaManager.h"
  17. static NSInteger const ExploreQueryList = 3001;
  18. static NSInteger const ExploreQueryBrand = 3002;
  19. @interface FKExploreKeywordController ()
  20. <UISearchBarDelegate, FKHistoryKeywordViewDelegate, UITableViewDelegate, UITableViewDataSource, FLNetworkDelegate>
  21. @property (nonatomic, strong) UIView *searchBarContainer;
  22. @property (nonatomic, strong) UISearchBar *searchBar;
  23. @property (nonatomic, strong) UIButton *cancelButton;
  24. @property (nonatomic, assign) FKExploreControllerType controllerType;
  25. @property (nonatomic, strong) FLExploreKeywordViewModel *viewModel;
  26. @property (nonatomic, strong) FKHistoryKeywordView *hisKeywordView;
  27. @property (nonatomic, strong) UITableView *queryResultView;
  28. @property (nonatomic, strong) NSArray *dataSource;
  29. @end
  30. static NSInteger const SEARCHBAR_NORMAL_STATE_LEFT_MARGIN = 12;
  31. static NSInteger const SEARCHBAR_NORMAL_STATE_RIGHT_MARGIN = 50;
  32. @implementation FKExploreKeywordController
  33. - (instancetype)initWithKeyword:(NSString *)keyword type:(FKExploreControllerType)controllerType {
  34. self = [super init];
  35. if (self) {
  36. self.controllerType = controllerType;
  37. self.searchBar.text = keyword;
  38. if (controllerType == FKExploreControllerTypeLocalBrand) {
  39. self.queryResultView.hidden = NO;
  40. self.searchBar.placeholder = @"搜索你想要的品牌";
  41. } else {
  42. self.searchBar.placeholder = [FKRemoteConfigManager searchPlaceholder];
  43. }
  44. }
  45. return self;
  46. }
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. self.view.backgroundColor = [UIColor whiteColor];
  50. [self addAllSubviews];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated {
  53. [super viewDidAppear:animated];
  54. [self.searchBar becomeFirstResponder];
  55. }
  56. #pragma mark - Response
  57. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header {
  58. }
  59. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header userInfo:(NSDictionary *)userInfo {
  60. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  61. if (identify == ExploreQueryList) {
  62. self.dataSource = [FKExploreKeywordReform parserQueryArray:response];
  63. [self.queryResultView reloadData];
  64. } else if (identify == ExploreQueryBrand) {
  65. self.dataSource = [FKExploreKeywordReform parserQueryArray:response];
  66. [self.queryResultView reloadData];
  67. }
  68. }
  69. }
  70. #pragma mark - Insert Brands
  71. - (NSInteger)updateLocalBrands:(NSArray *)brands {
  72. return 0;
  73. return [self.viewModel insertIntoBrandTable:brands];
  74. }
  75. #pragma mark - UISearchBarDelegate
  76. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  77. if ([text isEqualToString:@"\n"]) {
  78. NSString *keyword = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  79. FKExploreConditionType conditionType = FKExploreConditionTypeKeyword;
  80. switch (self.controllerType) {
  81. case FKExploreControllerTypeLocalKeyword:
  82. case FKExploreControllerTypeRemoteKeyword: {
  83. keyword = [self singleSpaceSeparateString:keyword];
  84. break;
  85. }
  86. case FKExploreControllerTypeLocalBrand: {
  87. conditionType = FKExploreConditionTypeBrand;
  88. break;
  89. }
  90. default:
  91. break;
  92. }
  93. if (keyword.length > 0) {
  94. [self.hisKeywordView insertHistoryKeyword:keyword];
  95. [self dismissController];
  96. self.executeSearchHandler(conditionType, nil, keyword);
  97. } else {
  98. if ([FKRemoteConfigManager defaultSearchTargetURL].length > 0) {
  99. [self dismissController];
  100. NSURL *url = [NSURL URLWithString:[FKRemoteConfigManager defaultSearchTargetURL]];
  101. [[SchemaManager sharedManager] parserURL:url shouldCache:NO];
  102. } else if ([FKRemoteConfigManager defaultSearchKeyword].length > 0) {
  103. [self dismissController];
  104. self.executeSearchHandler(FKExploreConditionTypeKeyword, nil, [FKRemoteConfigManager defaultSearchKeyword]);
  105. } else {
  106. [FLProgressHUDHelper showText:@"请输入搜索关键字" inView:self.view];
  107. }
  108. }
  109. return NO;
  110. }
  111. return YES;
  112. }
  113. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  114. NSString *keyword = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  115. [self executeQueryWithText:keyword];
  116. }
  117. - (NSString *)singleSpaceSeparateString:(NSString *)keyword {
  118. if (keyword.length > 0) {
  119. NSString *nospaceWord;
  120. NSMutableArray *array = [NSMutableArray array];
  121. for (NSString *word in [keyword componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
  122. nospaceWord = [word stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  123. if (nospaceWord.length > 0) {
  124. [array addObject:nospaceWord];
  125. }
  126. }
  127. return [array componentsJoinedByString:@" "];
  128. }
  129. return nil;
  130. }
  131. - (void)executeQueryWithText:(NSString *)text {
  132. if (self.controllerType == FKExploreControllerTypeLocalBrand) {
  133. self.queryResultView.hidden = NO;
  134. [FKExploreKeywordRequest requestQueryList:@"1"
  135. query:text
  136. identify:ExploreQueryBrand
  137. delegate:self];
  138. } else if (self.controllerType == FKExploreControllerTypeRemoteKeyword) {
  139. if (text.length == 0) {
  140. self.queryResultView.hidden = YES;
  141. } else {
  142. self.queryResultView.hidden = NO;
  143. [FKExploreKeywordRequest requestQueryList:@"2"
  144. query:text
  145. identify:ExploreQueryList
  146. delegate:self];
  147. }
  148. } else {
  149. self.queryResultView.hidden = YES;
  150. }
  151. }
  152. #pragma mark - UITableViewDelegate, UITableViewDataSource
  153. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  154. return self.dataSource.count;
  155. }
  156. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  157. FKQueryResultCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKQueryResultCell class])];
  158. FKExploreConditionItem *item = [self itemAtIndex:indexPath.row];
  159. if (item) {
  160. cell.titleLabel.text = item.name;
  161. }
  162. return cell;
  163. }
  164. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  165. if (tableView == self.queryResultView) {
  166. FKExploreConditionItem *item = [self itemAtIndex:indexPath.row];
  167. if (item) {
  168. [self dismissController];
  169. [self.hisKeywordView insertHistoryKeyword:item.name];
  170. self.executeSearchHandler(FKExploreConditionTypeBrand, nil, item.name);
  171. }
  172. }
  173. }
  174. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  175. [self.searchBar resignFirstResponder];
  176. }
  177. #pragma mark - FKHistoryKeywordViewDelegate
  178. - (void)historyKeywordViewDidScroll:(FKHistoryKeywordView *)hkView {
  179. [self.searchBar resignFirstResponder];
  180. }
  181. - (void)historyKeywordView:(FKHistoryKeywordView *)hkView didSelectKeyword:(NSString *)keyword source:(FKKeywordSource)source {
  182. // if (source == FKKeywordSourceNormal) {
  183. [self.hisKeywordView insertHistoryKeyword:keyword];
  184. // }
  185. [self dismissController];
  186. self.executeSearchHandler(FKExploreConditionTypeKeyword, nil, keyword);
  187. }
  188. #pragma mark - Layout
  189. - (void)addAllSubviews {
  190. [self.view addSubview:self.searchBarContainer];
  191. [self.searchBarContainer mas_makeConstraints:^(MASConstraintMaker *make) {
  192. make.left.equalTo(self.view).offset(SEARCHBAR_NORMAL_STATE_LEFT_MARGIN);
  193. make.top.equalTo(self.view).offset(IS_IPHONE_X ? 50 : 26);
  194. make.right.equalTo(self.view).offset(-SEARCHBAR_NORMAL_STATE_RIGHT_MARGIN);
  195. make.height.mas_equalTo(32);
  196. }];
  197. [self.searchBarContainer addSubview:self.searchBar];
  198. [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
  199. make.left.right.equalTo(self.searchBarContainer);
  200. make.top.equalTo(self.searchBarContainer).offset(1);
  201. make.height.mas_equalTo(30);
  202. }];
  203. [self.view addSubview:self.cancelButton];
  204. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  205. make.right.equalTo(self.view).offset(-6);
  206. make.centerY.equalTo(self.searchBarContainer).offset(1);
  207. make.width.mas_equalTo(44);
  208. make.height.mas_equalTo(40);
  209. }];
  210. [self.view addSubview:self.hisKeywordView];
  211. [self.hisKeywordView mas_makeConstraints:^(MASConstraintMaker *make) {
  212. make.left.right.bottom.equalTo(self.view);
  213. make.top.equalTo(self.searchBarContainer.mas_bottom).offset(12);
  214. }];
  215. [self.view addSubview:self.queryResultView];
  216. [self.queryResultView mas_makeConstraints:^(MASConstraintMaker *make) {
  217. make.left.right.bottom.equalTo(self.view);
  218. make.top.equalTo(self.searchBarContainer.mas_bottom).offset(12);
  219. }];
  220. }
  221. #pragma mark - Action
  222. - (IBAction)clickCancelButton:(id)sender {
  223. [self dismissController];
  224. }
  225. - (void)dismissController {
  226. [self.searchBar resignFirstResponder];
  227. [self dismissViewControllerAnimated:YES
  228. completion:^{}];
  229. }
  230. #pragma mark - Method
  231. - (FKExploreConditionItem *)itemAtIndex:(NSInteger)index {
  232. if (index< self.dataSource.count) {
  233. return self.dataSource[index];
  234. }
  235. return nil;
  236. }
  237. #pragma mark - Property
  238. - (FLExploreKeywordViewModel *)viewModel {
  239. if (!_viewModel) {
  240. _viewModel = [FLExploreKeywordViewModel new];
  241. }
  242. return _viewModel;
  243. }
  244. - (UIView *)searchBarContainer {
  245. if (!_searchBarContainer) {
  246. _searchBarContainer = [UIView new];
  247. _searchBarContainer.layer.cornerRadius = 6;
  248. _searchBarContainer.clipsToBounds = YES;
  249. _searchBarContainer.backgroundColor = UIColorFromRGB(0xf1f1f1);
  250. }
  251. return _searchBarContainer;
  252. }
  253. - (UISearchBar *)searchBar {
  254. if (!_searchBar) {
  255. _searchBar = [UISearchBar new];
  256. _searchBar.delegate = self;
  257. _searchBar.returnKeyType = UIReturnKeySearch;
  258. _searchBar.searchBarStyle = UISearchBarStyleMinimal;
  259. _searchBar.enablesReturnKeyAutomatically = NO;
  260. _searchBar.backgroundColor = UIColorFromRGB(0xf1f1f1);
  261. [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBarBackground"] forState:UIControlStateNormal];
  262. }
  263. return _searchBar;
  264. }
  265. - (FKHistoryKeywordView *)hisKeywordView {
  266. if (!_hisKeywordView) {
  267. _hisKeywordView = [FKHistoryKeywordView new];
  268. _hisKeywordView.delegate = self;
  269. _hisKeywordView.viewModel = self.viewModel;
  270. }
  271. return _hisKeywordView;
  272. }
  273. - (UITableView *)queryResultView {
  274. if (!_queryResultView) {
  275. _queryResultView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  276. _queryResultView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  277. _queryResultView.hidden = YES;
  278. _queryResultView.dataSource = self;
  279. _queryResultView.delegate = self;
  280. _queryResultView.separatorStyle = UITableViewCellSeparatorStyleNone;
  281. [_queryResultView registerClass:[FKQueryResultCell class] forCellReuseIdentifier:NSStringFromClass([FKQueryResultCell class])];
  282. }
  283. return _queryResultView;
  284. }
  285. - (UIButton *)cancelButton {
  286. if (!_cancelButton) {
  287. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  288. [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  289. [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  290. [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
  291. [_cancelButton addTarget:self action:@selector(clickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
  292. }
  293. return _cancelButton;
  294. }
  295. @end