mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-09-05 04:10:16 +00:00
* Implement ASCollectionGalleryLayoutDelegate - It arranges items of the same size into a multi-line stack (say photo gallery or pager). It takes advantage of the fact that its items always have a fixed size to measure as few items as possible while still being able to track their positions at all time. This helps reduce startup/reloadData time, as well as memory footprint. - It then uses a measure range, which also works as a allocate range, to figure out which items to measure ahead of time. And it guarantees that each item is scheduled to measure only once. - Lastly, ASCollectionLayoutDelegate has some new methods that allow delegates to hook up and stay ahead of layout attributes requests from the backing view. ASCollectionGalleryLayoutDelegate for example uses these methods to ensure elements that have their layout attributes requested are always ready for consumption, and to measure more elements in the background. * Handle items that span multiple pages and other improvements in gallery delegate * Minor fixes * Fix failing tests * Fix custom collection example * Implement missing method in gallery layout delegate * Fix warnings * Some improvements - Collection layout delegates must have a crollable directions property. - Simplify gallery delegate by not storing unmeasured attributes since calling measure on already measured elements should be cache hits and super fast. - Abstact some code in gallery delegate to ASCollectionLayoutState+Private and _ASCollectionGalleryLayoutItem. - Other improvements in gallery delegate * Fix file licenses * Move measure range logic to ASCollectionLayout * Track unmeasured elements * Remove pending layout in ASCollectionLayout * Get back pending layout because the timing to latch new data is not ideal * Add ASCollectionLayoutCache * Fix file licenses * Fix xcodeproj * Add async collection layout to examples/ASCollectionView * Measure method in ASCollectionLayout to be a class method * Encourage more immutable states - Make -calculateLayoutWithContext: to be class methods in ASDataControllerLayoutDelegate and ASCollectionLayoutDelegate. - Add layout delegate class and layout cache to ASCollectionLayoutContext+Private, to be use by ASCollectionLayout only. - ASDataController no longer allocates all nodes but lets ASCollectionLayout determine. - Add scrollableDirections to the layout context since it's often needed by the layout pass. Otherwise users have to wrap it in an info object. - Update built-in layout delegates and CustomCollectionView example. - Publish ASHashing. It might be helpful for clients that implement custom collection info objects. * Remove additionalInfo property in ASCollectionLayoutState * ASCollectionLayoutState to correctly filter unmeasured elements * Add ASHashing to umbrella header * Fix file licenses * Add ASDispatchAsync and use it in ASCollectionLayout * Improve code comment in ASCollectionLayoutState
341 lines
14 KiB
Plaintext
341 lines
14 KiB
Plaintext
//
|
|
// ASCollectionLayout.mm
|
|
// Texture
|
|
//
|
|
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under the BSD-style license found in the
|
|
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional
|
|
// grant of patent rights can be found in the PATENTS file in the same directory.
|
|
//
|
|
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
|
|
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
|
|
#import <AsyncDisplayKit/ASCollectionLayout.h>
|
|
|
|
#import <AsyncDisplayKit/ASAssert.h>
|
|
#import <AsyncDisplayKit/ASAbstractLayoutController.h>
|
|
#import <AsyncDisplayKit/ASCellNode.h>
|
|
#import <AsyncDisplayKit/ASCollectionElement.h>
|
|
#import <AsyncDisplayKit/ASCollectionLayoutCache.h>
|
|
#import <AsyncDisplayKit/ASCollectionLayoutContext+Private.h>
|
|
#import <AsyncDisplayKit/ASCollectionLayoutDelegate.h>
|
|
#import <AsyncDisplayKit/ASCollectionLayoutState+Private.h>
|
|
#import <AsyncDisplayKit/ASCollectionNode+Beta.h>
|
|
#import <AsyncDisplayKit/ASDispatch.h>
|
|
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
|
|
#import <AsyncDisplayKit/ASElementMap.h>
|
|
#import <AsyncDisplayKit/ASEqualityHelpers.h>
|
|
#import <AsyncDisplayKit/ASPageTable.h>
|
|
|
|
static const ASRangeTuningParameters kASDefaultMeasureRangeTuningParameters = {
|
|
.leadingBufferScreenfuls = 2.0,
|
|
.trailingBufferScreenfuls = 2.0
|
|
};
|
|
|
|
static const ASScrollDirection kASStaticScrollDirection = (ASScrollDirectionRight | ASScrollDirectionDown);
|
|
|
|
@interface ASCollectionLayout () <ASDataControllerLayoutDelegate> {
|
|
ASCollectionLayoutCache *_layoutCache;
|
|
ASCollectionLayoutState *_layout; // Main thread only.
|
|
|
|
struct {
|
|
unsigned int implementsAdditionalInfoForLayoutWithElements:1;
|
|
} _layoutDelegateFlags;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation ASCollectionLayout
|
|
|
|
- (instancetype)initWithLayoutDelegate:(id<ASCollectionLayoutDelegate>)layoutDelegate
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
ASDisplayNodeAssertNotNil(layoutDelegate, @"Collection layout delegate cannot be nil");
|
|
_layoutDelegate = layoutDelegate;
|
|
_layoutDelegateFlags.implementsAdditionalInfoForLayoutWithElements = [layoutDelegate respondsToSelector:@selector(additionalInfoForLayoutWithElements:)];
|
|
_layoutCache = [[ASCollectionLayoutCache alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - ASDataControllerLayoutDelegate
|
|
|
|
- (ASCollectionLayoutContext *)layoutContextWithElements:(ASElementMap *)elements
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
CGSize viewportSize = [self _viewportSize];
|
|
id additionalInfo = nil;
|
|
if (_layoutDelegateFlags.implementsAdditionalInfoForLayoutWithElements) {
|
|
additionalInfo = [_layoutDelegate additionalInfoForLayoutWithElements:elements];
|
|
}
|
|
return [[ASCollectionLayoutContext alloc] initWithViewportSize:viewportSize
|
|
scrollableDirections:[_layoutDelegate scrollableDirections]
|
|
elements:elements
|
|
layoutDelegateClass:[_layoutDelegate class]
|
|
layoutCache:_layoutCache
|
|
additionalInfo:additionalInfo];
|
|
}
|
|
|
|
+ (ASCollectionLayoutState *)calculateLayoutWithContext:(ASCollectionLayoutContext *)context
|
|
{
|
|
if (context.elements == nil) {
|
|
return [[ASCollectionLayoutState alloc] initWithContext:context
|
|
contentSize:CGSizeZero
|
|
elementToLayoutAttributesTable:[NSMapTable elementToLayoutAttributesTable]];
|
|
}
|
|
|
|
ASDisplayNodeAssertTrue([context.layoutDelegateClass conformsToProtocol:@protocol(ASCollectionLayoutDelegate)]);
|
|
ASCollectionLayoutState *layout = [context.layoutDelegateClass calculateLayoutWithContext:context];
|
|
[context.layoutCache setLayout:layout forContext:context];
|
|
|
|
// Measure elements in the measure range ahead of time, block on the initial rect as it'll be visible shortly
|
|
CGSize viewportSize = context.viewportSize;
|
|
// TODO Consider content offset of the collection node
|
|
CGRect initialRect = CGRectMake(0, 0, viewportSize.width, viewportSize.height);
|
|
CGRect measureRect = CGRectExpandToRangeWithScrollableDirections(initialRect,
|
|
kASDefaultMeasureRangeTuningParameters,
|
|
context.scrollableDirections,
|
|
kASStaticScrollDirection);
|
|
[self _measureElementsInRect:measureRect blockingRect:initialRect layout:layout];
|
|
|
|
return layout;
|
|
}
|
|
|
|
#pragma mark - UICollectionViewLayout overrides
|
|
|
|
- (void)prepareLayout
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
[super prepareLayout];
|
|
|
|
ASCollectionLayoutContext *context = [self layoutContextWithElements:_collectionNode.visibleElements];
|
|
if (_layout != nil && ASObjectIsEqual(_layout.context, context)) {
|
|
// The existing layout is still valid. No-op
|
|
return;
|
|
}
|
|
|
|
if (ASCollectionLayoutState *cachedLayout = [_layoutCache layoutForContext:context]) {
|
|
_layout = cachedLayout;
|
|
} else {
|
|
// A new layout is needed now. Calculate and apply it immediately
|
|
_layout = [ASCollectionLayout calculateLayoutWithContext:context];
|
|
}
|
|
}
|
|
|
|
- (void)invalidateLayout
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
[super invalidateLayout];
|
|
if (_layout != nil) {
|
|
[_layoutCache removeLayoutForContext:_layout.context];
|
|
_layout = nil;
|
|
}
|
|
}
|
|
|
|
- (CGSize)collectionViewContentSize
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
ASDisplayNodeAssertNotNil(_layout, @"Collection layout state should not be nil at this point");
|
|
return _layout.contentSize;
|
|
}
|
|
|
|
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)blockingRect
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
if (CGRectIsEmpty(blockingRect)) {
|
|
return nil;
|
|
}
|
|
|
|
// Measure elements in the measure range, block on the requested rect
|
|
CGRect measureRect = CGRectExpandToRangeWithScrollableDirections(blockingRect,
|
|
kASDefaultMeasureRangeTuningParameters,
|
|
_layout.context.scrollableDirections,
|
|
kASStaticScrollDirection);
|
|
[ASCollectionLayout _measureElementsInRect:measureRect blockingRect:blockingRect layout:_layout];
|
|
|
|
NSArray<UICollectionViewLayoutAttributes *> *result = [_layout layoutAttributesForElementsInRect:blockingRect];
|
|
|
|
ASElementMap *elements = _layout.context.elements;
|
|
for (UICollectionViewLayoutAttributes *attrs in result) {
|
|
ASCollectionElement *element = [elements elementForLayoutAttributes:attrs];
|
|
ASCollectionLayoutSetSizeToElement(attrs.frame.size, element);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
ASDisplayNodeAssertMainThread();
|
|
|
|
ASCollectionElement *element = [_layout.context.elements elementForItemAtIndexPath:indexPath];
|
|
UICollectionViewLayoutAttributes *attrs = [_layout layoutAttributesForElement:element];
|
|
|
|
ASCellNode *node = element.node;
|
|
CGSize elementSize = attrs.frame.size;
|
|
if (! CGSizeEqualToSize(elementSize, node.calculatedSize)) {
|
|
[node layoutThatFits:ASCollectionLayoutElementSizeRangeFromSize(elementSize)];
|
|
}
|
|
|
|
ASCollectionLayoutSetSizeToElement(attrs.frame.size, element);
|
|
return attrs;
|
|
}
|
|
|
|
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
ASCollectionElement *element = [_layout.context.elements supplementaryElementOfKind:elementKind atIndexPath:indexPath];
|
|
UICollectionViewLayoutAttributes *attrs = [_layout layoutAttributesForElement:element];
|
|
|
|
ASCellNode *node = element.node;
|
|
CGSize elementSize = attrs.frame.size;
|
|
if (! CGSizeEqualToSize(elementSize, node.calculatedSize)) {
|
|
[node layoutThatFits:ASCollectionLayoutElementSizeRangeFromSize(elementSize)];
|
|
}
|
|
|
|
ASCollectionLayoutSetSizeToElement(attrs.frame.size, element);
|
|
return attrs;
|
|
}
|
|
|
|
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
|
|
{
|
|
return (! CGSizeEqualToSize([self _viewportSize], newBounds.size));
|
|
}
|
|
|
|
#pragma mark - Private methods
|
|
|
|
- (CGSize)_viewportSize
|
|
{
|
|
ASCollectionNode *collectionNode = _collectionNode;
|
|
if (collectionNode != nil && !collectionNode.isNodeLoaded) {
|
|
// TODO consider calculatedSize as well
|
|
return collectionNode.threadSafeBounds.size;
|
|
} else {
|
|
ASDisplayNodeAssertMainThread();
|
|
return self.collectionView.bounds.size;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Measures all elements in the specified rect and blocks the calling thread while measuring those in the blocking rect.
|
|
*/
|
|
+ (void)_measureElementsInRect:(CGRect)rect blockingRect:(CGRect)blockingRect layout:(ASCollectionLayoutState *)layout
|
|
{
|
|
if (CGRectIsEmpty(rect) || layout.context.elements == nil) {
|
|
return;
|
|
}
|
|
BOOL hasBlockingRect = !CGRectIsEmpty(blockingRect);
|
|
if (hasBlockingRect && CGRectContainsRect(rect, blockingRect) == NO) {
|
|
ASDisplayNodeCAssert(NO, @"Blocking rect, if specified, must be within the other (outer) rect");
|
|
return;
|
|
}
|
|
|
|
// Step 1: Clamp the specified rects between the bounds of content rect
|
|
CGSize contentSize = layout.contentSize;
|
|
CGRect contentRect = CGRectMake(0, 0, contentSize.width, contentSize.height);
|
|
rect = CGRectIntersection(contentRect, rect);
|
|
if (CGRectIsNull(rect)) {
|
|
return;
|
|
}
|
|
if (hasBlockingRect) {
|
|
blockingRect = CGRectIntersection(contentRect, blockingRect);
|
|
hasBlockingRect = !CGRectIsNull(blockingRect);
|
|
}
|
|
|
|
// Step 2: Get layout attributes of all elements within the specified outer rect
|
|
ASCollectionLayoutContext *context = layout.context;
|
|
CGSize pageSize = context.viewportSize;
|
|
ASPageToLayoutAttributesTable *attrsTable = [layout getAndRemoveUnmeasuredLayoutAttributesPageTableInRect:rect
|
|
contentSize:contentSize
|
|
pageSize:pageSize];
|
|
if (attrsTable.count == 0) {
|
|
// No elements in this rect! Bail early
|
|
return;
|
|
}
|
|
|
|
// Step 3: Split all those attributes into blocking and non-blocking buckets
|
|
// Use ordered sets here because some items may span multiple pages, and the sets will be accessed by indexes later on.
|
|
NSMutableOrderedSet<UICollectionViewLayoutAttributes *> *blockingAttrs = hasBlockingRect ? [NSMutableOrderedSet orderedSet] : nil;
|
|
NSMutableOrderedSet<UICollectionViewLayoutAttributes *> *nonBlockingAttrs = [NSMutableOrderedSet orderedSet];
|
|
for (id pagePtr in attrsTable) {
|
|
ASPageCoordinate page = (ASPageCoordinate)pagePtr;
|
|
NSArray<UICollectionViewLayoutAttributes *> *attrsInPage = [attrsTable objectForPage:page];
|
|
// Calculate the page's rect but only if it's going to be used.
|
|
CGRect pageRect = hasBlockingRect ? ASPageCoordinateGetPageRect(page, pageSize) : CGRectZero;
|
|
|
|
if (hasBlockingRect && CGRectContainsRect(blockingRect, pageRect)) {
|
|
// The page fits well within the blocking rect. All attributes in this page are blocking.
|
|
[blockingAttrs addObjectsFromArray:attrsInPage];
|
|
} else if (hasBlockingRect && CGRectIntersectsRect(blockingRect, pageRect)) {
|
|
// The page intersects the blocking rect. Some elements in this page are blocking, some are not.
|
|
for (UICollectionViewLayoutAttributes *attrs in attrsInPage) {
|
|
if (CGRectIntersectsRect(blockingRect, attrs.frame)) {
|
|
[blockingAttrs addObject:attrs];
|
|
} else {
|
|
[nonBlockingAttrs addObject:attrs];
|
|
}
|
|
}
|
|
} else {
|
|
// The page doesn't intersect the blocking rect. All elements in this page are non-blocking.
|
|
[nonBlockingAttrs addObjectsFromArray:attrsInPage];
|
|
}
|
|
}
|
|
|
|
// Step 4: Allocate and measure blocking elements' node
|
|
ASElementMap *elements = context.elements;
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
|
if (NSUInteger count = blockingAttrs.count) {
|
|
ASDispatchApply(count, queue, 0, ^(size_t i) {
|
|
UICollectionViewLayoutAttributes *attrs = blockingAttrs[i];
|
|
ASCellNode *node = [elements elementForItemAtIndexPath:attrs.indexPath].node;
|
|
CGSize expectedSize = attrs.frame.size;
|
|
if (! CGSizeEqualToSize(expectedSize, node.calculatedSize)) {
|
|
[node layoutThatFits:ASCollectionLayoutElementSizeRangeFromSize(expectedSize)];
|
|
}
|
|
});
|
|
}
|
|
|
|
// Step 5: Allocate and measure non-blocking ones
|
|
if (NSUInteger count = nonBlockingAttrs.count) {
|
|
__weak ASElementMap *weakElements = elements;
|
|
ASDispatchAsync(count, queue, 0, ^(size_t i) {
|
|
__strong ASElementMap *strongElements = weakElements;
|
|
if (strongElements) {
|
|
UICollectionViewLayoutAttributes *attrs = nonBlockingAttrs[i];
|
|
ASCellNode *node = [elements elementForItemAtIndexPath:attrs.indexPath].node;
|
|
CGSize expectedSize = attrs.frame.size;
|
|
if (! CGSizeEqualToSize(expectedSize, node.calculatedSize)) {
|
|
[node layoutThatFits:ASCollectionLayoutElementSizeRangeFromSize(expectedSize)];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
# pragma mark - Convenient inline functions
|
|
|
|
ASDISPLAYNODE_INLINE ASSizeRange ASCollectionLayoutElementSizeRangeFromSize(CGSize size)
|
|
{
|
|
// The layout delegate consulted us that this element must fit within this size,
|
|
// and the only way to achieve that without asking it again is to use an exact size range here.
|
|
return ASSizeRangeMake(size);
|
|
}
|
|
|
|
ASDISPLAYNODE_INLINE void ASCollectionLayoutSetSizeToElement(CGSize size, ASCollectionElement *element)
|
|
{
|
|
if (ASCellNode *node = element.node) {
|
|
if (! CGSizeEqualToSize(size, node.frame.size)) {
|
|
CGRect frame = CGRectZero;
|
|
frame.size = size;
|
|
node.frame = frame;
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|