Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Add ASLegacyCollectionLayoutCalculator for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy committed Mar 6, 2017
1 parent 8e601be commit 00f0fcc
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 111 deletions.
8 changes: 8 additions & 0 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@
DECBD6E81BE56E1900CF4905 /* ASButtonNode.h in Headers */ = {isa = PBXBuildFile; fileRef = DECBD6E51BE56E1900CF4905 /* ASButtonNode.h */; settings = {ATTRIBUTES = (Public, ); }; };
DECBD6EA1BE56E1900CF4905 /* ASButtonNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = DECBD6E61BE56E1900CF4905 /* ASButtonNode.mm */; };
DEFAD8131CC48914000527C4 /* ASVideoNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEEC47E01C20C2DD00EC1693 /* ASVideoNode.mm */; };
E52465C01E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.h in Headers */ = {isa = PBXBuildFile; fileRef = E52465BE1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.h */; settings = {ATTRIBUTES = (Public, ); }; };
E52465C11E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.m in Sources */ = {isa = PBXBuildFile; fileRef = E52465BF1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.m */; };
E55D86331CA8A14000A0C26F /* ASLayoutElement.mm in Sources */ = {isa = PBXBuildFile; fileRef = E55D86311CA8A14000A0C26F /* ASLayoutElement.mm */; };
E5711A2C1C840C81009619D4 /* ASCollectionElement.h in Headers */ = {isa = PBXBuildFile; fileRef = E5711A2A1C840C81009619D4 /* ASCollectionElement.h */; settings = {ATTRIBUTES = (Private, ); }; };
E5711A301C840C96009619D4 /* ASCollectionElement.mm in Sources */ = {isa = PBXBuildFile; fileRef = E5711A2D1C840C96009619D4 /* ASCollectionElement.mm */; };
Expand Down Expand Up @@ -775,6 +777,8 @@
DECBD6E61BE56E1900CF4905 /* ASButtonNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASButtonNode.mm; sourceTree = "<group>"; };
E52405B21C8FEF03004DC8E7 /* ASLayoutTransition.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASLayoutTransition.mm; sourceTree = "<group>"; };
E52405B41C8FEF16004DC8E7 /* ASLayoutTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLayoutTransition.h; sourceTree = "<group>"; };
E52465BE1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLegacyCollectionLayoutCalculator.h; sourceTree = "<group>"; };
E52465BF1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASLegacyCollectionLayoutCalculator.m; sourceTree = "<group>"; };
E55D86311CA8A14000A0C26F /* ASLayoutElement.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASLayoutElement.mm; sourceTree = "<group>"; };
E5711A2A1C840C81009619D4 /* ASCollectionElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionElement.h; sourceTree = "<group>"; };
E5711A2D1C840C96009619D4 /* ASCollectionElement.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASCollectionElement.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1399,6 +1403,8 @@
E5B077F01E6855B100C24B5B /* ASCollectionLayoutSpecCalculator.m */,
E5B0780A1E6A01E200C24B5B /* ASCollectionFlowLayoutCalculator.h */,
E5B0780B1E6A01E200C24B5B /* ASCollectionFlowLayoutCalculator.m */,
E52465BE1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.h */,
E52465BF1E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.m */,
E5B077E11E6843A600C24B5B /* ASCollectionViewLayout.h */,
E5B077F41E68576B00C24B5B /* ASCollectionViewLayout.m */,
);
Expand All @@ -1422,6 +1428,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
E52465C01E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.h in Headers */,
E5B0780C1E6A01E200C24B5B /* ASCollectionFlowLayoutCalculator.h in Headers */,
E5B077EE1E68558200C24B5B /* ASCollectionLayoutSpecCalculator.h in Headers */,
E5B077EA1E6843A600C24B5B /* ASCollectionLayoutCalculating.h in Headers */,
Expand Down Expand Up @@ -1891,6 +1898,7 @@
68FC85EC1CE29C7D00EDD713 /* ASVisibilityProtocols.m in Sources */,
CC55A7121E52A0F200594372 /* ASResponderChainEnumerator.m in Sources */,
68B8A4E41CBDB958007E4543 /* ASWeakProxy.m in Sources */,
E52465C11E6DE877003632D0 /* ASLegacyCollectionLayoutCalculator.m in Sources */,
9C70F20A1CDBE949007D6C76 /* ASTableNode.mm in Sources */,
69CB62AE1CB8165900024920 /* _ASDisplayViewAccessiblity.mm in Sources */,
B35061F61B010EFD0018CF92 /* ASCollectionView.mm in Sources */,
Expand Down
47 changes: 33 additions & 14 deletions Source/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
#import <AsyncDisplayKit/ASCollectionInternal.h>
#import <AsyncDisplayKit/ASCollectionViewLayoutController.h>
#import <AsyncDisplayKit/ASCollectionViewLayout.h>
#import <AsyncDisplayKit/ASCollectionLayoutCalculating.h>
#import <AsyncDisplayKit/ASCollectionFlowLayoutCalculator.h>
#import <AsyncDisplayKit/ASCollectionViewFlowLayoutInspector.h>
#import <AsyncDisplayKit/ASDataController.h>
#import <AsyncDisplayKit/ASDisplayNodeExtras.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASLegacyCollectionLayoutCalculator.h>
#import <AsyncDisplayKit/UICollectionViewLayout+ASConvenience.h>
#import <AsyncDisplayKit/ASRangeController.h>
#import <AsyncDisplayKit/ASCollectionNode.h>
Expand Down Expand Up @@ -144,6 +143,8 @@ @interface ASCollectionView () <ASRangeControllerDataSource, ASRangeControllerDe
*/
NSInteger _batchUpdateCount;

