-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNNDiffReloader.m
More file actions
76 lines (61 loc) · 2.78 KB
/
Copy pathNNDiffReloader.m
File metadata and controls
76 lines (61 loc) · 2.78 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
//
// NNDiffReloader.m
// ArrayDiff
//
// Created by Nick Tymchenko on 27/04/14.
// Copyright (c) 2014 Nick Tymchenko. All rights reserved.
//
#import "NNDiffReloader.h"
#import "NNSectionsDiffChange.h"
@implementation NNDiffReloader
#pragma mark - Public
- (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff
options:(NNDiffReloadOptions *)options
completion:(void (^)())completion
{
NSParameterAssert(diff != nil);
NSParameterAssert(options != nil);
if ([diff isEqual:[[NNSectionsDiff alloc] init]]) {
if (completion) {
completion();
}
return;
}
[self performUpdates:^{
[self deleteSections:diff.deletedSections];
[self insertSections:diff.insertedSections];
[self deleteItemsAtIndexPaths:[diff.deleted allObjects]];
[self insertItemsAtIndexPaths:[diff.inserted allObjects]];
for (NNSectionsDiffChange *change in diff.changed) {
if (change.type & NNDiffChangeUpdate) {
if (options.useUpdateBlockForReload) {
[self updateItemsAtIndexPaths:@[ change.before ]];
} else {
[self reloadItemsAtIndexPaths:@[ change.before ]];
}
}
if (change.type & NNDiffChangeMove) {
if (options.useMoveIfPossible) {
[self moveItemAtIndexPath:change.before toIndexPath:change.after];
} else {
[self deleteItemsAtIndexPaths:@[ change.before ]];
[self insertItemsAtIndexPaths:@[ change.after ]];
}
}
}
} withOptions:options completion:completion];
}
#pragma mark - Abstract
#define methodNotImplemented() \
@throw [NSException exceptionWithName:NSInternalInconsistencyException \
reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \
userInfo:nil]
- (void)performUpdates:(void (^)())updates withOptions:(NNDiffReloadOptions *)options completion:(void (^)())completion { methodNotImplemented(); }
- (void)insertSections:(NSIndexSet *)sections { methodNotImplemented(); }
- (void)deleteSections:(NSIndexSet *)sections { methodNotImplemented(); }
- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths { methodNotImplemented(); }
- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths { methodNotImplemented(); }
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths { methodNotImplemented(); }
- (void)updateItemsAtIndexPaths:(NSArray *)indexPaths { methodNotImplemented(); }
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath { methodNotImplemented(); }
@end