Skip to content

Commit

Permalink
rebase, naming nits, formatting, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Nystrom committed Feb 6, 2017
1 parent 03d5f78 commit faf77a6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Source/Common/IGListBatchUpdateData.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ IGLK_SUBCLASSING_RESTRICTED
@property (nonatomic, strong, readonly) NSIndexSet *deleteSections;

/**
section moves.
Section moves.
*/
@property (nonatomic, strong, readonly) NSSet<IGListMoveIndex *> *moveSections;

Expand All @@ -49,7 +49,7 @@ IGLK_SUBCLASSING_RESTRICTED
@property (nonatomic, strong, readonly) NSSet<NSIndexPath *> *deleteIndexPaths;

/**
Item delete index paths.
Item moves.
*/
@property (nonatomic, strong, readonly) NSSet<IGListMoveIndexPath *> *moveIndexPaths;

Expand Down
54 changes: 27 additions & 27 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -356,7 +356,7 @@ - (NSInteger)sectionForSectionController:(IGListSectionController <IGListSection
- (id)objectForSectionController:(IGListSectionController <IGListSectionType> *)sectionController {
IGAssertMainThread();
IGParameterAssert(sectionController != nil);

const NSInteger section = [self.sectionMap sectionForSectionController:sectionController];
return [self.sectionMap objectForSection:section];
}
Expand Down Expand Up @@ -421,19 +421,19 @@ - (NSArray *)visibleObjects {
- (NSArray<UICollectionViewCell *> *)visibleCellsForObject:(id)object {
IGAssertMainThread();
IGParameterAssert(object != nil);

const NSInteger section = [self.sectionMap sectionForObject:object];
if (section == NSNotFound) {
return [NSArray new];
}

NSArray<UICollectionViewCell *> *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];
}

Expand Down Expand Up @@ -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;
Expand All @@ -568,10 +568,10 @@ - (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)usePreviousSection {

- (NSArray<NSIndexPath *> *)indexPathsFromSectionController:(IGListSectionController <IGListSectionType> *)sectionController
indexes:(NSIndexSet *)indexes
usePreviousSection:(BOOL)usePreviousSection {
usePreviousIfInUpdateBlock:(BOOL)usePreviousIfInUpdateBlock {
NSMutableArray<NSIndexPath *> *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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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];
}

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

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

Expand All @@ -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];
Expand Down Expand Up @@ -956,7 +956,7 @@ - (void)reloadInSectionController:(IGListSectionController<IGListSectionType> *)
[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]];
}
Expand All @@ -973,7 +973,7 @@ - (void)insertInSectionController:(IGListSectionController<IGListSectionType> *)
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]];
}
Expand All @@ -989,7 +989,7 @@ - (void)deleteInSectionController:(IGListSectionController<IGListSectionType> *)
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]];
}
Expand All @@ -1000,11 +1000,11 @@ - (void)moveInSectionController:(IGListSectionController<IGListSectionType> *)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;
Expand All @@ -1019,7 +1019,7 @@ - (void)reloadSectionController:(IGListSectionController <IGListSectionType> *)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;
Expand Down Expand Up @@ -1056,7 +1056,7 @@ - (void)scrollToSectionController:(IGListSectionController<IGListSectionType> *)
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];
}

Expand Down
4 changes: 2 additions & 2 deletions Source/IGListUpdatingDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions Source/Internal/IGListAdapterInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ IGListCollectionContext

- (NSArray *)indexPathsFromSectionController:(IGListSectionController <IGListSectionType> *)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

Expand Down
6 changes: 3 additions & 3 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ - (void)test_whenQueryingIndexPaths_withSectionController_thatPathsAreEqual {
IGListSectionController <IGListSectionType> * 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],
Expand All @@ -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],
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit faf77a6

Please sign in to comment.