NoiseListController.m
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// 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