123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- //
- // 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 ()<SPSBChooseCitySearchViewDelegate> {
- 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
|