// // MOSelectPartitionVC.m // MiMoLive // // Created by SuperC on 2023/10/16. // #define SectionHeaderHeight 32.0 #import "MOSelectPartitionVC.h" #import "SCIndexView.h" #import "UITableView+SCIndexView.h" #import "MOSelectPartitionCell.h" #import "MOSearchBar.h" #import "UIImage+YYAdd.h" #import "MOGlobalView.h" #import "ChineseToPinyin.h" @implementation MOCountryDataSourceModel @end @interface MOSelectPartitionVC () @property (nonatomic, strong) BigBtn *backButton; @property (nonatomic, strong) UILabel *titleLab; /** 搜索框*/ @property (nonatomic, strong) MOSearchBar *searchTef; /** 主显示界面 */ @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, strong) NSMutableArray *filteredDataArr; @property (nonatomic, assign) BOOL isSearching; @property (nonatomic, strong) MOGlobalView *globalView; @end @implementation MOSelectPartitionVC - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; } - (void)viewDidLoad { [super viewDidLoad]; // self.navigationItem.title = NSLocalString(@"mimo_recharge_select_country"); [self setupUI]; if(self.setCountyInfo || self.justCounty || self.isGlobal){ [self getHttpDataAboutCountry]; [self toSetTableViewHeaderView]; } else{ //数据 [self getPartitionNumData]; } } - (void)toSetTableViewHeaderView{ if(self.isGlobal){ self.globalView.frame = CGRectMake(0.0, 0.0, SCREENWIDTH, 56.0); self.tableView.tableHeaderView = self.globalView; } } - (void)setupUI{ self.view.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"]; [self.view addSubview:self.backButton]; [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(kTopSafeAreaInset + 10); make.left.mas_equalTo(16); make.size.mas_equalTo(CGSizeMake(30, 30)); }]; [self.view addSubview:self.titleLab]; [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(55.0); make.right.equalTo(self.view).offset(-55.0); make.centerY.equalTo(self.backButton.mas_centerY); }]; [self.view addSubview:self.searchTef]; [self.searchTef mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.backButton.mas_bottom).offset(14); make.left.mas_equalTo(16); make.right.mas_equalTo(-16); make.height.mas_equalTo(44); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.searchTef.mas_bottom).offset(0.5); make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; } - (void)textFieldDidChange:(UITextField *)textField{ NSString *searchText = textField.text; if (searchText.length == 0) { self.isSearching = NO; [self.tableView reloadData]; return; } self.isSearching = YES; NSMutableArray *filteredSections = [NSMutableArray array]; for (MOCountryDataSourceModel *sectionModel in self.dataArr) { NSMutableArray *filteredCountries = [NSMutableArray array]; for (MOCountryList *country in sectionModel.sectionDataSourceArray) { if ([country.name containsString:searchText] || [country.code containsString:searchText]) { [filteredCountries addObject:country]; } } if (filteredCountries.count > 0) { MOCountryDataSourceModel *filteredSection = [MOCountryDataSourceModel new]; filteredSection.indexTitle = sectionModel.indexTitle; filteredSection.sectionDataSourceArray = filteredCountries; [filteredSections addObject:filteredSection]; } } self.filteredDataArr = filteredSections; [self.tableView reloadData]; } - (void)backButtonAction { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - SCTableViewSectionIndexDelegate - (void)tableView:(UITableView *)tableView didSelectIndexViewAtSection:(NSUInteger)section { } - (NSUInteger)sectionOfTableViewDidScroll:(UITableView *)tableView { return SCIndexViewInvalidSection; } #pragma mark - UITableViewDelegate,UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.isSearching ? self.filteredDataArr.count : self.dataArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[section] : self.dataArr[section]; return sectionModel.sectionDataSourceArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[indexPath.section] : self.dataArr[indexPath.section]; MOCountryList *cellModel = sectionModel.sectionDataSourceArray[indexPath.row]; MOSelectPartitionCell *cell = [tableView dequeueReusableCellWithIdentifier:MOSelectPartitionCell_ID]; if (cell == nil){ cell = [[MOSelectPartitionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOSelectPartitionCell_ID]; } cell.model = cellModel; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO]; MOCountryDataSourceModel *sectionModel = self.isSearching ? self.filteredDataArr[indexPath.section] : self.dataArr[indexPath.section]; MOCountryList *cellModel = sectionModel.sectionDataSourceArray[indexPath.row]; if(self.setCountyInfo){ [self setUserInfoAboutCountyWith:cellModel]; } else{ self.selectCellBlock ? self.selectCellBlock(cellModel) : nil; [self.navigationController popViewControllerAnimated:YES]; } } - (void)setUserInfoAboutCountyWith:(MOCountryList *)model{ WEAKSELF if(model.code.length == 0){ return; } NSString *countryStr = model.code; NSDictionary *dict = @{@"country":countryStr}; [kHttpManager toSettingInfoWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { if(kCode_Success){ NSDictionary *baseDict = data[@"data"]; [MBProgressHUD showTipMessageInView:NSLocalString(@"mimo_common_success")]; //更新个人信息缓存 MOMeDataInfo *userInfoData = (MOMeDataInfo *)[[MODataCache sharedYYCache] objectForKey:kMineUserInfo]; userInfoData.userProfile.country = countryStr; [[MODataCache sharedYYCache] setObject:userInfoData forKey:kMineUserInfo]; weakSelf.selectCellBlock ? weakSelf.selectCellBlock(model) : nil; [weakSelf performSelector:@selector(toPopVC) withObject:nil afterDelay:1.0]; } else{ kShowNetError(data) } }]; } - (void)toPopVC{ [self.navigationController popViewControllerAnimated:YES]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ NSArray *source = self.isSearching ? self.filteredDataArr : self.dataArr; if (section >= source.count) return 0; return SectionHeaderHeight; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSString *sectionTitle = @""; NSArray *source = self.isSearching ? self.filteredDataArr : self.dataArr; if (section < source.count) { sectionTitle = ((MOCountryDataSourceModel *)source[section]).indexTitle ?: @""; } UIView *contentView = [UIView new]; contentView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 100, SectionHeaderHeight)]; label.backgroundColor = [UIColor clearColor]; label.text = sectionTitle; label.font = [MOTextTools regularFont:16]; label.textColor = kBaseTextColor_3; [contentView addSubview:label]; return contentView; } #pragma mark - Http - (void)getPartitionNumData{ WEAKSELF [kHttpManager getCountryMobileListWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOCountryBaseData *baseData = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]]; self.dataArr = [self sortDataArray:baseData.countryList]; self.tableView.sc_indexViewDataSource = [self getIndexTitleArray]; [self.tableView reloadData]; } else { kShowNetError(data) } }]; } //获取国家列表 - (void)getHttpDataAboutCountry{ WEAKSELF [kHttpManager getCountryAndRegionListWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; if(kCode_Success){ MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOCountryBaseData *baseData = [MOCountryBaseData modelObjectWithDictionary:data[@"data"]]; self.dataArr = [self sortDataArray:baseData.countryList]; self.tableView.sc_indexViewDataSource = [self getIndexTitleArray]; [self.tableView reloadData]; } else { kShowNetError(data) } }]; } #pragma mark - sort - (NSMutableArray *)sortDataArray:(NSArray *)dataArray { if (dataArray.count == 0) { return [NSMutableArray array]; } //建立索引的核心 UILocalizedIndexedCollation *indexCollation = [UILocalizedIndexedCollation currentCollation]; NSArray *sectionTitles = [indexCollation sectionTitles]; //返回27,是a-z和# NSInteger highSection = sectionTitles.count; //tableView 会被分成27个section NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:highSection]; for (int i = 0; i <= highSection; i++) { NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1]; [sortedArray addObject:sectionArray]; } //名字分section for (int i = 0;i < [dataArray count]; i++) { MOCountryList *data = [dataArray objectAtIndex:i]; NSString *firstLetter = [ChineseToPinyin pinyinFromChineseString:data.name]; if (firstLetter.length > 0) { NSInteger section = [indexCollation sectionForObject:[firstLetter substringToIndex:1] collationStringSelector:@selector(uppercaseString)]; NSMutableArray *array = [sortedArray objectAtIndex:section]; [array addObject:data]; } } //每个section内的数组排序 for (int i = 0; i < [sortedArray count]; i++) { NSArray *array = [MOTools sortArrayByPinYin:sortedArray[i] objectTextKeyPath:@"name"]; [sortedArray replaceObjectAtIndex:i withObject:[NSMutableArray arrayWithArray:array]]; } NSMutableArray *modelArray = [NSMutableArray arrayWithCapacity:1]; //section数组为空的过滤掉 for (int i = 0; i < sortedArray.count; i++) { NSMutableArray *sectionArray = sortedArray[i]; if (sectionArray.count > 0) { MOCountryDataSourceModel *model = [MOCountryDataSourceModel new]; model.indexTitle = sectionTitles[i]; model.sectionDataSourceArray = sectionArray; [modelArray addObject:model]; } } return modelArray; } /** 根据数据源创建索引标题 */ - (NSArray *)getIndexTitleArray { NSMutableArray *indexArray = [NSMutableArray arrayWithCapacity:1]; for (int i = 0; i < self.dataArr.count; i++) { MOCountryDataSourceModel *model = self.dataArr[i]; [indexArray addObject:model.indexTitle?:@""]; } return [NSArray arrayWithArray:indexArray]; } - (BigBtn *)backButton { if (!_backButton) { _backButton = [[BigBtn alloc] init]; [_backButton setImage:[UIImage imageNamed:@"v_2_icon_new_back_black"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } - (UITableView *)tableView { if (!_tableView) { _tableView= [[UITableView alloc] init]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.dataSource = self; _tableView.delegate = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; _tableView.rowHeight = 56.0; _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; //iOS15适配 if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } [_tableView registerClass:[MOSelectPartitionCell class] forCellReuseIdentifier:MOSelectPartitionCell_ID]; SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configuration]; configuration.indexItemTextColor = kBaseTextColor_3; configuration.indexItemTextFont = [MOTextTools poppinsRegularFont:16.0]; configuration.indexItemHeight = 32.0; configuration.indexItemRightMargin = 10.0; configuration.indexItemsSpace = 0.0; _tableView.sc_indexViewConfiguration = configuration; _tableView.sc_translucentForTableViewInNavigationBar = NO; _tableView.sc_indexViewDelegate = self; } return _tableView; } - (MOSearchBar *)searchTef { if (!_searchTef) { _searchTef = [[MOSearchBar alloc] init]; _searchTef.backgroundColor = [MOTools colorWithHexString:@"#F3F4FA"]; _searchTef.layer.cornerRadius = 8.0; _searchTef.layer.masksToBounds = YES; _searchTef.clearButtonMode = UITextFieldViewModeWhileEditing; _searchTef.leftViewMode = UITextFieldViewModeAlways; _searchTef.returnKeyType = UIReturnKeySearch; _searchTef.delegate = self; _searchTef.textColor = kBaseTextColor_1; _searchTef.font = [MOTextTools regularFont:14]; UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search_36"]]; icon.contentMode = UIViewContentModeScaleAspectFit; icon.frame = CGRectMake(0, 0, 18, 18); _searchTef.leftView = icon; [_searchTef addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalString(@"mimo_login_input_country") attributes:@{ NSForegroundColorAttributeName : [MOTools colorWithHexString:@"#878A99" alpha:1.0], NSFontAttributeName : [MOTextTools poppinsRegularFont:14] }]; _searchTef.attributedPlaceholder = placeholderString; } return _searchTef; } - (MOGlobalView *)globalView{ if(!_globalView){ _globalView = [[MOGlobalView alloc] init]; WEAKSELF _globalView.viewClickBlock = ^{ MOCountryList *model = [[MOCountryList alloc] init]; model.code = @""; weakSelf.selectCellBlock ? weakSelf.selectCellBlock(model) : nil; [weakSelf.navigationController popViewControllerAnimated:YES]; }; } return _globalView; } - (UILabel *)titleLab{ if(!_titleLab) { _titleLab = [UILabel new]; _titleLab.textColor = kBaseTextColor_1; _titleLab.textAlignment = NSTextAlignmentCenter; _titleLab.font = [MOTextTools poppinsMediumFont:18.0]; _titleLab.backgroundColor = [UIColor clearColor]; _titleLab.text = NSLocalString(@"mimo_recharge_select_country"); } return _titleLab; } @end