SleepReadyController.m
8.4 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// SleepReadyController.m
// DreamSleep
//
// Created by peter on 2022/7/11.
//
#import "SleepReadyController.h"
#import "ReadyListController.h"
#import "NoisePlayerManager.h"
#import "WhiteNoiseRequestModel.h"
#import "SRTipsView.h"
#import "SRMusicView.h"
#import "StartReadyView.h"
#import "ReadyItemView.h"
#import "RelaxTrainController.h"
#import "SleepReadyRequestModel.h"
@interface SleepReadyController () <ReadyListControllerDelegate, StartReadyViewDelegate, ReadyItemViewDelegate>
@property (nonatomic, strong) UIButton *tipsBtn;
@property (nonatomic, strong) UIButton *setBtn;
@property (nonatomic, strong) SRTipsView *sRTipsView;
@property (nonatomic, strong) StartReadyView *startReadyView;
@property (nonatomic, strong) ReadyItemView *readyItemView;
@property (nonatomic, strong) UIButton *musicBtn;
@property (nonatomic, strong) SRMusicView *musicView;
@property (nonatomic, strong) NSArray *adjustedItems;
@end
@implementation SleepReadyController
- (void)viewDidLoad {
[super viewDidLoad];
[self buildUI];
// 暂停白噪音
[[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
// 获取白噪音类型请求
[self queryRelaxWhiteNoiseType];
// 获取睡前准备任务items数据
[self getUserPrepareSleepItemsRequest];
}
- (void)queryRelaxWhiteNoiseType {
[WhiteNoiseRequestModel queryRelaxWhiteNoiseTypeWithCompletion:^(WhiteNoiseRequestModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
DSLog(@"获取白噪音类型请求成功...");
[self.musicView refreshNoiseTypeData:requestModel.noiseTypeArr];
}
}];
}
- (void)getUserPrepareSleepItemsRequest {
[SleepReadyRequestModel userPrepareSleepItemsWithCompletion:^(SleepReadyRequestModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
self.adjustedItems = requestModel.relax_items;
[self.startReadyView updateAdjustView:self.adjustedItems];
}
}];
}
- (void)backAction {
// 暂停白噪音
[[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
// 刷新AI睡眠教练
[[NSNotificationCenter defaultCenter] postNotificationName:NeedUpdateAICoach object:nil];
// 退出当前页面
[super backAction];
}
#pragma mark - ReadyListControllerDelegate
- (void)passAdjustedReadyItems:(NSArray *)items {
if (items && [items isKindOfClass:[NSArray class]]) {
self.adjustedItems = items;
[self.startReadyView updateAdjustView:self.adjustedItems];
}
}
#pragma mark - StartReadyViewDelegate
- (void)startTask {
if (self.adjustedItems.count == 0) {
// 准备页面点击开始仪式如果没有任务则直接进入列表设置页面
ReadyListController *setListVC = [[ReadyListController alloc] initWithDelegate:self];
[self.navigationController pushViewController:setListVC animated:YES];
return;
}
self.tipsBtn.hidden = YES;
self.setBtn.hidden = YES;
self.startReadyView.hidden = YES;
[self.readyItemView startWithItems:self.adjustedItems];
}
#pragma mark - ReadyItemViewDelegate
- (void)curItemName:(NSString *)itemName {
self.naviTitle = itemName;
}
- (void)dealFinishAlert:(SRFinishAlertType)type finishModel:(SRFinishModel *)finishModel {
if (type == SRFinishAlertTypeNormal) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if (type == SRFinishAlertTypeRelax) {
RelaxTrainController *relaxVC = [RelaxTrainController new];
relaxVC.finishModel = finishModel;
[self.navigationController pushViewController:relaxVC animated:YES];
// 移除安睡准备页面
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[SleepReadyController class]]) {
*stop = YES;
[vcs removeObject:obj];
}
}];
self.navigationController.viewControllers = [vcs copy];
}
// 暂停白噪音
[[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
// 刷新AI睡眠教练
[[NSNotificationCenter defaultCenter] postNotificationName:NeedUpdateAICoach object:nil];
}
#pragma mark - UI
- (void)buildUI {
self.naviTitle = @"睡前准备";
self.naviBarAlpha = 1.0;
self.dsNaviBar.dk_backgroundColorPicker = DKColorPickerWithColors(DSClearColor, DSClearColor, DSWhite);
[self.dsNaviBar addSubview:self.backBtn];
self.titleLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
[self.backBtn dk_setImage:DKImagePickerWithNames(@"cus_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
[self.dsNaviBar addSubview:self.tipsBtn];
[self.dsNaviBar addSubview:self.setBtn];
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self.view insertSubview:self.startReadyView atIndex:0];
[self.view insertSubview:self.readyItemView atIndex:1];
[self.view addSubview:self.musicBtn];
[self.tipsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.titleLab);
}];
[self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.tipsBtn);
make.left.equalTo(self.tipsBtn.mas_right).offset(24);
make.right.equalTo(self.view).offset(-15);
}];
[self.musicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-15);
make.bottom.equalTo(self.view).offset(-94-Bottom_SafeArea_Height);
}];
[self.startReadyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[self.readyItemView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// 首次弹提示框
if (!kGetUserDefaultsBOOL(IsFirstSRTipsAlert)) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.sRTipsView showTipsAlertView];
kSetUserDefaultsBOOL(YES, IsFirstSRTipsAlert);
kUserDefaultsSynchronize;
});
}
}
#pragma mark - lazy
- (UIButton *)tipsBtn {
if (!_tipsBtn) {
WS(weakSelf);
_tipsBtn = [UIButton new];
[_tipsBtn dk_setImage:DKImagePickerWithNames(@"ic_zhunbei_zhushi", @"dk_ic_zhunbei_zhushi", @"ic_zhunbei_zhushi") forState:UIControlStateNormal];
[_tipsBtn addTouchUpInsideHandler:^(NSInteger tag) {
[weakSelf.sRTipsView showTipsAlertView];
}];
}
return _tipsBtn;
}
- (UIButton *)setBtn {
if (!_setBtn) {
WS(weakSelf);
_setBtn = [UIButton new];
[_setBtn dk_setImage:DKImagePickerWithNames(@"ic_zhunbei_set", @"dk_ic_zhunbei_set", @"ic_zhunbei_set") forState:UIControlStateNormal];
[_setBtn addTouchUpInsideHandler:^(NSInteger tag) {
ReadyListController *setListVC = [[ReadyListController alloc] initWithDelegate:weakSelf];
[weakSelf.navigationController pushViewController:setListVC animated:YES];
}];
}
return _setBtn;
}
- (SRTipsView *)sRTipsView {
if (!_sRTipsView) {
_sRTipsView = [SRTipsView new];
}
return _sRTipsView;
}
- (StartReadyView *)startReadyView {
if (!_startReadyView) {
_startReadyView = [[StartReadyView alloc] initWithDelegate:self];
}
return _startReadyView;
}
- (ReadyItemView *)readyItemView {
if (!_readyItemView) {
_readyItemView = [ReadyItemView new];
_readyItemView.delegate = self;
}
return _readyItemView;
}
- (UIButton *)musicBtn {
if (!_musicBtn) {
WS(weakSelf);
_musicBtn = [UIButton new];
[_musicBtn setImage:[UIImage imageNamed:@"ic_zhunbei_baizaoyin"] forState:UIControlStateNormal];
[_musicBtn addTouchUpInsideHandler:^(NSInteger tag) {
[weakSelf.musicView showMusicView];
}];
}
return _musicBtn;
}
- (SRMusicView *)musicView {
if (!_musicView) {
_musicView = [[SRMusicView alloc] initWithFrame:CGRectZero];
}
return _musicView;
}
- (NSArray *)adjustedItems {
if (!_adjustedItems) {
_adjustedItems = [NSArray array];
}
return _adjustedItems;
}
@end