ReadyListController.m
7.3 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
//
// ReadyListController.m
// DreamSleep
//
// Created by peter on 2022/7/11.
//
#import "ReadyListController.h"
#import "SleepReadyRequestModel.h"
#import "RelaxItemsView.h"
#import "PrepareItemsCell.h"
@interface ReadyListController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) RelaxItemsView *relaxItemsView;
@property (nonatomic, strong) UITableView *prepareItemsView;
@property (nonatomic, strong) NSArray *prepare_items;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end
@implementation ReadyListController
- (instancetype)initWithDelegate:(id<ReadyListControllerDelegate>)delegate {
if (self = [super init]) {
_delegate = delegate;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
self.naviTitle = @"设置睡前安排";
self.naviBarAlpha = 1.0;
self.dsNaviBar.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, BrandColor);
[self.dsNaviBar addSubview:self.backBtn];
self.titleLab.dk_textColorPicker = DKColorPickerWithKey(Sub_Navi_TITLE);
[self.backBtn dk_setImage:DKImagePickerWithNames(@"sys_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
[self queryPreparePeaceListRequest];
[DataStatisticsUtil event:SleepReadyEvent attributes:@{@"name":@"设置睡前安排页面"}];
}
- (void)queryPreparePeaceListRequest {
WS(weakSelf);
NSURLSessionDataTask *task = [SleepReadyRequestModel queryPreparePeaceListWithCompletion:^(SleepReadyRequestModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
weakSelf.exceptionView.hidden = YES;
weakSelf.prepare_items = requestModel.prepare_items;
[weakSelf.view addSubview:weakSelf.relaxItemsView];
[weakSelf.view addSubview:weakSelf.prepareItemsView];
[weakSelf.relaxItemsView adjustRelax:requestModel.relax_items relaxTime:requestModel.relax_time];
} else {
weakSelf.exceptionView.hidden = NO;
[weakSelf.exceptionView showServerErrInfo:requestModel.errMessage];
}
}];
[self autoCancelRequestOnDealloc:task];
}
#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.prepare_items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PrepareItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PrepareItemsCell class]) forIndexPath:indexPath];
ReadyItem *item = self.prepare_items[indexPath.row];
cell.item = item;
// 根据item_id在已安排好列表中匹配获取下标
[cell adjustSelectBtnOrder:[self getIndex:item.item_id]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PrepareItemsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ReadyItem *item = self.prepare_items[indexPath.row];
if (cell.selectBtn.selected == NO) {
if (self.relaxItemsView.itemCount >= 3) {
[DSProgressHUD showDetailInfo:@"最多选择3个,请先取消1个"];
return;
}
[self.relaxItemsView addItem:item];
cell.selectBtn.selected = YES;
item.status = 1;
} else {
[self.relaxItemsView removeItem:item.item_id];
cell.selectBtn.selected = NO;
item.status = 0;
}
[cell adjustSelectBtnState];
[self.prepareItemsView reloadData];
}
#pragma mark - lazy
- (RelaxItemsView *)relaxItemsView {
if (!_relaxItemsView) {
_relaxItemsView = [[RelaxItemsView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.dsNaviBar.frame) + 15, kScreenWidth - 30, 162)];
}
return _relaxItemsView;
}
- (UITableView *)prepareItemsView {
if (!_prepareItemsView) {
_prepareItemsView = [[UITableView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.relaxItemsView.frame) + 15, kScreenWidth - 30, kScreenHeight - self.dsNaviBar.height - self.relaxItemsView.height - 30 - 24 - Bottom_SafeArea_Height) style:UITableViewStylePlain];
_prepareItemsView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
[_prepareItemsView cornerRadius:24.0];
_prepareItemsView.showsVerticalScrollIndicator = NO;
_prepareItemsView.separatorStyle = UITableViewCellSeparatorStyleNone;
_prepareItemsView.dataSource = self;
_prepareItemsView.delegate = self;
[_prepareItemsView registerClass:[PrepareItemsCell class] forCellReuseIdentifier:NSStringFromClass([PrepareItemsCell class])];
_prepareItemsView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
_prepareItemsView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
}
return _prepareItemsView;
}
- (NSArray *)prepare_items {
if (!_prepare_items) {
_prepare_items = [NSArray array];
}
return _prepare_items;
}
- (ExceptionDefaultView *)exceptionView {
if (!_exceptionView) {
WS(weakSelf);
_exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeNet block:^{
weakSelf.exceptionView.hidden = YES;
[weakSelf queryPreparePeaceListRequest];
} superView:self.view];
}
return _exceptionView;
}
#pragma mark - others
- (NSUInteger)getIndex:(int)itemID {
NSArray *relax_items = self.relaxItemsView.has_relax_items;
__block NSUInteger index = -1;
[relax_items enumerateObjectsUsingBlock:^(ReadyItem * obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (itemID == obj.item_id) {
*stop = YES;
index = idx;
}
}];
return index;
}
- (void)backAction {
if (self.delegate) { // AI睡眠教练页面->安睡准备页面->安睡设置页面
if (self.prepare_items.count == 0) {
[super backAction];
return;
}
[self userSetupPrepareItemsRequest:NO];
} else { // AI睡眠教练页面->安睡设置页面
[self userSetupPrepareItemsRequest:YES];
}
}
- (void)userSetupPrepareItemsRequest:(BOOL)needUpdateAICoach {
[DSProgressHUD showProgressHUDWithInfo:@""];
[SleepReadyRequestModel userSetupPrepareItems:self.relaxItemsView.has_relax_items completion:^(SleepReadyRequestModel * _Nonnull requestModel) {
[DSProgressHUD dissmissProgressHUD];
if (requestModel.resCode == DSResCodeSuccess) {
if (self.delegate && [self.delegate respondsToSelector:@selector(passAdjustedReadyItems:)]) {
[self.delegate passAdjustedReadyItems:self.relaxItemsView.has_relax_items];
}
if (needUpdateAICoach) {
// 刷新AI睡眠教练
[[NSNotificationCenter defaultCenter] postNotificationName:NeedUpdateAICoach object:nil];
}
[self.navigationController popViewControllerAnimated:YES];
}
}];
}
#pragma mark - 设置状态栏文字颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end