DailyTaskCell.m 8.3 KB
//
//  DailyTaskCell.m
//  DreamSleep
//
//  Created by peter on 2022/6/7.
//

#import "DailyTaskCell.h"

@interface DailyTaskCell ()
@property (nonatomic, strong) UIImageView *taskIcon;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *descrLab;
@property (nonatomic, strong) UIButton *goFinishBtn;
@property (nonatomic, assign) int state;
@end

@implementation DailyTaskCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        
        [self.contentView addSubview:self.taskIcon];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.descrLab];
        [self.contentView addSubview:self.goFinishBtn];
        
        [self.taskIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(15);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(12);
            make.left.equalTo(self.taskIcon.mas_right).offset(12);
            make.right.equalTo(self.goFinishBtn.mas_left).offset(-12);
        }];
        [self.descrLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab);
            make.bottom.equalTo(self.contentView).offset(-12);
            make.right.equalTo(self.goFinishBtn.mas_left).offset(-12);
        }];
        [self.goFinishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(80, 34));
            make.right.equalTo(self.contentView).offset(-15);
        }];
    }
    return self;
}

- (void)goFinishAction {
    if (self.state == 0) { // 去完成
        if (self.delegate && [self.delegate respondsToSelector:@selector(finishWithModel:taskType:)]) {
            if (self.taskType == TaskTypeDaily) {
                [self.delegate finishWithModel:self.dailyTaskModel taskType:TaskTypeDaily];
            } else {
                [self.delegate finishWithModel:self.nTaskModel taskType:TaskTypeNew];
            }
        }
    } else if (self.state == 1) { // 待领取
        if (self.delegate && [self.delegate respondsToSelector:@selector(waitingReceiveWithTaskID:taskType:sender:)]) {
            if (self.taskType == TaskTypeDaily) {
                [self.delegate waitingReceiveWithTaskID:self.dailyTaskModel.task_id taskType:TaskTypeDaily sender:self.goFinishBtn];
            } else {
                [self.delegate waitingReceiveWithTaskID:self.nTaskModel.task_id taskType:TaskTypeNew sender:self.goFinishBtn];
            }
        }
    }
}

