This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
BumpTableModel.m
247 lines (206 loc) · 7.93 KB
/
BumpTableModel.m
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
#import "BumpTableModel.h"
#import "BumpTableUtils.h"
@interface BumpTableModel ()
@property (nonatomic) NSMutableDictionary *sectionNumberForRow;
@property (nonatomic) NSMutableDictionary *rowNumberForRow;
@end
@implementation BumpTableModel : NSObject
+ (instancetype)modelWithSections:(NSArray *)sections {
BumpTableModel *model = [self new];
model.sections = sections;
return model;
}
+ (instancetype)modelWithRows:(NSArray *)rows {
[rows enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSAssert([obj isKindOfClass:[BumpTableRow class]],
@"Array passed into modelWithRows: must only contain BumpTableRow objects");
}];
BumpTableSection *section = [BumpTableSection sectionWithKey:@"all" rows:rows];
NSArray *sections = [NSArray arrayWithObject:section];
return [BumpTableModel modelWithSections:sections];
}
#pragma mark - Setters
- (void)setSections:(NSArray *)sections {
[sections enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSAssert([obj isKindOfClass:[BumpTableSection class]],
@"Each element in the sections array must be a BumpTableSection");
}];
_sections = [NSArray arrayWithArray:sections];
}
- (void)generateIndexPathIndex {
self.sectionNumberForRow = [NSMutableDictionary dictionary];
self.rowNumberForRow = [NSMutableDictionary dictionary];
for (int sectionNumber = 0; sectionNumber < [self.sections count]; sectionNumber++) {
BumpTableSection *section = [self.sections objectAtIndex:sectionNumber];
for (int rowNumber = 0; rowNumber < [[section rows] count]; rowNumber++) {
BumpTableRow *row = [[section rows] objectAtIndex:rowNumber];
[self.sectionNumberForRow setObject:@(sectionNumber) forKey:row.key];
[self.rowNumberForRow setObject:@(rowNumber) forKey:row.key];
}
}
}
- (NSIndexPath *)indexPathForRow:(BumpTableRow *)row {
if(!self.rowNumberForRow) {
[self generateIndexPathIndex];
}
NSNumber *rowNumber = self.rowNumberForRow[row.key];
NSNumber *sectionNumber = self.sectionNumberForRow[row.key];
if (rowNumber && sectionNumber) {
return [NSIndexPath indexPathForRow:[rowNumber intValue]
inSection:[sectionNumber intValue]];
}
return nil;
}
- (NSDictionary *)sectionIndexes {
NSMutableDictionary *indexes = [NSMutableDictionary dictionaryWithCapacity:[_sections count]];
[_sections enumerateObjectsUsingBlock:^(BumpTableSection *s, NSUInteger idx, BOOL *stop) {
[indexes setObject:[NSIndexSet indexSetWithIndex:idx] forKey:s.key];
}];
return indexes;
}
- (NSDictionary *)rowIndexPaths {
NSMutableDictionary *indexPaths = [NSMutableDictionary dictionaryWithCapacity:
[BumpTableUtils sumArray:_sections withBlock:
^int(BumpTableSection *s) {
return s.rows.count + 1;
}]];
[_sections enumerateObjectsUsingBlock:^(BumpTableSection *s, NSUInteger sidx, BOOL *stop) {
NSMutableDictionary *sectionIndexPaths = [NSMutableDictionary dictionaryWithCapacity:s.rows.count];
[s.rows enumerateObjectsUsingBlock:^(BumpTableRow *r, NSUInteger ridx, BOOL *stop) {
[sectionIndexPaths setObject:[NSIndexPath indexPathForRow:ridx inSection:sidx]
forKey:r.key];
}];
[indexPaths setObject:sectionIndexPaths forKey:s.key];
[indexPaths addEntriesFromDictionary:sectionIndexPaths];
}];
return indexPaths;
}
- (NSMutableArray *)rowsForSearchString:(NSString *)searchString {
searchString = [searchString lowercaseString];
NSMutableArray *results = [NSMutableArray array];
[_sections enumerateObjectsUsingBlock:^(BumpTableSection *s, NSUInteger sidx, BOOL *stop) {
[s.rows enumerateObjectsUsingBlock:^(BumpTableRow *r, NSUInteger ridx, BOOL *stop) {
if (r.searchString && [r.searchString rangeOfString:searchString].location != NSNotFound) {
[results addObject:r];
}
}];
}];
return results;
}
- (BumpTableModel *)modelForSearchString:(NSString *)searchString {
return [BumpTableModel modelWithRows:[self rowsForSearchString:searchString]];
}
- (NSArray *)rowsForPredicate:(BumpTableRowPredicate)predicate {
NSMutableArray *rows = [NSMutableArray array];
[_sections enumerateObjectsUsingBlock:^(BumpTableSection *s, NSUInteger idx, BOOL *stop) {
[s.rows enumerateObjectsUsingBlock:^(BumpTableRow *r, NSUInteger idx, BOOL *stop) {
if (predicate(r)) [rows addObject:r];
}];
}];
return rows;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<Model sections:%@\n>", [BumpTableUtils indentedDescriptionForObject:_sections]];
}
@end
@implementation BumpTableHeaderFooter {
CGFloat _height;
}
@dynamic height;
+ (instancetype)headerFooterForHeight:(CGFloat)height generator:(BumpTableHeaderFooterGenerator)generator {
BumpTableHeaderFooter *hf = [BumpTableHeaderFooter new];
hf.height = height;
hf.generator = generator;
return hf;
}
+ (instancetype)headerFooterWithTitle:(NSString *)title {
BumpTableHeaderFooter *hf = [BumpTableHeaderFooter new];
hf.title = title;
return hf;
}
- (UIView *)view {
if (_generator) return _generator();
return nil;
}
- (void)setHeight:(CGFloat)height {
_height = height;
}
- (CGFloat)height {
if (_height == 0.0) {
if (!_title)
return 0.0;
if ([[UIDevice currentDevice].systemVersion intValue] >= 5)
return UITableViewAutomaticDimension;
else return 22.0; // Grouped table views should be 10.0, this only affects < iOS 5
}
return _height;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<Header/Footer height: %f generator:%d\n>", _height, !!_generator];
}
@end
@implementation BumpTableSection
+ (instancetype)sectionWithKey:(NSObject <NSCopying>*)key rows:(NSArray*)rows {
BumpTableSection *section = [self new];
section.key = key;
section.rows = [NSArray arrayWithArray:rows];
return section;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<Section key:%@\nheader:%@\nfooter:%@\nrows:%@\n>",
self.key,
self.header,
self.footer,
[BumpTableUtils indentedDescriptionForObject:self.rows]];
}
@end
@implementation BumpTableRow {
BOOL _selected;
NSString *_searchString;
}
@dynamic searchString, selected;
+ (instancetype)rowWithKey:(NSObject <NSCopying>*)key
height:(CGFloat)height
reuseIdentifier:(NSString *)reuseIdentifier
generator:(BumpTableCellGenerator)generator {
BumpTableRow *row = [self new];
row.key = key;
row.height = height;
row.reuseIdentifier = reuseIdentifier;
row.generator = generator;
row.selectable = YES;
return row;
}
+ (instancetype)rowWithKey:(NSObject <NSCopying>*)key
height:(CGFloat)height
reuseIdentifier:(NSString *)reuseIdentifier
{
return [[self class] rowWithKey:key
height:height
reuseIdentifier:reuseIdentifier
generator:NULL];
}
- (void)setSelected:(BOOL)selected {
assert(self.selectable);
_selected = selected;
}
- (BOOL)selected {
return _selected;
}
- (void)setSearchString:(NSString *)searchString {
_searchString = [[searchString lowercaseString] copy];
}
- (NSString *)searchString {
return _searchString;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<Row key:%@\nsearch string:%@\nheight:%f\nreuse:%@\ngenerator:%d\ncustomizer:%d\nonTap:%d\n>",
self.key,
self.searchString,
self.height,
self.reuseIdentifier,
!!self.generator,
!!self.customizer,
!!self.onTap];
}
@end