// // SPSBChooseCityViewController.m // 我的社保 // // Created by shanp on 2021/5/19. // #import "SPSBChooseCityViewController.h" #import "SPSBUIGeneralHeader.h" #import "SPSBChooseCityTableViewController.h" #import "SPSBChooseCityResultTableViewController.h" #import "SPSBChooseCitySearchView.h" #import "SPSBChooseCityNavigationView.h" #import "UIViewController+SPSBInitialization.h" #import "UIViewController+SPSBNavigationBar.h" #import "SPSBSQLProfile.h" #import "UIView+SPSBLoadingTipsView.h" #import "SPSBQueryCityModel.h" @interface SPSBChooseCityViewController () { SPSBChooseCityTableViewController *_tableVC; SPSBChooseCityNavigationView *_cityNavigationView; UIView *_searchResultView; SPSBChooseCityResultTableViewController *_resultTableVC; SPSBChooseCitySearchView *_searchView; UITapGestureRecognizer *_cancelTap; NSString *_lastContition;//上一次搜索的关键字 NSDictionary *_data; } @end @implementation SPSBChooseCityViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view showLoadingToastWithTitle:@"正在加载" userClick:false]; [self initData]; [self setupUI]; [self.view dismissToast]; } - (void)dealloc { //导航条去除KVO [_cityNavigationView removeObserver:self forKeyPath:@"spsb_section"]; } - (void)initData { _data = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:SPSBqueryCitySQLKey condition:^id(FMDatabase *database) { return [database executeQuery:@"select * from cache_data where key = ?", SPSBqueryCitySQLKey]; }]; } #pragma mark - SPSBChooseCitySearchViewDelegate - (void)searchView:(SPSBChooseCitySearchView *)searchView searchWithCondition:(NSString *)condition { NSString *str = [self deleteSpace:condition]; if ([str isEqualToString:@""]) { [_searchResultView addGestureRecognizer:_cancelTap]; _lastContition = @""; [self noResult]; } else if (![str isEqualToString:_lastContition]) { [_searchResultView removeGestureRecognizer:_cancelTap]; NSArray *data = [JXHFMDBManager searchWithPath:SPSBShanpQueryCitySQL backKey:nil condition:^id(FMDatabase *database) { return [database executeQuery:@"select * from t_sp_query_city where pinyin_abbreviation like ? or pinyin like ? or city_name like ?", [NSString stringWithFormat:@"%%%@%%", str], [NSString stringWithFormat:@"%%%@%%", str], [NSString stringWithFormat:@"%%%@%%", str]]; }]; _resultTableVC.spsb_dataArray = data; [self haveResult]; _lastContition = str; } } - (NSString *)deleteSpace:(NSString *)str { NSError* error = NULL; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"\\s" options:0 error:&error]; NSString* result = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@""]; debugLog(@"Result:%@", result); return result; } - (void)searchViewBeginEditing:(SPSBChooseCitySearchView *)searchView { [searchView remakeConstraints:^(JXHConstraintMaker *make) { make.top.leading.and.trailing.equalTo(0); if (@available(iOS 11.0, *)) { make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight()); } else { make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight()); } }]; [self.spsb_navigationBar remakeConstraints:^(JXHConstraintMaker *make) { make.leading.and.trailing.equalTo(0); make.top.equalTo(-64); make.bottom.equalTo(self.view.top); }]; _searchResultView.hidden = false; [UIView animateWithDuration:0.3 animations:^{ self->_searchResultView.alpha = 1; [searchView layoutIfNeeded]; [self.view layoutSubviews]; } completion:^(BOOL finished) { }]; } - (void)searchViewCancelButtonClicked:(SPSBChooseCitySearchView *)searchView { _lastContition = @""; [self noResult]; [searchView remakeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom); make.leading.equalTo(0); make.trailing.equalTo(45); make.height.equalTo(44); }]; [self.spsb_navigationBar remakeConstraints:^(JXHConstraintMaker *make) { make.top.leading.and.trailing.equalTo(0); if (@available(iOS 11.0, *)) { make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight()); } else { make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight()); } }]; [UIView animateWithDuration:0.3 animations:^{ self->_searchResultView.alpha = 0; [searchView layoutIfNeeded]; [self.view layoutSubviews]; } completion:^(BOOL finished) { self->_searchResultView.hidden = true; }]; } #pragma mark - Action - (void)haveResult { _resultTableVC.tableView.hidden = false; [_resultTableVC.tableView reloadData]; } - (void)noResult { _resultTableVC.tableView.hidden = true; [_resultTableVC.tableView reloadData]; } - (void)selectActionWithCityId:(NSString *)cityId { jxh_endEdit(); NSArray *array = [JXHFMDBManager searchWithPath:SPSBShanpQueryCitySQL backKey:nil condition:^id(FMDatabase *database) { return [database executeQuery:@"select * from t_sp_query_city where cid = ?", cityId]; }]; NSDictionary *dict; if (array.count != 0) { dict = array[0]; } else { dict = @{}; } _spsb_finishChoose([SPSBQueryCityModel getModelWithDictionary:dict]); [self dismissViewControllerAnimated:true completion:nil]; } - (void)cancelSearchAction { [_searchView cancelAction]; } #pragma mark - KVO回调 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"spsb_section"]) { NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:[[change objectForKey:@"new"] integerValue]]; [_tableVC.tableView scrollToRowAtIndexPath:index atScrollPosition:1 animated:false]; } } #pragma mark - Network Action #pragma mark - UI - (void)setupUI { self.spsb_isHiddenNavigationBar = true; self.title = @"选择城市"; self.view.backgroundColor = spsb_F5F5F5_color(); [self createNavigationBar]; [self createCityNavigationView]; [self createSearchView]; [self createTableView]; [self.view sendSubviewToBack:_tableVC.tableView]; [self createResultView]; } - (void)createResultView { _searchResultView = UIView.new; _searchResultView.backgroundColor = spsb_000000_color(0.25); _searchResultView.alpha = 0; _searchResultView.hidden = true; [self.view addSubview:_searchResultView]; _cancelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSearchAction)]; [_searchResultView addGestureRecognizer:_cancelTap]; [_searchResultView makeConstraints:^(JXHConstraintMaker *make) { make.bottom.leading.and.trailing.equalTo(0); make.top.equalTo(self->_searchView.bottom); }]; _resultTableVC = SPSBChooseCityResultTableViewController.new; @weakify(self) _resultTableVC.spsb_choose = ^(NSString * _Nonnull cityId) { @strongify(self) [self selectActionWithCityId:cityId]; }; [self addChildViewController:_resultTableVC]; [_searchResultView addSubview:_resultTableVC.tableView]; _resultTableVC.tableView.hidden = true; [_resultTableVC.tableView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self->_searchResultView); }]; } - (void)createTableView { _tableVC = [[SPSBChooseCityTableViewController alloc] initWithData:_data]; @weakify(self) _tableVC.spsb_choose = ^(NSString * _Nonnull cityId) { @strongify(self) [self selectActionWithCityId:cityId]; }; [self addChildViewController:_tableVC]; [self.view addSubview:_tableVC.tableView]; [_tableVC.tableView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self->_searchView.bottom); make.bottom.and.leading.and.trailing.equalTo(0); }]; } - (void)createCityNavigationView { //添加城市导航条 NSArray *sectionArray = [_data[@"key"] copy]; _cityNavigationView = [[SPSBChooseCityNavigationView alloc] initWithArray:sectionArray]; [self.view addSubview:_cityNavigationView]; [_cityNavigationView makeConstraints:^(JXHConstraintMaker *make) { if (@available(iOS 11.0, *)) { make.top.equalTo(self.view.safetop).offset(jxh_navigationViewHeight()); } else { make.top.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight()); } make.trailing.equalTo(0); make.width.equalTo(14); make.bottom.equalTo(0); }]; //导航条加KVO [_cityNavigationView addObserver:self forKeyPath:@"spsb_section" options:NSKeyValueObservingOptionNew context:nil]; } - (void)createSearchView { _searchView = SPSBChooseCitySearchView.new; _searchView.spsb_delegate = self; [self.view addSubview:_searchView]; [_searchView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.spsb_navigationBar.bottom); make.leading.equalTo(0); make.trailing.equalTo(45); make.height.equalTo(44); }]; } @end