SetTableView.m
10.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// SetTableView.m
// DreamSleep
//
// Created by peter on 2022/4/17.
//
#import "SetTableView.h"
#import "DsCacheUtils.h"
/// 设置自定义数据模型
@interface SetModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *detail;
+ (NSArray *)getAllSetDatas;
@end
@implementation SetModel
+ (NSArray *)getAllSetDatas {
NSArray *titles = @[@"应用版本及升级", @"用户协议", @"隐私政策", @"清除缓存"];
NSString *version = [NSString stringWithFormat:@"当前版本%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
NSString *cacheSize = [DsCacheUtils getCacheSize];
NSArray *details = @[version, @"", @"", cacheSize];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < titles.count; i++) {
SetModel *m = [SetModel new];
m.title = titles[i];
m.detail = details[i];
[tmpArr addObject:m];
}
return [tmpArr copy];
}
@end
/// 设置自定义Cell
@interface SetCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *detailLab;
@property (nonatomic, strong) UIImageView *rightIcon;
- (void)updateConstraintsWithModel:(SetModel *)model;
@end
@implementation SetCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
self.rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]];
[self.contentView addSubview:self.rightIcon];
self.titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
[self.contentView addSubview:self.titleLab];
self.detailLab = [UILabel labWithFont:SysFont(14.0)];
self.detailLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHex(0x8C8F9D), DSWhite);
[self.contentView addSubview:self.detailLab];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = DSClearColor;
[super setSelected:selected animated:animated];
}
- (void)updateConstraintsWithModel:(SetModel *)model {
self.titleLab.text = model.title;
self.detailLab.text = model.detail;
[self.titleLab sizeToFit];
[self.detailLab sizeToFit];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(15);
make.centerY.equalTo(self.contentView);
}];
[self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.rightIcon.mas_left).offset(-8);
make.centerY.equalTo(self.contentView);
}];
[self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.contentView);
make.right.equalTo(self.contentView).offset(-8);
}];
}
@end
@interface SetTableView () <UITableViewDelegate>
@property (nonatomic, strong) DSDataSource *setDataSource;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UIImageView *slideView;
@end
@implementation SetTableView
- (instancetype)initWithDelegate:(id<SetTableViewDelegate>)delegate {
if (self = [super initWithFrame:CGRectMake(15, 15, kScreenWidth - 30, 348) style:UITableViewStylePlain]) {
self.dataArr = [SetModel getAllSetDatas];
self.setDelegate = delegate;
[self cornerRadius:10];
self.scrollEnabled = NO;
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
self.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.setDataSource addDataArray:self.dataArr];
self.tableFooterView = self.footView;
}
return self;
}
- (DSDataSource *)setDataSource {
if (!_setDataSource) {
CellConfigureBlock cellBlock = ^(SetCell * cell, SetModel * model, NSIndexPath * indexPath) {
[cell updateConstraintsWithModel:model];
};
NSString * const setCellID = @"SetCellID";
_setDataSource = [[DSDataSource alloc] initWithIdentifier:setCellID datas:@[] isSection:NO configureBlock:cellBlock];
self.dataSource = _setDataSource;
self.delegate = self;
[self registerClass:[SetCell class] forCellReuseIdentifier:setCellID];
}
return _setDataSource;
}
- (UIView *)footView {
if (!_footView) {
_footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 200)];
UILabel *titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
titleLab.text = @"主题模式";
[titleLab sizeToFit];
[_footView addSubview:titleLab];
UIView *longView = [UIView new];
[longView cornerRadius:10];
longView.dk_backgroundColorPicker = DKColorPickerWithColors(BGColor, DarkColor, DSWhite);
[_footView addSubview:longView];
CGFloat leftRightSpace = 24;
CGFloat btnH = 20;
CGFloat btnW = (self.width - 2*leftRightSpace)/3.0;
for (int i = 0; i < 3; i++) {
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
btn.tag = i;
[btn addTarget:self action:@selector(switchThemeAction:) forControlEvents:UIControlEventTouchUpInside];
[longView addSubview:btn];
}
_slideView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slideIcon"]];
[_footView addSubview:_slideView];
[titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_footView).offset(15);
make.top.equalTo(_footView);
}];
[longView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_footView).offset(leftRightSpace);
make.top.equalTo(titleLab.mas_bottom).offset(24);
make.right.equalTo(_footView).offset(-leftRightSpace);
make.height.equalTo(@(btnH));
}];
[_slideView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(longView);
make.width.equalTo(@50);
make.height.equalTo(@34);
// slideView初始化位置
if (kGetUserDefaultsBOOL(ThemeAutoSwitch)) { // 自动切换状态
make.centerX.equalTo(_footView);
} else {
if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal]) {
make.left.equalTo(_footView).offset(19);
} else {
make.right.equalTo(_footView).offset(-19);
}
}
}];
NSArray *themes = @[@"日间模式", @"自动切换", @"夜间模式"];
for (int i = 0; i < 3; i++) {
UILabel *lab = [UILabel labWithText:themes[i] font:SysFont(14.0) fit:YES];
lab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, DSWhite, DSWhite);
[_footView addSubview:lab];
[lab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(longView.mas_bottom).offset(8);
}];
if (i == 0) {
[lab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_footView).offset(8);
make.bottom.equalTo(_footView).offset(-19);
}];
} else if (i == 1) {
[lab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(_footView);
}];
} else if (i == 2) {
[lab mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_footView).offset(-8);
}];
}
}
}
return _footView;
}
- (void)switchThemeAction:(UIButton *)sender {
WS(weakSelf);
if (sender.tag == 1) { // 自动切换
[UIView animateWithDuration:.3 animations:^{
weakSelf.slideView.centerX = self.footView.centerX;
}];
// 处理自动切换逻辑
if ([NSDate judgeTimeWithStartTime:StartTime1 expireTime:ExpireTime1] || [NSDate judgeTimeWithStartTime:StartTime2 expireTime:ExpireTime2]) {
self.dk_manager.themeVersion = DKThemeVersionNight;
} else {
self.dk_manager.themeVersion = DKThemeVersionNormal;
}
// 保存自动切换状态
kSetUserDefaultsBOOL(YES, ThemeAutoSwitch);
kUserDefaultsSynchronize;
} else {
CGFloat x = sender.tag == 0 ? 19 : self.footView.width - self.slideView.width - 19;
[UIView animateWithDuration:.3 animations:^{
weakSelf.slideView.x = x;
}];
self.dk_manager.themeVersion = sender.tag == 0 ? DKThemeVersionNormal : DKThemeVersionNight;
kSetUserDefaultsBOOL(NO, ThemeAutoSwitch);
kUserDefaultsSynchronize;
}
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { // 应用版本升级
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/%@", AppSotreID]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
} else if (indexPath.row == 3) { // 清除缓存
[DsCacheUtils cleanCacheWithSuccess:^(BOOL success) {
}];
[DSProgressHUD showToast:@"清除成功"];
SetModel *m = self.dataArr[indexPath.row];
m.detail = @"0.0M";
[tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
} else {
if (self.setDelegate && [self.setDelegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) {
[self.setDelegate didSelectRowAtIndexPath:indexPath];
}
}
}
@end