SPSBChooseCityViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // SPSBChooseCityViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/19.
  6. //
  7. #import "SPSBChooseCityViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBChooseCityTableViewController.h"
  10. #import "SPSBChooseCityResultTableViewController.h"
  11. #import "SPSBChooseCitySearchView.h"
  12. #import "SPSBChooseCityNavigationView.h"
  13. #import "UIViewController+SPSBInitialization.h"
  14. #import "UIViewController+SPSBNavigationBar.h"
  15. #import "SPSBSQLProfile.h"
  16. #import "UIView+SPSBLoadingTipsView.h"
  17. #import "SPSBQueryCityModel.h"
  18. @interface SPSBChooseCityViewController ()<SPSBChooseCitySearchViewDelegate> {
  19. SPSBChooseCityTableViewController *_tableVC;
  20. SPSBChooseCityNavigationView *_cityNavigationView;
  21. UIView *_searchResultView;
  22. SPSBChooseCityResultTableViewController *_resultTableVC;
  23. SPSBChooseCitySearchView *_searchView;
  24. UITapGestureRecognizer *_cancelTap;
  25. NSString *_lastContition;//上一次搜索的关键字
  26. NSDictionary *_data;
  27. }
  28. @end
  29. @implementation SPSBChooseCityViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. [self.view showLoadingToastWithTitle:@"正在加载" userClick:false];
  33. [self initData];
  34. [self setupUI];
  35. [self.view dismissToast];
  36. }
  37. - (void)dealloc {
  38. //导航条去除KVO
  39. [_cityNavigationView removeObserver:self forKeyPath:@"spsb_section"];
  40. }
  41. - (void)initData {
  42. _data = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:SPSBqueryCitySQLKey condition:^id(FMDatabase *database) {
  43. return [database executeQuery:@"select * from cache_data where key = ?", SPSBqueryCitySQLKey];
  44. }];
  45. }
  46. #pragma mark - SPSBChooseCitySearchViewDelegate
  47. - (void)searchView:(SPSBChooseCitySearchView *)searchView searchWithCondition:(NSString *)condition {
  48. NSString *str = [self deleteSpace:condition];
  49. if ([str isEqualToString:@""]) {
  50. [_searchResultView addGestureRecognizer:_cancelTap];
  51. _lastContition = @"";
  52. [self noResult];
  53. } else if (![str isEqualToString:_lastContition]) {
  54. [_searchResultView removeGestureRecognizer:_cancelTap];
  55. NSArray *data = [JXHFMDBManager searchWithPath:SPSBShanpQueryCitySQL backKey:nil condition:^id(FMDatabase *database) {
  56. 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]];
  57. }];
  58. _resultTableVC.spsb_dataArray = data;
  59. [self haveResult];
  60. _lastContition = str;
  61. }
  62. }
  63. - (NSString *)deleteSpace:(NSString *)str {
  64. NSError* error = NULL;
  65. NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"\\s" options:0 error:&error];
  66. NSString* result = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@""];
  67. debugLog(@"Result:%@", result);
  68. return result;
  69. }
  70. - (void)searchViewBeginEditing:(SPSBChooseCitySearchView *)searchView {
  71. [searchView remakeConstraints:^(JXHConstraintMaker *make) {
  72. make.top.leading.and.trailing.equalTo(0);
  73. if (@available(iOS 11.0, *)) {
  74. make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight());
  75. } else {
  76. make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight());
  77. }
  78. }];
  79. [self.spsb_navigationBar remakeConstraints:^(JXHConstraintMaker *make) {
  80. make.leading.and.trailing.equalTo(0);
  81. make.top.equalTo(-64);
  82. make.bottom.equalTo(self.view.top);
  83. }];
  84. _searchResultView.hidden = false;
  85. [UIView animateWithDuration:0.3 animations:^{
  86. self->_searchResultView.alpha = 1;
  87. [searchView layoutIfNeeded];
  88. [self.view layoutSubviews];
  89. } completion:^(BOOL finished) {
  90. }];
  91. }
  92. - (void)searchViewCancelButtonClicked:(SPSBChooseCitySearchView *)searchView {
  93. _lastContition = @"";
  94. [self noResult];
  95. [searchView remakeConstraints:^(JXHConstraintMaker *make) {
  96. make.top.equalTo(self.spsb_navigationBar.bottom);
  97. make.leading.equalTo(0);
  98. make.trailing.equalTo(45);
  99. make.height.equalTo(44);
  100. }];
  101. [self.spsb_navigationBar remakeConstraints:^(JXHConstraintMaker *make) {
  102. make.top.leading.and.trailing.equalTo(0);
  103. if (@available(iOS 11.0, *)) {
  104. make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight());
  105. } else {
  106. make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight());
  107. }
  108. }];
  109. [UIView animateWithDuration:0.3 animations:^{
  110. self->_searchResultView.alpha = 0;
  111. [searchView layoutIfNeeded];
  112. [self.view layoutSubviews];
  113. } completion:^(BOOL finished) {
  114. self->_searchResultView.hidden = true;
  115. }];
  116. }
  117. #pragma mark - Action
  118. - (void)haveResult {
  119. _resultTableVC.tableView.hidden = false;
  120. [_resultTableVC.tableView reloadData];
  121. }
  122. - (void)noResult {
  123. _resultTableVC.tableView.hidden = true;
  124. [_resultTableVC.tableView reloadData];
  125. }
  126. - (void)selectActionWithCityId:(NSString *)cityId {
  127. jxh_endEdit();
  128. NSArray *array = [JXHFMDBManager searchWithPath:SPSBShanpQueryCitySQL backKey:nil condition:^id(FMDatabase *database) {
  129. return [database executeQuery:@"select * from t_sp_query_city where cid = ?", cityId];
  130. }];
  131. NSDictionary *dict;
  132. if (array.count != 0) {
  133. dict = array[0];
  134. } else {
  135. dict = @{};
  136. }
  137. _spsb_finishChoose([SPSBQueryCityModel getModelWithDictionary:dict]);
  138. [self dismissViewControllerAnimated:true completion:nil];
  139. }
  140. - (void)cancelSearchAction {
  141. [_searchView cancelAction];
  142. }
  143. #pragma mark - KVO回调
  144. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  145. if ([keyPath isEqualToString:@"spsb_section"]) {
  146. NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:[[change objectForKey:@"new"] integerValue]];
  147. [_tableVC.tableView scrollToRowAtIndexPath:index atScrollPosition:1 animated:false];
  148. }
  149. }
  150. #pragma mark - Network Action
  151. #pragma mark - UI
  152. - (void)setupUI {
  153. self.spsb_isHiddenNavigationBar = true;
  154. self.title = @"选择城市";
  155. self.view.backgroundColor = spsb_F5F5F5_color();
  156. [self createNavigationBar];
  157. [self createCityNavigationView];
  158. [self createSearchView];
  159. [self createTableView];
  160. [self.view sendSubviewToBack:_tableVC.tableView];
  161. [self createResultView];
  162. }
  163. - (void)createResultView {
  164. _searchResultView = UIView.new;
  165. _searchResultView.backgroundColor = spsb_000000_color(0.25);
  166. _searchResultView.alpha = 0;
  167. _searchResultView.hidden = true;
  168. [self.view addSubview:_searchResultView];
  169. _cancelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSearchAction)];
  170. [_searchResultView addGestureRecognizer:_cancelTap];
  171. [_searchResultView makeConstraints:^(JXHConstraintMaker *make) {
  172. make.bottom.leading.and.trailing.equalTo(0);
  173. make.top.equalTo(self->_searchView.bottom);
  174. }];
  175. _resultTableVC = SPSBChooseCityResultTableViewController.new;
  176. @weakify(self)
  177. _resultTableVC.spsb_choose = ^(NSString * _Nonnull cityId) {
  178. @strongify(self)
  179. [self selectActionWithCityId:cityId];
  180. };
  181. [self addChildViewController:_resultTableVC];
  182. [_searchResultView addSubview:_resultTableVC.tableView];
  183. _resultTableVC.tableView.hidden = true;
  184. [_resultTableVC.tableView makeConstraints:^(JXHConstraintMaker *make) {
  185. make.edges.equalTo(self->_searchResultView);
  186. }];
  187. }
  188. - (void)createTableView {
  189. _tableVC = [[SPSBChooseCityTableViewController alloc] initWithData:_data];
  190. @weakify(self)
  191. _tableVC.spsb_choose = ^(NSString * _Nonnull cityId) {
  192. @strongify(self)
  193. [self selectActionWithCityId:cityId];
  194. };
  195. [self addChildViewController:_tableVC];
  196. [self.view addSubview:_tableVC.tableView];
  197. [_tableVC.tableView makeConstraints:^(JXHConstraintMaker *make) {
  198. make.top.equalTo(self->_searchView.bottom);
  199. make.bottom.and.leading.and.trailing.equalTo(0);
  200. }];
  201. }
  202. - (void)createCityNavigationView {
  203. //添加城市导航条
  204. NSArray *sectionArray = [_data[@"key"] copy];
  205. _cityNavigationView = [[SPSBChooseCityNavigationView alloc] initWithArray:sectionArray];
  206. [self.view addSubview:_cityNavigationView];
  207. [_cityNavigationView makeConstraints:^(JXHConstraintMaker *make) {
  208. if (@available(iOS 11.0, *)) {
  209. make.top.equalTo(self.view.safetop).offset(jxh_navigationViewHeight());
  210. } else {
  211. make.top.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight());
  212. }
  213. make.trailing.equalTo(0);
  214. make.width.equalTo(14);
  215. make.bottom.equalTo(0);
  216. }];
  217. //导航条加KVO
  218. [_cityNavigationView addObserver:self forKeyPath:@"spsb_section" options:NSKeyValueObservingOptionNew context:nil];
  219. }
  220. - (void)createSearchView {
  221. _searchView = SPSBChooseCitySearchView.new;
  222. _searchView.spsb_delegate = self;
  223. [self.view addSubview:_searchView];
  224. [_searchView makeConstraints:^(JXHConstraintMaker *make) {
  225. make.top.equalTo(self.spsb_navigationBar.bottom);
  226. make.leading.equalTo(0);
  227. make.trailing.equalTo(45);
  228. make.height.equalTo(44);
  229. }];
  230. }
  231. @end