SetTableView.m 10.0 KB
//
//  SetTableView.m
//  DreamSleep
//
//  Created by peter on 2022/4/17.
//

#import "SetTableView.h"
#import "DsCacheUtils.h"

/// 设置自定义数据模型
@interface SetModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *detail;
+ (NSArray *)getAllSetDatas;
@end

@implementation SetModel
+ (NSArray *)getAllSetDatas {
    NSArray *titles = @[@"当前小梦睡眠版本", @"用户协议", @"隐私政策", @"清除缓存"];
    NSString *version = [NSString stringWithFormat:@"V %@", DSAppVersion];
    NSString *cacheSize = [DsCacheUtils getCacheSize];
    NSArray *details = @[version, @"", @"", cacheSize];
    NSMutableArray *tmpArr = [NSMutableArray array];
    for (int i = 0; i < titles.count; i++) {
        SetModel *m = [SetModel new];
        m.title = titles[i];
        m.detail = details[i];
        [tmpArr addObject:m];
    }
    return [tmpArr copy];
}
@end

/// 设置自定义Cell
@interface SetCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *detailLab;
@property (nonatomic, strong) UIImageView *rightIcon;
- (void)updateConstraintsWithModel:(SetModel *)model;
@end

@implementation SetCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
        self.rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]];
        [self.contentView addSubview:self.rightIcon];
        
        self.titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
        [self.contentView addSubview:self.titleLab];
        
        self.detailLab = [UILabel labWithFont:SysFont(14.0)];
        self.detailLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHex(0x8C8F9D), DSWhite);
        [self.contentView addSubview:self.detailLab];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
    self.selectedBackgroundView.backgroundColor = DSClearColor;
    [super setSelected:selected animated:animated];
}

- (void)updateConstraintsWithModel:(SetModel *)model {
    self.titleLab.text = model.title;
    self.detailLab.text = model.detail;
    [self.titleLab sizeToFit];
    [self.detailLab sizeToFit];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightIcon.mas_left).offset(-8);
        make.centerY.equalTo(self.contentView);
    }];
    [self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-8);
    }];
}

@end

@interface SetTableView () <UITableViewDelegate>
@property (nonatomic, strong) DSDataSource *setDataSource;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UIImageView *slideView;
@end

@implementation SetTableView

- (instancetype)initWithDelegate:(id<SetTableViewDelegate>)delegate {
    if (self = [super initWithFrame:CGRectMake(15, 15, kScreenWidth - 30, 348) style:UITableViewStylePlain]) {
        self.dataArr = [SetModel getAllSetDatas];
        self.setDelegate = delegate;
        [self cornerRadius:10];
        self.scrollEnabled = NO;
        self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.setDataSource addDataArray:self.dataArr];
        self.tableFooterView = self.footView;
    }
    return self;
}

- (DSDataSource *)setDataSource {
    if (!_setDataSource) {
        CellConfigureBlock cellBlock = ^(SetCell * cell, SetModel * model, NSIndexPath * indexPath) {
            [cell updateConstraintsWithModel:model];
        };
        NSString * const setCellID = @"SetCellID";
        _setDataSource = [[DSDataSource alloc] initWithIdentifier:setCellID datas:@[] isSection:NO configureBlock:cellBlock];
        self.dataSource = _setDataSource;
        self.delegate = self;
        [self registerClass:[SetCell class] forCellReuseIdentifier:setCellID];
    }
    return _setDataSource;
}

- (UIView *)footView {
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 200)];
        
        UILabel *titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
        titleLab.text = @"主题模式";
        [titleLab sizeToFit];
        [_footView addSubview:titleLab];
        
        UIView *longView = [UIView new];
        [longView cornerRadius:10];
        longView.dk_backgroundColorPicker = DKColorPickerWithColors(BGColor, DarkColor, DSWhite);
        [_footView addSubview:longView];
        CGFloat leftRightSpace = 24;
        CGFloat btnH = 20;
        CGFloat btnW = (self.width - 2*leftRightSpace)/3.0;
        for (int i = 0; i < 3; i++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
            btn.tag = i;
            [btn addTarget:self action:@selector(switchThemeAction:) forControlEvents:UIControlEventTouchUpInside];
            [longView addSubview:btn];
        }
        _slideView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slideIcon"]];
        [_footView addSubview:_slideView];
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_footView).offset(15);
            make.top.equalTo(_footView);
        }];
        [longView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_footView).offset(leftRightSpace);
            make.top.equalTo(titleLab.mas_bottom).offset(24);
            make.right.equalTo(_footView).offset(-leftRightSpace);
            make.height.equalTo(@(btnH));
        }];
        [_slideView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(longView);
            make.width.equalTo(@50);
            make.height.equalTo(@34);
            
            // slideView初始化位置
            if (kGetUserDefaultsBOOL(ThemeAutoSwitch)) { // 自动切换状态
                make.centerX.equalTo(_footView);
            } else {
                if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal]) {
                    make.left.equalTo(_footView).offset(19);
                } else {
                    make.right.equalTo(_footView).offset(-19);
                }
            }
        }];
        
        NSArray *themes = @[@"日间模式", @"自动切换", @"夜间模式"];
        for (int i = 0; i < 3; i++) {
            UILabel *lab = [UILabel labWithText:themes[i] font:SysFont(14.0) fit:YES];
            lab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, DSWhite, DSWhite);
            [_footView addSubview:lab];
            
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(longView.mas_bottom).offset(8);
            }];
            if (i == 0) {
                [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(_footView).offset(8);
                    make.bottom.equalTo(_footView).offset(-19);
                }];
            } else if (i == 1) {
                [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(_footView);
                }];
            } else if (i == 2) {
                [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(_footView).offset(-8);
                }];
            }
        }
    }
    return _footView;
}

- (void)switchThemeAction:(UIButton *)sender {
    WS(weakSelf);
    if (sender.tag == 1) { // 自动切换
        [UIView animateWithDuration:.3 animations:^{
            weakSelf.slideView.centerX = self.footView.centerX;
        }];
        
        // 处理自动切换逻辑
        if ([NSDate judgeTimeWithStartTime:StartTime1 expireTime:ExpireTime1] || [NSDate judgeTimeWithStartTime:StartTime2 expireTime:ExpireTime2]) {
            self.dk_manager.themeVersion = DKThemeVersionNight;
        } else {
            self.dk_manager.themeVersion = DKThemeVersionNormal;
        }
        
        // 保存自动切换状态
        kSetUserDefaultsBOOL(YES, ThemeAutoSwitch);
        kUserDefaultsSynchronize;
    } else {
        CGFloat x = sender.tag == 0 ? 19 : self.footView.width - self.slideView.width - 19;
        [UIView animateWithDuration:.3 animations:^{
            weakSelf.slideView.x = x;
        }];
        self.dk_manager.themeVersion = sender.tag == 0 ? DKThemeVersionNormal : DKThemeVersionNight;
        [BaseNaviController setNavigationBarStyle:NaviStyleDefault vc:self.ds_viewController];
        
        kSetUserDefaultsBOOL(NO, ThemeAutoSwitch);
        kUserDefaultsSynchronize;
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
    } else if (indexPath.row == 3) { // 清除缓存
        [DsCacheUtils cleanCacheWithSuccess:^(BOOL success) {
        }];
        [DSProgressHUD showToast:@"清除成功"];
        SetModel *m = self.dataArr[indexPath.row];
        m.detail = @"0.0M";
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    } else {
        if (self.setDelegate && [self.setDelegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) {
            [self.setDelegate didSelectRowAtIndexPath:indexPath];
        }
    }
}

@end