PSTCollectionViewItemKey.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // PSTCollectionViewItemKey.m
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTCollectionViewItemKey.h"
  8. NSString *const PSTCollectionElementKindCell = @"UICollectionElementKindCell";
  9. @implementation PSTCollectionViewItemKey
  10. ///////////////////////////////////////////////////////////////////////////////////////////
  11. #pragma mark - Static
  12. + (id)collectionItemKeyForCellWithIndexPath:(NSIndexPath *)indexPath {
  13. PSTCollectionViewItemKey *key = [self.class new];
  14. key.indexPath = indexPath;
  15. key.type = PSTCollectionViewItemTypeCell;
  16. key.identifier = PSTCollectionElementKindCell;
  17. return key;
  18. }
  19. + (id)collectionItemKeyForLayoutAttributes:(PSTCollectionViewLayoutAttributes *)layoutAttributes {
  20. PSTCollectionViewItemKey *key = [self.class new];
  21. key.indexPath = layoutAttributes.indexPath;
  22. PSTCollectionViewItemType const itemType = layoutAttributes.representedElementCategory;
  23. key.type = itemType;
  24. key.identifier = layoutAttributes.representedElementKind;
  25. return key;
  26. }
  27. NSString *PSTCollectionViewItemTypeToString(PSTCollectionViewItemType type) {
  28. switch (type) {
  29. case PSTCollectionViewItemTypeCell: return @"Cell";
  30. case PSTCollectionViewItemTypeDecorationView: return @"Decoration";
  31. case PSTCollectionViewItemTypeSupplementaryView: return @"Supplementary";
  32. default: return @"<INVALID>";
  33. }
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////////////////
  36. #pragma mark - NSObject
  37. - (NSString *)description {
  38. return [NSString stringWithFormat:@"<%@: %p Type = %@ Identifier=%@ IndexPath = %@>", NSStringFromClass(self.class),
  39. self, PSTCollectionViewItemTypeToString(self.type), _identifier, self.indexPath];
  40. }
  41. - (NSUInteger)hash {
  42. return (([_indexPath hash] + _type) * 31) + [_identifier hash];
  43. }
  44. - (BOOL)isEqual:(id)other {
  45. if ([other isKindOfClass:self.class]) {
  46. PSTCollectionViewItemKey *otherKeyItem = (PSTCollectionViewItemKey *)other;
  47. // identifier might be nil?
  48. if (_type == otherKeyItem.type && [_indexPath isEqual:otherKeyItem.indexPath] && ([_identifier isEqualToString:otherKeyItem.identifier] || _identifier == otherKeyItem.identifier)) {
  49. return YES;
  50. }
  51. }
  52. return NO;
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////////////////
  55. #pragma mark - NSCopying
  56. - (id)copyWithZone:(NSZone *)zone {
  57. PSTCollectionViewItemKey *itemKey = [self.class new];
  58. itemKey.indexPath = self.indexPath;
  59. itemKey.type = self.type;
  60. itemKey.identifier = self.identifier;
  61. return itemKey;
  62. }
  63. @end