From 2fd487b44789e7e83f70c8d5a1e828664bfc8a3c Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Fri, 24 Feb 2017 18:47:01 -0800 Subject: [PATCH] Add collectionNode:nodeBlockForSupplementaryElementOfKind:atIndexPath: (#3078) * Add optional support for nodeBlockForSupplementaryElementOfKind: * Update example * Update harder --- AsyncDisplayKit/ASCollectionNode.h | 9 +++++ AsyncDisplayKit/ASCollectionView.mm | 34 +++++++++++++------ AsyncDisplayKit/ASSupplementaryNodeSource.h | 6 ++-- .../Private/ASIGListAdapterBasedDataSource.m | 4 +-- .../Sample/PhotoFeedSectionController.m | 6 ++-- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionNode.h b/AsyncDisplayKit/ASCollectionNode.h index 3f095656..53939448 100644 --- a/AsyncDisplayKit/ASCollectionNode.h +++ b/AsyncDisplayKit/ASCollectionNode.h @@ -480,6 +480,15 @@ NS_ASSUME_NONNULL_BEGIN */ - (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForItemAtIndexPath:(NSIndexPath *)indexPath; +/** + * Asks the data source to provide a node-block to display for the given supplementary element in the collection view. + * + * @param collectionNode The sender. + * @param kind The kind of supplementary element. + * @param indexPath The index path of the supplementary element. + */ +- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; + /** * Asks the data source to provide a node to display for the given supplementary element in the collection view. * diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index 387fca46..916475d8 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -190,6 +190,7 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; unsigned int collectionNodeNodeForItem:1; unsigned int collectionNodeNodeBlockForItem:1; unsigned int collectionNodeNodeForSupplementaryElement:1; + unsigned int collectionNodeNodeBlockForSupplementaryElement:1; unsigned int collectionNodeSupplementaryElementKindsInSection:1; unsigned int numberOfSectionsInCollectionNode:1; unsigned int collectionNodeNumberOfItemsInSection:1; @@ -422,6 +423,7 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; _asyncDataSourceFlags.collectionNodeNumberOfItemsInSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:numberOfItemsInSection:)]; _asyncDataSourceFlags.collectionNodeContextForSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:contextForSection:)]; _asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement = [_asyncDataSource respondsToSelector:@selector(collectionNode:nodeForSupplementaryElementOfKind:atIndexPath:)]; + _asyncDataSourceFlags.collectionNodeNodeBlockForSupplementaryElement = [_asyncDataSource respondsToSelector:@selector(collectionNode:nodeBlockForSupplementaryElementOfKind:atIndexPath:)]; _asyncDataSourceFlags.collectionNodeSupplementaryElementKindsInSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:supplementaryElementKindsInSection:)]; _asyncDataSourceFlags.interop = [_asyncDataSource conformsToProtocol:@protocol(ASCollectionDataSourceInterop)]; @@ -1555,25 +1557,35 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; - (ASCellNodeBlock)dataController:(ASDataController *)dataController supplementaryNodeBlockOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { - //TODO _asyncDataSource to support supplementary node block and deprecate nodeForSupplementaryElementOfKind - ASCellNode *node = nil; - if (_asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement) { + ASCellNodeBlock nodeBlock = nil; + if (_asyncDataSourceFlags.collectionNodeNodeBlockForSupplementaryElement) { GET_COLLECTIONNODE_OR_RETURN(collectionNode, ^{ return [[ASCellNode alloc] init]; }); - node = [_asyncDataSource collectionNode:collectionNode nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath]; + nodeBlock = [_asyncDataSource collectionNode:collectionNode nodeBlockForSupplementaryElementOfKind:kind atIndexPath:indexPath]; + ASDisplayNodeAssert(nodeBlock != nil, @"A node block must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath); + } else if (_asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement) { + GET_COLLECTIONNODE_OR_RETURN(collectionNode, ^{ return [[ASCellNode alloc] init]; }); + ASCellNode *node = [_asyncDataSource collectionNode:collectionNode nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath]; + ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath); + nodeBlock = ^{ return node; }; } else if (_asyncDataSourceFlags.collectionViewNodeForSupplementaryElement) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - node = [_asyncDataSource collectionView:self nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath]; + ASCellNode *node = [_asyncDataSource collectionView:self nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath]; + ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath); + nodeBlock = ^{ return node; }; #pragma clang diagnostic pop } - if (node == nil && _asyncDataSourceFlags.interop) { - node = [[ASCellNode alloc] init]; - node.shouldUseUIKitCell = YES; + if (nodeBlock == nil) { + BOOL useUIKitCell = _asyncDataSourceFlags.interop; + nodeBlock = ^{ + ASCellNode *node = [[ASCellNode alloc] init]; + node.shouldUseUIKitCell = useUIKitCell; + return node; + }; } - - ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath); - return ^{ return node; }; + + return nodeBlock; } - (NSArray *)dataController:(ASDataController *)dataController supplementaryNodeKindsInSections:(NSIndexSet *)sections diff --git a/AsyncDisplayKit/ASSupplementaryNodeSource.h b/AsyncDisplayKit/ASSupplementaryNodeSource.h index 7ded50e1..9d3233c9 100644 --- a/AsyncDisplayKit/ASSupplementaryNodeSource.h +++ b/AsyncDisplayKit/ASSupplementaryNodeSource.h @@ -16,14 +16,14 @@ NS_ASSUME_NONNULL_BEGIN @protocol ASSupplementaryNodeSource /** - * A method to provide the node for the item at the given index. + * A method to provide the node-block for the supplementary element. * * @param elementKind The kind of supplementary element. * @param index The index of the item. - * @return A node for the supplementary element. + * @return A node block for the supplementary element. * @see collectionNode:nodeForSupplementaryElementOfKind:atIndexPath: */ -- (ASCellNode *)nodeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index; +- (ASCellNodeBlock)nodeBlockForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index; @optional diff --git a/AsyncDisplayKit/Private/ASIGListAdapterBasedDataSource.m b/AsyncDisplayKit/Private/ASIGListAdapterBasedDataSource.m index fded3459..9b0717a4 100644 --- a/AsyncDisplayKit/Private/ASIGListAdapterBasedDataSource.m +++ b/AsyncDisplayKit/Private/ASIGListAdapterBasedDataSource.m @@ -199,9 +199,9 @@ typedef struct { } } -- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath +- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { - return [[self supplementaryElementSourceForSection:indexPath.section] nodeForSupplementaryElementOfKind:kind atIndex:indexPath.item]; + return [[self supplementaryElementSourceForSection:indexPath.section] nodeBlockForSupplementaryElementOfKind:kind atIndex:indexPath.item]; } - (NSArray *)collectionNode:(ASCollectionNode *)collectionNode supplementaryElementKindsInSection:(NSInteger)section diff --git a/examples/ASDKgram/Sample/PhotoFeedSectionController.m b/examples/ASDKgram/Sample/PhotoFeedSectionController.m index 963eba95..07be3dcd 100644 --- a/examples/ASDKgram/Sample/PhotoFeedSectionController.m +++ b/examples/ASDKgram/Sample/PhotoFeedSectionController.m @@ -96,10 +96,12 @@ ASIGSectionControllerCellForIndexImplementation; #pragma mark - ASSupplementaryNodeSource -- (ASCellNode *)nodeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index +- (ASCellNodeBlock)nodeBlockForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index { ASDisplayNodeAssert([elementKind isEqualToString:UICollectionElementKindSectionHeader], nil); - return [[FeedHeaderNode alloc] init]; + return ^{ + return [[FeedHeaderNode alloc] init]; + }; } - (ASSizeRange)sizeRangeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index