Fix it better

This commit is contained in:
Adlai Holler
2015-11-24 18:44:10 -08:00
parent 6b3f0ada12
commit 2d633f118b

View File

@@ -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];
}