From 63efdbde8f958fcf5cf09a8df19e96cb90843a54 Mon Sep 17 00:00:00 2001 From: appleguy Date: Tue, 31 Oct 2017 06:20:58 -0700 Subject: [PATCH] [ASCollectionView] Call -invalidateFlowLayoutDelegateMetrics when rotating. #trivial (#616) * [ASCollectionView] Ensure -invalidateFlowLayoutDelegateMetrics is called for UIKit passthrough cells. This allows rotation to work properly when rotating UIKit passthrough cells that need to change width. * [ASCollectionView] No need to verify node is still in model to handle view-only notifications. --- Source/ASCollectionView.mm | 51 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/Source/ASCollectionView.mm b/Source/ASCollectionView.mm index da31222ec2..339728d83a 100644 --- a/Source/ASCollectionView.mm +++ b/Source/ASCollectionView.mm @@ -369,6 +369,7 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; { [_dataController relayoutAllNodesWithInvalidationBlock:^{ [self.collectionViewLayout invalidateLayout]; + [self invalidateFlowLayoutDelegateMetrics]; }]; } @@ -1165,9 +1166,8 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; { if (_asyncDelegateFlags.interopWillDisplayCell) { ASCellNode *node = [self nodeForItemAtIndexPath:indexPath]; - NSIndexPath *modelIndexPath = [self indexPathForNode:node]; - if (modelIndexPath && node.shouldUseUIKitCell) { - [(id )_asyncDelegate collectionView:collectionView willDisplayCell:rawCell forItemAtIndexPath:modelIndexPath]; + if (node.shouldUseUIKitCell) { + [(id )_asyncDelegate collectionView:collectionView willDisplayCell:rawCell forItemAtIndexPath:indexPath]; } } @@ -1226,9 +1226,8 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; { if (_asyncDelegateFlags.interopDidEndDisplayingCell) { ASCellNode *node = [self nodeForItemAtIndexPath:indexPath]; - NSIndexPath *modelIndexPath = [self indexPathForNode:node]; - if (modelIndexPath && node.shouldUseUIKitCell) { - [(id )_asyncDelegate collectionView:collectionView didEndDisplayingCell:rawCell forItemAtIndexPath:modelIndexPath]; + if (node.shouldUseUIKitCell) { + [(id )_asyncDelegate collectionView:collectionView didEndDisplayingCell:rawCell forItemAtIndexPath:indexPath]; } } @@ -1271,10 +1270,9 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)rawView forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { if (_asyncDelegateFlags.interopWillDisplaySupplementaryView) { - ASCellNode *node = [self nodeForItemAtIndexPath:indexPath]; - NSIndexPath *modelIndexPath = [self indexPathForNode:node]; - if (modelIndexPath && node.shouldUseUIKitCell) { - [(id )_asyncDelegate collectionView:collectionView willDisplaySupplementaryView:rawView forElementKind:elementKind atIndexPath:modelIndexPath]; + ASCellNode *node = [self supplementaryNodeForElementKind:elementKind atIndexPath:indexPath]; + if (node.shouldUseUIKitCell) { + [(id )_asyncDelegate collectionView:collectionView willDisplaySupplementaryView:rawView forElementKind:elementKind atIndexPath:indexPath]; } } @@ -1312,10 +1310,9 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)rawView forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { if (_asyncDelegateFlags.interopdidEndDisplayingSupplementaryView) { - ASCellNode *node = [self nodeForItemAtIndexPath:indexPath]; - NSIndexPath *modelIndexPath = [self indexPathForNode:node]; - if (modelIndexPath && node.shouldUseUIKitCell) { - [(id )_asyncDelegate collectionView:collectionView didEndDisplayingSupplementaryView:rawView forElementOfKind:elementKind atIndexPath:modelIndexPath]; + ASCellNode *node = [self supplementaryNodeForElementKind:elementKind atIndexPath:indexPath]; + if (node.shouldUseUIKitCell) { + [(id )_asyncDelegate collectionView:collectionView didEndDisplayingSupplementaryView:rawView forElementOfKind:elementKind atIndexPath:indexPath]; } } @@ -2253,18 +2250,17 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; */ - (void)layer:(CALayer *)layer didChangeBoundsWithOldValue:(CGRect)oldBounds newValue:(CGRect)newBounds { - if (_hasDataControllerLayoutDelegate) { - // Let the layout delegate handle bounds changes if it's available. - return; - } - if (self.collectionViewLayout == nil) { - return; - } + CGSize newSize = newBounds.size; CGSize lastUsedSize = _lastBoundsSizeUsedForMeasuringNodes; - if (CGSizeEqualToSize(lastUsedSize, newBounds.size)) { + if (CGSizeEqualToSize(lastUsedSize, newSize)) { return; } - _lastBoundsSizeUsedForMeasuringNodes = newBounds.size; + if (_hasDataControllerLayoutDelegate || self.collectionViewLayout == nil) { + // Let the layout delegate handle bounds changes if it's available. If no layout, it will init in the new state. + return; + } + + _lastBoundsSizeUsedForMeasuringNodes = newSize; // Laying out all nodes is expensive. // We only need to do this if the bounds changed in the non-scrollable direction. @@ -2272,15 +2268,14 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier"; // appearance update, we do not need to relayout all nodes. // For a more permanent fix to the unsafety mentioned above, see https://github.com/facebook/AsyncDisplayKit/pull/2182 ASScrollDirection scrollDirection = self.scrollableDirections; - BOOL fixedVertically = (ASScrollDirectionContainsVerticalDirection(scrollDirection) == NO); + BOOL fixedVertically = (ASScrollDirectionContainsVerticalDirection (scrollDirection) == NO); BOOL fixedHorizontally = (ASScrollDirectionContainsHorizontalDirection(scrollDirection) == NO); - BOOL changedInNonScrollingDirection = (fixedHorizontally && newBounds.size.width != lastUsedSize.width) || (fixedVertically && newBounds.size.height != lastUsedSize.height); + BOOL changedInNonScrollingDirection = (fixedHorizontally && newSize.width != lastUsedSize.width) || + (fixedVertically && newSize.height != lastUsedSize.height); if (changedInNonScrollingDirection) { - [_dataController relayoutAllNodesWithInvalidationBlock:^{ - [self.collectionViewLayout invalidateLayout]; - }]; + [self relayoutItems]; } }