Skip to content

Commit

Permalink
ASCollectionLayout to exclude content inset on scrollable directions …
Browse files Browse the repository at this point in the history
…from viewport size
  • Loading branch information
nguyenhuy committed Sep 11, 2017
1 parent fcee108 commit 25f5076
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- Negate iOS 11 automatic estimated table row heights. [Christian Selig](https://github.com/christianselig) [#485](https://github.com/TextureGroup/Texture/pull/485)
- Add content inset and offset bridging properties to ASTableNode and ASCollectionNode. Deprecate related properties and methods in ASTableView and ASCollectionView [Huy Nguyen](https://github.com/nguyenhuy) [#460](https://github.com/TextureGroup/Texture/pull/460) [#560](https://github.com/TextureGroup/Texture/pull/560)
- Remove re-entrant access to self.view when applying initial pending state. [Adlai Holler](https://github.com/Adlai-Holler) [#510](https://github.com/TextureGroup/Texture/pull/510)
- Small improvements in ASCollectionLayout [Huy Nguyen](https://github.com/nguyenhuy) [#509](https://github.com/TextureGroup/Texture/pull/509) [#513](https://github.com/TextureGroup/Texture/pull/513)
- Small improvements in ASCollectionLayout [Huy Nguyen](https://github.com/nguyenhuy) [#509](https://github.com/TextureGroup/Texture/pull/509) [#513](https://github.com/TextureGroup/Texture/pull/513) [#562]((https://github.com/TextureGroup/Texture/pull/562)
- Fix retain cycle between ASImageNode and PINAnimatedImage [Phil Larson](https://github.com/plarson) [#520](https://github.com/TextureGroup/Texture/pull/520)
- Change the API for disabling logging from a compiler flag to a runtime C function ASDisableLogging(). [Adlai Holler](https://github.com/Adlai-Holler) [#528](https://github.com/TextureGroup/Texture/pull/528)
- Table and collection views to consider content inset when calculating (default) element size range [Huy Nguyen](https://github.com/nguyenhuy) [#525](https://github.com/TextureGroup/Texture/pull/525)
Expand Down
59 changes: 48 additions & 11 deletions Source/Private/ASCollectionLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,35 @@ - (instancetype)initWithLayoutDelegate:(id<ASCollectionLayoutDelegate>)layoutDel
- (ASCollectionLayoutContext *)layoutContextWithElements:(ASElementMap *)elements
{
ASDisplayNodeAssertMainThread();
CGSize viewportSize = [self _viewportSize];
CGPoint contentOffset = _collectionNode.contentOffset;

Class<ASCollectionLayoutDelegate> layoutDelegateClass = [_layoutDelegate class];
ASCollectionLayoutCache *layoutCache = _layoutCache;
ASCollectionNode *collectionNode = _collectionNode;
if (collectionNode == nil) {
return [[ASCollectionLayoutContext alloc] initWithViewportSize:CGSizeZero
initialContentOffset:CGPointZero
scrollableDirections:ASScrollDirectionNone
elements:[[ASElementMap alloc] init]
layoutDelegateClass:layoutDelegateClass
layoutCache:layoutCache
additionalInfo:nil];
}

ASScrollDirection scrollableDirections = [_layoutDelegate scrollableDirections];
CGSize viewportSize = [ASCollectionLayout _viewportSizeForCollectionNode:collectionNode scrollableDirections:scrollableDirections];
CGPoint contentOffset = collectionNode.contentOffset;

id additionalInfo = nil;
if (_layoutDelegateFlags.implementsAdditionalInfoForLayoutWithElements) {
additionalInfo = [_layoutDelegate additionalInfoForLayoutWithElements:elements];
}

return [[ASCollectionLayoutContext alloc] initWithViewportSize:viewportSize
initialContentOffset:contentOffset
scrollableDirections:[_layoutDelegate scrollableDirections]
scrollableDirections:scrollableDirections
elements:elements
layoutDelegateClass:[_layoutDelegate class]
layoutCache:_layoutCache
layoutDelegateClass:layoutDelegateClass
layoutCache:layoutCache
additionalInfo:additionalInfo];
}

Expand Down Expand Up @@ -208,21 +225,41 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return (! CGSizeEqualToSize([self _viewportSize], newBounds.size));
return (! CGSizeEqualToSize([ASCollectionLayout _boundsForCollectionNode:_collectionNode], newBounds.size));
}

#pragma mark - Private methods

- (CGSize)_viewportSize
+ (CGSize)_boundsForCollectionNode:(nonnull ASCollectionNode *)collectionNode
{
ASCollectionNode *collectionNode = _collectionNode;
if (collectionNode != nil && !collectionNode.isNodeLoaded) {
if (collectionNode == nil) {
return CGSizeZero;
}

if (!collectionNode.isNodeLoaded) {
// TODO consider calculatedSize as well
return collectionNode.threadSafeBounds.size;
}

ASDisplayNodeAssertMainThread();
return collectionNode.view.bounds.size;
}

+ (CGSize)_viewportSizeForCollectionNode:(nonnull ASCollectionNode *)collectionNode scrollableDirections:(ASScrollDirection)scrollableDirections
{
if (collectionNode == nil) {
return CGSizeZero;
}

CGSize result = [ASCollectionLayout _boundsForCollectionNode:collectionNode];
// TODO: Consider using adjustedContentInset on iOS 11 and later, to include the safe area of the scroll view
UIEdgeInsets contentInset = collectionNode.contentInset;
if (ASScrollDirectionContainsHorizontalDirection(scrollableDirections)) {
result.height -= (contentInset.top + contentInset.bottom);
} else {
ASDisplayNodeAssertMainThread();
return self.collectionView.bounds.size;
result.width -= (contentInset.left + contentInset.right);
}
return result;
}

/**
Expand Down

0 comments on commit 25f5076

Please sign in to comment.