RABatchChanges.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2014 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RABatchChanges.h"
  20. #import "RABatchChangeEntity.h"
  21. @interface RABatchChanges ()
  22. @property (nonatomic, strong) NSMutableArray *operationsStorage;
  23. @property (nonatomic) NSInteger batchChangesCounter;
  24. @end
  25. @implementation RABatchChanges
  26. - (id)init
  27. {
  28. self = [super init];
  29. if (self) {
  30. _batchChangesCounter = 0;
  31. }
  32. return self;
  33. }
  34. - (void)beginUpdates
  35. {
  36. if (self.batchChangesCounter++ == 0) {
  37. self.operationsStorage = [NSMutableArray array];
  38. }
  39. }
  40. - (void)endUpdates
  41. {
  42. self.batchChangesCounter--;
  43. if (self.batchChangesCounter == 0) {
  44. [self.operationsStorage sortUsingSelector:@selector(compare:)];
  45. for (RABatchChangeEntity *entity in self.operationsStorage) {
  46. entity.updatesBlock();
  47. }
  48. self.operationsStorage = nil;
  49. }
  50. }
  51. - (void)insertItemWithBlock:(void (^)())update atIndex:(NSInteger)index
  52. {
  53. RABatchChangeEntity *entity = [RABatchChangeEntity batchChangeEntityWithBlock:update
  54. type:RABatchChangeTypeItemRowInsertion
  55. ranking:index];
  56. [self addBatchChangeEntity:entity];
  57. }
  58. - (void)expandItemWithBlock:(void (^)())update atIndex:(NSInteger)index
  59. {
  60. RABatchChangeEntity *entity= [RABatchChangeEntity batchChangeEntityWithBlock:update
  61. type:RABatchChangeTypeItemRowExpansion
  62. ranking:index];
  63. [self addBatchChangeEntity:entity];
  64. }
  65. - (void)deleteItemWithBlock:(void (^)())update lastIndex:(NSInteger)lastIndex
  66. {
  67. RABatchChangeEntity *entity = [RABatchChangeEntity batchChangeEntityWithBlock:update
  68. type:RABatchChangeTypeItemRowDeletion
  69. ranking:lastIndex];
  70. [self addBatchChangeEntity:entity];
  71. }
  72. - (void)collapseItemWithBlock:(void (^)())update lastIndex:(NSInteger)lastIndex
  73. {
  74. RABatchChangeEntity *entity = [RABatchChangeEntity batchChangeEntityWithBlock:update
  75. type:RABatchChangeTypeItemRowCollapse
  76. ranking:lastIndex];
  77. [self addBatchChangeEntity:entity];
  78. }
  79. - (void)moveItemWithDeletionBlock:(void (^)())deletionUpdate fromLastIndex:(NSInteger)lastIndex additionBlock:(void (^)())additionUpdate toIndex:(NSInteger)index
  80. {
  81. RABatchChangeEntity *firstEntity = [RABatchChangeEntity batchChangeEntityWithBlock:deletionUpdate
  82. type:RABatchChangeTypeItemRowDeletion
  83. ranking:lastIndex];
  84. RABatchChangeEntity *secondEntity = [RABatchChangeEntity batchChangeEntityWithBlock:additionUpdate
  85. type:RABatchChangeTypeItemRowInsertion
  86. ranking:index];
  87. [self addBatchChangeEntity:firstEntity];
  88. [self addBatchChangeEntity:secondEntity];
  89. }
  90. #pragma mark -
  91. - (void)addBatchChangeEntity:(RABatchChangeEntity *)entity
  92. {
  93. if (self.batchChangesCounter > 0) {
  94. [self.operationsStorage addObject:entity];
  95. } else {
  96. entity.updatesBlock();
  97. }
  98. }
  99. @end