ASSearchViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // ASSearchViewController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/7/10.
  6. //
  7. #import "ASSearchViewController.h"
  8. #import "KWHisAndHotWordsView.h"
  9. #import "KWLenovoWordListView.h"
  10. @interface ASSearchViewController () <UITextFieldDelegate>
  11. @property (nonatomic, strong) UITextField *searchTf;
  12. @property (nonatomic, strong) KWHisAndHotWordsView *hisWordV;
  13. @property (nonatomic, strong) KWLenovoWordListView *lenovoV;
  14. @property (nonatomic, strong) UIButton *cusBackBt;
  15. @end
  16. @implementation ASSearchViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. [self loadSubVs];
  20. [self.hisWordV reloadData];
  21. }
  22. - (void)loadSubVs {
  23. self.customNavBar.backgroundColor = _E0FFF5;
  24. self.statusBgV.backgroundColor = _E0FFF5;
  25. self.customNavBar.frame = CGRectMake(0, kStatusBarH, KScreenWidth, 70);
  26. self.nav_bottomLineV.frame = CGRectMake(0, 70-1, KScreenWidth, 1);
  27. [self.customNavBar addSubview:self.cusBackBt];
  28. [self.customNavBar addSubview:self.searchTf];
  29. [self.cusBackBt mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.leading.equalTo(self.customNavBar).offset(10);
  31. make.centerY.equalTo(self.customNavBar);
  32. make.width.height.equalTo(@44);
  33. }];
  34. [self.searchTf mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.left.equalTo(self.cusBackBt.mas_right).offset(10);
  36. make.right.equalTo(self.customNavBar).offset(-20);
  37. make.height.equalTo(@45);
  38. make.top.equalTo(self.customNavBar).offset(13);
  39. make.bottom.equalTo(self.customNavBar).offset(-12);
  40. }];
  41. [self.view addSubview:self.hisWordV];
  42. [self.hisWordV mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.leading.trailing.bottom.equalTo(self.view);
  44. make.top.equalTo(self.customNavBar.mas_bottom);
  45. }];
  46. [self.view addSubview:self.lenovoV];
  47. self.lenovoV.hidden = true;
  48. [self.lenovoV mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.leading.trailing.equalTo(self.searchTf);
  50. make.top.equalTo(self.searchTf.mas_bottom);
  51. make.height.equalTo(@0);
  52. }];
  53. }
  54. // MARK: - subVs
  55. -(UITextField *)searchTf {
  56. if (!_searchTf) {
  57. UITextField *tf = [[UITextField alloc] init];
  58. tf.delegate = self;
  59. tf.borderStyle = UITextBorderStyleNone;
  60. tf.backgroundColor = UIColor.whiteColor;
  61. tf.font = [UIFont fontWithName:Rob_Regular size:14];
  62. tf.textAlignment = NSTextAlignmentLeft;
  63. tf.placeholder = @"SEARCH FOR PRODUCTS";
  64. tf.textColor = _0B0B0B;
  65. tf.layer.masksToBounds = true;
  66. tf.clearButtonMode = UITextFieldViewModeAlways;
  67. UIView *tfLeftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 45)];
  68. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  69. btn.frame = tfLeftV.bounds;
  70. [btn setImage:[UIImage imageNamed:@"search_tf"] forState:UIControlStateNormal];
  71. [tfLeftV addSubview:btn];
  72. tf.leftView = tfLeftV;
  73. tf.leftViewMode = UITextFieldViewModeAlways;
  74. _searchTf = tf;
  75. }
  76. return _searchTf;
  77. }
  78. -(KWHisAndHotWordsView *)hisWordV {
  79. if (!_hisWordV) {
  80. KWHisAndHotWordsView *v = [[KWHisAndHotWordsView alloc] initWithFrame:CGRectZero];
  81. v.isSearch = true;
  82. @weakify(self);
  83. [v setCallBack:^(NSString * _Nonnull word) {
  84. weak_self.searchTf.text = word;
  85. [self.hisWordV.vm addSearchData:word];
  86. [weak_self toResult:word type:@"" keyWord:word];
  87. }];
  88. [v setHotCallBack:^(KWSearchHotKeyModel * _Nonnull hot) {
  89. NSString *word = hot.title;
  90. if (hot.code != nil && ![hot.code isEqualToString:@""]) {
  91. [self.hisWordV.vm addSearchData:word];
  92. [weak_self toResult:word type:hot.code keyWord:@""];
  93. return;
  94. }
  95. [self.hisWordV.vm addSearchData:word];
  96. [weak_self toResult:word type:@"" keyWord:word];
  97. }];
  98. _hisWordV = v;
  99. }
  100. return _hisWordV;
  101. }
  102. - (KWLenovoWordListView *)lenovoV {
  103. if (!_lenovoV) {
  104. KWLenovoWordListView *v = [[KWLenovoWordListView alloc] initWithFrame:CGRectMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8, self.searchTf.frame.size.width, 200)];
  105. @weakify(self);
  106. [v setCallBack:^(NSString * _Nonnull word) {
  107. weak_self.searchTf.text = word;
  108. [self.hisWordV.vm addSearchData:word];
  109. [weak_self toResult:word type:@"" keyWord:word];
  110. }];
  111. _lenovoV = v;
  112. }
  113. return _lenovoV;
  114. }
  115. - (UIButton *)cusBackBt {
  116. if (!_cusBackBt) {
  117. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  118. [bt setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
  119. [bt addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  120. _cusBackBt = bt;
  121. }
  122. return _cusBackBt;
  123. }
  124. // MARK: - events
  125. /// show lenovo Words
  126. - (void) showLenovoV:(NSString *)key {
  127. // self.lenovoV.hidden = false;
  128. // [self.lenovoV setData:@[@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",] origen:CGPointMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8)];
  129. __weak typeof(self) weak_self = self;
  130. [self.hisWordV.vm getAboutKeyList:key back:^{
  131. if (weak_self.hisWordV.vm.aboutKeys.count > 0 && [self.searchTf.text isEqualToString:key]) {
  132. weak_self.lenovoV.hidden = false;
  133. [weak_self.lenovoV setData:weak_self.hisWordV.vm.aboutKeys origen:CGPointMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8)];
  134. } else {
  135. weak_self.lenovoV.hidden = true;
  136. }
  137. }];
  138. }
  139. - (void)hideLenovoV {
  140. self.lenovoV.hidden = true;
  141. }
  142. // MARK: - pushResult
  143. - (void)toResult:(NSString *)key type:(NSString *)type keyWord:(NSString *)word {
  144. NSDictionary *para = @{
  145. @"type":type, //String
  146. @"title":key,//String
  147. @"searchKey":word,//String
  148. };
  149. UIViewController *vc = [CTMediator.sharedInstance getProductListVc:para];
  150. [self.navigationController pushViewController:vc animated:true];
  151. }
  152. - (void)backAction {
  153. [self dismissViewControllerAnimated:true completion:nil];
  154. }
  155. // MARK: - uitextfiled delegate
  156. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  157. NSString *txt = textField.text;
  158. if ([txt isEqualToString:@""] ) {
  159. [self hideLenovoV];
  160. } else {
  161. [self showLenovoV:txt];
  162. }
  163. return true;
  164. }
  165. - (BOOL)textFieldShouldClear:(UITextField *)textField {
  166. // self.hisVm.aboutKeys = @[];
  167. [self hideLenovoV];
  168. return true;
  169. }
  170. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  171. NSString *txt = textField.text;
  172. txt = [txt stringByReplacingCharactersInRange:range withString:string];
  173. if ([txt isEqualToString:@""] ) {
  174. [self hideLenovoV];
  175. } else {
  176. [self showLenovoV:txt];
  177. }
  178. return true;
  179. }
  180. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  181. return true;
  182. }
  183. - (void)textFieldDidEndEditing:(UITextField *)textField {
  184. [self hideLenovoV];
  185. }
  186. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  187. [textField resignFirstResponder];
  188. NSString *key = textField.text;
  189. if (![key isEmpty]){
  190. [self.hisWordV.vm addSearchData:key];
  191. [self toResult:key type:@"" keyWord:key];
  192. }
  193. return true;
  194. }
  195. @end