- (void)setDailyTaskModel:(DailyTaskModel *)dailyTaskModel {
    _dailyTaskModel = dailyTaskModel;
    
    // 任务图标处理
    [self.taskIcon yy_setImageWithURL:[NSURL URLWithString:dailyTaskModel.img_url] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
    
    // 任务名称标签处理(AI睡眠教练特殊处理(根据id进行判断))
    if (dailyTaskModel.task_id == 1) {
        NSString *str1 = dailyTaskModel.name;
        NSString *space = @" ";
        NSString *str2 = [NSString stringWithFormat:@"%d", dailyTaskModel.finish_point];
        NSString *str3 = [NSString stringWithFormat:@"/%d分", dailyTaskModel.integral];
        NSString *ai_title = [NSString stringWithFormat:@"%@%@%@%@", str1, space, str2, str3];
        self.titleLab.text = ai_title;
        
        NSMutableAttributedString *title_attText = [[NSMutableAttributedString alloc] initWithString:ai_title];
        UIColor *dkColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? MainTextColor : DkTitleColor;
        [title_attText addAttribute:NSForegroundColorAttributeName value:dkColor range:NSMakeRange(0, str1.length)];
        [title_attText addAttribute:NSForegroundColorAttributeName value:BrandColor range:NSMakeRange(str1.length + space.length, str2.length)];
        [title_attText addAttribute:NSForegroundColorAttributeName value:dkColor range:NSMakeRange(str1.length + space.length + str2.length, str3.length)];
        self.titleLab.attributedText = title_attText;
    } else {
        self.titleLab.text = dailyTaskModel.name;
    }
    
    // 任务描述标签处理
    int desc_point = dailyTaskModel.task_id == 1 ? dailyTaskModel.points : dailyTaskModel.integral;
    NSString *leftStr = [NSString stringWithFormat:@"+%d分", desc_point];
    NSString *spaceStr = @" ";
    NSString *rightStr = dailyTaskModel.task_id == 1 ? dailyTaskModel.item_name : dailyTaskModel.task_description;
    NSString *descStr = [NSString stringWithFormat:@"%@%@%@", leftStr, spaceStr, rightStr];
    self.descrLab.text = descStr;
    
    NSMutableAttributedString *desc_attText = [[NSMutableAttributedString alloc] initWithString:descStr];
    UIColor *dkColor1 = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? BrandColor : ColorFromHexA(0x62C3D5, .5);
    UIColor *dkColor2 = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? SubTitleColor : ColorFromHexA(0xFFFFFF, .5);;
    [desc_attText addAttribute:NSForegroundColorAttributeName value:dkColor1 range:NSMakeRange(0, leftStr.length)];
    [desc_attText addAttribute:NSForegroundColorAttributeName value:dkColor2 range:NSMakeRange(leftStr.length + spaceStr.length, rightStr.length)];
    self.descrLab.attributedText = desc_attText;
    
    // 完成按钮背景色
    [self dealFinishBtn:dailyTaskModel.state];
}

- (void)setNTaskModel:(NewTaskModel *)nTaskModel {
    _nTaskModel = nTaskModel;
    
    [self.taskIcon yy_setImageWithURL:[NSURL URLWithString:nTaskModel.img_url] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
    self.titleLab.text = nTaskModel.name;
    self.descrLab.text = [NSString stringWithFormat:@"+%d分", nTaskModel.integral];
    // 完成按钮背景色
    [self dealFinishBtn:nTaskModel.state];
}

- (void)dealFinishBtn:(int)state {
    self.state = state;
    BOOL isEnable = YES;
    NSString *btn_title = @"去完成";
    DKColorPicker dk_bg_color = DKColorPickerWithColors(BrandColor, ColorFromHex(0x45A4B5), DSWhite);
    DKColorPicker dk_title_color = DKColorPickerWithColors(DSWhite, DSWhite, DSWhite);
    if (state == 1) {
        btn_title = @"待领取";
        dk_bg_color = DKColorPickerWithColors(ColorFromHexA(0xF23D4F, .1), ColorFromHexA(0xF23D4F, .1), DSWhite);
        dk_title_color = DKColorPickerWithColors(ColorFromHex(0xF23D4F), ColorFromHex(0xF23D4F), DSWhite);
    } else if (state == 2) {
        isEnable = NO;
        btn_title = @"已领取";
        dk_bg_color = DKColorPickerWithColors(ColorFromHexA(0xFC7032, .1), ColorFromHexA(0xFC7032, .1), DSWhite);
        dk_title_color = DKColorPickerWithColors(ColorFromHex(0xFC7032), ColorFromHex(0xFC7032), DSWhite);
    }
    self.goFinishBtn.userInteractionEnabled = isEnable;
    [self.goFinishBtn setTitle:btn_title forState:UIControlStateNormal];
    self.goFinishBtn.dk_backgroundColorPicker = dk_bg_color;
    [self.goFinishBtn dk_setTitleColorPicker:dk_title_color forState:UIControlStateNormal];
}

#pragma mark - lazy
- (UIImageView *)taskIcon {
    if (!_taskIcon) {
        _taskIcon = [UIImageView new];
        _taskIcon.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, 1.0);
    }
    return _taskIcon;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel labWithFont:SysFont(15.0)];
        _titleLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, DkTitleColor, DSWhite);
    }
    return _titleLab;
}

- (UILabel *)descrLab {
    if (!_descrLab) {
        _descrLab = [UILabel labWithTextColor:BrandColor font:SysFont(14.0)];
    }
    return _descrLab;
}

- (UIButton *)goFinishBtn {
    if (!_goFinishBtn) {
        _goFinishBtn = [UIButton btnWithTitle:@"去完成" font:BoldFont(14.0)];
        _goFinishBtn.userInteractionEnabled = NO;
        [_goFinishBtn addTarget:self action:@selector(goFinishAction) forControlEvents:UIControlEventTouchUpInside];
        [_goFinishBtn cornerRadius:17.0];
    }
    return _goFinishBtn;
}

@end