RABatchChangeEntity.m 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // RABatchChangesEntity.m
  3. // RATreeView
  4. //
  5. // Created by Rafal Augustyniak on 17/11/15.
  6. // Copyright © 2015 Rafal Augustyniak. All rights reserved.
  7. //
  8. #import "RABatchChangeEntity.h"
  9. @implementation RABatchChangeEntity
  10. + (instancetype)batchChangeEntityWithBlock:(void (^)())updates type:(RABatchChangeType)type ranking:(NSInteger)ranking
  11. {
  12. NSParameterAssert(updates);
  13. RABatchChangeEntity *entity = [RABatchChangeEntity new];
  14. entity.type = type;
  15. entity.ranking = ranking;
  16. entity.updatesBlock = updates;
  17. return entity;
  18. }
  19. - (NSComparisonResult)compare:(RABatchChangeEntity *)otherEntity
  20. {
  21. if ([self destructiveOperation]) {
  22. if (![otherEntity destructiveOperation]) {
  23. return NSOrderedAscending;
  24. } else {
  25. return [@(otherEntity.ranking) compare:@(self.ranking)];
  26. }
  27. } else if (self.type == RABatchChangeTypeItemMove && otherEntity.type != RABatchChangeTypeItemMove) {
  28. return [otherEntity destructiveOperation] ? NSOrderedAscending : NSOrderedDescending;
  29. } else if ([self constructiveOperation]) {
  30. if (![otherEntity constructiveOperation]) {
  31. return NSOrderedDescending;
  32. } else {
  33. return [@(self.ranking) compare:@(otherEntity.ranking)];
  34. }
  35. } else {
  36. return NSOrderedSame;
  37. }
  38. }
  39. - (BOOL)constructiveOperation
  40. {
  41. return self.type == RABatchChangeTypeItemRowExpansion
  42. || self.type == RABatchChangeTypeItemRowInsertion;
  43. }
  44. - (BOOL)destructiveOperation
  45. {
  46. return self.type == RABatchChangeTypeItemRowCollapse
  47. || self.type == RABatchChangeTypeItemRowDeletion;
  48. }
  49. @end