-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDWFormTableViewController.m
More file actions
284 lines (240 loc) · 11.3 KB
/
DWFormTableViewController.m
File metadata and controls
284 lines (240 loc) · 11.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// Created by Andrew Podkovyrin
// Copyright © 2018 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "DWFormTableViewController.h"
#import "DWActionFormTableViewCell.h"
#import "DWKeyValueFormTableViewCell.h"
#import "DWPlaceholderFormTableViewCell.h"
#import "DWPublicKeyGenerationTableViewCell.h"
#import "DWSelectorFormTableViewCell.h"
#import "DWSharedUIConstants.h"
#import "DWSwitcherFormTableViewCell.h"
#import "DWUIKit.h"
NS_ASSUME_NONNULL_BEGIN
static CGFloat const DEFAULT_CELL_HEIGHT = 74.0;
static CGFloat const PUBLIC_KEY_GENERATION_CELL_HEIGHT = 124.0;
static CGFloat const SECTION_SPACING = 10.0;
@interface DWFormTableViewController ()
@property (nullable, copy, nonatomic) NSArray<DWFormSectionModel *> *sections;
@property (nullable, copy, nonatomic) NSArray<DWFormSectionModel *> *internalDataSource;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *customCellModels;
@end
@implementation DWFormTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.customCellModels = [NSMutableDictionary dictionary];
self.view.backgroundColor = [UIColor dw_secondaryBackgroundColor];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.tableFooterView = [[UIView alloc] init];
self.tableView.contentInset = UIEdgeInsetsMake(DWDefaultMargin(), 0.0, 0.0, 0.0);
self.tableView.sectionHeaderHeight = SECTION_SPACING;
NSArray<Class> *cellClasses = @[
DWSelectorFormTableViewCell.class,
DWSwitcherFormTableViewCell.class,
DWPlaceholderFormTableViewCell.class,
DWKeyValueFormTableViewCell.class,
DWPublicKeyGenerationTableViewCell.class,
DWActionFormTableViewCell.class,
];
for (Class cellClass in cellClasses) {
[self.tableView registerClass:cellClass forCellReuseIdentifier:NSStringFromClass(cellClass)];
}
}
- (void)setSections:(nullable NSArray<DWFormSectionModel *> *)sections
placeholderText:(nullable NSString *)placeholderText {
[self setSections:sections placeholderText:placeholderText shouldReloadData:YES];
}
- (void)setSections:(nullable NSArray<DWFormSectionModel *> *)sections
placeholderText:(nullable NSString *)placeholderText
shouldReloadData:(BOOL)shouldReloadData {
self.sections = sections;
if (placeholderText) {
BOOL hasItems = NO;
for (DWFormSectionModel *section in sections) {
if (section.items.count > 0) {
hasItems = YES;
break;
}
}
if (hasItems) {
self.internalDataSource = self.sections;
}
else {
DWPlaceholderFormCellModel *placeholderCellModel = [[DWPlaceholderFormCellModel alloc] initWithTitle:placeholderText];
DWFormSectionModel *placeholderSection = [[DWFormSectionModel alloc] init];
placeholderSection.items = @[ placeholderCellModel ];
self.internalDataSource = @[ placeholderSection ];
}
}
else {
self.internalDataSource = self.sections;
}
if (shouldReloadData) {
[self.tableView reloadData];
}
}
- (void)registerCustomCellModelClass:(Class)cellModelClass forCellClass:(Class)cellClass {
NSParameterAssert(cellModelClass);
NSParameterAssert(cellClass);
NSAssert([cellModelClass isSubclassOfClass:DWBaseFormCellModel.class], @"Unsupported cell model class");
NSAssert([cellClass isSubclassOfClass:DWBaseFormTableViewCell.class], @"Unsupported cell class");
NSString *cellId = NSStringFromClass(cellClass);
[self.tableView registerClass:cellClass forCellReuseIdentifier:cellId];
self.customCellModels[NSStringFromClass(cellModelClass)] = cellId;
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.internalDataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
DWFormSectionModel *sectionModel = self.internalDataSource[section];
return sectionModel.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DWFormSectionModel *sectionModel = self.internalDataSource[indexPath.section];
NSArray<DWBaseFormCellModel *> *items = sectionModel.items;
DWBaseFormCellModel *cellModel = items[indexPath.row];
DWFormCellRoundMask roundMask = [self maskForIndexPath:indexPath];
if ([cellModel isKindOfClass:DWSelectorFormCellModel.class]) {
NSString *cellId = NSStringFromClass(DWSelectorFormTableViewCell.class);
DWSelectorFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWSelectorFormCellModel *)cellModel;
cell.roundMask = roundMask;
return cell;
}
else if ([cellModel isKindOfClass:DWSwitcherFormCellModel.class]) {
NSString *cellId = NSStringFromClass(DWSwitcherFormTableViewCell.class);
DWSwitcherFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWSwitcherFormCellModel *)cellModel;
cell.roundMask = roundMask;
return cell;
}
else if ([cellModel isKindOfClass:DWKeyValueFormCellModel.class]) {
NSString *cellId = NSStringFromClass(DWKeyValueFormTableViewCell.class);
DWKeyValueFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWKeyValueFormCellModel *)cellModel;
return cell;
}
else if ([cellModel isKindOfClass:DWPublicKeyGenerationCellModel.class]) {
NSString *cellId = NSStringFromClass(DWPublicKeyGenerationTableViewCell.class);
DWPublicKeyGenerationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWPublicKeyGenerationCellModel *)cellModel;
return cell;
}
else if ([cellModel isKindOfClass:DWPlaceholderFormCellModel.class]) {
NSString *cellId = NSStringFromClass(DWPlaceholderFormTableViewCell.class);
DWPlaceholderFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWPlaceholderFormCellModel *)cellModel;
return cell;
}
else if ([cellModel isKindOfClass:DWActionFormCellModel.class]) {
NSString *cellId = NSStringFromClass(DWActionFormTableViewCell.class);
DWActionFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
cell.cellModel = (DWActionFormCellModel *)cellModel;
return cell;
}
else {
NSString *cellModelClass = NSStringFromClass(cellModel.class);
NSString *cellId = self.customCellModels[cellModelClass];
if (!cellId) {
NSAssert(NO, @"Unknown cell model %@", cellModel);
return [UITableViewCell new];
}
DWBaseFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
forIndexPath:indexPath];
if ([cell respondsToSelector:@selector(setCellModel:)]) {
[cell performSelector:@selector(setCellModel:) withObject:cellModel];
}
cell.roundMask = roundMask;
return cell;
}
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
DWFormSectionModel *sectionModel = self.internalDataSource[indexPath.section];
NSArray<DWBaseFormCellModel *> *items = sectionModel.items;
DWBaseFormCellModel *cellModel = items[indexPath.row];
if ([cellModel isKindOfClass:DWPlaceholderFormCellModel.class]) {
return CGRectGetHeight(tableView.bounds);
}
else if ([cellModel isKindOfClass:DWPublicKeyGenerationTableViewCell.class]) {
return PUBLIC_KEY_GENERATION_CELL_HEIGHT;
}
else {
return DEFAULT_CELL_HEIGHT;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DWFormSectionModel *sectionModel = self.internalDataSource[indexPath.section];
NSArray<DWBaseFormCellModel *> *items = sectionModel.items;
DWBaseFormCellModel *cellModel = items[indexPath.row];
if ([cellModel isKindOfClass:DWSelectorFormCellModel.class]) {
DWSelectorFormCellModel *selectorCellModel = (DWSelectorFormCellModel *)cellModel;
if (selectorCellModel.didSelectBlock) {
selectorCellModel.didSelectBlock(selectorCellModel, indexPath);
}
}
else if ([cellModel isKindOfClass:DWActionFormCellModel.class]) {
DWActionFormCellModel *actionCellModel = (DWActionFormCellModel *)cellModel;
if (actionCellModel.didSelectBlock) {
actionCellModel.didSelectBlock(actionCellModel, indexPath);
}
}
else if ([cellModel isKindOfClass:DWKeyValueFormCellModel.class]) {
DWKeyValueFormCellModel *keyValueCellModel = (DWKeyValueFormCellModel *)cellModel;
DWKeyValueFormTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell) {
[cell resignFirstResponder];
}
if (keyValueCellModel.actionBlock) {
keyValueCellModel.actionBlock();
}
}
else if ([cellModel isKindOfClass:DWSwitcherFormCellModel.class]) {
DWSwitcherFormCellModel *switcherCellModel = (DWSwitcherFormCellModel *)cellModel;
switcherCellModel.on = !switcherCellModel.on;
if (switcherCellModel.didChangeValueBlock) {
switcherCellModel.didChangeValueBlock(switcherCellModel);
}
}
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
return view;
}
#pragma mark - Private
- (DWFormCellRoundMask)maskForIndexPath:(NSIndexPath *)indexPath {
DWFormCellRoundMask mask = 0;
if (indexPath.row == 0) {
mask |= DWFormCellRoundMask_Top;
}
DWFormSectionModel *sectionModel = self.internalDataSource[indexPath.section];
NSArray<DWBaseFormCellModel *> *items = sectionModel.items;
if (indexPath.row == items.count - 1) {
mask |= DWFormCellRoundMask_Bottom;
}
return mask;
}
@end
NS_ASSUME_NONNULL_END