diff --git a/Source/Common/IGListBatchUpdateData.h b/Source/Common/IGListBatchUpdateData.h index 8bdd35e09..81b9706d4 100644 --- a/Source/Common/IGListBatchUpdateData.h +++ b/Source/Common/IGListBatchUpdateData.h @@ -34,7 +34,7 @@ IGLK_SUBCLASSING_RESTRICTED @property (nonatomic, strong, readonly) NSIndexSet *deleteSections; /** - section moves. + Section moves. */ @property (nonatomic, strong, readonly) NSSet *moveSections; @@ -49,7 +49,7 @@ IGLK_SUBCLASSING_RESTRICTED @property (nonatomic, strong, readonly) NSSet *deleteIndexPaths; /** - Item delete index paths. + Item moves. */ @property (nonatomic, strong, readonly) NSSet *moveIndexPaths; diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 72d35cee8..877e0bb45 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -312,7 +312,7 @@ - (void)reloadObjects:(NSArray *)objects { NSMutableIndexSet *sections = [[NSMutableIndexSet alloc] init]; // use the item map based on whether or not we're in an update block - IGListSectionMap *map = [self sectionMapAdjustForUpdateBlock:YES]; + IGListSectionMap *map = [self sectionMapUsingPreviousIfInUpdateBlock:YES]; for (id object in objects) { // look up the item using the map's lookup function. might not be the same item @@ -356,7 +356,7 @@ - (NSInteger)sectionForSectionController:(IGListSectionController *)sectionController { IGAssertMainThread(); IGParameterAssert(sectionController != nil); - + const NSInteger section = [self.sectionMap sectionForSectionController:sectionController]; return [self.sectionMap objectForSection:section]; } @@ -421,19 +421,19 @@ - (NSArray *)visibleObjects { - (NSArray *)visibleCellsForObject:(id)object { IGAssertMainThread(); IGParameterAssert(object != nil); - + const NSInteger section = [self.sectionMap sectionForObject:object]; if (section == NSNotFound) { return [NSArray new]; } - + NSArray *visibleCells = [self.collectionView visibleCells]; UICollectionView *collectionView = self.collectionView; NSPredicate *controllerPredicate = [NSPredicate predicateWithBlock:^BOOL(UICollectionViewCell* cell, NSDictionary* bindings) { NSIndexPath *indexPath = [collectionView indexPathForCell:cell]; return indexPath.section == section; }]; - + return [visibleCells filteredArrayUsingPredicate:controllerPredicate]; } @@ -556,10 +556,10 @@ - (BOOL)itemCountIsZero { return isZero; } -- (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)usePreviousSection { +- (IGListSectionMap *)sectionMapUsingPreviousIfInUpdateBlock:(BOOL)usePreviousMapIfInUpdateBlock { // if we are inside an update block, we may have to use the /previous/ item map for some operations IGListSectionMap *previousSectionMap = self.previousSectionMap; - if (usePreviousSection && self.isInUpdateBlock && previousSectionMap != nil) { + if (usePreviousMapIfInUpdateBlock && self.isInUpdateBlock && previousSectionMap != nil) { return previousSectionMap; } else { return self.sectionMap; @@ -568,10 +568,10 @@ - (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)usePreviousSection { - (NSArray *)indexPathsFromSectionController:(IGListSectionController *)sectionController indexes:(NSIndexSet *)indexes - usePreviousSection:(BOOL)usePreviousSection { + usePreviousIfInUpdateBlock:(BOOL)usePreviousIfInUpdateBlock { NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; - IGListSectionMap *map = [self sectionMapAdjustForUpdateBlock:usePreviousSection]; + IGListSectionMap *map = [self sectionMapUsingPreviousIfInUpdateBlock:usePreviousIfInUpdateBlock]; const NSInteger section = [map sectionForSectionController:sectionController]; if (section != NSNotFound) { [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { @@ -583,8 +583,8 @@ - (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)usePreviousSection { - (NSIndexPath *)indexPathForSectionController:(IGListSectionController *)controller index:(NSInteger)index - usePreviousSection:(BOOL)usePreviousSection { - IGListSectionMap *map = [self sectionMapAdjustForUpdateBlock:usePreviousSection]; + usePreviousIfInUpdateBlock:(BOOL)usePreviousIfInUpdateBlock { + IGListSectionMap *map = [self sectionMapUsingPreviousIfInUpdateBlock:usePreviousIfInUpdateBlock]; const NSInteger section = [map sectionForSectionController:controller]; if (section == NSNotFound) { return nil; @@ -786,7 +786,7 @@ - (__kindof UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index return nil; } - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; // prevent querying the collection view if it isn't fully reloaded yet for the current data set if (indexPath != nil && indexPath.section < [self.collectionView numberOfSections]) { @@ -818,7 +818,7 @@ - (void)deselectItemAtIndex:(NSInteger)index animated:(BOOL)animated { IGAssertMainThread(); IGParameterAssert(sectionController != nil); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; [self.collectionView deselectItemAtIndexPath:indexPath animated:animated]; } @@ -832,7 +832,7 @@ - (__kindof UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Dequeueing cell of class %@ from section controller %@ without a collection view at index %zi", NSStringFromClass(cellClass), sectionController, index); NSString *identifier = IGListReusableViewIdentifier(cellClass, nil, nil); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; if (![self.registeredCellClasses containsObject:cellClass]) { [self.registeredCellClasses addObject:cellClass]; [collectionView registerClass:cellClass forCellWithReuseIdentifier:identifier]; @@ -848,7 +848,7 @@ - (__kindof UICollectionViewCell *)dequeueReusableCellFromStoryboardWithIdentifi IGParameterAssert(identifier.length > 0); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; return [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; } @@ -862,7 +862,7 @@ - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName IGParameterAssert(index >= 0); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Dequeueing cell with nib name %@ and bundle %@ from section controller %@ without a collection view at index %zi.", nibName, bundle, sectionController, index); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; if (![self.registeredNibNames containsObject:nibName]) { [self.registeredNibNames addObject:nibName]; UINib *nib = [UINib nibWithNibName:nibName bundle:bundle]; @@ -883,7 +883,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Dequeueing cell of class %@ from section controller %@ without a collection view at index %zi with supplementary view %@", NSStringFromClass(viewClass), sectionController, index, elementKind); NSString *identifier = IGListReusableViewIdentifier(viewClass, nil, elementKind); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; if (![self.registeredSupplementaryViewIdentifiers containsObject:identifier]) { [self.registeredSupplementaryViewIdentifiers addObject:identifier]; [collectionView registerClass:viewClass forSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier]; @@ -902,7 +902,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewFromStory IGParameterAssert(index >= 0); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Dequeueing Supplementary View from storyboard of kind %@ with identifier %@ for section controller %@ without a collection view at index %zi", elementKind, identifier, sectionController, index); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath]; } @@ -916,7 +916,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N IGParameterAssert([elementKind length] > 0); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; if (![self.registeredSupplementaryViewNibNames containsObject:nibName]) { [self.registeredSupplementaryViewNibNames addObject:nibName]; UINib *nib = [UINib nibWithNibName:nibName bundle:bundle]; @@ -956,7 +956,7 @@ - (void)reloadInSectionController:(IGListSectionController *) [self deleteInSectionController:sectionController atIndexes:indexes]; [self insertInSectionController:sectionController atIndexes:indexes]; } else { - NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousSection:YES]; + NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousIfInUpdateBlock:YES]; [self.updater reloadItemsInCollectionView:collectionView indexPaths:indexPaths]; [self updateBackgroundViewShouldHide:![self itemCountIsZero]]; } @@ -973,7 +973,7 @@ - (void)insertInSectionController:(IGListSectionController *) return; } - NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousSection:NO]; + NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousIfInUpdateBlock:NO]; [self.updater insertItemsIntoCollectionView:collectionView indexPaths:indexPaths]; [self updateBackgroundViewShouldHide:![self itemCountIsZero]]; } @@ -989,7 +989,7 @@ - (void)deleteInSectionController:(IGListSectionController *) return; } - NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousSection:YES]; + NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes usePreviousIfInUpdateBlock:YES]; [self.updater deleteItemsFromCollectionView:collectionView indexPaths:indexPaths]; [self updateBackgroundViewShouldHide:![self itemCountIsZero]]; } @@ -1000,11 +1000,11 @@ - (void)moveInSectionController:(IGListSectionController *)se IGParameterAssert(fromIndex >= 0); IGParameterAssert(toIndex >= 0); UICollectionView *collectionView = self.collectionView; - IGAssert(collectionView != nil, @"Moving items from %@ without a collection view from index to %zi index %zi.", + IGAssert(collectionView != nil, @"Moving items from %@ without a collection view from index %zi to index %zi.", sectionController, fromIndex, toIndex); - NSIndexPath *fromIndexPath = [self indexPathForSectionController:sectionController index:fromIndex usePreviousSection:YES]; - NSIndexPath *toIndexPath = [self indexPathForSectionController:sectionController index:toIndex usePreviousSection:NO]; + NSIndexPath *fromIndexPath = [self indexPathForSectionController:sectionController index:fromIndex usePreviousIfInUpdateBlock:YES]; + NSIndexPath *toIndexPath = [self indexPathForSectionController:sectionController index:toIndex usePreviousIfInUpdateBlock:NO]; if (fromIndexPath == nil || toIndexPath == nil) { return; @@ -1019,7 +1019,7 @@ - (void)reloadSectionController:(IGListSectionController *)s UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading items from %@ without a collection view.", sectionController); - IGListSectionMap *map = [self sectionMapAdjustForUpdateBlock:YES]; + IGListSectionMap *map = [self sectionMapUsingPreviousIfInUpdateBlock:YES]; const NSInteger section = [map sectionForSectionController:sectionController]; if (section == NSNotFound) { return; @@ -1056,7 +1056,7 @@ - (void)scrollToSectionController:(IGListSectionController *) IGAssertMainThread(); IGParameterAssert(sectionController != nil); - NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousSection:NO]; + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated]; } diff --git a/Source/IGListUpdatingDelegate.h b/Source/IGListUpdatingDelegate.h index 647ca7787..30ddc4eeb 100644 --- a/Source/IGListUpdatingDelegate.h +++ b/Source/IGListUpdatingDelegate.h @@ -97,8 +97,8 @@ typedef void (^IGListReloadUpdateBlock)(); Tells the delegate to move an item from and to given index paths. @param collectionView The collection view on which to perform the transition. - @param fromIndexPath The original index path of the item to move. - @param toIndexPath The index path to move the item to. + @param fromIndexPath The source index path of the item to move. + @param toIndexPath The destination index path of the item to move. */ - (void)moveItemInCollectionView:(UICollectionView *)collectionView fromIndexPath:(NSIndexPath *)fromIndexPath diff --git a/Source/Internal/IGListAdapterInternal.h b/Source/Internal/IGListAdapterInternal.h index a111932d6..a8dac24b0 100644 --- a/Source/Internal/IGListAdapterInternal.h +++ b/Source/Internal/IGListAdapterInternal.h @@ -61,10 +61,11 @@ IGListCollectionContext - (NSArray *)indexPathsFromSectionController:(IGListSectionController *)sectionController indexes:(NSIndexSet *)indexes - usePreviousSection:(BOOL)usePreviousSection; + usePreviousIfInUpdateBlock:(BOOL)usePreviousIfInUpdateBlock; + - (nullable NSIndexPath *)indexPathForSectionController:(IGListSectionController *)controller - index:(NSInteger)index - usePreviousSection:(BOOL)usePreviousSection; + index:(NSInteger)index + usePreviousIfInUpdateBlock:(BOOL)usePreviousIfInUpdateBlock; @end diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index f86b0b602..e40b13750 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -134,7 +134,7 @@ - (void)test_whenQueryingIndexPaths_withSectionController_thatPathsAreEqual { IGListSectionController * second = [self.adapter sectionControllerForObject:@1]; NSArray *paths0 = [self.adapter indexPathsFromSectionController:second indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 4)] - usePreviousSection:NO]; + usePreviousIfInUpdateBlock:NO]; NSArray *expected = @[ [NSIndexPath indexPathForItem:2 inSection:1], [NSIndexPath indexPathForItem:3 inSection:1], @@ -153,7 +153,7 @@ - (void)test_whenQueryingIndexPaths_insideBatchUpdateBlock_thatPathsAreEqual { [self.adapter performBatchAnimated:YES updates:^{ NSArray *paths = [self.adapter indexPathsFromSectionController:second indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)] - usePreviousSection:YES]; + usePreviousIfInUpdateBlock:YES]; NSArray *expected = @[ [NSIndexPath indexPathForItem:2 inSection:1], [NSIndexPath indexPathForItem:3 inSection:1], @@ -750,7 +750,7 @@ - (void)test_whenQueryingIndexPath_withOOBSectionController_thatNilReturned { [self.adapter reloadDataWithCompletion:nil]; id randomSectionController = [IGListSectionController new]; - XCTAssertNil([self.adapter indexPathForSectionController:randomSectionController index:0 usePreviousSection:NO]); + XCTAssertNil([self.adapter indexPathForSectionController:randomSectionController index:0 usePreviousIfInUpdateBlock:NO]); } - (void)test_whenQueryingSectionForObject_thatSectionReturned {