NoiseListController.m
5.6 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// NoiseListController.m
// DreamSleep
//
// Created by peter on 2022/5/11.
//
#import "NoiseListController.h"
#import "WhiteNoiseRequestModel.h"
#import "NoiseAudioCell.h"
#import "NoiseDataManager.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];
// 重置
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList;
for (NoiseAudioCell *audioCell in playList) {
[audioCell.model.audioStream stop];
}
[NoiseDataManager sharedNoiseDataManager].playList = @[];
} 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]];
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList;
NSMutableArray *selectedDataArr = [NSMutableArray arrayWithArray:playList];
// 白噪音未被选择则添加到播放列表
if (cell.audioIV.selected == NO) {
if (selectedDataArr.count >= 8) {
[DSProgressHUD showToast:@"音频最多同时播放8个"];
return;
}
// 添加播放器
FSAudioStream *audioStream = [[FSAudioStream alloc] init];
audioStream.strictContentTypeChecking = NO;
audioStream.defaultContentType = @"audio/mpeg";
audioStream.volume = .5;
model.audioStream = audioStream;
[selectedDataArr addObject:cell];
playList = [selectedDataArr copy];
} else {
// 白噪音已经选择了则从播放列表移除
if (model.audioStream) { [model.audioStream stop]; }
[selectedDataArr removeObject:cell];
}
cell.audioIV.selected = !cell.audioIV.selected;
[NoiseDataManager sharedNoiseDataManager].playList = [selectedDataArr copy];
}
#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