-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSTCollectionViewItemKey.m
executable file
·95 lines (79 loc) · 3.35 KB
/
PSTCollectionViewItemKey.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
//
// PSTCollectionViewItemKey.m
// PSPDFKit
//
// Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
//
#import "PSTCollectionViewItemKey.h"
#import "PSTCollectionViewLayout.h"
NSString *const PSTCollectionElementKindCell = @"UICollectionElementKindCell";
NSString *const PSTCollectionElementKindDecorationView = @"PSTCollectionElementKindDecorationView";
@implementation PSTCollectionViewItemKey
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Static
+ (id)collectionItemKeyForCellWithIndexPath:(NSIndexPath *)indexPath {
PSTCollectionViewItemKey *key = [[self class] new];
key.indexPath = indexPath;
key.type = PSTCollectionViewItemTypeCell;
key.identifier = PSTCollectionElementKindCell;
return key;
}
+ (id)collectionItemKeyForLayoutAttributes:(PSTCollectionViewLayoutAttributes *)layoutAttributes {
PSTCollectionViewItemKey *key = [[self class] new];
key.indexPath = layoutAttributes.indexPath;
key.type = layoutAttributes.representedElementCategory;
key.identifier = layoutAttributes.representedElementKind;
return key;
}
// elementKind or reuseIdentifier?
+ (id)collectionItemKeyForDecorationViewOfKind:(NSString *)elementKind andIndexPath:(NSIndexPath *)indexPath {
PSTCollectionViewItemKey *key = [[self class] new];
key.indexPath = indexPath;
key.type = PSTCollectionViewItemTypeDecorationView;
key.identifier = elementKind;
return key;
}
+ (id)collectionItemKeyForSupplementaryViewOfKind:(NSString *)elementKind andIndexPath:(NSIndexPath *)indexPath {
PSTCollectionViewItemKey *key = [[self class] new];
key.indexPath = indexPath;
key.identifier = elementKind;
key.type = PSTCollectionViewItemTypeSupplementaryView;
return key;
}
NSString *PSTCollectionViewItemTypeToString(PSTCollectionViewItemType type) {
switch (type) {
case PSTCollectionViewItemTypeCell: return @"Cell";
case PSTCollectionViewItemTypeDecorationView: return @"Decoration";
case PSTCollectionViewItemTypeSupplementaryView: return @"Supplementary";
default: return @"<INVALID>";
}
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p Type = %@ Identifier=%@ IndexPath = %@>", NSStringFromClass([self class]),
self, PSTCollectionViewItemTypeToString(self.type), _identifier, self.indexPath];
}
- (NSUInteger)hash {
return (([_indexPath hash] + _type) * 31) + [_identifier hash];
}
- (BOOL)isEqual:(id)other {
if ([other isKindOfClass:[self class]]) {
PSTCollectionViewItemKey *otherKeyItem = (PSTCollectionViewItemKey *)other;
// identifier might be nil?
if (_type == otherKeyItem.type && [_indexPath isEqual:otherKeyItem.indexPath] && ([_identifier isEqualToString:otherKeyItem.identifier] || _identifier == otherKeyItem.identifier)) {
return YES;
}
}
return NO;
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
PSTCollectionViewItemKey *itemKey = [[self class] new];
itemKey.indexPath = self.indexPath;
itemKey.type = self.type;
itemKey.identifier = self.identifier;
return itemKey;
}
@end