StartReadyView.m 8.3 KB
//
//  StartReadyView.m
//  DreamSleep
//
//  Created by peter on 2022/7/13.
//

#import "StartReadyView.h"
#import "ReadyItem.h"

@interface LineView : UIView
@end

@implementation LineView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        UILabel *lineLab = [UILabel labWithText:@"- - - - - - - - - - - - - - - -" font:SysFont(10) fit:YES];
        lineLab.lineBreakMode = NSLineBreakByClipping;
        lineLab.dk_textColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite);
        [self addSubview:lineLab];
        
        UIView *circle = [UIView new];
        circle.layer.borderWidth = 1.0;
        circle.layer.dk_borderColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite);
        [circle cornerRadius:3.0];
        [self addSubview:circle];
        
        UIView *center = [UIView new];
        [center cornerRadius:1.0];
        center.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite);
        [self addSubview:center];
        
        [lineLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.right.equalTo(circle.mas_left);
        }];
        [circle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@6.0);
            make.top.bottom.right.equalTo(self);
        }];
        [center mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@2.0);
            make.center.equalTo(circle);
        }];
    }
    return self;
}
@end

@interface ItemCell : UIView
@property (nonatomic, strong) ReadyItem *item;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *timeLab;
@end

@implementation ItemCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.icon];
        [self addSubview:self.nameLab];
        [self addSubview:self.timeLab];
        
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.centerX.equalTo(self);
            make.width.height.equalTo(@40);
        }];
        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.icon.mas_bottom).offset(16);
            make.centerX.equalTo(self);
            make.left.equalTo(self).offset(1);
            make.right.equalTo(self).offset(-1);
        }];
        [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.bottom.equalTo(self);
            make.left.equalTo(self).offset(2);
            make.right.equalTo(self).offset(-2);
        }];
    }
    return self;
}

- (void)setItem:(ReadyItem *)item {
    _item = item;
    
    [self.icon yy_setImageWithURL:[NSURL URLWithString:item.icon] options:YYWebImageOptionShowNetworkActivity];
    self.nameLab.text = item.item_name;
    self.timeLab.text = [NSString stringWithFormat:@"%dmin", item.time];
}

- (UIImageView *)icon {
    if (!_icon) {
        _icon = [UIImageView new];
    }
    return _icon;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel labWithFont:BoldFont(14.0)];
        _nameLab.textAlignment = NSTextAlignmentCenter;
        _nameLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    }
    return _nameLab;
}

- (UILabel *)timeLab {
    if (!_timeLab) {
        _timeLab = [UILabel labWithFont:SysFont(12.0)];
        _timeLab.textAlignment = NSTextAlignmentCenter;
        _timeLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
    }
    return _timeLab;
}
@end

@interface StartReadyView ()
@property (nonatomic, strong) UIImageView *headIV;
@property (nonatomic, strong) UIButton *startBtn;
@property (nonatomic, strong) NSArray *adjustedItems;
@property (nonatomic, strong) NSMutableArray *adjustedCells;
@property (nonatomic, strong) NSMutableArray *sepLines;
@property (nonatomic, strong) UILabel *adviceMessageLab;
@end

@implementation StartReadyView

- (instancetype)initWithDelegate:(id<StartReadyViewDelegate>)delegate {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _delegate = delegate;
        
        self.adjustedItems = [NSArray array];
        self.adjustedCells = [NSMutableArray array];
        self.sepLines = [NSMutableArray array];
        
        [self addSubview:self.headIV];
        [self addSubview:self.startBtn];
        [self addSubview:self.adviceMessageLab];

        [self.headIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
        }];
        [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(155, 40));
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).offset(-94-Bottom_SafeArea_Height);
        }];
        [self.adviceMessageLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.equalTo(self).offset(5);
            make.right.equalTo(self).offset(-5);
            make.top.equalTo(self.startBtn.mas_bottom).offset(10);
        }];
    }
    return self;
}

- (void)updateAdjustView:(NSArray *)adjustedItems {
    // 移除所有cell
    for (ItemCell *cell in self.adjustedCells) {
        [cell removeFromSuperview];
    }
    [self.adjustedCells removeAllObjects];
    // 移除所有分割线
    for (LineView *lineV in self.sepLines) {
        [lineV removeFromSuperview];
    }
    [self.sepLines removeAllObjects];
    
    // 新建所有cell和分割线
    self.adjustedItems = [adjustedItems copy];
    
    CGFloat itemH = 102;
    CGFloat y = 310;
    CGFloat left_right_margin = 30;
    CGFloat itemW = (self.width - 2*left_right_margin)/adjustedItems.count;
    for (int index = 0; index < adjustedItems.count; index++) {
        ItemCell *cell = [[ItemCell alloc] initWithFrame:CGRectMake(left_right_margin + index*itemW, y, itemW, itemH)];
        cell.item = adjustedItems[index];
        [self addSubview:cell];
        [self.adjustedCells addObject:cell];
    }
    
    if (adjustedItems.count > 1) { // 至少是2个cell
        for (int index = 0; index < adjustedItems.count - 1; index++) {
            ItemCell *leftCell = self.adjustedCells[index > 0 ? index : 0];
            ItemCell *rightCell = self.adjustedCells[index + 1];
            
            LineView *lineView = [LineView new];
            [self addSubview:lineView];
            [self.sepLines addObject:lineView];
            
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(leftCell.icon.mas_right).offset(0);
                make.right.equalTo(rightCell.icon.mas_left).offset(0);
                make.height.equalTo(@6);
                make.centerY.equalTo(leftCell.icon);
            }];
        }
    }
}

- (void)updateAdviceMessage:(NSString *)adviceMessage {
    self.adviceMessageLab.text = adviceMessage;
}

#pragma mark - lazy
- (UIImageView *)headIV {
    if (!_headIV) {
        _headIV = [UIImageView new];
        _headIV.dk_imagePicker = DKImagePickerWithNames(@"bg_anshui_start", @"dk_bg_anshui_start", @"bg_anshui_start");
    }
    return _headIV;
}

- (UIButton *)startBtn {
    if (!_startBtn) {
        WS(weakSelf);
        _startBtn = [UIButton btnWithTitle:@"开始仪式" font:BoldFont(16.0)];
        [_startBtn cornerRadius:20.0];
        [_startBtn dk_setBackgroundColorPicker:DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite)];
        [_startBtn dk_setTitleColorPicker:DKColorPickerWithColors(DSWhite, DkTitleColor, DSWhite) forState:UIControlStateNormal];
        [_startBtn addTouchUpInsideHandler:^(NSInteger tag) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(startTask)]) {
                [weakSelf.delegate startTask];
            }
        }];
    }
    return _startBtn;
}

- (UILabel *)adviceMessageLab {
    if (!_adviceMessageLab) {
        _adviceMessageLab = [UILabel labWithFont:SysFont(12.0)];
        _adviceMessageLab.textAlignment = NSTextAlignmentCenter;
        _adviceMessageLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
    }
    return _adviceMessageLab;
}

@end