AudioDetailHeaderView.m 5.1 KB
//
//  AudioDetailHeaderView.m
//  DreamSleep
//
//  Created by peter on 2022/5/6.
//

#import "AudioDetailHeaderView.h"

@interface AudioDetailHeaderView ()
@property (nonatomic, strong) UIImageView *bgIV;
@property (nonatomic, strong) UIImageView *audioIV;
@property (nonatomic, strong) UILabel *audioNameLab;
@property (nonatomic, strong) UILabel *audioDescLab;
@property (nonatomic, strong) UIView *cornerView;
@end

@implementation AudioDetailHeaderView

- (instancetype)initWithModel:(CourseModel *)model courseType:(CourseType)courseType {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, courseType == CourseTypeSafe ? 250 : 300)]) {
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        self.clipsToBounds = YES;
        
        [self addSubview:self.bgIV];
        [self addSubview:self.audioIV];
        [self addSubview:self.audioNameLab];
        
        if (courseType == CourseTypeSafe) {
            [self addSubview:self.audioDescLab];
            [self addSubview:self.cornerView];
            
            self.audioDescLab.text = model.audio_desc;
        }
        
        [self.bgIV yy_setImageWithURL:[NSURL URLWithString:model.bg_url] placeholder:[UIImage imageNamed:@"bannerPlaceholder"]];
        [self.audioIV yy_setImageWithURL:[NSURL URLWithString:model.audio_img] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
        self.audioNameLab.text = model.audio_name;
        self.audioNameLab.font = courseType == CourseTypeSafe ? BoldFont(16.0) : BoldFont(18.0);
        
        [self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
            if (courseType == CourseTypeSafe) {
                make.edges.equalTo(self);
            } else {
                make.top.left.right.equalTo(self);
                make.height.equalTo(@210);
            }
        }];
        [self.audioIV mas_makeConstraints:^(MASConstraintMaker *make) {
            if (courseType == CourseTypeSafe) {
                make.left.equalTo(self).offset(17);
                make.top.equalTo(self).offset(105);
                make.width.height.equalTo(@100);
            } else {
                make.centerX.equalTo(self);
                make.width.height.equalTo(@118);
                make.top.equalTo(self).offset(118);
            }
        }];
        [self.audioNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (courseType == CourseTypeSafe) {
                make.left.equalTo(self.audioIV.mas_right).offset(30);
                make.top.equalTo(self.audioIV);
            } else {
                make.centerX.equalTo(self);
                make.top.equalTo(self.audioIV.mas_bottom).offset(15);
            }
        }];
        
        if (courseType == CourseTypeSafe) {
            [self.audioDescLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.audioNameLab);
                make.top.equalTo(self.audioNameLab.mas_bottom).offset(10);
                make.right.equalTo(self).offset(-17);
            }];
            [self.cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                make.bottom.equalTo(self).offset(20);
                make.height.equalTo(@40);
            }];
        }
        // 更新布局
        [self layoutIfNeeded];
        if (courseType == CourseTypeRelax) {
            // 添加遮罩
            if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
                [self.bgIV addMaskWithType:MaskTypeCornerBottom cornerRadius:22.0];
                [self.audioIV addMaskWithType:MaskTypeAllCorner cornerRadius:15.5];
            }
            [self.bgIV setCornerRadiusRect:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadius:22.0];
        } else {
            [self.audioIV cornerRadius:15.5];
            self.bgIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .3, .5);
            self.audioIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .3, .5);
        }
    }
    return self;
}

#pragma mark - lazy
- (UIImageView *)bgIV {
    if (!_bgIV) {
        _bgIV = [UIImageView new];
        _bgIV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bgIV;
}

- (UIImageView *)audioIV {
    if (!_audioIV) {
        _audioIV = [UIImageView new];
    }
    return _audioIV;
}

- (UILabel *)audioNameLab {
    if (!_audioNameLab) {
        _audioNameLab = [UILabel labWithFont:BoldFont(16.0)];
        _audioNameLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHex(0xE8E9E9), DSWhite);
    }
    return _audioNameLab;
}

- (UILabel *)audioDescLab {
    if (!_audioDescLab) {
        _audioDescLab = [UILabel labWithFont:SysFont(14.0)];
        _audioDescLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        _audioDescLab.numberOfLines = 0;
    }
    return _audioDescLab;
}

- (UIView *)cornerView {
    if (!_cornerView) {
        _cornerView = [UIView new];
        _cornerView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        [_cornerView cornerRadius:24.0];
    }
    return _cornerView;
}

@end