GRKArrayDiff+UICollectionView.m 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2016 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Vincent L. Cone <vincent.l.cone # tuta.io>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "GRKArrayDiff+UICollectionView.h"

@implementation GRKArrayDiff (UICollectionView)
- (void)performBatchUpdatesWithCollectionView:(UICollectionView *)collectionView section:(NSUInteger)section dataSourceUpdate:(void (^)(void))update completion:(void (^ __nullable)(BOOL finished))completion
{
    [collectionView performBatchUpdates:^{
        NSArray<NSIndexPath *> *deletions = [self indexPathsForDiffType:GRKArrayDiffTypeDeletions withSection:section];
        if (deletions.count) {
            deletions = [deletions sortedArrayUsingSelector:@selector(compare:)];
            [collectionView deleteItemsAtIndexPaths:deletions];
        }
        NSArray<NSIndexPath *> *insertions = [self indexPathsForDiffType:GRKArrayDiffTypeInsertions withSection:section];
        if (insertions.count) {
            insertions = [insertions sortedArrayUsingSelector:@selector(compare:)];
            [collectionView insertItemsAtIndexPaths:insertions];
        }
        for (GRKArrayDiffInfo *moveInfo in self.moves) {
            NSIndexPath *previousIndexPath = [moveInfo indexPathForIndexType:GRKArrayDiffInfoIndexTypePrevious withSection:section];
            NSIndexPath *currentIndexPath = [moveInfo indexPathForIndexType:GRKArrayDiffInfoIndexTypeCurrent withSection:section];
            [collectionView moveItemAtIndexPath:previousIndexPath toIndexPath:currentIndexPath];
        }

        NSArray<NSIndexPath *> *modifications = [self indexPathsForDiffType:GRKArrayDiffTypeModifications withSection:section];
        if (modifications.count) {
            [collectionView reloadItemsAtIndexPaths:modifications];
        }

        // perform data source update within batchUpdateBlock
        if (update) {
            update();
        }

    } completion:completion];
}
@end