Files
peko-ios/YuMi/Modules/YMMine/View/Setting/XPMineSwitchLanguageVC.m

151 lines
6.0 KiB
Objective-C

//
// XPMineSwitchLanguageVC.m
// YuMi
//
// Created by duoban on 2024/4/8.
//
#import "XPMineSwitchLanguageVC.h"
#import "XPMineSettingItemModel.h"
#import "XPMineSwitchLanguageCell.h"
#import "YMLanguageConfig.h"
@interface XPMineSwitchLanguageVC ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,strong) UIButton *saveBtn;
@property (nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) XPMineSettingItemModel * chooseModel;
///数据源
@property (nonatomic,strong) NSArray<XPMineSettingItemModel *> *datasource;
@end
@implementation XPMineSwitchLanguageVC
- (void)viewDidLoad {
[super viewDidLoad];
[self installUI];
[self installConstraints];
}
-(void)installUI{
self.title = YMLocalizedString(@"XPMineSwitchLanguageVC0");
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveBtn];
[self.view addSubview:self.tableView];
}
-(void)installConstraints{
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(20));
make.leading.trailing.equalTo(self.view).inset(kGetScaleWidth(16));
make.height.mas_equalTo(kGetScaleWidth(51 * [YMLanguageConfig supportedLanguages].count));
}];
}
-(void)saveBtnAction{
// 根据选择的显示名称找到对应的语言代码
NSString *language = [self languageCodeForDisplayName:self.chooseModel.title];
// 设置RTL属性
if ([YMLanguageConfig isRTLanguage:language]) {
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
} else {
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
}
[NSBundle setLanguageText:language];
[self.navigationController popToRootViewControllerAnimated:YES];
[self showLoading];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hideHUD];
[[NSNotificationCenter defaultCenter] postNotificationName:@"kNavViewUpdateWhenLanguageUpdate" object:language];
[[NSNotificationCenter defaultCenter] postNotificationName:@"kSwitchLanguage" object:language];
});
}
// 根据显示名称获取语言代码
- (NSString *)languageCodeForDisplayName:(NSString *)displayName {
for (NSDictionary *language in [YMLanguageConfig supportedLanguages]) {
if ([language[@"displayName"] isEqualToString:displayName]) {
return language[@"code"];
}
}
return [YMLanguageConfig defaultLanguage];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kGetScaleWidth(51);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPMineSwitchLanguageCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineSwitchLanguageCell class])];
XPMineSettingItemModel * model = [self.datasource xpSafeObjectAtIndex:indexPath.row];
cell.model = model;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
XPMineSettingItemModel * model = [self.datasource xpSafeObjectAtIndex:indexPath.row] ;
for (XPMineSettingItemModel *obj in self.datasource) {
obj.isChoose = NO;
if ([model.title isEqualToString:obj.title]){
model.isChoose = YES;
}
}
self.chooseModel = model;
[self.tableView reloadData];
}
#pragma mark - 懒加载
- (NSArray<XPMineSettingItemModel *> *)datasource{
if(!_datasource){
NSMutableArray<XPMineSettingItemModel *> *items = [NSMutableArray array];
NSString *currentLanguage = [NSBundle getLanguageText];
// 动态创建语言选项
for (NSDictionary *languageConfig in [YMLanguageConfig supportedLanguages]) {
XPMineSettingItemModel *item = [XPMineSettingItemModel new];
item.title = languageConfig[@"displayName"];
item.isChoose = [languageConfig[@"code"] isEqualToString:currentLanguage];
if (item.isChoose) {
self.chooseModel = item;
}
[items addObject:item];
}
_datasource = [items copy];
}
return _datasource;
}
- (UIButton *)saveBtn {
if (!_saveBtn) {
_saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_saveBtn setFrame:CGRectMake(0, 0, 50, 30)];
[_saveBtn setTitle:YMLocalizedString(@"XPMineSwitchLanguageVC1") forState:UIControlStateNormal];
[_saveBtn setTitleColor:[DJDKMIMOMColor mainTextColor] forState:UIControlStateNormal];
_saveBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _saveBtn;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.scrollEnabled = NO;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.layer.cornerRadius = kGetScaleWidth(8);
_tableView.layer.masksToBounds = YES;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerClass:[XPMineSwitchLanguageCell class] forCellReuseIdentifier:NSStringFromClass([XPMineSwitchLanguageCell class])];
}
return _tableView;
}
@end