TimingView.m 7.8 KB
//
//  TimingView.m
//  DreamSleep
//
//  Created by peter on 2022/5/9.
//

#import "TimingView.h"

@interface TimingView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *audioTimeLab;
@property (nonatomic, strong) UIPickerView *timePickerView;
@property (nonatomic, strong) UILabel *minuteLab;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSArray *minuteDatas;
@property (nonatomic, assign) NSInteger currentTimeIndex;
@property (nonatomic, assign) NSInteger sureIndex;
@end

@implementation TimingView

- (instancetype)initWithSureBlock:(TimingBlock)timingBlock cancel:(CancelBlock)cancelBlock defalutIndex:(NSInteger)defalutIndex {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _currentTimeIndex = defalutIndex;
        _sureIndex = defalutIndex;
        _timingBlock = timingBlock;
        _cancelBlock = cancelBlock;
        
        self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0x6F7587), DSClearColor, DSWhite);
        self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.6];
        
        NSMutableArray *tmpArr = [NSMutableArray array];
        for (int i = 1; i < 121; i++) {
            [tmpArr addObject:[NSString stringWithFormat:@"%d", i]];
        }
        self.minuteDatas = [tmpArr copy];
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.titleLab];
        [self.bgView addSubview:self.closeBtn];
        [self.bgView addSubview:self.audioTimeLab];
        [self.bgView addSubview:self.timePickerView];
        [self.bgView addSubview:self.minuteLab];
        [self.bgView addSubview:self.cancelBtn];
        [self.bgView addSubview:self.sureBtn];
        [self.bgView addSubview:self.lineView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(280, 236));
            make.center.equalTo(self);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bgView);
            make.top.equalTo(self.bgView).offset(20);
        }];
        [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.titleLab);
            make.top.equalTo(self.bgView).offset(15);
            make.right.equalTo(self.bgView).offset(-15);
        }];
        [self.audioTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).offset(63);
            make.top.equalTo(self.bgView).offset(97);
        }];
        [self.timePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.audioTimeLab.mas_right);
            make.right.equalTo(self.minuteLab.mas_left);
            make.top.equalTo(self.titleLab.mas_bottom).offset(22);
            make.bottom.equalTo(self.sureBtn.mas_top).offset(-22);
        }];
        [self.minuteLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.audioTimeLab);
            make.right.equalTo(self.bgView).offset(-63);
        }];
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView);
            make.bottom.equalTo(self.bgView);
            make.height.equalTo(@64);
        }];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgView);
            make.bottom.equalTo(self.bgView);
            make.size.equalTo(self.cancelBtn);
            make.left.equalTo(self.cancelBtn.mas_right);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bgView);
            make.centerY.equalTo(self.sureBtn);
            make.size.mas_equalTo(CGSizeMake(1, 40));
        }];
    }
    return self;
}

#pragma mark - Actions
- (void)display {
    [DSKeyWindow addSubview:self];
    self.hidden = NO;
    [self.timePickerView selectRow:self.sureIndex inComponent:0 animated:NO];
}

- (void)dismissTimingView {
    self.hidden = YES;
}

- (void)sureAction {
    [self dismissTimingView];
    self.sureIndex = self.currentTimeIndex;
    if (self.timingBlock) {
        self.timingBlock(self.sureIndex, self.minuteDatas);
    }
}

- (void)cancelAction {
    [self dismissTimingView];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - UIPickerViewDataSource && UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.minuteDatas.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.currentTimeIndex = row;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *lab = [UILabel labWithTextColor:BrandColor font:SysFont(16)];
    lab.frame = CGRectMake(0.0, 0.0, 70, 35);
    lab.textAlignment = NSTextAlignmentCenter;
    lab.text = self.minuteDatas[row];
    if (@available(iOS 14.0, *)) {
        pickerView.subviews[1].backgroundColor = DSClearColor;
    }
    return lab;
}

#pragma mark - lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        [_bgView cornerRadius:24];
    }
    return _bgView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel dkLabWithText:@"设置时间" font:BoldFont(16)];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn dk_setImage:DKImagePickerWithNames(@"home_close", @"dk_home_close", @"home_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(dismissTimingView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UILabel *)audioTimeLab {
    if (!_audioTimeLab) {
        _audioTimeLab = [UILabel dkLabWithText:@"音频时长" font:SysFont(15)];
        _audioTimeLab.textAlignment = NSTextAlignmentCenter;
    }
    return _audioTimeLab;
}

- (UIPickerView *)timePickerView {
    if (!_timePickerView) {
        _timePickerView = [UIPickerView new];
        _timePickerView.delegate = self;
        _timePickerView.dataSource = self;
    }
    return _timePickerView;
}

- (UILabel *)minuteLab {
    if (!_minuteLab) {
        _minuteLab = [UILabel dkLabWithText:@"分钟" font:SysFont(15)];
        _minuteLab.textAlignment = NSTextAlignmentCenter;
    }
    return _minuteLab;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton dkBtnTitle:@"取消设置" font:SysFont(15)];
        [_cancelBtn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton btnWithTitle:@"确定" titleColor:BrandColor font:SysFont(15)];
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xE6E6E6), DarkColor, DSWhite);
    }
    return _lineView;
}

@end