MOSelectPartitionVC.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //
  2. // MOSelectPartitionVC.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/10/16.
  6. //
  7. #define SectionHeaderHeight 32.0
  8. #import "MOSelectPartitionVC.h"
  9. #import "SCIndexView.h"
  10. #import "UITableView+SCIndexView.h"
  11. #import "MOSelectPartitionCell.h"
  12. #import "MOSearchBar.h"
  13. #import "UIImage+YYAdd.h"
  14. #import "MOGlobalView.h"
  15. #import "ChineseToPinyin.h"
  16. @implementation MOCountryDataSourceModel
  17. @end
  18. @interface MOSelectPartitionVC ()<SCTableViewSectionIndexDelegate,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
  19. @property (nonatomic, strong) BigBtn *backButton;
  20. @property (nonatomic, strong) UILabel *titleLab;
  21. /** 搜索框*/
  22. @property (nonatomic, strong) MOSearchBar *searchTef;
  23. /** 主显示界面 */
  24. @property (nonatomic, strong) UITableView *tableView;
  25. @property (nonatomic, strong) NSMutableArray *dataArr;
  26. @property (nonatomic, strong) NSMutableArray<MOCountryDataSourceModel *> *filteredDataArr;
  27. @property (nonatomic, assign) BOOL isSearching;
  28. @property (nonatomic, strong) MOGlobalView *globalView;
  29. @end
  30. @implementation MOSelectPartitionVC
  31. - (void)viewWillAppear:(BOOL)animated{
  32. [super viewWillAppear:animated];
  33. [self.navigationController setNavigationBarHidden:YES animated:animated];
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // self.navigationItem.title = NSLocalString(@"mimo_recharge_select_country");
  38. [self setupUI];
  39. if(self.setCountyInfo || self.justCounty || self.isGlobal){
  40. [self getHttpDataAboutCountry];
  41. [self toSetTableViewHeaderView];
  42. }
  43. else{
  44. //数据
  45. [self getPartitionNumData];
  46. }
  47. }
  48. - (void)toSetTableViewHeaderView{
  49. if(self.isGlobal){
  50. self.globalView.frame = CGRectMake(0.0, 0.0, SCREENWIDTH, 56.0);
  51. self.tableView.tableHeaderView = self.globalView;
  52. }
  53. }
  54. - (void)setupUI{
  55. self.view.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  56. [self.view addSubview:self.backButton];
  57. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.mas_equalTo(kTopSafeAreaInset + 10);
  59. make.left.mas_equalTo(16);
  60. make.size.mas_equalTo(CGSizeMake(30, 30));
  61. }];
  62. [self.view addSubview:self.titleLab];
  63. [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.equalTo(self.view).offset(55.0);
  65. make.right.equalTo(self.view).offset(-55.0);
  66. make.centerY.equalTo(self.backButton.mas_centerY);
  67. }];
  68. [self.view addSubview:self.searchTef];
  69. [self.searchTef mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.top.equalTo(self.backButton.mas_bottom).offset(14);
  71. make.left.mas_equalTo(16);
  72. make.right.mas_equalTo(-16);
  73. make.height.mas_equalTo(44);
  74. }];
  75. [self.view addSubview:self.tableView];
  76. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.top.equalTo(self.searchTef.mas_bottom).offset(0.5);
  78. make.left.mas_equalTo(0);
  79. make.right.mas_equalTo(0);
  80. make.bottom.mas_equalTo(0);
  81. }];
  82. }
  83. - (void)textFieldDidChange:(UITextField *)textField{
  84. NSString *searchText = textField.text;
  85. if (searchText.length == 0) {
  86. self.isSearching = NO;
  87. [self.tableView reloadData];
  88. return;
  89. }
  90. self.isSearching = YES;
  91. NSMutableArray<MOCountryDataSourceModel *> *filteredSections = [NSMutableArray array];
  92. for (MOCountryDataSourceModel *sectionModel in self.dataArr) {
  93. NSMutableArray<MOCountryList *> *filteredCountries = [NSMutableArray array];
  94. for (MOCountryList *country in sectionModel.sectionDataSourceArray) {
  95. if ([country.name containsString:searchText] || [country.code containsString:searchText]) {
  96. [filteredCountries addObject:country];
  97. }
  98. }
  99. if (filteredCountries.count > 0) {
  100. MOCountryDataSourceModel *filteredSection = [MOCountryDataSourceModel new];
  101. filteredSection.indexTitle = sectionModel.indexTitle;
  102. filteredSection.sectionDataSourceArray = filteredCountries;
  103. [filteredSections addObject:filteredSection];
  104. }
  105. }
  106. self.filteredDataArr = filteredSections;
  107. [self.tableView reloadData];
  108. }
  109. - (void)backButtonAction {
  110. [self.navigationController popViewControllerAnimated:YES];
  111. }
  112. #pragma mark - SCTableViewSectionIndexDelegate
  113. - (void)tableView:(UITableView *)tableView didSelectIndexViewAtSection:(NSUInteger)section
  114. {
  115. }
  116. - (NSUInteger)sectionOfTableViewDidScroll:(UITableView *)tableView
  117. {
  118. return SCIndexViewInvalidSection;
  119. }
  120. #pragma mark - UITableViewDelegate,UITableViewDataSource
  121. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  122. return self.isSearching ? self.filteredDataArr.count : self.dataArr.count;
  123. }
  124. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  125. MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[section] : self.dataArr[section];
  126. return sectionModel.sectionDataSourceArray.count;
  127. }
  128. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  129. MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[indexPath.section] : self.dataArr[indexPath.section];
  130. MOCountryList *cellModel = sectionModel.sectionDataSourceArray[indexPath.row];
  131. MOSelectPartitionCell *cell = [tableView dequeueReusableCellWithIdentifier:MOSelectPartitionCell_ID];
  132. if (cell == nil){
  133. cell = [[MOSelectPartitionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOSelectPartitionCell_ID];
  134. }
  135. cell.model = cellModel;
  136. return cell;
  137. }
  138. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  139. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  140. MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[indexPath.section] : self.dataArr[indexPath.section];
  141. MOCountryList *cellModel = sectionModel.sectionDataSourceArray[indexPath.row];
  142. if(self.setCountyInfo){
  143. [self setUserInfoAboutCountyWith:cellModel];
  144. }
  145. else{
  146. self.selectCellBlock ? self.selectCellBlock(cellModel) : nil;
  147. [self.navigationController popViewControllerAnimated:YES];
  148. }
  149. }
  150. - (void)setUserInfoAboutCountyWith:(MOCountryList *)model{
  151. WEAKSELF
  152. if(model.code.length == 0){
  153. return;
  154. }
  155. NSString *countryStr = model.code;
  156. NSDictionary *dict = @{@"country":countryStr};
  157. [kHttpManager toSettingInfoWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  158. if(kCode_Success){
  159. NSDictionary *baseDict = data[@"data"];
  160. [MBProgressHUD showTipMessageInView:NSLocalString(@"mimo_common_success")];
  161. //更新个人信息缓存
  162. MOMeDataInfo *userInfoData = (MOMeDataInfo *)[[MODataCache sharedYYCache] objectForKey:kMineUserInfo];
  163. userInfoData.userProfile.country = countryStr;
  164. [[MODataCache sharedYYCache] setObject:userInfoData forKey:kMineUserInfo];
  165. weakSelf.selectCellBlock ? weakSelf.selectCellBlock(model) : nil;
  166. [weakSelf performSelector:@selector(toPopVC) withObject:nil afterDelay:1.0];
  167. }
  168. else{
  169. kShowNetError(data)
  170. }
  171. }];
  172. }
  173. - (void)toPopVC{
  174. [self.navigationController popViewControllerAnimated:YES];
  175. }
  176. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  177. NSArray *source = self.isSearching ? self.filteredDataArr : self.dataArr;
  178. if (section >= source.count) return 0;
  179. return SectionHeaderHeight;
  180. }
  181. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  182. NSString *sectionTitle = @"";
  183. NSArray *source = self.isSearching ? self.filteredDataArr : self.dataArr;
  184. if (section < source.count) {
  185. sectionTitle = ((MOCountryDataSourceModel *)source[section]).indexTitle ?: @"";
  186. }
  187. UIView *contentView = [UIView new];
  188. contentView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  189. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 100, SectionHeaderHeight)];
  190. label.backgroundColor = [UIColor clearColor];
  191. label.text = sectionTitle;
  192. label.font = [MOTextTools regularFont:16];
  193. label.textColor = kBaseTextColor_3;
  194. [contentView addSubview:label];
  195. return contentView;
  196. }
  197. #pragma mark - Http
  198. - (void)getPartitionNumData{
  199. WEAKSELF
  200. [kHttpManager getCountryMobileListWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  201. __strong typeof(weakSelf) self = weakSelf;
  202. if(kCode_Success){
  203. MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  204. MOCountryBaseData *baseData = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]];
  205. self.dataArr = [self sortDataArray:baseData.countryList];
  206. self.tableView.sc_indexViewDataSource = [self getIndexTitleArray];
  207. [self.tableView reloadData];
  208. }
  209. else
  210. {
  211. kShowNetError(data)
  212. }
  213. }];
  214. }
  215. //获取国家列表
  216. - (void)getHttpDataAboutCountry{
  217. WEAKSELF
  218. [kHttpManager getCountryAndRegionListWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  219. __strong typeof(weakSelf) self = weakSelf;
  220. if(kCode_Success){
  221. MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  222. MOCountryBaseData *baseData = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]];
  223. self.dataArr = [self sortDataArray:baseData.countryList];
  224. self.tableView.sc_indexViewDataSource = [self getIndexTitleArray];
  225. [self.tableView reloadData];
  226. }
  227. else
  228. {
  229. kShowNetError(data)
  230. }
  231. }];
  232. }
  233. #pragma mark - sort
  234. - (NSMutableArray<MOCountryDataSourceModel *> *)sortDataArray:(NSArray<MOCountryList *> *)dataArray
  235. {
  236. if (dataArray.count == 0)
  237. {
  238. return [NSMutableArray array];
  239. }
  240. //建立索引的核心
  241. UILocalizedIndexedCollation *indexCollation = [UILocalizedIndexedCollation currentCollation];
  242. NSArray *sectionTitles = [indexCollation sectionTitles];
  243. //返回27,是a-z和#
  244. NSInteger highSection = sectionTitles.count;
  245. //tableView 会被分成27个section
  246. NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:highSection];
  247. for (int i = 0; i <= highSection; i++)
  248. {
  249. NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
  250. [sortedArray addObject:sectionArray];
  251. }
  252. //名字分section
  253. for (int i = 0;i < [dataArray count]; i++)
  254. {
  255. MOCountryList *data = [dataArray objectAtIndex:i];
  256. NSString *firstLetter = [ChineseToPinyin pinyinFromChineseString:data.name];
  257. if (firstLetter.length > 0)
  258. {
  259. NSInteger section = [indexCollation sectionForObject:[firstLetter substringToIndex:1] collationStringSelector:@selector(uppercaseString)];
  260. NSMutableArray *array = [sortedArray objectAtIndex:section];
  261. [array addObject:data];
  262. }
  263. }
  264. //每个section内的数组排序
  265. for (int i = 0; i < [sortedArray count]; i++)
  266. {
  267. NSArray *array = [MOTools sortArrayByPinYin:sortedArray[i] objectTextKeyPath:@"name"];
  268. [sortedArray replaceObjectAtIndex:i withObject:[NSMutableArray arrayWithArray:array]];
  269. }
  270. NSMutableArray *modelArray = [NSMutableArray arrayWithCapacity:1];
  271. //section数组为空的过滤掉
  272. for (int i = 0; i < sortedArray.count; i++)
  273. {
  274. NSMutableArray *sectionArray = sortedArray[i];
  275. if (sectionArray.count > 0)
  276. {
  277. MOCountryDataSourceModel *model = [MOCountryDataSourceModel new];
  278. model.indexTitle = sectionTitles[i];
  279. model.sectionDataSourceArray = sectionArray;
  280. [modelArray addObject:model];
  281. }
  282. }
  283. return modelArray;
  284. }
  285. /** 根据数据源创建索引标题 */
  286. - (NSArray *)getIndexTitleArray
  287. {
  288. NSMutableArray *indexArray = [NSMutableArray arrayWithCapacity:1];
  289. for (int i = 0; i < self.dataArr.count; i++)
  290. {
  291. MOCountryDataSourceModel *model = self.dataArr[i];
  292. [indexArray addObject:model.indexTitle?:@""];
  293. }
  294. return [NSArray arrayWithArray:indexArray];
  295. }
  296. - (BigBtn *)backButton {
  297. if (!_backButton) {
  298. _backButton = [[BigBtn alloc] init];
  299. [_backButton setImage:[UIImage imageNamed:@"v_2_icon_new_back_black"] forState:UIControlStateNormal];
  300. [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
  301. }
  302. return _backButton;
  303. }
  304. - (UITableView *)tableView {
  305. if (!_tableView) {
  306. _tableView= [[UITableView alloc] init];
  307. _tableView.backgroundColor = [UIColor clearColor];
  308. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  309. _tableView.dataSource = self;
  310. _tableView.delegate = self;
  311. _tableView.showsVerticalScrollIndicator = NO;
  312. _tableView.showsHorizontalScrollIndicator = NO;
  313. _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  314. _tableView.rowHeight = 56.0;
  315. _tableView.estimatedRowHeight = 0;
  316. _tableView.estimatedSectionHeaderHeight = 0;
  317. _tableView.estimatedSectionFooterHeight = 0;
  318. //iOS15适配
  319. if (@available(iOS 15.0, *)) {
  320. _tableView.sectionHeaderTopPadding = 0;
  321. }
  322. [_tableView registerClass:[MOSelectPartitionCell class] forCellReuseIdentifier:MOSelectPartitionCell_ID];
  323. SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configuration];
  324. configuration.indexItemTextColor = kBaseTextColor_3;
  325. configuration.indexItemTextFont = [MOTextTools poppinsRegularFont:16.0];
  326. configuration.indexItemHeight = 32.0;
  327. configuration.indexItemRightMargin = 10.0;
  328. configuration.indexItemsSpace = 0.0;
  329. _tableView.sc_indexViewConfiguration = configuration;
  330. _tableView.sc_translucentForTableViewInNavigationBar = NO;
  331. _tableView.sc_indexViewDelegate = self;
  332. }
  333. return _tableView;
  334. }
  335. - (MOSearchBar *)searchTef {
  336. if (!_searchTef) {
  337. _searchTef = [[MOSearchBar alloc] init];
  338. _searchTef.backgroundColor = [MOTools colorWithHexString:@"#F3F4FA"];
  339. _searchTef.layer.cornerRadius = 8.0;
  340. _searchTef.layer.masksToBounds = YES;
  341. _searchTef.clearButtonMode = UITextFieldViewModeWhileEditing;
  342. _searchTef.leftViewMode = UITextFieldViewModeAlways;
  343. _searchTef.returnKeyType = UIReturnKeySearch;
  344. _searchTef.delegate = self;
  345. _searchTef.textColor = kBaseTextColor_1;
  346. _searchTef.font = [MOTextTools regularFont:14];
  347. UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search_36"]];
  348. icon.contentMode = UIViewContentModeScaleAspectFit;
  349. icon.frame = CGRectMake(0, 0, 18, 18);
  350. _searchTef.leftView = icon;
  351. [_searchTef addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  352. NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalString(@"mimo_login_input_country") attributes:@{
  353. NSForegroundColorAttributeName : [MOTools colorWithHexString:@"#878A99" alpha:1.0],
  354. NSFontAttributeName : [MOTextTools poppinsRegularFont:14]
  355. }];
  356. _searchTef.attributedPlaceholder = placeholderString;
  357. }
  358. return _searchTef;
  359. }
  360. - (MOGlobalView *)globalView{
  361. if(!_globalView){
  362. _globalView = [[MOGlobalView alloc] init];
  363. WEAKSELF
  364. _globalView.viewClickBlock = ^{
  365. MOCountryList *model = [[MOCountryList alloc] init];
  366. model.code = @"";
  367. weakSelf.selectCellBlock ? weakSelf.selectCellBlock(model) : nil;
  368. [weakSelf.navigationController popViewControllerAnimated:YES];
  369. };
  370. }
  371. return _globalView;
  372. }
  373. - (UILabel *)titleLab{
  374. if(!_titleLab)
  375. {
  376. _titleLab = [UILabel new];
  377. _titleLab.textColor = kBaseTextColor_1;
  378. _titleLab.textAlignment = NSTextAlignmentCenter;
  379. _titleLab.font = [MOTextTools poppinsMediumFont:18.0];
  380. _titleLab.backgroundColor = [UIColor clearColor];
  381. _titleLab.text = NSLocalString(@"mimo_recharge_select_country");
  382. }
  383. return _titleLab;
  384. }
  385. @end