From a2e00053578dda9784dcbc46bf79b13da0bdf5dc Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Tue, 11 Oct 2016 17:51:33 -0700 Subject: [PATCH] Replace reusable identifier methods with a C function (#40) Summary: Replaced the two methods for generating a reusable identifier with an inline C function (as per #40) which has 3 parameters: `viewClass` (unchanged), `nibName` (for when #1 is added), and `kind`. The string is generated following the same pattern as before. A few things: - The current test only tests one of the options. May want to add more tests for each. - Not sure if you guys prefer `NS_INLINE` vs `static inline`. - Not sure if we want to add assertions for empty strings for nibName/kind parameters. - [x] All tests pass. Demo project builds and runs. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/49 Differential Revision: D4005549 Pulled By: jessesquires fbshipit-source-id: e1683ebe8882d1a8de934c8166f67d3508cffba9 --- Source/IGListAdapter.m | 13 ++----------- Source/Internal/IGListAdapterInternal.h | 7 +++++-- Tests/IGListAdapterTests.m | 7 ++++++- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 36b21956..cb9524b2 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -443,15 +443,6 @@ _collectionView.backgroundView.hidden = itemCount > 0; } -// use the string representation of a reusable view class when registering with a UICollectionView -- (NSString *)reusableViewIdentifierForClass:(Class)viewClass { - return NSStringFromClass(viewClass); -} - -- (NSString *)reusableViewIdentifierForClass:(Class)viewClass elementKind:(NSString *)elementKind { - return [elementKind stringByAppendingString:NSStringFromClass(viewClass)]; -} - - (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)adjustForUpdateBlock { // if we are inside an update block, we may have to use the /previous/ item map for some operations if (adjustForUpdateBlock && self.isInUpdateBlock && self.previoussectionMap != nil) { @@ -720,7 +711,7 @@ IGParameterAssert(cellClass != nil); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); - NSString *identifier = [self reusableViewIdentifierForClass:cellClass]; + NSString *identifier = IGListReusableViewIdentifier(cellClass, nil); NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index]; if (![self.registeredCellClasses containsObject:cellClass]) { [self.registeredCellClasses addObject:cellClass]; @@ -736,7 +727,7 @@ IGAssertMainThread(); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); - NSString *identifier = [self reusableViewIdentifierForClass:viewClass elementKind:elementKind]; + NSString *identifier = IGListReusableViewIdentifier(viewClass, elementKind); NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index]; if (![self.registeredSupplementaryViewIdentifiers containsObject:identifier]) { [self.registeredSupplementaryViewIdentifiers addObject:identifier]; diff --git a/Source/Internal/IGListAdapterInternal.h b/Source/Internal/IGListAdapterInternal.h index 3dd217ab..9f1e5701 100644 --- a/Source/Internal/IGListAdapterInternal.h +++ b/Source/Internal/IGListAdapterInternal.h @@ -17,6 +17,11 @@ NS_ASSUME_NONNULL_BEGIN +/// Generate a string representation of a reusable view class when registering with a UICollectionView. +NS_INLINE NSString *IGListReusableViewIdentifier(Class viewClass, NSString * _Nullable kind) { + return [NSString stringWithFormat:@"%@%@", kind ?: @"", NSStringFromClass(viewClass)]; +} + @interface IGListAdapter () < UICollectionViewDataSource, @@ -55,8 +60,6 @@ IGListCollectionContext indexes:(NSIndexSet *)indexes adjustForUpdateBlock:(BOOL)adjustForUpdateBlock; -- (NSString *)reusableViewIdentifierForClass:(Class)viewClass; - @end NS_ASSUME_NONNULL_END diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index 08d15e34..de7c307d 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -143,10 +143,15 @@ } - (void)test_whenQueryingReusableIdentifier_thatIdentifierEqualsClassName { - NSString *identifier = [self.adapter reusableViewIdentifierForClass:UICollectionViewCell.class]; + NSString *identifier = IGListReusableViewIdentifier(UICollectionViewCell.class, nil); XCTAssertEqualObjects(identifier, @"UICollectionViewCell"); } +- (void)test_whenQueryingReusableIdentifier_thatIdentifierEqualsClassNameAndSupplimentaryKind { + NSString *identifier = IGListReusableViewIdentifier(UICollectionViewCell.class, UICollectionElementKindSectionFooter); + XCTAssertEqualObjects(identifier, @"UICollectionElementKindSectionFooterUICollectionViewCell"); +} + - (void)test_whenDataSourceChanges_thatBackgroundViewVisibilityChanges { self.dataSource.objects = @[@1]; UIView *background = [[UIView alloc] init];