From 67489492ee0380ab28dcefda24420bc350ed071d Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Wed, 19 Oct 2016 11:49:49 -0700 Subject: [PATCH] Fix Uncalled Collection Delegate Methods (#2433) * Fix batch fetching * Fix not calling constrainedSizeForItemAtIndexPath: * Fix constrainedSizeForItemAtIndexPath: not being called --- AsyncDisplayKit/ASCollectionView.mm | 6 +++++- .../Details/ASCollectionViewFlowLayoutInspector.m | 7 +++++-- .../ASCollectionViewFlowLayoutInspectorTests.m | 11 ++++++----- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index ded30340ea..27079d1441 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -1224,7 +1224,11 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; - (void)_beginBatchFetching { [_batchContext beginBatchFetching]; - if (_asyncDelegateFlags.collectionViewWillBeginBatchFetch) { + if (_asyncDelegateFlags.collectionNodeWillBeginBatchFetch) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [_asyncDelegate collectionNode:self.collectionNode willBeginBatchFetchWithContext:_batchContext]; + }); + } else if (_asyncDelegateFlags.collectionViewWillBeginBatchFetch) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m index ef9b35a7ca..d82aa45a58 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m @@ -34,6 +34,7 @@ static inline ASSizeRange NodeConstrainedSizeForScrollDirection(ASCollectionView @implementation ASCollectionViewLayoutInspector { struct { + unsigned int implementsConstrainedSizeForNodeAtIndexPathDeprecated:1; unsigned int implementsConstrainedSizeForNodeAtIndexPath:1; } _delegateFlags; } @@ -56,14 +57,16 @@ static inline ASSizeRange NodeConstrainedSizeForScrollDirection(ASCollectionView if (delegate == nil) { memset(&_delegateFlags, 0, sizeof(_delegateFlags)); } else { - _delegateFlags.implementsConstrainedSizeForNodeAtIndexPath = [delegate respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)]; + _delegateFlags.implementsConstrainedSizeForNodeAtIndexPathDeprecated = [delegate respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)]; + _delegateFlags.implementsConstrainedSizeForNodeAtIndexPath = [delegate respondsToSelector:@selector(collectionNode:constrainedSizeForItemAtIndexPath:)]; } } - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath { if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPath) { - // TODO: Handle collection node + return [collectionView.asyncDelegate collectionNode:collectionView.collectionNode constrainedSizeForItemAtIndexPath:indexPath]; + } else if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPathDeprecated) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [collectionView.asyncDelegate collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]; diff --git a/AsyncDisplayKitTests/ASCollectionViewFlowLayoutInspectorTests.m b/AsyncDisplayKitTests/ASCollectionViewFlowLayoutInspectorTests.m index f4c7e229a1..22b2768d6d 100644 --- a/AsyncDisplayKitTests/ASCollectionViewFlowLayoutInspectorTests.m +++ b/AsyncDisplayKitTests/ASCollectionViewFlowLayoutInspectorTests.m @@ -377,24 +377,25 @@ - (void)testThatItThrowsIfNodeConstrainedSizeIsImplementedOnDataSourceButNotOnDelegateLayoutInspector { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; - ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; + ASCollectionNode *node = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout]; + ASCollectionView *collectionView = node.view; id dataSourceAndDelegate = [OCMockObject mockForProtocol:@protocol(InspectorTestDataSourceDelegateProtocol)]; ASSizeRange constrainedSize = ASSizeRangeMake(CGSizeZero, CGSizeZero); NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; NSValue *value = [NSValue value:&constrainedSize withObjCType:@encode(ASSizeRange)]; - [[[dataSourceAndDelegate stub] andReturnValue:value] collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]; - collectionView.asyncDataSource = dataSourceAndDelegate; + [[[dataSourceAndDelegate stub] andReturnValue:value] collectionNode:node constrainedSizeForItemAtIndexPath:indexPath]; + node.dataSource = dataSourceAndDelegate; id delegate = [InspectorTestDataSourceDelegateWithoutNodeConstrainedSize new]; - collectionView.asyncDelegate = delegate; + node.delegate = delegate; ASCollectionViewLayoutInspector *inspector = [[ASCollectionViewLayoutInspector alloc] initWithCollectionView:collectionView]; collectionView.layoutInspector = inspector; XCTAssertThrows([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]); - collectionView.asyncDelegate = dataSourceAndDelegate; + node.delegate = dataSourceAndDelegate; XCTAssertNoThrow([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]); }