NoisePlayView.m 7.2 KB
//
//  NoisePlayView.m
//  DreamSleep
//
//  Created by peter on 2022/5/13.
//

#import "NoisePlayView.h"
#import "NoisePlayCell.h"
#import "NoisePlayerManager.h"

@interface NoisePlayView () <UITableViewDelegate, UITableViewDataSource, NoisePlayCellDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) NSArray *audioArr;
@property (nonatomic, strong) UITableView *playListView;
@property (nonatomic, strong) UIButton *oneClickBtn;
@end

@implementation NoisePlayView

- (instancetype)init {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHexA(0x161E38, .6), ColorFromHexA(0x161E38, .6), DSWhite);
        
        [self addSubview:self.headView];
        [self addSubview:self.playListView];
        [self addSubview:self.oneClickBtn];
        
        [self.oneClickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(155, 40));
            make.bottom.equalTo(self).offset(-Bottom_SafeArea_Height-50);
        }];
    }
    return self;
}

- (void)showNoisePlayViewWith:(BOOL)selected {
    [DSKeyWindow addSubview:self];
    
    NSArray *playCellList = [NoisePlayerManager sharedNoisePlayerManager].playItemList;
    if (playCellList) {
        self.audioArr = playCellList;
        [self.playListView reloadData];
    }
    
    self.oneClickBtn.selected = selected;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    self.oneClickBtn.selected = selected;
}

#pragma mark - Actions
- (void)oneClickAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [[NoisePlayerManager sharedNoisePlayerManager] playAll];
    } else {
        [[NoisePlayerManager sharedNoisePlayerManager] pauseAll];
    }
}

- (void)clearAllAction:(UIButton *)sender {
    [[NoisePlayerManager sharedNoisePlayerManager] removeAllNoisePlayItem];
    self.audioArr = @[];
    [self.playListView reloadData];
    [self removeFromSuperview];
}

- (void)closeAction:(UIButton *)sender {
    [self removeFromSuperview];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.audioArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"NoisePlayCellID";
    NoisePlayCell *cell = (NoisePlayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[NoisePlayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.delegate = self;
    NoisePlayItem *item = self.audioArr[indexPath.row];
    cell.item = item;
    cell.indexPath = indexPath;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 77;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 93 + Bottom_SafeArea_Height;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 93 + Bottom_SafeArea_Height)];
}

#pragma mark - NoisePlayCellDelegate
- (void)didRemoveItem:(NSIndexPath *)indexPath {
    NoisePlayItem *item = self.audioArr[indexPath.row];
    // 删除数据源
    NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:self.audioArr];
    [tmpArr removeObjectAtIndex:indexPath.row];
    self.audioArr = [tmpArr copy];
    [[NoisePlayerManager sharedNoisePlayerManager] removeNoisePlayItemWithAudioID:item.noise_audio_id];
    // 执行删除动画
    [self.playListView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    dispatch_after(.3, dispatch_get_main_queue(), ^{
        [self.playListView reloadData];
    });
    if (self.audioArr.count == 0) { [self removeFromSuperview]; }
}

- (void)didSetVolume:(float)volume indexPath:(NSIndexPath *)indexPath {
    NoisePlayItem *item = self.audioArr[indexPath.row];
    item.volume = volume;
}

#pragma mark - lazy
- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.width, 60)];
        _headView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        [_headView setCornerRadiusRect:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadius:24];
        
        UIButton *clearAllBtn = [UIButton btnWithTitle:@"清除全部" titleColor:BrandColor font:SysFont(12.0)];
        [clearAllBtn cornerRadius:12.0];
        clearAllBtn.layer.borderColor = BrandColor.CGColor;
        clearAllBtn.layer.borderWidth = 1.0;
        [clearAllBtn addTarget:self action:@selector(clearAllAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:clearAllBtn];
        
        UIButton *closeBtn = [UIButton new];
        [closeBtn dk_setImage:DKImagePickerWithNames(@"home_close", @"dk_home_close", @"home_close") forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:closeBtn];
        
        [clearAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headView).offset(15);
            make.size.mas_equalTo(CGSizeMake(67, 26));
            make.centerY.equalTo(_headView);
        }];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_headView).offset(-15);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            make.centerY.equalTo(_headView);
        }];
    }
    return _headView;
}

- (UITableView *)playListView {
    if (!_playListView) {
        CGFloat y = CGRectGetMaxY(self.headView.frame);
        _playListView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, self.width, self.height - y) style:UITableViewStyleGrouped];
        _playListView.showsVerticalScrollIndicator = NO;
        _playListView.bounces = NO;
        _playListView.delegate = self;
        _playListView.dataSource = self;
        _playListView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _playListView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
    }
    return _playListView;
}

- (UIButton *)oneClickBtn {
    if (!_oneClickBtn) {
        _oneClickBtn = [UIButton btnWithTitle:@"" titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
        [_oneClickBtn addTarget:self action:@selector(oneClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_oneClickBtn cornerRadius:20];
        [_oneClickBtn setTitle:@"一键开启" forState:UIControlStateNormal];
        [_oneClickBtn setTitle:@"一键暂停" forState:UIControlStateSelected];
    }
    return _oneClickBtn;
}

- (NSArray *)audioArr {
    if (!_audioArr) {
        _audioArr = [NSArray array];
    }
    return _audioArr;
}

@end