// // ASSearchViewController.m // Asteria // // Created by iOS on 2023/7/10. // #import "ASSearchViewController.h" #import "KWHisAndHotWordsView.h" #import "KWLenovoWordListView.h" @interface ASSearchViewController () @property (nonatomic, strong) UITextField *searchTf; @property (nonatomic, strong) KWHisAndHotWordsView *hisWordV; @property (nonatomic, strong) KWLenovoWordListView *lenovoV; @property (nonatomic, strong) UIButton *cusBackBt; @end @implementation ASSearchViewController - (void)viewDidLoad { [super viewDidLoad]; [self loadSubVs]; [self.hisWordV reloadData]; } - (void)loadSubVs { self.customNavBar.backgroundColor = _E0FFF5; self.statusBgV.backgroundColor = _E0FFF5; self.customNavBar.frame = CGRectMake(0, kStatusBarH, KScreenWidth, 70); self.nav_bottomLineV.frame = CGRectMake(0, 70-1, KScreenWidth, 1); [self.customNavBar addSubview:self.cusBackBt]; [self.customNavBar addSubview:self.searchTf]; [self.cusBackBt mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.customNavBar).offset(10); make.centerY.equalTo(self.customNavBar); make.width.height.equalTo(@44); }]; [self.searchTf mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.cusBackBt.mas_right).offset(10); make.right.equalTo(self.customNavBar).offset(-20); make.height.equalTo(@45); make.top.equalTo(self.customNavBar).offset(13); make.bottom.equalTo(self.customNavBar).offset(-12); }]; [self.view addSubview:self.hisWordV]; [self.hisWordV mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.bottom.equalTo(self.view); make.top.equalTo(self.customNavBar.mas_bottom); }]; [self.view addSubview:self.lenovoV]; self.lenovoV.hidden = true; [self.lenovoV mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.searchTf); make.top.equalTo(self.searchTf.mas_bottom); make.height.equalTo(@0); }]; } // MARK: - subVs -(UITextField *)searchTf { if (!_searchTf) { UITextField *tf = [[UITextField alloc] init]; tf.delegate = self; tf.borderStyle = UITextBorderStyleNone; tf.backgroundColor = UIColor.whiteColor; tf.font = [UIFont fontWithName:Rob_Regular size:14]; tf.textAlignment = NSTextAlignmentLeft; tf.placeholder = @"SEARCH FOR PRODUCTS"; tf.textColor = _0B0B0B; tf.layer.masksToBounds = true; tf.clearButtonMode = UITextFieldViewModeAlways; UIView *tfLeftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 45)]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = tfLeftV.bounds; [btn setImage:[UIImage imageNamed:@"search_tf"] forState:UIControlStateNormal]; [tfLeftV addSubview:btn]; tf.leftView = tfLeftV; tf.leftViewMode = UITextFieldViewModeAlways; _searchTf = tf; } return _searchTf; } -(KWHisAndHotWordsView *)hisWordV { if (!_hisWordV) { KWHisAndHotWordsView *v = [[KWHisAndHotWordsView alloc] initWithFrame:CGRectZero]; v.isSearch = true; @weakify(self); [v setCallBack:^(NSString * _Nonnull word) { weak_self.searchTf.text = word; [self.hisWordV.vm addSearchData:word]; [weak_self toResult:word type:@"" keyWord:word]; }]; [v setHotCallBack:^(KWSearchHotKeyModel * _Nonnull hot) { NSString *word = hot.title; if (hot.code != nil && ![hot.code isEqualToString:@""]) { [self.hisWordV.vm addSearchData:word]; [weak_self toResult:word type:hot.code keyWord:@""]; return; } [self.hisWordV.vm addSearchData:word]; [weak_self toResult:word type:@"" keyWord:word]; }]; _hisWordV = v; } return _hisWordV; } - (KWLenovoWordListView *)lenovoV { if (!_lenovoV) { KWLenovoWordListView *v = [[KWLenovoWordListView alloc] initWithFrame:CGRectMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8, self.searchTf.frame.size.width, 200)]; @weakify(self); [v setCallBack:^(NSString * _Nonnull word) { weak_self.searchTf.text = word; [self.hisWordV.vm addSearchData:word]; [weak_self toResult:word type:@"" keyWord:word]; }]; _lenovoV = v; } return _lenovoV; } - (UIButton *)cusBackBt { if (!_cusBackBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal]; [bt addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; _cusBackBt = bt; } return _cusBackBt; } // MARK: - events /// show lenovo Words - (void) showLenovoV:(NSString *)key { // self.lenovoV.hidden = false; // [self.lenovoV setData:@[@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",@"124356",] origen:CGPointMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8)]; __weak typeof(self) weak_self = self; [self.hisWordV.vm getAboutKeyList:key back:^{ if (weak_self.hisWordV.vm.aboutKeys.count > 0 && [self.searchTf.text isEqualToString:key]) { weak_self.lenovoV.hidden = false; [weak_self.lenovoV setData:weak_self.hisWordV.vm.aboutKeys origen:CGPointMake(self.searchTf.frame.origin.x, self.customNavBar.maxY - 8)]; } else { weak_self.lenovoV.hidden = true; } }]; } - (void)hideLenovoV { self.lenovoV.hidden = true; } // MARK: - pushResult - (void)toResult:(NSString *)key type:(NSString *)type keyWord:(NSString *)word { NSDictionary *para = @{ @"type":type, //String @"title":key,//String @"searchKey":word,//String }; UIViewController *vc = [CTMediator.sharedInstance getProductListVc:para]; [self.navigationController pushViewController:vc animated:true]; } - (void)backAction { [self dismissViewControllerAnimated:true completion:nil]; } // MARK: - uitextfiled delegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSString *txt = textField.text; if ([txt isEqualToString:@""] ) { [self hideLenovoV]; } else { [self showLenovoV:txt]; } return true; } - (BOOL)textFieldShouldClear:(UITextField *)textField { // self.hisVm.aboutKeys = @[]; [self hideLenovoV]; return true; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *txt = textField.text; txt = [txt stringByReplacingCharactersInRange:range withString:string]; if ([txt isEqualToString:@""] ) { [self hideLenovoV]; } else { [self showLenovoV:txt]; } return true; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { return true; } - (void)textFieldDidEndEditing:(UITextField *)textField { [self hideLenovoV]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; NSString *key = textField.text; if (![key isEmpty]){ [self.hisWordV.vm addSearchData:key]; [self toResult:key type:@"" keyWord:key]; } return true; } @end