// // TFLanguageSetController.m // TFReader // // Created by 谢腾飞 on 2020/12/14. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFLanguageSetController.h" #import "TFLanguageSetCell.h" #import "TFLanguageSetModel.h" @interface TFLanguageSetController () @property (nonatomic, strong) NSMutableArray *dataArray; /// 当前选择的下标 @property (nonatomic, assign) NSInteger selectedIndex; @property (nonatomic, weak) UIButton *saveBtn; @end @implementation TFLanguageSetController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } #pragma mark - 初始化 - (void)initialize { self.view.backgroundColor = kGrayViewColor; [self setNavigationBarTitle:TFLocalizedString(@"多语言环境")]; self.dataArray = [NSMutableArray array]; TFLanguageSetModel *t_model = [[TFLanguageSetModel alloc] init]; t_model.language = TFLanguageTypeDefault; t_model.title = TFLocalizedString(@"自动"); switch (TFLanguageManager.localizedLanguage) { case TFLanguageTypeSimplifiedChinese: t_model.subtitle = @"简体中文"; break; case TFLanguageTypeTraditionalChinese: t_model.subtitle = @"繁体中文"; break; case TFLanguageTypeEnglish: t_model.subtitle = @"English"; break; case TFLanguageTypeTail: t_model.subtitle = @"ไทย"; break; default: break; } [self.dataArray addObject:t_model]; t_model = [[TFLanguageSetModel alloc] init]; t_model.language = TFLanguageTypeSimplifiedChinese; t_model.title = @"简体中文"; t_model.subtitle = TFLocalizedString(@"简体中文"); [self.dataArray addObject:t_model]; t_model = [[TFLanguageSetModel alloc] init]; t_model.language = TFLanguageTypeTraditionalChinese; t_model.title = @"繁體中文"; t_model.subtitle = TFLocalizedString(@"繁體中文"); [self.dataArray addObject:t_model]; t_model = [[TFLanguageSetModel alloc] init]; t_model.language = TFLanguageTypeEnglish; t_model.title = @"English"; t_model.subtitle = TFLocalizedString(@"English"); [self.dataArray addObject:t_model]; t_model = [[TFLanguageSetModel alloc] init]; t_model.language = TFLanguageTypeTail; t_model.title = @"ไทย"; t_model.subtitle = TFLocalizedString(@"ไทย"); [self.dataArray addObject:t_model]; self.selectedIndex = TFLanguageManager.userLanguage; } #pragma mark - UI - (void)createSubviews { UIButton *saveBtn = nil; [self setNavigationBarRightButton:({ saveBtn = [UIButton buttonWithType:UIButtonTypeSystem]; self.saveBtn = saveBtn; [saveBtn setTitle:TFLocalizedString(@"保存") forState:UIControlStateNormal]; [saveBtn setTitleColor:kGrayTextColor forState:UIControlStateDisabled]; [saveBtn setTitleColor:kColorRGB(44, 144, 255) forState:UIControlStateNormal]; saveBtn.backgroundColor = [UIColor clearColor]; saveBtn.titleLabel.font = kFont15; saveBtn.enabled = NO; [saveBtn addTarget:self action:@selector(changeLanguage) forControlEvents:UIControlEventTouchUpInside]; saveBtn; })]; [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.navigationBar).offset(-kQuarterMargin); make.centerY.equalTo(self.navigationBar.navTitleLabel); make.size.mas_equalTo(CGSizeMake(50.0, 20.0)); }]; saveBtn.hidden = YES; self.mainTableView.dataSource = self; self.mainTableView.delegate = self; [self.view addSubview:self.mainTableView]; self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0); [self.mainTableView registerClass:TFLanguageSetCell.class forCellReuseIdentifier:TFLanguageSetCell.className]; [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.width.bottom.equalTo(self.view); make.top.equalTo(self.view).offset(PUB_NAVBAR_HEIGHT); }]; } #pragma mark UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFLanguageSetCell *cell = [tableView dequeueReusableCellWithIdentifier:TFLanguageSetCell.className forIndexPath:indexPath]; cell.languageModel = self.dataArray[indexPath.row]; cell.lineView.hidden = (indexPath.row == self.dataArray.count - 1); [cell set_selected:(self.selectedIndex == indexPath.row)]; return cell; } #pragma mark UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return kHalfMargin; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.backgroundColor = kGrayViewColor; return view; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TFLanguageSetCell *oldCell = (TFLanguageSetCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:indexPath.section]]; [oldCell set_selected:NO]; self.selectedIndex = indexPath.row; [self changeLanguage]; } /// 切换语言 - (void)changeLanguage { TFLanguageSetModel *t_model = self.dataArray[self.selectedIndex]; [TFLanguageManager setLanguageType:t_model.language]; [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"切换成功")]; [TFNetworkTools POST:Switch_Language parameters:nil model:nil success:nil failure:nil]; } @end