GRKArrayDiff+UICollectionView.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2016 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Vincent L. Cone <vincent.l.cone # tuta.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "GRKArrayDiff+UICollectionView.h"
  12. @implementation GRKArrayDiff (UICollectionView)
  13. - (void)performBatchUpdatesWithCollectionView:(UICollectionView *)collectionView section:(NSUInteger)section dataSourceUpdate:(void (^)(void))update completion:(void (^ __nullable)(BOOL finished))completion
  14. {
  15. [collectionView performBatchUpdates:^{
  16. NSArray<NSIndexPath *> *deletions = [self indexPathsForDiffType:GRKArrayDiffTypeDeletions withSection:section];
  17. if (deletions.count) {
  18. deletions = [deletions sortedArrayUsingSelector:@selector(compare:)];
  19. [collectionView deleteItemsAtIndexPaths:deletions];
  20. }
  21. NSArray<NSIndexPath *> *insertions = [self indexPathsForDiffType:GRKArrayDiffTypeInsertions withSection:section];
  22. if (insertions.count) {
  23. insertions = [insertions sortedArrayUsingSelector:@selector(compare:)];
  24. [collectionView insertItemsAtIndexPaths:insertions];
  25. }
  26. for (GRKArrayDiffInfo *moveInfo in self.moves) {
  27. NSIndexPath *previousIndexPath = [moveInfo indexPathForIndexType:GRKArrayDiffInfoIndexTypePrevious withSection:section];
  28. NSIndexPath *currentIndexPath = [moveInfo indexPathForIndexType:GRKArrayDiffInfoIndexTypeCurrent withSection:section];
  29. [collectionView moveItemAtIndexPath:previousIndexPath toIndexPath:currentIndexPath];
  30. }
  31. NSArray<NSIndexPath *> *modifications = [self indexPathsForDiffType:GRKArrayDiffTypeModifications withSection:section];
  32. if (modifications.count) {
  33. [collectionView reloadItemsAtIndexPaths:modifications];
  34. }
  35. // perform data source update within batchUpdateBlock
  36. if (update) {
  37. update();
  38. }
  39. } completion:completion];
  40. }
  41. @end