Hibit_iOS/HiBit/Project/Publics/MultiLanguage/Controllers/HBMultiLanguageController.m
2024-06-07 13:38:31 +08:00

247 lines
9.2 KiB
Objective-C

//
// HBMultiLanguageController.m
// QinJiuTV
//
// Created by HI_LOSER on 2024/5/6.
//
#import "HBMultiLanguageController.h"
@interface HBMultiLanguageController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) RSButton * sysButton;
@property (weak, nonatomic) UICollectionView * collectionView;
@property (strong, nonatomic) NSString * abbr;
@end
@implementation HBMultiLanguageController
-(NSArray<HBLanguageModel *> *)models {
if (!_models) {
_models = [HBMultiLanguageManager languages];
}
return _models;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadNavigationBar];
[self loadSubViews];
}
- (void)loadNavigationBar {
self.navigationItem.title = [@"mine_title_language" language];
}
-(void)loadSubViews {
UIButton * doneButton = [[UIButton alloc]init];
[doneButton acs_radiusWithRadius:21.0 corner:UIRectCornerAllCorners];
doneButton.layer.masksToBounds = YES;
[doneButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[doneButton setTitle:[@"common_button_ok" language] forState:UIControlStateNormal];
[doneButton setBackgroundImage:[UIImage imageNamed:@"publics_gradient"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
UIView * sysView = [[UIView alloc]init];
[sysView acs_radiusWithRadius:10.0 corner:UIRectCornerAllCorners];
sysView.backgroundColor = HBColor.color_FF0049;
[sysView addTapGestureRecognizerWithTarget:self action:@selector(systemAction)];
[self.view addSubview:sysView];
RSButton * sysButton = [[RSButton alloc]initWithType:RSButtonTypeAbove];
sysButton.selected = [HBMultiLanguageManager isSystem];
sysButton.margin = 4.0;
sysButton.insets = UIEdgeInsetsMake(0, 4.0, 0, 4.0);
[sysButton setTitleFont:[UIFont systemFontOfSize:12.0 weight:UIFontWeightSemibold]];
[sysButton setTitle:[@"language_text_system" language] forState:UIControlStateNormal];
[sysButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[sysButton setImage:[UIImage imageNamed:@"language_select_normal"] forState:UIControlStateNormal];
[sysButton setImage:[UIImage imageNamed:@"language_select_selected"] forState:UIControlStateSelected];
[sysButton addTarget:self action:@selector(systemAction) forControlEvents:UIControlEventTouchUpInside];
[sysView addSubview:sysButton];
self.sysButton = sysButton;
UILabel * sysDetailTextLabel = [[UILabel alloc]init];
sysDetailTextLabel.text = [@"language_text_system_tip" language];
sysDetailTextLabel.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];
sysDetailTextLabel.textColor = UIColor.whiteColor;
sysDetailTextLabel.numberOfLines = 0;
[sysView addSubview:sysDetailTextLabel];
CGFloat width = (self.view.width - 42.0) * 0.5;
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.minimumLineSpacing = 10.0;
flowLayout.itemSize = CGSizeMake(width, width);
UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
collectionView.backgroundColor = HBColor.color_080B16;
[collectionView registerClass:HBMultiLanguageCollectionViewCell.class forCellWithReuseIdentifier:@"HBMultiLanguageCollectionViewCell"];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
self.collectionView = collectionView;
[sysView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.offset(16.0);
make.right.offset(-16.0);
make.height.offset(71.0);
}];
[sysButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.centerY.equalTo(sysView);
make.width.offset(80.0);
}];
[sysDetailTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(sysButton.mas_right);
make.top.offset(10.0);
make.right.offset(-12.0);
make.bottom.offset(-10.0);
}];
[doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(16.0);
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-20.0);
make.right.offset(-16.0);
make.height.offset(42.0);
}];
[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(16.0);
make.right.offset(-16.0);
make.top.equalTo(sysView.mas_bottom).offset(20.0);
make.bottom.equalTo(doneButton.mas_top).offset(-20.0);
}];
}
#pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.sysButton.selected = NO;
self.abbr = self.models[indexPath.row].abbr;
}
#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.models.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HBMultiLanguageCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBMultiLanguageCollectionViewCell" forIndexPath:indexPath];
cell.model = self.models[indexPath.row];
if (cell.model.selected) {
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
}
return cell;
}
#pragma mark Action
- (void)systemAction {
if (!self.sysButton.selected) { // 防止重复点击
self.abbr = @"--"; // @"--" 表示跟随系统
self.sysButton.selected = YES;
[self.collectionView deselectItemAtIndexPath:self.collectionView.indexPathsForSelectedItems.firstObject animated:YES];
}
}
- (void)doneAction {
[HBMultiLanguageManager saveCurrentLanguageAbbr:self.abbr];
}
@end
@interface HBMultiLanguageCollectionViewCell()
@property (weak, nonatomic) UIButton * button;
@property (weak, nonatomic) UIImageView * imageView;
@property (weak, nonatomic) UILabel * titleLabel;
@property (weak, nonatomic) UILabel * subTitleLabel;
@end
@implementation HBMultiLanguageCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = 1.0;
self.layer.borderColor = HBColor.color_272A30.CGColor;
self.backgroundColor = HBColor.color_272A30;
[self acs_radiusWithRadius:10.0 corner:UIRectCornerAllCorners];
[self loadSubViews];
}
return self;
}
- (void)loadSubViews {
UIButton * button = [[UIButton alloc]init];
[button setImage:[UIImage imageNamed:@"publics_select_button_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"publics_select_button_selected"] forState:UIControlStateSelected];
[self addSubview:button];
self.button = button;
UIImageView * imageView = [[UIImageView alloc] init];
imageView.hidden = YES;
imageView.image = [UIImage imageNamed:@"language_country_zh"];
[self addSubview:imageView];
self.imageView = imageView;
UILabel * titleLabel = [[UILabel alloc]init];
titleLabel.text = @"中文";
titleLabel.textColor = UIColor.whiteColor;
titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];
[self addSubview:titleLabel];
self.titleLabel = titleLabel;
UILabel * subTitleLabel = [[UILabel alloc]init];
subTitleLabel.numberOfLines = 0;
subTitleLabel.text = @"选择语言后,您的系统将会切换成中文";
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.textColor = UIColor.whiteColor;
subTitleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];
[self addSubview:subTitleLabel];
self.subTitleLabel = subTitleLabel;
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.offset(8.0);
}];
[subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(8.0);
make.right.offset(-8.0);
make.bottom.offset(-18.0);
make.top.equalTo(titleLabel.mas_bottom).offset(6.0);
}];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(25.0);
make.centerX.equalTo(self);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(imageView.mas_bottom).offset(6.0);
}];
}
-(void)setModel:(HBLanguageModel *)model{
_model = model;
self.imageView.image = [UIImage imageNamed:model.imageNamed];
self.titleLabel.text = model.title;
self.subTitleLabel.text = model.subTitle;
}
-(void)setSelected:(BOOL)selected {
self.button.selected = selected;
if (selected) {
self.layer.borderColor = HBColor.color_FF0049.CGColor;
self.backgroundColor = [HBColor.color_FF0049 alpha:0.2];
} else {
self.layer.borderColor = HBColor.color_272A30.CGColor;
self.backgroundColor = HBColor.color_272A30;
}
}
@end