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