PSTCollectionViewUpdateItem.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // PSTCollectionViewUpdateItem.m
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. // Contributed by Sergey Gavrilyuk.
  7. //
  8. #import "PSTCollectionViewUpdateItem.h"
  9. #import "NSIndexPath+PSTCollectionViewAdditions.h"
  10. @interface PSTCollectionViewUpdateItem () {
  11. NSIndexPath *_initialIndexPath;
  12. NSIndexPath *_finalIndexPath;
  13. PSTCollectionUpdateAction _updateAction;
  14. id _gap;
  15. }
  16. @end
  17. @implementation PSTCollectionViewUpdateItem
  18. @synthesize updateAction = _updateAction;
  19. @synthesize indexPathBeforeUpdate = _initialIndexPath;
  20. @synthesize indexPathAfterUpdate = _finalIndexPath;
  21. - (id)initWithInitialIndexPath:(NSIndexPath *)initialIndexPath finalIndexPath:(NSIndexPath *)finalIndexPath updateAction:(PSTCollectionUpdateAction)updateAction {
  22. if ((self = [super init])) {
  23. _initialIndexPath = initialIndexPath;
  24. _finalIndexPath = finalIndexPath;
  25. _updateAction = updateAction;
  26. }
  27. return self;
  28. }
  29. - (id)initWithAction:(PSTCollectionUpdateAction)updateAction forIndexPath:(NSIndexPath *)indexPath {
  30. if (updateAction == PSTCollectionUpdateActionInsert)
  31. return [self initWithInitialIndexPath:nil finalIndexPath:indexPath updateAction:updateAction];
  32. else if (updateAction == PSTCollectionUpdateActionDelete)
  33. return [self initWithInitialIndexPath:indexPath finalIndexPath:nil updateAction:updateAction];
  34. else if (updateAction == PSTCollectionUpdateActionReload)
  35. return [self initWithInitialIndexPath:indexPath finalIndexPath:indexPath updateAction:updateAction];
  36. return nil;
  37. }
  38. - (id)initWithOldIndexPath:(NSIndexPath *)oldIndexPath newIndexPath:(NSIndexPath *)newIndexPath {
  39. return [self initWithInitialIndexPath:oldIndexPath finalIndexPath:newIndexPath updateAction:PSTCollectionUpdateActionMove];
  40. }
  41. - (NSString *)description {
  42. NSString *action = nil;
  43. switch (_updateAction) {
  44. case PSTCollectionUpdateActionInsert: action = @"insert"; break;
  45. case PSTCollectionUpdateActionDelete: action = @"delete"; break;
  46. case PSTCollectionUpdateActionMove: action = @"move"; break;
  47. case PSTCollectionUpdateActionReload: action = @"reload"; break;
  48. default: break;
  49. }
  50. return [NSString stringWithFormat:@"Index path before update (%@) index path after update (%@) action (%@).", _initialIndexPath, _finalIndexPath, action];
  51. }
  52. - (void)setNewIndexPath:(NSIndexPath *)indexPath {
  53. _finalIndexPath = indexPath;
  54. }
  55. - (void)setGap:(id)gap {
  56. _gap = gap;
  57. }
  58. - (BOOL)isSectionOperation {
  59. return (_initialIndexPath.item == NSNotFound || _finalIndexPath.item == NSNotFound);
  60. }
  61. - (NSIndexPath *)newIndexPath {
  62. return _finalIndexPath;
  63. }
  64. - (id)gap {
  65. return _gap;
  66. }
  67. - (PSTCollectionUpdateAction)action {
  68. return _updateAction;
  69. }
  70. - (id)indexPath {
  71. //TODO: check this
  72. return _initialIndexPath;
  73. }
  74. - (NSComparisonResult)compareIndexPaths:(PSTCollectionViewUpdateItem *)otherItem {
  75. NSComparisonResult result = NSOrderedSame;
  76. NSIndexPath *selfIndexPath = nil;
  77. NSIndexPath *otherIndexPath = nil;
  78. switch (_updateAction) {
  79. case PSTCollectionUpdateActionInsert:
  80. selfIndexPath = _finalIndexPath;
  81. otherIndexPath = [otherItem newIndexPath];
  82. break;
  83. case PSTCollectionUpdateActionDelete:
  84. selfIndexPath = _initialIndexPath;
  85. otherIndexPath = [otherItem indexPath];
  86. default: break;
  87. }
  88. if (self.isSectionOperation) result = [@(selfIndexPath.section) compare:@(otherIndexPath.section)];
  89. else result = [selfIndexPath compare:otherIndexPath];
  90. return result;
  91. }
  92. - (NSComparisonResult)inverseCompareIndexPaths:(PSTCollectionViewUpdateItem *)otherItem {
  93. return (NSComparisonResult)([self compareIndexPaths:otherItem] * -1);
  94. }
  95. @end