SleepReadyController.m 5.7 KB
//
//  SleepReadyController.m
//  DreamSleep
//
//  Created by peter on 2022/7/11.
//

#import "SleepReadyController.h"
#import "ReadyListController.h"
#import "NoisePlayerManager.h"
#import "WhiteNoiseRequestModel.h"
#import "SRTipsView.h"
#import "SRMusicView.h"
#import "StartReadyView.h"
#import "ReadyItemView.h"

@interface SleepReadyController () <ReadyListControllerDelegate, StartReadyViewDelegate>
@property (nonatomic, strong) UIButton *tipsBtn;
@property (nonatomic, strong) UIButton *setBtn;
@property (nonatomic, strong) SRTipsView *sRTipsView;
@property (nonatomic, strong) StartReadyView *startReadyView;
@property (nonatomic, strong) ReadyItemView *readyItemView;
@property (nonatomic, strong) UIButton *musicBtn;
@property (nonatomic, strong) SRMusicView *musicView;
@end

@implementation SleepReadyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buildUI];
    
    // 暂停白噪音
    [[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
    // 获取白噪音类型请求
    [self queryRelaxWhiteNoiseType];
}

- (void)queryRelaxWhiteNoiseType {
    [WhiteNoiseRequestModel queryRelaxWhiteNoiseTypeWithCompletion:^(WhiteNoiseRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            DSLog(@"获取白噪音类型请求成功...");
            [self.musicView refreshNoiseTypeData:requestModel.noiseTypeArr];
        }
    }];
}

- (void)backAction {
    // 暂停白噪音
    [[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
    
    [super backAction];
}

#pragma mark - ReadyListControllerDelegate

#pragma mark - StartReadyViewDelegate
- (void)startTask {
    self.tipsBtn.hidden = YES;
    self.setBtn.hidden = YES;
    self.startReadyView.hidden = YES;
    [self.readyItemView start];
    self.naviTitle = @"洗澡";
}

#pragma mark - UI
- (void)buildUI {
    self.naviTitle = @"睡前准备";
    self.naviBarAlpha = 1.0;
    self.dsNaviBar.dk_backgroundColorPicker = DKColorPickerWithColors(DSClearColor, DSClearColor, DSWhite);
    [self.dsNaviBar addSubview:self.backBtn];
    self.titleLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    [self.backBtn dk_setImage:DKImagePickerWithNames(@"cus_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
    
    [self.dsNaviBar addSubview:self.tipsBtn];
    [self.dsNaviBar addSubview:self.setBtn];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    [self.view insertSubview:self.startReadyView atIndex:0];
    [self.view insertSubview:self.readyItemView atIndex:1];
    [self.view addSubview:self.musicBtn];
    
    [self.tipsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab);
    }];
    [self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tipsBtn);
        make.left.equalTo(self.tipsBtn.mas_right).offset(24);
        make.right.equalTo(self.view).offset(-15);
    }];
    [self.musicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.view).offset(-94-Bottom_SafeArea_Height);
    }];
    [self.startReadyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.readyItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    // 首次弹提示框
    if (!kGetUserDefaultsBOOL(IsFirstSRTipsAlert)) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.sRTipsView showTipsAlertView];
            kSetUserDefaultsBOOL(YES, IsFirstSRTipsAlert);
            kUserDefaultsSynchronize;
        });
    }
}

#pragma mark - lazy
- (UIButton *)tipsBtn {
    if (!_tipsBtn) {
        WS(weakSelf);
        _tipsBtn = [UIButton new];
        [_tipsBtn dk_setImage:DKImagePickerWithNames(@"ic_zhunbei_zhushi", @"dk_ic_zhunbei_zhushi", @"ic_zhunbei_zhushi") forState:UIControlStateNormal];
        [_tipsBtn addTouchUpInsideHandler:^(NSInteger tag) {
            [weakSelf.sRTipsView showTipsAlertView];
        }];
    }
    return _tipsBtn;
}

- (UIButton *)setBtn {
    if (!_setBtn) {
        WS(weakSelf);
        _setBtn = [UIButton new];
        [_setBtn dk_setImage:DKImagePickerWithNames(@"ic_zhunbei_set", @"dk_ic_zhunbei_set", @"ic_zhunbei_set") forState:UIControlStateNormal];
        [_setBtn addTouchUpInsideHandler:^(NSInteger tag) {
            ReadyListController *setListVC = [[ReadyListController alloc] initWithDelegate:weakSelf];
            [weakSelf.navigationController pushViewController:setListVC animated:YES];
        }];
    }
    return _setBtn;
}

- (SRTipsView *)sRTipsView {
    if (!_sRTipsView) {
        _sRTipsView = [SRTipsView new];
    }
    return _sRTipsView;
}

- (StartReadyView *)startReadyView {
    if (!_startReadyView) {
        _startReadyView = [[StartReadyView alloc] initWithDelegate:self];
    }
    return _startReadyView;
}

- (ReadyItemView *)readyItemView {
    if (!_readyItemView) {
        _readyItemView = [ReadyItemView new];
    }
    return _readyItemView;
}

- (UIButton *)musicBtn {
    if (!_musicBtn) {
        WS(weakSelf);
        _musicBtn = [UIButton new];
        [_musicBtn setImage:[UIImage imageNamed:@"ic_zhunbei_baizaoyin"] forState:UIControlStateNormal];
        [_musicBtn addTouchUpInsideHandler:^(NSInteger tag) {
            [weakSelf.musicView showMusicView];
        }];
    }
    return _musicBtn;
}

- (SRMusicView *)musicView {
    if (!_musicView) {
        _musicView = [[SRMusicView alloc] initWithFrame:CGRectZero];
    }
    return _musicView;
}

@end