RelaxTrainController.m 7.6 KB
//
//  RelaxTrainController.m
//  DreamSleep
//
//  Created by peter on 2022/6/1.
//

#import "RelaxTrainController.h"
#import "RelaxTrainRequestModel.h"
#import "RelaxBodyView.h"
#import "SleepReadyDoneView.h"
#import "UnityGameController.h"

@interface RelaxTrainController () <RelaxBodyViewDelegate, UIScrollViewDelegate, SleepReadyDoneViewDelegate>
@property (nonatomic, strong) UIImageView *bgIV;
@property (nonatomic, strong) UILabel *cusTitleLab;
@property (nonatomic, strong) NSArray *listArr;
@property (nonatomic, strong) UIScrollView *lucencyView;
@property (nonatomic, strong) RelaxBodyView *bodyView;
@property (nonatomic, strong) SleepReadyDoneView *srDoneAlertView;
@end

@implementation RelaxTrainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    [self.view addSubview:self.bgIV];
    [self.view addSubview:self.lucencyView];
    [self.lucencyView addSubview:self.bodyView];
    [self.view addSubview:self.dsNaviBar];
    [self.view addSubview:self.dkBackBtn];
    [self.dkBackBtn dk_setImage:DKImagePickerWithNames(@"cus_back_icon", @"cus_back_icon", @"dk_cus_back_icon") forState:UIControlStateNormal];
    [self.view addSubview:self.cusTitleLab];
    
    CGFloat iv_h = kScreenWidth*1233/1125.0;
    [self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(iv_h));
    }];
    [self.cusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.dsNaviBar);
        make.bottom.equalTo(self.dsNaviBar).offset(-9);
    }];
    [self.lucencyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    [RelaxTrainRequestModel queryRelaxAudioListWithCompletion:^(RelaxTrainRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            if (requestModel.trainAudioList.count) {
                self.listArr = requestModel.trainAudioList;
                if (self.listArr && self.listArr.count > 0) {
                    NSInteger currentIndex = [self getIndexWithListData:self.listArr];
                    [self.bodyView refreshData:self.listArr currentIndex:currentIndex];
                }
            }
        }
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
    
    [DataStatisticsUtil event:RelaxTrain attributes:@{@"data":@"腹式放松法页面"}];
}

#pragma mark - 根据title匹配下标索引
- (NSInteger)getIndexWithListData:(NSArray *)listData {
    __block NSInteger currentIndex = 0;
    if (!self.params) {
        return currentIndex;
    }
    NSString *title = self.params[@"title"];
    // 放松音频随机选一个
    if ([title isEqualToString:@"练习放松训练"]) {
        currentIndex = arc4random() % listData.count;
    } else {
        [listData enumerateObjectsUsingBlock:^(RelaxTrainModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.audio_name containsString:title]) {
                currentIndex = idx;
                *stop = YES;
            }
        }];
    }
    return currentIndex;
}

#pragma mark - Actions
- (void)backAction {
    [super backAction];
    [self.bodyView stopAudio];
    if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(reloadAIPage)]) {
        [self.refreshDelegate reloadAIPage];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - RelaxBodyViewDelegate
- (void)updateNaviTitleAndBgImg:(RelaxTrainModel *)trainModel {
    [self.bgIV setImageWithURL:[NSURL URLWithString:trainModel.bg_url] placeholderImage:[UIImage imageNamed:@"basicPlaceholder"]];
    self.cusTitleLab.text = trainModel.audio_name;
    self.titleLab.text = trainModel.audio_name;
}

- (void)showScoreAlertView {
    SRFinishModel *finishModel = self.finishModel;
    finishModel.isCoax = YES;
    [self.srDoneAlertView showSleepReadyFinishView:finishModel];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    CGFloat offset = scrollView.contentOffset.y;
    if (offset >= self.lucencyView.contentSize.height - kScreenHeight) {
        self.dkBackBtn.hidden = YES;
        self.dsNaviBar.alpha = 1;
        self.titleLab.hidden = NO;
        self.cusTitleLab.hidden = YES;
        self.isNeedUpdateStatusBarStyle = YES;
    }
    if (offset <= 0) {
        self.dkBackBtn.hidden = NO;
        self.dsNaviBar.alpha = 0;
        self.titleLab.hidden = YES;
        self.cusTitleLab.hidden = NO;
        self.isNeedUpdateStatusBarStyle = NO;
    }
}

#pragma mark - SleepReadyDoneViewDelegate
- (void)enterDiffModule:(SRFinishAlertType)type {
    if (type == SRFinishAlertTypeNormal) {
        // 返回AI睡眠教练
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else if (type == SRFinishAlertTypeCoax) {
        // 进入轻拍哄睡
        UnityGameController *gameVC = [UnityGameController new];
        gameVC.gameType = GameTypeCoax;
        [self.navigationController pushViewController:gameVC animated:YES];
        
        // 移除放松训练页面
        NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        [vcs enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[RelaxTrainController class]]) {
                *stop = YES;
                [vcs removeObject:obj];
            }
        }];
        self.navigationController.viewControllers = [vcs copy];
    }
}

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

- (UILabel *)cusTitleLab {
    if (!_cusTitleLab) {
        _cusTitleLab = [UILabel labWithTextColor:MainTextColor font:BoldFont(18)];
        _cusTitleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _cusTitleLab;
}

- (NSArray *)listArr {
    if (!_listArr) {
        _listArr = [NSArray array];
    }
    return _listArr;
}

- (UIScrollView *)lucencyView {
    if (!_lucencyView) {
        _lucencyView = [UIScrollView new];
        _lucencyView.showsVerticalScrollIndicator = NO;
        _lucencyView.delegate = self;
        CGFloat bodyTopMargin = 351;
        CGFloat bodyH = 420 + Bottom_SafeArea_Height;
        _lucencyView.contentSize = CGSizeMake(kScreenWidth, bodyH + bodyTopMargin);
        _lucencyView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _lucencyView;
}

- (RelaxBodyView *)bodyView {
    if (!_bodyView) {
        _bodyView = [[RelaxBodyView alloc] initWithFrame:CGRectMake(0, 351, kScreenWidth, 420 + Bottom_SafeArea_Height)];
        _bodyView.delegate = self;
        _bodyView.isSleepReady = self.finishModel ? YES : NO;
        _bodyView.updateParams = self.params;
    }
    return _bodyView;
}

- (SleepReadyDoneView *)srDoneAlertView {
    if (!_srDoneAlertView) {
        _srDoneAlertView = [[SleepReadyDoneView alloc] init];
        _srDoneAlertView.delegate = self;
    }
    return _srDoneAlertView;
}

#pragma mark - 设置状态栏文字颜色(重写父类)
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.isNeedUpdateStatusBarStyle) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

#pragma mark - setter
- (void)setFinishModel:(SRFinishModel *)finishModel {
    _finishModel = finishModel;
    NSDictionary *task_relax = finishModel.task_relax;
    self.params = @{@"title":task_relax[@"title"], @"step":task_relax[@"step"]};
}

@end