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

#import "SetTableView.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:@"当前版本%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    NSArray *details = @[version, @"", @"", @"10.0M"];
    NSMutableArray *tmpArr = [NSMutableArray array];
    for (int i = 0; i < 4; 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, ColorFromHex(0x1F263F), DSWhite);
        self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]];
        
        self.titleLab = [UILabel dkLabWithFont:BoldFont(15.0)];
        [self addSubview:self.titleLab];
        
        self.detailLab = [UILabel labWithFont:SysFont(14.0)];
        self.detailLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHex(0x8C8F9D), DSWhite);
        [self 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).offset(15);
        make.centerY.equalTo(self);
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.accessoryView.mas_left).offset(-8);
        make.centerY.equalTo(self);
    }];
}

@end

@interface SetTableView () <UITableViewDelegate>
@property (nonatomic, strong) DSDataSource *setDataSource;
@property (nonatomic, strong) NSArray *dataArr;
@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, ColorFromHex(0x1F263F), DSWhite);
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.setDataSource addDataArray:self.dataArr];
    }
    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;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { // 应用版本升级
        // 跳转到AppStore
        [DSProgressHUD showToast:@"暂无新版本"];
    } else if (indexPath.row == 3) { // 清除缓存
        [DSProgressHUD showToast:@"清除成功"];
        SetModel *m = self.dataArr[indexPath.row];
        m.detail = @"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