NoiseListController.m 5.1 KB
//
//  NoiseListController.m
//  DreamSleep
//
//  Created by peter on 2022/5/11.
//

#import "NoiseListController.h"
#import "WhiteNoiseRequestModel.h"
#import "NoisePlayerManager.h"

@interface NoiseListController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *noiseAudioView;
@property (nonatomic, strong) NSArray *noiseAudioArr;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end

@implementation NoiseListController {
    NSInteger _typeID;
}

- (instancetype)initWithNoiseTypeID:(NSInteger)typeID {
    if (self = [super init]) {
        // 白噪音类型id
        _typeID = typeID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    [self.view addSubview:self.noiseAudioView];
    
    [self queryRelaxWhiteNoiseAudiosRequest];
}

- (void)queryRelaxWhiteNoiseAudiosRequest {
    [WhiteNoiseRequestModel queryRelaxWhiteNoiseAudiosWithTypeID:_typeID completion:^(WhiteNoiseRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            self.exceptionView.hidden = YES;
            self.noiseAudioArr = requestModel.noiseAudioArr;
            [self.noiseAudioView reloadData];
            // 重置
            [[NoisePlayerManager sharedNoisePlayerManager] removeAllNoiseAudioCell];
        } else {
            self.exceptionView.hidden = NO;
        }
    }];
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.noiseAudioArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NoiseAudioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NoiseAudioCellID" forIndexPath:indexPath];
    NoiseAudioModel *model = self.noiseAudioArr[indexPath.row];
    cell.model = model;
    
    // 处理数据刷新后之前已经被选中的cell需要恢复被选中状态并配置播放器(只能根据音频id判断),暂不处理
    //    NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList;
    //    for (__strong NoiseAudioCell *oldCell in playList) {
    //        if (cell.model.noise_audio_id == oldCell.model.noise_audio_id) {
    //            cell.audioIV.selected = YES;
    //            cell.model = oldCell.model;
    //            // 将oldCell替换成新的cell
    //            oldCell = cell;
    //            // 替换model
    //            model = oldCell.model;
    //        }
    //    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NoiseAudioModel *model = [self.noiseAudioArr objectAtIndex:indexPath.row];
    NoiseAudioCell *cell = (NoiseAudioCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
    
    // 白噪音未被选择则添加到播放列表
    if (cell.audioIV.selected == NO) {
        if ([NoisePlayerManager sharedNoisePlayerManager].playCellList.count >= 8) {
            [DSProgressHUD showToast:@"音频最多同时播放8个"];
            return;
        }
        NoisePlayItem *playItem = [[NoisePlayItem alloc] initWithUrl:model.audio_url volume:.5];
        playItem.isSeamlessLoopPlayback = YES;
        cell.playItem = playItem;
        [[NoisePlayerManager sharedNoisePlayerManager] addNoiseAudioCell:cell];
    } else {
        cell.playItem = nil;
        [[NoisePlayerManager sharedNoisePlayerManager] removeNoiseAudioCell:cell];
    }
    cell.audioIV.selected = !cell.audioIV.selected;
}

#pragma mark - lazy
- (UICollectionView *)noiseAudioView {
    if (!_noiseAudioView) {
        CGFloat space = (kScreenWidth - 240 - 40)/3.0;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(15, 20, 0, 20);
        layout.itemSize = CGSizeMake(60, 53);
        layout.minimumLineSpacing = 12;
        layout.minimumInteritemSpacing = space;
        _noiseAudioView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 327) collectionViewLayout:layout];
        _noiseAudioView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        _noiseAudioView.delegate = self;
        _noiseAudioView.dataSource = self;
        [_noiseAudioView registerClass:[NoiseAudioCell class] forCellWithReuseIdentifier:@"NoiseAudioCellID"];
    }
    return _noiseAudioView;
}

- (NSArray *)noiseAudioArr {
    if (!_noiseAudioArr) {
        _noiseAudioArr = [NSArray array];
    }
    return _noiseAudioArr;
}

- (ExceptionDefaultView *)exceptionView {
    if (!_exceptionView) {
        WS(weakSelf);
        _exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeNet block:^{
            weakSelf.exceptionView.hidden = YES;
            [weakSelf queryRelaxWhiteNoiseAudiosRequest];
        } superView:self.noiseAudioView];
    }
    return _exceptionView;
}

@end