From 2d633f118bddd4b9abeefc87dcac41122d97a8ed Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Tue, 24 Nov 2015 18:44:10 -0800 Subject: [PATCH] Fix it better --- .../Private/_ASHierarchyChangeSet.m | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/AsyncDisplayKit/Private/_ASHierarchyChangeSet.m b/AsyncDisplayKit/Private/_ASHierarchyChangeSet.m index d35748c849..c4d9af059f 100644 --- a/AsyncDisplayKit/Private/_ASHierarchyChangeSet.m +++ b/AsyncDisplayKit/Private/_ASHierarchyChangeSet.m @@ -217,38 +217,35 @@ // Create new changes by grouping sorted changes by animation option NSMutableArray *result = [NSMutableArray new]; - __block ASDataControllerAnimationOptions currentOptions = 0; - __block NSMutableIndexSet *currentIndexes = nil; + __block ASDataControllerAnimationOptions currentOptions = NSUIntegerMax; + __block NSMutableIndexSet *currentIndexes = [NSMutableIndexSet indexSet]; - BOOL reverse = (type == _ASHierarchyChangeTypeDelete); - NSEnumerationOptions options = reverse ? NSEnumerationReverse : kNilOptions; - NSUInteger endIndex = reverse ? allIndexes.firstIndex : allIndexes.lastIndex; + NSEnumerationOptions options = type == _ASHierarchyChangeTypeDelete ? NSEnumerationReverse : kNilOptions; [allIndexes enumerateIndexesWithOptions:options usingBlock:^(NSUInteger idx, __unused BOOL * stop) { ASDataControllerAnimationOptions options = [animationOptions[@(idx)] integerValue]; - BOOL endingCurrentGroup = NO; - - if (currentIndexes == nil) { - // Starting a new group - currentIndexes = [NSMutableIndexSet indexSetWithIndex:idx]; - currentOptions = options; - } else if (options == currentOptions) { - // Continuing the current group - [currentIndexes addIndex:idx]; - } else { - endingCurrentGroup = YES; - } - - BOOL endingLastGroup = (currentIndexes != nil && endIndex == idx); - - if (endingCurrentGroup || endingLastGroup) { - _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc] initWithChangeType:type indexSet:currentIndexes animationOptions:currentOptions]; + + // End the previous group if needed. + if (options != currentOptions && currentIndexes.count > 0) { + _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc] initWithChangeType:type indexSet:[currentIndexes copy] animationOptions:currentOptions]; [result addObject:change]; - currentOptions = 0; - currentIndexes = nil; + [currentIndexes removeAllIndexes]; } + + // Start a new group if needed. + if (currentIndexes.count == 0) { + currentOptions = options; + } + + [currentIndexes addIndex:idx]; }]; - + + // Finish up the last group. + if (currentIndexes.count > 0) { + _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc] initWithChangeType:type indexSet:[currentIndexes copy] animationOptions:currentOptions]; + [result addObject:change]; + } + [changes setArray:result]; } @@ -303,36 +300,34 @@ // Create new changes by grouping sorted changes by animation option NSMutableArray *result = [NSMutableArray new]; - + ASDataControllerAnimationOptions currentOptions = 0; - NSMutableArray *currentIndexPaths = nil; - NSIndexPath *lastIndexPath = allIndexPaths.lastObject; - + NSMutableArray *currentIndexPaths = [NSMutableArray array]; + for (NSIndexPath *indexPath in allIndexPaths) { ASDataControllerAnimationOptions options = [animationOptions[indexPath] integerValue]; - BOOL endingCurrentGroup = NO; - - if (currentIndexPaths == nil) { - // Starting a new group - currentIndexPaths = [NSMutableArray arrayWithObject:indexPath]; - currentOptions = options; - } else if (options == currentOptions) { - // Continuing the current group - [currentIndexPaths addObject:indexPath]; - } else { - endingCurrentGroup = YES; - } - - BOOL endingLastGroup = (currentIndexPaths != nil && (NSOrderedSame == [lastIndexPath compare:indexPath])); - if (endingCurrentGroup || endingLastGroup) { - _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc] initWithChangeType:type indexPaths:currentIndexPaths animationOptions:currentOptions presorted:YES]; + // End the previous group if needed. + if (options != currentOptions && currentIndexPaths.count > 0) { + _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc] initWithChangeType:type indexPaths:[currentIndexPaths copy] animationOptions:currentOptions presorted:YES]; [result addObject:change]; - currentOptions = 0; - currentIndexPaths = nil; + [currentIndexPaths removeAllObjects]; } + + // Start a new group if needed. + if (currentIndexPaths.count == 0) { + currentOptions = options; + } + + [currentIndexPaths addObject:indexPath]; } - + + // Finish up the last group. + if (currentIndexPaths.count > 0) { + _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc] initWithChangeType:type indexPaths:[currentIndexPaths copy] animationOptions:currentOptions presorted:YES]; + [result addObject:change]; + } + [changes setArray:result]; }