This repository was archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSSFilterableFetchedResultsController.m
More file actions
418 lines (318 loc) · 14.4 KB
/
Copy pathSSFilterableFetchedResultsController.m
File metadata and controls
418 lines (318 loc) · 14.4 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// SSFilterableFetchedResultsController.m
// SSDataKit
//
// Created by Sam Soffes on 4/30/12.
// Copyright (c) 2012-2013 Sam Soffes. All rights reserved.
//
#import "SSFilterableFetchedResultsController.h"
#import "SSFilteredResultsSection.h"
@interface SSFilterableFetchedResultsController () <NSFetchedResultsControllerDelegate>
- (void)_updateObjectsForCurrentFilter:(SSFilteredResultsFilter *)currentFilter newFilter:(SSFilteredResultsFilter *)newFilter;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSMutableDictionary *filters;
@property (nonatomic, assign) SSFilteredResultsFilter *currentFilter;
@end
@implementation SSFilterableFetchedResultsController
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize filters = _filters;
@synthesize currentFilter = _currentFilter;
@synthesize delegate = _delegate;
- (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest
managedObjectContext:(NSManagedObjectContext *)context
sectionNameKeyPath:(NSString *)sectionNameKeyPath
cacheName:(NSString *)name {
self = [super init];
if (!self) {
return nil;
}
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:sectionNameKeyPath cacheName:name];
fetchedResultsController.delegate = self;
self.fetchedResultsController = fetchedResultsController;
self.filters = [NSMutableDictionary dictionary];
return self;
}
- (void)dealloc {
self.delegate = nil;
self.currentFilter = nil;
self.fetchedResultsController.delegate = nil;
}
- (void)addFilterPredicate:(SSFilterableFetchedResultsFilterPredicate)predicate forKey:(NSString *)key {
SSFilteredResultsFilter *f = [[SSFilteredResultsFilter alloc] init];
f.predicate = predicate;
f.sections = [NSMutableArray array];
[self.filters setObject:f forKey:key];
}
- (void)setActiveFilterByKey:(NSString *)key {
SSFilteredResultsFilter *currentFilter = self.currentFilter;
SSFilteredResultsFilter *newFilter = nil;
if (key) {
newFilter = [self.filters objectForKey:key];
}
if (![newFilter isEqual:currentFilter]) {
self.currentFilter = newFilter;
[self _updateObjectsForCurrentFilter:currentFilter newFilter:newFilter];
}
}
- (void)removeCurrentFilter {
[self addFilterPredicate:^BOOL(id obj) {
return YES;
} forKey:@"__all"];
[self setActiveFilterByKey:@"__all"];
self.currentFilter = nil;
[self.filters removeAllObjects];
}
- (void)rebuildCurrentFilter
{
[self.currentFilter.sections removeAllObjects];
for(int i = 0; i < self.fetchedResultsController.sections.count; i++) {
id<NSFetchedResultsSectionInfo> section = (id<NSFetchedResultsSectionInfo>)[self.fetchedResultsController.sections objectAtIndex:i];
SSFilteredResultsSection *newFilteredSection = [[SSFilteredResultsSection alloc] init];
newFilteredSection.internalName = section.name;
newFilteredSection.internalIndexTitle = section.indexTitle;
for(int j = 0; j < section.objects.count; j++) {
NSObject *o = [section.objects objectAtIndex:j];
if (self.currentFilter.predicate(o)) {
[newFilteredSection addObject:o];
}
}
[self.currentFilter.sections addObject:newFilteredSection];
}
}
- (void)_updateObjectsForCurrentFilter:(SSFilteredResultsFilter *)currentFilter newFilter:(SSFilteredResultsFilter *)newFilter {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controllerWillChangeContent:)]) {
[self.delegate controllerWillChangeContent:self.fetchedResultsController];
}
if (!newFilter && currentFilter) {
//Update Back To "Show All / Do not filter at all"
for(int i = 0; i < self.fetchedResultsController.sections.count; i++) {
id<NSFetchedResultsSectionInfo> section = (id<NSFetchedResultsSectionInfo>)[self.fetchedResultsController.sections objectAtIndex:i];
for(int j = 0; j < section.objects.count; j++) {
NSObject *o = [section.objects objectAtIndex:j];
if (!currentFilter.predicate(o)) {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
[self.delegate controller:self.fetchedResultsController
didChangeObject:o
atIndexPath:nil
forChangeType:NSFetchedResultsChangeInsert
newIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
}
} else if (newFilter && !currentFilter) {
//Going From showing "All" to showing a filter
[newFilter.sections removeAllObjects];
for(int i = 0; i < self.fetchedResultsController.sections.count; i++) {
id<NSFetchedResultsSectionInfo> section = (id<NSFetchedResultsSectionInfo>)[self.fetchedResultsController.sections objectAtIndex:i];
SSFilteredResultsSection *filteredSection = [[SSFilteredResultsSection alloc] init];
filteredSection.internalName = section.name;
filteredSection.internalIndexTitle = section.indexTitle;
for(int j = 0; j < section.objects.count; j++) {
NSObject *o = [section.objects objectAtIndex:j];
if (!newFilter.predicate(o)) {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
[self.delegate controller:self.fetchedResultsController
didChangeObject:o
atIndexPath:[NSIndexPath indexPathForRow:j inSection:i]
forChangeType:NSFetchedResultsChangeDelete
newIndexPath:nil];
}
} else {
[filteredSection addObject:o];
}
}
[newFilter.sections addObject:filteredSection];
}
} else if (newFilter && currentFilter) {
//Going from one filter to another filter
for(int i = 0; i < currentFilter.sections.count; i++) {
SSFilteredResultsSection *section = (SSFilteredResultsSection *)[currentFilter.sections objectAtIndex:i];
// Delete rows that do not pass the new filter
for(int j = 0; j < section.objects.count; j++) {
NSObject *o = [section.objects objectAtIndex:j];
if (!newFilter.predicate(o)) {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
[self.delegate controller:self.fetchedResultsController
didChangeObject:o
atIndexPath:[NSIndexPath indexPathForRow:j inSection:i]
forChangeType:NSFetchedResultsChangeDelete
newIndexPath:nil];
}
}
}
}
[newFilter.sections removeAllObjects];
for(int i = 0; i < self.fetchedResultsController.sections.count; i++) {
id<NSFetchedResultsSectionInfo> section = (id<NSFetchedResultsSectionInfo>)[self.fetchedResultsController.sections objectAtIndex:i];
SSFilteredResultsSection *currentFilteredSection = [currentFilter.sections objectAtIndex:i];
SSFilteredResultsSection *filteredSection = [[SSFilteredResultsSection alloc] init];
filteredSection.internalName = section.name;
filteredSection.internalIndexTitle = section.indexTitle;
// add all passable objects in section to the new filter
for(int j = 0; j < section.objects.count; j++) {
NSObject *o = [section.objects objectAtIndex:j];
if (newFilter.predicate(o)) {
[filteredSection addObject:o];
}
}
[newFilter.sections addObject:filteredSection];
for (int j = 0; j < filteredSection.objects.count; j++) {
NSObject *o = [filteredSection.objects objectAtIndex:j];
BOOL foundInCurrentFilter = NO;
for (int k = 0; k < currentFilteredSection.objects.count; k++) {
if (o == [currentFilteredSection.objects objectAtIndex:k]) {
foundInCurrentFilter = YES;
if (j != k) {
// Move rows that still pass the filter but need to be moved.
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
[self.delegate controller:self.fetchedResultsController
didChangeObject:o
atIndexPath:[NSIndexPath indexPathForRow:k inSection:i]
forChangeType:NSFetchedResultsChangeMove
newIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
}
if (!foundInCurrentFilter) {
// Insert rows that did not pass the current filter but do pass the new filter
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
[self.delegate controller:self.fetchedResultsController
didChangeObject:o
atIndexPath:nil
forChangeType:NSFetchedResultsChangeInsert
newIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
}
}
if ([(NSObject *)self.delegate respondsToSelector:@selector(controllerDidChangeContent:)]) {
[self.delegate controllerDidChangeContent:self.fetchedResultsController];
}
}
#pragma mark NSFetchedResultsController Pass Throughs
- (BOOL) performFetch:(NSError **)error {
return [self.fetchedResultsController performFetch:error];
}
- (NSArray *)fetchedObjects {
NSMutableArray *arr = [NSMutableArray array];
if (self.currentFilter == nil) {
for(int i = 0; i < self.fetchedResultsController.sections.count; i++) {
id<NSFetchedResultsSectionInfo> section = (id<NSFetchedResultsSectionInfo>)[self.fetchedResultsController.sections objectAtIndex:i];
[arr addObjectsFromArray:section.objects];
}
} else {
for(int i = 0; i < self.currentFilter.sections.count; i++) {
SSFilteredResultsSection *section = (SSFilteredResultsSection *)[self.currentFilter.sections objectAtIndex:i];
[arr addObjectsFromArray:section.objects];
}
}
return arr;
}
- (NSArray *)unfilteredFetchedObjects {
return self.fetchedResultsController.fetchedObjects;
}
- (NSArray *)sections {
if (self.currentFilter == nil) {
return [self.fetchedResultsController sections];
} else {
return [self.currentFilter sections];
}
}
- (id)objectAtIndexPath:(NSIndexPath *)indexPath {
if (!indexPath) {
return nil;
}
if (!self.currentFilter) {
return [self.fetchedResultsController objectAtIndexPath:indexPath];
} else {
return [self.currentFilter objectAtIndexPath:indexPath];
}
}
- (NSIndexPath *)indexPathForObject:(id)object {
if (!self.currentFilter) {
return [self.fetchedResultsController indexPathForObject:object];
} else {
return [self.currentFilter indexPathForObject:object];
}
}
- (NSManagedObjectContext *)managedObjectContext {
return self.fetchedResultsController.managedObjectContext;
}
- (NSString *)cacheName {
return self.fetchedResultsController.cacheName;
}
- (NSString *)sectionNameKeyPath {
return self.fetchedResultsController.sectionNameKeyPath;
}
- (NSFetchRequest *)fetchRequest {
return self.fetchedResultsController.fetchRequest;
}
+ (void)deleteCacheWithName:(NSString *)name {
[NSFetchedResultsController deleteCacheWithName:name];
}
- (NSArray *)sectionIndexTitles {
// TODO: This may not be reliable when filtering is on
return self.fetchedResultsController.sectionIndexTitles;
}
- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName {
// TODO: This may not be reliable when filtering is on
return [self.fetchedResultsController sectionIndexTitleForSectionName:sectionName];
}
- (NSInteger)sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)sectionIndex {
// TODO: This may not be reliable when filtering is on
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:sectionIndex];
}
#pragma mark NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controllerWillChangeContent:)]) {
[self.delegate controllerWillChangeContent:controller];
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:)]) {
if (self.currentFilter) {
if (type == NSFetchedResultsChangeInsert || type == NSFetchedResultsChangeUpdate) {
[self rebuildCurrentFilter];
indexPath = [self indexPathForObject:anObject];
if (indexPath) {
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
}
} else if (type == NSFetchedResultsChangeMove) {
NSIndexPath *previousIndexInFilter = [self.currentFilter indexPathForObject:anObject];
[self rebuildCurrentFilter];
NSIndexPath *indexInFilter = [self.currentFilter indexPathForObject:anObject];
if (previousIndexInFilter && indexInFilter) {
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:NSFetchedResultsChangeMove newIndexPath:newIndexPath];
} else if (!previousIndexInFilter && indexInFilter) {
newIndexPath = [self.currentFilter indexPathForObject:anObject];
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:NSFetchedResultsChangeInsert newIndexPath:newIndexPath];
} else if (previousIndexInFilter && !indexInFilter) {
indexPath = previousIndexInFilter;
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:NSFetchedResultsChangeDelete newIndexPath:newIndexPath];
}
} else if (type == NSFetchedResultsChangeDelete) {
indexPath = [self.currentFilter indexPathForObject:anObject];
if (indexPath) {
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:NSFetchedResultsChangeDelete newIndexPath:newIndexPath];
}
[self rebuildCurrentFilter];
}
} else {
[self.delegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
}
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controller:didChangeSection:atIndex:forChangeType:)]) {
[self.delegate controller:controller didChangeSection:sectionInfo atIndex:sectionIndex forChangeType:type];
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
if ([(NSObject *)self.delegate respondsToSelector:@selector(controllerDidChangeContent:)]) {
[self.delegate controllerDidChangeContent:controller];
}
}
@end