-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTACellModel.m
More file actions
313 lines (267 loc) · 10.7 KB
/
STACellModel.m
File metadata and controls
313 lines (267 loc) · 10.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// STACellModel.m
// STACollapsableTable
//
// Created by Aaron Jubbal on 2/7/16.
// Copyright © 2016 Aaron Jubbal. All rights reserved.
//
#import "STACellModel.h"
#import <UIKit/UIKit.h>
#import "STACollapsableTableModel.h"
#import "STACollapsableTableModel+Private.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "NSArray+STAAdditions.h"
typedef NSIndexPath * (^ObjectEnumeratorBlock)(STACellModel *cellModel, NSUInteger row);
@interface STACellModel ()
@property (atomic, strong) NSCountedSet *descendantSearchResultSet;
@property (nonatomic, assign, readonly) NSUInteger displayedDescendantsCount;
@property (nonatomic, weak) NSIndexPath *indexPath;
@property (nonatomic, weak, readwrite) STACollapsableTableModel *tableModel;
@end
@implementation STACellModel
- (instancetype)initWithModelSpecifier:(STATableModelSpecifier *)modelSpecifier
parent:(STACellModel *)parent
tableModel:(STACollapsableTableModel *)tableModel
{
if (self = [super init]) {
_title = modelSpecifier.title;
_specifier = modelSpecifier;
_isExpanded = NO;
_isSearchResult = YES; // make cells show up as black instead of gray initially
_tableModel = tableModel;
if (parent) {
_parents = [NSMutableSet setWithObject:parent];
_depth = parent.depth + 1;
} else {
_depth = 0; // no parent means this element is a root (depth of 0)
}
[self updateSelfBasedOnSearchStatusOfTableModel];
@weakify(self);
[[RACObserve(tableModel, isSearching)
combinePreviousWithStart:@(tableModel.isSearching)
reduce:^id(NSNumber *previousValue, NSNumber *currentValue)
{
return @(([previousValue boolValue] != [currentValue boolValue]));
}] subscribeNext:^(NSNumber *statusChanged) {
@strongify(self);
if ([statusChanged boolValue]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self updateSelfBasedOnSearchStatusOfTableModel];
});
}
}];
NSMutableArray<STACellModel *> *childrenArray = [NSMutableArray arrayWithCapacity:modelSpecifier.children.count];
for (STATableModelSpecifier *specifier in modelSpecifier.children) {
STACellModel *cellModel = [tableModel cellModelForSpecifier:specifier parent:self tableModel:tableModel];
if (cellModel) {
[childrenArray addObject:cellModel];
}
}
_children = childrenArray;
_descendantSearchResultSet = [NSCountedSet setWithCapacity:childrenArray.count];
}
return self;
}
#pragma mark - Getters
- (NSUInteger)descendantsInSearchResults {
@synchronized (self) {
return self.descendantSearchResultSet.count;
}
}
- (NSUInteger)displayedDescendantsCount {
if (self.isExpanded) {
return self.children.count;
}
return self.descendantsInSearchResults;
}
#pragma mark - Setters
- (void)setIsExpanded:(BOOL)isExpanded {
if (!isExpanded) {
// collapsing the parent collapses all of its children
if (!self.tableModel.isSearching) {
for (STACellModel *cellModel in self.children) {
cellModel.isExpanded = NO;
}
}
} else {
if (self.children.count == 0) { // a cell with no children can't be expanded!
return;
}
}
_isExpanded = isExpanded;
}
- (void)setIsSearchResult:(BOOL)isSearchResult {
if (_isSearchResult != isSearchResult) {
for (STACellModel *parent in [self.parents allObjects]) {
[parent descendant:self isSearchResult:isSearchResult];
}
}
_isSearchResult = isSearchResult;
}
#pragma mark - Public Methods
- (void)addParent:(STACellModel *)cellModel {
[self.parents addObject:cellModel];
}
- (NSArray *)indexPathsToAddForExpansionFromIndexPath:(NSIndexPath *)indexPath {
// indexPath specifies row
NSUInteger offsetCount = 1;
NSUInteger rowsCounter = indexPath.row;
if (indexPath.row == NSNotFound) { // indexPath specifies section
offsetCount = 0;
rowsCounter = 0;
}
return [self indexPathsToAddFromOffset:offsetCount rowsCounter:rowsCounter];
}
- (NSArray *)indexPathsToRemoveForCollapseFromIndexPath:(NSIndexPath *)indexPath {
self.indexPath = indexPath;
return [self indexPathsToBeRemovedFromSection:indexPath.section];
}
- (NSArray *)filterContentsWithSearchString:(NSString *)searchString {
NSPredicate *filterPredicate = [NSPredicate predicateWithBlock:^BOOL(STACellModel *object, NSDictionary *bindings) {
// Inner, for cellModels, so we check if there is a valid search string, and if it matches a name or tag
if (searchString.length > 0 &&
([object.title rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound ||
[object.tags arrayContainsText:searchString options:NSCaseInsensitiveSearch].count > 0))
{
object.isSearchResult = YES;
return YES;
}
object.isSearchResult = NO;
return NO;
}];
NSArray<STACellModel *> *searchResults = [NSMutableArray arrayWithArray:[self.children filteredArrayUsingPredicate:filterPredicate]];
NSMutableArray<STACellModel *> *allSearchResults = [NSMutableArray array];
for (NSUInteger i = 0; i < self.children.count; i++) {
STACellModel *container = self.children[i];
NSArray<STACellModel *> *filteredArray = [container filterContentsWithSearchString:searchString];
if (filteredArray.count > 0) {
[allSearchResults addObject:container];
[allSearchResults addObjectsFromArray:filteredArray]; // filteredArray
continue;
}
NSPredicate *titlePredicate = [NSPredicate predicateWithFormat:@"title = %@", container.title];
STACellModel *matchingContainer = [[searchResults filteredArrayUsingPredicate:titlePredicate] firstObject];
if (matchingContainer) {
[allSearchResults addObject:matchingContainer];
}
}
// Update collapse/expand triangles according with how many children are displaying
if (self.descendantsInSearchResults == self.children.count) {
self.isExpanded = YES;
[self.tableModel cellModelExpanded:self];
} else {
self.isExpanded = NO;
[self.tableModel cellModelCollapsed:self];
}
return allSearchResults;
}
- (void)descendant:(STACellModel *)cellModel isSearchResult:(BOOL)isSearchResult {
if (isSearchResult) {
[self.descendantSearchResultSet addObject:cellModel];
} else {
if (cellModel) {
[self.descendantSearchResultSet removeObject:cellModel];
}
}
for (id parent in [self.parents allObjects]) {
[parent descendant:self isSearchResult:isSearchResult];
}
}
- (BOOL)shouldExpandAndIncludeCellModel:(STACellModel *)cellModel {
if (self.tableModel.isSearching) {
// Only include cell models that aren't being shown
if (!cellModel.isSearchResult && !cellModel.descendantsInSearchResults) {
return YES;
}
} else {
return YES;
}
return NO;
}
- (BOOL)shouldCollapseAndRemoveCellModel:(STACellModel *)cellModel {
if (self.tableModel.isSearching) {
// Only collapse cell models that are being shown and are not a search result
if (!cellModel.isSearchResult && !cellModel.descendantsInSearchResults) {
return YES;
}
} else {
return YES;
}
return NO;
}
#pragma mark - Helper Methods
- (NSArray *)indexPathsToAddFromOffset:(NSUInteger)offset rowsCounter:(NSUInteger)rowsCounter {
NSMutableArray *addedIndexPaths = [NSMutableArray array];
for (STACellModel *cellModel in self.children) {
if ([self shouldExpandAndIncludeCellModel:cellModel]) {
[addedIndexPaths addObject:@{@"container" : cellModel,
@"index" : @(rowsCounter + offset)}];
} else {
if (cellModel.isExpanded) {
offset += cellModel.children.count;
} else {
offset += cellModel.descendantsInSearchResults;
}
}
offset++;
}
return addedIndexPaths;
}
- (NSArray *)indexPathsToBeRemovedFromSection:(NSInteger)section {
return [self enumerateObjectsToBeRemoved:^NSIndexPath * (STACellModel *cellModel, NSUInteger row) {
NSIndexPath *removableIndexPath = nil;
if ([self shouldCollapseAndRemoveCellModel:cellModel]) {
removableIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
}
return removableIndexPath;
}];
}
- (void)updateSelfBasedOnSearchStatusOfTableModel {
if (!self.tableModel.isSearching) { // if not searching, we want all cells to have black text
self.isSearchResult = YES;
}
}
- (BOOL)hasDescendant:(STACellModel *)cellModel {
if (!cellModel.parents) {
return NO;
}
if ([cellModel.parents containsObject:self]) {
return YES;
}
BOOL isDescendant = NO;
for (STACellModel *parent in [cellModel.parents allObjects]) {
isDescendant = [self hasDescendant:parent];
if (isDescendant) {
return YES;
}
}
return isDescendant;
}
/**
Enumerates through already displaying objects.
@retuns Array of index paths that should be removed.
*/
- (NSArray *)enumerateObjectsToBeRemoved:(ObjectEnumeratorBlock)block {
NSMutableArray *indexPathsToRemoveArray = [NSMutableArray arrayWithCapacity:self.displayedDescendantsCount];
NSUInteger r = (self.indexPath.row != NSNotFound) ? self.indexPath.row : 0;
NSInteger i = 1;
STACellModel *cellModel = [self.tableModel cellModelAtIndexPath:[NSIndexPath indexPathForRow:r + i inSection:self.indexPath.section]];
if (self.indexPath.row == NSNotFound) {
r = 0;
i = 0; // if cell model represents a header, there is no need to skip the first row
cellModel = [self.tableModel cellModelAtIndexPath:[NSIndexPath indexPathForRow:r + i inSection:self.indexPath.section]];
}
while (cellModel && [self hasDescendant:cellModel]) {
NSIndexPath *removableIndexPath = block(cellModel, r + i);
if (removableIndexPath) {
[indexPathsToRemoveArray addObject:removableIndexPath];
}
i++;
cellModel = [self.tableModel cellModelAtIndexPath:[NSIndexPath indexPathForRow:r + i inSection:self.indexPath.section]];
}
return indexPathsToRemoveArray;
}
- (NSString *)description {
return [NSString stringWithFormat:@"STACellModel:\ntitle: %@\nchildren: %@", self.title, self.children];
}
@end