Swiftgram/Source/Private/ASCollectionLayout.mm
Garrett Moon 8013e25524 Update license v2 (#67)
* Fixed license

* Update all licenses

* Update Dangerfile for new license

* Update already updated licenses

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…

* Closer…
2017-04-24 16:59:57 -07:00

179 lines
5.9 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/ASCellNode.h>
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASCollectionLayoutContext+Private.h>
#import <AsyncDisplayKit/ASCollectionLayoutDelegate.h>
#import <AsyncDisplayKit/ASCollectionLayoutState.h>
#import <AsyncDisplayKit/ASCollectionNode+Beta.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASEqualityHelpers.h>
#import <AsyncDisplayKit/ASThread.h>
@interface ASCollectionLayout () <ASDataControllerLayoutDelegate> {
ASDN::Mutex __instanceLock__; // Non-recursive mutex, ftw!
// Main thread only.
ASCollectionLayoutState *_state;
// The pending state calculated ahead of time, if any.
ASCollectionLayoutState *_pendingState;
// The context used to calculate _pendingState
ASCollectionLayoutContext *_layoutContextForPendingState;
BOOL _layoutDelegateImplementsAdditionalInfoForLayoutWithElements;
}
@end
@implementation ASCollectionLayout
- (instancetype)initWithLayoutDelegate:(id<ASCollectionLayoutDelegate>)layoutDelegate
{
self = [super init];
if (self) {
ASDisplayNodeAssertNotNil(layoutDelegate, @"Collection layout delegate cannot be nil");
_layoutDelegate = layoutDelegate;
_layoutDelegateImplementsAdditionalInfoForLayoutWithElements = [layoutDelegate respondsToSelector:@selector(additionalInfoForLayoutWithElements:)];
}
return self;
}
#pragma mark - ASDataControllerLayoutDelegate
- (id)layoutContextWithElements:(ASElementMap *)elements
{
ASDisplayNodeAssertMainThread();
id additionalInfo = nil;
if (_layoutDelegateImplementsAdditionalInfoForLayoutWithElements) {
additionalInfo = [_layoutDelegate additionalInfoForLayoutWithElements:elements];
}
return [[ASCollectionLayoutContext alloc] initWithViewportSize:[self viewportSize] elements:elements additionalInfo:additionalInfo];
}
- (void)prepareLayoutWithContext:(id)context
{
ASCollectionLayoutState *state = [_layoutDelegate calculateLayoutWithContext:context];
ASDN::MutexLocker l(__instanceLock__);
_pendingState = state;
_layoutContextForPendingState = context;
}
#pragma mark - UICollectionViewLayout overrides
- (void)prepareLayout
{
ASDisplayNodeAssertMainThread();
[super prepareLayout];
ASCollectionLayoutContext *context = [self layoutContextWithElements:_collectionNode.visibleElements];
ASCollectionLayoutState *state = nil;
{
ASDN::MutexLocker l(__instanceLock__);
if (_pendingState != nil && ASObjectIsEqual(_layoutContextForPendingState, context)) {
// Looks like we can use the pending state. Great!
state = _pendingState;
_pendingState = nil;
_layoutContextForPendingState = nil;
}
}
if (state == nil) {
state = [_layoutDelegate calculateLayoutWithContext:context];
}
_state = state;
}
- (void)invalidateLayout
{
ASDisplayNodeAssertMainThread();
[super invalidateLayout];
_state = nil;
}
- (CGSize)collectionViewContentSize
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssertNotNil(_state, @"Collection layout state should not be nil at this point");
return _state.contentSize;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributesInRect = [NSMutableArray array];
NSMapTable *attrsMap = _state.elementToLayoutArrtibutesMap;
for (ASCollectionElement *element in attrsMap) {
UICollectionViewLayoutAttributes *attrs = [attrsMap objectForKey:element];
if (CGRectIntersectsRect(rect, attrs.frame)) {
[ASCollectionLayout setSize:attrs.frame.size toElement:element];
[attributesInRect addObject:attrs];
}
}
return attributesInRect;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = _state;
ASCollectionElement *element = [state.elements elementForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *attrs = [state.elementToLayoutArrtibutesMap objectForKey:element];
[ASCollectionLayout setSize:attrs.frame.size toElement:element];
return attrs;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = _state;
ASCollectionElement *element = [state.elements supplementaryElementOfKind:elementKind atIndexPath:indexPath];
UICollectionViewLayoutAttributes *attrs = [state.elementToLayoutArrtibutesMap objectForKey:element];
[ASCollectionLayout setSize:attrs.frame.size toElement:element];
return attrs;
}
#pragma mark - Private methods
+ (void)setSize:(CGSize)size toElement:(ASCollectionElement *)element
{
ASCellNode *node = element.node;
if (! CGSizeEqualToSize(size, node.frame.size)) {
CGRect nodeFrame = CGRectZero;
nodeFrame.size = size;
node.frame = nodeFrame;
}
}
- (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;
}
}
@end