id<ASCollectionLayoutCalculating> _layoutCalculator;

struct {
unsigned int scrollViewDidScroll:1;
unsigned int scrollViewWillBeginDragging:1;
Expand Down Expand Up @@ -218,8 +219,9 @@ @interface ASCollectionView () <ASRangeControllerDataSource, ASRangeControllerDe
} _layoutInspectorFlags;

struct {
unsigned int providesLayoutCalculator;
unsigned int consumesResultOfLayoutCalculator;
unsigned int isASCollectionViewLayout:1;
unsigned int providesLayoutCalculator:1;
unsigned int consumesResultOfLayoutCalculator:1;
} _collectionViewLayoutFlags;

}
Expand Down Expand Up @@ -256,7 +258,7 @@ - (instancetype)_initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionV
if (!(self = [super initWithFrame:frame collectionViewLayout:layout]))
return nil;

[self _configureCollectionViewLayout:layout];
[self _configureCollectionViewLayoutFlags:layout];

// Disable UICollectionView prefetching.
// Experiments done by Instagram show that this option being YES (default)
Expand Down Expand Up @@ -545,7 +547,7 @@ - (void)setCollectionViewLayout:(nonnull UICollectionViewLayout *)collectionView
{
[super setCollectionViewLayout:collectionViewLayout];

[self _configureCollectionViewLayout:collectionViewLayout];
[self _configureCollectionViewLayoutFlags:collectionViewLayout];

// Trigger recreation of layout inspector with new collection view layout
if (_layoutInspector != nil) {
Expand Down Expand Up @@ -754,8 +756,9 @@ - (NSArray *)visibleNodes

#pragma mark Internal

- (void)_configureCollectionViewLayout:(nonnull UICollectionViewLayout *)layout
- (void)_configureCollectionViewLayoutFlags:(nonnull UICollectionViewLayout *)layout
{
_collectionViewLayoutFlags.isASCollectionViewLayout = [layout isKindOfClass:[ASCollectionViewLayout class]];
_collectionViewLayoutFlags.providesLayoutCalculator = [layout conformsToProtocol:@protocol(ASCollectionLayoutCalculatorProviding)];
_collectionViewLayoutFlags.consumesResultOfLayoutCalculator = [layout conformsToProtocol:@protocol(ASCollectionLayoutCalculatorResultConsuming)];
}
Expand Down Expand Up @@ -1519,14 +1522,30 @@ - (void)_beginBatchFetching

#pragma mark - ASCollectionLayoutCalculatorProviding

- (id<ASCollectionLayoutCalculating>)collectionViewLayoutCalculator
- (id<ASCollectionLayoutCalculating>)collectionLayoutCalculator
{
return _collectionViewLayoutFlags.providesLayoutCalculator ? [(id<ASCollectionLayoutCalculatorProviding>)self.collectionViewLayout collectionViewLayoutCalculator] : nil;
ASDisplayNodeAssertMainThread();
// If layout object provides layout calculator, use it. But clear any existing default calculator first.
if (_collectionViewLayoutFlags.providesLayoutCalculator) {
_layoutCalculator = nil;
return [(id<ASCollectionLayoutCalculatorProviding>)self.collectionViewLayout collectionLayoutCalculator];
}

// Layout object doesn't provide a layout calculator. Lazily create a default one.
if (_layoutCalculator == nil) {
_layoutCalculator = [[ASLegacyCollectionLayoutCalculator alloc] init];
}
return _layoutCalculator;
}

- (CGSize)viewportSizeForCollectionLayout
{
return _collectionViewLayoutFlags.providesLayoutCalculator ? [(id<ASCollectionLayoutCalculatorProviding>)self.collectionViewLayout viewportSizeForCollectionLayout] : CGSizeZero;
ASDisplayNodeAssertMainThread();
if (_collectionViewLayoutFlags.providesLayoutCalculator) {
return [(id<ASCollectionLayoutCalculatorProviding>)self.collectionViewLayout viewportSizeForCollectionLayout];
} else {
return self.bounds.size;
}
}

#pragma mark - ASCollectionLayoutCalculatorResultConsuming
Expand Down Expand Up @@ -1705,9 +1724,7 @@ - (ASSizeRange)dataController:(ASDataController *)dataController constrainedSize
return [self.layoutInspector collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
}

if (_collectionViewLayoutFlags.providesLayoutCalculator == NO) {
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutInspector for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
}
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutInspector for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
}

Expand Down Expand Up @@ -2050,7 +2067,9 @@ - (void)layer:(CALayer *)layer didChangeBoundsWithOldValue:(CGRect)oldBounds new
if (self.collectionViewLayout == nil) {
return;
}
if (_collectionViewLayoutFlags.providesLayoutCalculator) {
if (_collectionViewLayoutFlags.isASCollectionViewLayout) {
// ASCollectionViewLayout should be able to handle bounds changes and automatically re-run its calculator.
// TODO This is more or less a workaround for the current relayout system which is destined for an overhaul.
return;
}
CGSize lastUsedSize = _lastBoundsSizeUsedForMeasuringNodes;
Expand Down
19 changes: 18 additions & 1 deletion Source/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASLegacyCollectionLayoutCalculator.h>
#import <AsyncDisplayKit/ASTableNode.h>
#import <AsyncDisplayKit/ASRangeController.h>
#import <AsyncDisplayKit/ASEqualityHelpers.h>
Expand Down Expand Up @@ -181,6 +182,8 @@ @interface ASTableView () <ASRangeControllerDataSource, ASRangeControllerDelegat
*/
NSInteger _batchUpdateCount;

id<ASCollectionLayoutCalculating> _layoutCalculator;

struct {
unsigned int scrollViewDidScroll:1;
unsigned int scrollViewWillBeginDragging:1;
Expand Down Expand Up @@ -288,6 +291,8 @@ - (void)configureWithDataControllerClass:(Class)dataControllerClass eventLog:(AS

_proxyDataSource = [[ASTableViewProxy alloc] initWithTarget:nil interceptor:self];
super.dataSource = (id<UITableViewDataSource>)_proxyDataSource;

_layoutCalculator = [[ASLegacyCollectionLayoutCalculator alloc] init];

[self registerClass:_ASTableViewCell.class forCellReuseIdentifier:kCellReuseIdentifier];
}
Expand Down Expand Up @@ -1542,7 +1547,19 @@ - (void)rangeController:(ASRangeController *)rangeController didUpdateWithChange
[changeSet executeCompletionHandlerWithFinished:YES];
}

#pragma mark - ASDataControllerDelegate
#pragma mark - ASDataControllerSource

- (id<ASCollectionLayoutCalculating>)collectionLayoutCalculator
{
ASDisplayNodeAssertMainThread();
return _layoutCalculator;
}

- (CGSize)viewportSizeForCollectionLayout
{
ASDisplayNodeAssertMainThread();
return CGSizeMake(_nodesConstrainedWidth, self.bounds.size.height);
}

- (ASCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAtIndexPath:(NSIndexPath *)indexPath {
ASCellNodeBlock block = nil;
Expand Down
3 changes: 2 additions & 1 deletion Source/AsyncDisplayKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
#import <AsyncDisplayKit/ASTableNode.h>
#import <AsyncDisplayKit/ASCollectionView.h>
#import <AsyncDisplayKit/ASCollectionNode.h>
#import <AsyncDisplayKit/ASCollectionViewLayout.h>
#import <AsyncDisplayKit/ASCollectionViewLayoutInspector.h>
#import <AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h>
#import <AsyncDisplayKit/ASCellNode.h>
#import <AsyncDisplayKit/ASSectionContext.h>

#import <AsyncDisplayKit/ASCollectionViewLayout.h>
#import <AsyncDisplayKit/ASCollectionLayoutCalculating.h>
#import <AsyncDisplayKit/ASCollectionLayoutSpecCalculator.h>
#import <AsyncDisplayKit/ASCollectionFlowLayoutCalculator.h>
#import <AsyncDisplayKit/ASLegacyCollectionLayoutCalculator.h>

#import <AsyncDisplayKit/ASSectionController.h>
#import <AsyncDisplayKit/ASSupplementaryNodeSource.h>
Expand Down
8 changes: 2 additions & 6 deletions Source/Details/ASCollectionFlowLayoutCalculator.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
#import <AsyncDisplayKit/ASBaseDefines.h>

@implementation ASCollectionFlowLayoutCalculator

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)sizeRange forElementMap:(ASElementMap *)map
{
// TODO Use ASArrayByFlatMapping
NSMutableArray<ASCellNode *> *children = [NSMutableArray array];
for (ASCollectionElement *element in map) {
[children addObject:element.node];
}

NSArray<ASCellNode *> *children = ASArrayByFlatMapping(map, ASCollectionElement *element, element.node);
ASStackLayoutSpec *stackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
Expand Down
11 changes: 9 additions & 2 deletions Source/Details/ASCollectionLayoutCalculating.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ NS_ASSUME_NONNULL_BEGIN

@protocol ASCollectionLayoutCalculating <NSObject>

/**
* Whether or not size ranges must be provided via collection element in the element map.
* Default to NO, which means the calculator can determine size range for each collection element.
* ASLegacyCollectionLayoutCalculator might return YES because size ranges used to be provided via async delegate of table/collection view or layout inspector of collection view.
*/
@property (nonatomic, assign, readonly) BOOL sizeRangesShouldBeProvided;

/**
* @abstract Return a layout of elements.
*
* @param viewportSize The viewport size that the resulting layout should fit, usually equals to the bounds of the containing collection view.
* @param viewportSize The viewport size that the resulting layout should fit, usually equals to the bounds of the containing table/collection view.
*
* @param map An element map containings all elements to be laid out.
*
Expand All @@ -31,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN

@protocol ASCollectionLayoutCalculatorProviding <NSObject>

- (nullable id<ASCollectionLayoutCalculating>)collectionViewLayoutCalculator;
- (id<ASCollectionLayoutCalculating>)collectionLayoutCalculator;

- (CGSize)viewportSizeForCollectionLayout;

Expand Down
5 changes: 5 additions & 0 deletions Source/Details/ASCollectionLayoutSpecCalculator.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

@implementation ASCollectionLayoutSpecCalculator

- (BOOL)sizeRangesShouldBeProvided
{
return NO;
}

- (instancetype)init
{
return [self initWithScrollableDirections:ASScrollDirectionVerticalDirections];
Expand Down
2 changes: 1 addition & 1 deletion Source/Details/ASCollectionViewLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (instancetype)initWithLayoutCalculator:(id<ASCollectionLayoutCalculating>)calc

#pragma mark - ASCollectionLayoutCalculatorProviding

- (id<ASCollectionLayoutCalculating>)collectionViewLayoutCalculator
- (id<ASCollectionLayoutCalculating>)collectionLayoutCalculator
{
return _calculator;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Details/ASDataController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#import <UIKit/UIKit.h>
#import <AsyncDisplayKit/ASBlockTypes.h>
#import <AsyncDisplayKit/ASCollectionLayoutCalculating.h>
#import <AsyncDisplayKit/ASDimension.h>
#import <AsyncDisplayKit/ASEventLog.h>
#ifdef __cplusplus
Expand Down Expand Up @@ -46,7 +47,7 @@ extern NSString * const ASCollectionInvalidUpdateException;
It will be invoked in the same thread as the api call of ASDataController.
*/

@protocol ASDataControllerSource <NSObject>
@protocol ASDataControllerSource <ASCollectionLayoutCalculatorProviding>

/**
Fetch the ASCellNode block for specific index path. This block should return the ASCellNode for the specified index path.
Expand All @@ -56,7 +57,6 @@ extern NSString * const ASCollectionInvalidUpdateException;
/**
The constrained size range for layout.
*/
// TODO now that there is a layout calculator, this might be optional?
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath;

/**
Expand Down
Loading

0 comments on commit 00f0fcc

Please sign in to comment.