/
PSTCollectionViewUpdateItem.m
117 lines (95 loc) · 3.73 KB
/
PSTCollectionViewUpdateItem.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
//
// PSTCollectionViewUpdateItem.m
// PSPDFKit
//
// Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
// Contributed by Sergey Gavrilyuk.
//
#import "PSTCollectionViewUpdateItem.h"
#import "NSIndexPath+PSTCollectionViewAdditions.h"
@interface PSTCollectionViewUpdateItem () {
NSIndexPath *_initialIndexPath;
NSIndexPath *_finalIndexPath;
PSTCollectionUpdateAction _updateAction;
id _gap;
}
@end
@implementation PSTCollectionViewUpdateItem
@synthesize updateAction = _updateAction;
@synthesize indexPathBeforeUpdate = _initialIndexPath;
@synthesize indexPathAfterUpdate = _finalIndexPath;
- (id)initWithInitialIndexPath:(NSIndexPath *)initialIndexPath finalIndexPath:(NSIndexPath *)finalIndexPath updateAction:(PSTCollectionUpdateAction)updateAction {
if ((self = [super init])) {
_initialIndexPath = initialIndexPath;
_finalIndexPath = finalIndexPath;
_updateAction = updateAction;
}
return self;
}
- (id)initWithAction:(PSTCollectionUpdateAction)updateAction forIndexPath:(NSIndexPath *)indexPath {
if (updateAction == PSTCollectionUpdateActionInsert)
return [self initWithInitialIndexPath:nil finalIndexPath:indexPath updateAction:updateAction];
else if (updateAction == PSTCollectionUpdateActionDelete)
return [self initWithInitialIndexPath:indexPath finalIndexPath:nil updateAction:updateAction];
else if (updateAction == PSTCollectionUpdateActionReload)
return [self initWithInitialIndexPath:indexPath finalIndexPath:indexPath updateAction:updateAction];
return nil;
}
- (id)initWithOldIndexPath:(NSIndexPath *)oldIndexPath newIndexPath:(NSIndexPath *)newIndexPath {
return [self initWithInitialIndexPath:oldIndexPath finalIndexPath:newIndexPath updateAction:PSTCollectionUpdateActionMove];
}
- (NSString *)description {
NSString *action = nil;
switch (_updateAction) {
case PSTCollectionUpdateActionInsert: action = @"insert"; break;
case PSTCollectionUpdateActionDelete: action = @"delete"; break;
case PSTCollectionUpdateActionMove: action = @"move"; break;
case PSTCollectionUpdateActionReload: action = @"reload"; break;
default: break;
}
return [NSString stringWithFormat:@"Index path before update (%@) index path after update (%@) action (%@).", _initialIndexPath, _finalIndexPath, action];
}
- (void)setNewIndexPath:(NSIndexPath *)indexPath {
_finalIndexPath = indexPath;
}
- (void)setGap:(id)gap {
_gap = gap;
}
- (BOOL)isSectionOperation {
return (_initialIndexPath.item == NSNotFound || _finalIndexPath.item == NSNotFound);
}
- (NSIndexPath *)newIndexPath {
return _finalIndexPath;
}
- (id)gap {
return _gap;
}
- (PSTCollectionUpdateAction)action {
return _updateAction;
}
- (id)indexPath {
//TODO: check this
return _initialIndexPath;
}
- (NSComparisonResult)compareIndexPaths:(PSTCollectionViewUpdateItem *)otherItem {
NSComparisonResult result = NSOrderedSame;
NSIndexPath *selfIndexPath = nil;
NSIndexPath *otherIndexPath = nil;
switch (_updateAction) {
case PSTCollectionUpdateActionInsert:
selfIndexPath = _finalIndexPath;
otherIndexPath = [otherItem newIndexPath];
break;
case PSTCollectionUpdateActionDelete:
selfIndexPath = _initialIndexPath;
otherIndexPath = [otherItem indexPath];
default: break;
}
if (self.isSectionOperation) result = [@(selfIndexPath.section) compare:@(otherIndexPath.section)];
else result = [selfIndexPath compare:otherIndexPath];
return result;
}
- (NSComparisonResult)inverseCompareIndexPaths:(PSTCollectionViewUpdateItem *)otherItem {
return (NSComparisonResult)([self compareIndexPaths:otherItem] * -1);
}
@end