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

Commit 00d9372

Browse files
committed
Add a "Null Object" for a layout inspector if no no flow layout is given
The purpose of the ASCollectionViewCustomLayoutInspector is to provide a default implementation if given a custom layout, but no layout inspector.
1 parent 50ca58f commit 00d9372

4 files changed

Lines changed: 73 additions & 40 deletions

File tree

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ @interface ASCollectionView () <ASRangeControllerDataSource, ASRangeControllerDe
9898
ASCollectionDataController *_dataController;
9999
ASRangeController *_rangeController;
100100
ASCollectionViewLayoutController *_layoutController;
101-
ASCollectionViewFlowLayoutInspector *_flowLayoutInspector;
101+
id<ASCollectionViewLayoutInspecting> _defaultLayoutInspector;
102102
NSMutableSet *_cellsForVisibilityUpdates;
103103
id<ASCollectionViewLayoutFacilitatorProtocol> _layoutFacilitator;
104104

@@ -244,11 +244,19 @@ - (instancetype)_initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionV
244244
// and should not trigger a relayout.
245245
_ignoreMaxSizeChange = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero);
246246

247-
// Register the default layout inspector delegate for flow layouts only, custom layouts
248-
// will need to roll their own ASCollectionViewLayoutInspecting implementation and set a layout delegate
249247
if ([layout asdk_isFlowLayout]) {
250-
_layoutInspector = [self flowLayoutInspector];
248+
// Register the default layout inspector delegate for flow layouts only
249+
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
250+
ASDisplayNodeAssertNotNil(layout, @"Collection view layout must be a flow layout to use the built-in inspector");
251+
_defaultLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] initWithCollectionView:self flowLayout:layout];
252+
} else {
253+
// Custom layouts will need to roll their own ASCollectionViewLayoutInspecting implementation and set a layout
254+
// delegate. In the meantime ASDK provides a custom layout inspector that does not provide any implementation
255+
// and asserts for methods that should be implemented in the <ASCollectionViewLayoutInspecting>
256+
_defaultLayoutInspector = [[ASCollectionViewCustomLayoutInspector alloc] init];
251257
}
258+
_layoutInspector = _defaultLayoutInspector;
259+
252260
_layoutFacilitator = layoutFacilitator;
253261

254262
_proxyDelegate = [[ASCollectionViewProxy alloc] initWithTarget:nil interceptor:self];
@@ -275,19 +283,6 @@ - (void)dealloc
275283
[self setAsyncDataSource:nil];
276284
}
277285

278-
/**
279-
* A layout inspector implementation specific for the sizing behavior of UICollectionViewFlowLayouts
280-
*/
281-
- (ASCollectionViewFlowLayoutInspector *)flowLayoutInspector
282-
{
283-
if (_flowLayoutInspector == nil) {
284-
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
285-
ASDisplayNodeAssertNotNil(layout, @"Collection view layout must be a flow layout to use the built-in inspector");
286-
_flowLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] initWithCollectionView:self flowLayout:layout];
287-
}
288-
return _flowLayoutInspector;
289-
}
290-
291286
#pragma mark -
292287
#pragma mark Overrides.
293288

@@ -405,7 +400,9 @@ - (void)setAsyncDelegate:(id<ASCollectionViewDelegate>)asyncDelegate
405400

406401
super.delegate = (id<UICollectionViewDelegate>)_proxyDelegate;
407402

408-
[_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
403+
if ([_layoutInspector respondsToSelector:@selector(didChangeCollectionViewDelegate:)]) {
404+
[_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
405+
}
409406
}
410407

411408
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeType:(ASLayoutRangeType)rangeType
@@ -864,28 +861,21 @@ - (ASCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAt
864861

865862
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
866863
{
867-
ASSizeRange constrainedSize = kInvalidSizeRange;
868-
if (_layoutInspector) {
869-
constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
870-
}
871-
864+
ASSizeRange constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
872865
if (!ASSizeRangeEqualToSizeRange(constrainedSize, kInvalidSizeRange)) {
873866
return constrainedSize;
874867
}
875868

876-
// TODO: Move this logic into the flow layout inspector. Create a simple inspector for non-flow layouts that don't
877-
// implement a custom inspector.
878-
if (_asyncDataSourceFlags.asyncDataSourceConstrainedSizeForNode) {
879-
constrainedSize = [_asyncDataSource collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
869+
CGSize maxSize = _maxSizeForNodesConstrainedSize;
870+
if (CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero)) {
871+
maxSize = self.bounds.size;
872+
}
873+
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
874+
maxSize.width = FLT_MAX;
880875
} else {
881-
CGSize maxSize = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero) ? self.bounds.size : _maxSizeForNodesConstrainedSize;
882-
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
883-
maxSize.width = FLT_MAX;
884-
} else {
885-
maxSize.height = FLT_MAX;
886-
}
887-
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
876+
maxSize.height = FLT_MAX;
888877
}
878+
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
889879

890880
return constrainedSize;
891881
}
@@ -947,19 +937,16 @@ - (NSArray *)supplementaryNodeKindsInDataController:(ASCollectionDataController
947937

948938
- (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
949939
{
950-
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
951940
return [_layoutInspector collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
952941
}
953942

954943
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
955944
{
956-
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
957945
return [_layoutInspector collectionView:self supplementaryNodesOfKind:kind inSection:section];
958946
}
959947

960948
- (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind;
961949
{
962-
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
963950
return [_layoutInspector collectionView:self numberOfSectionsForSupplementaryNodeOfKind:kind];
964951
}
965952

AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@
4747

4848
@end
4949

50+
/**
51+
* Simple "Null Object" inspector for non-flow layouts that does not implement a custom inspector, provides a zero
52+
* constrained size and throws an exception if methods are called from <ASCollectionViewLayoutInspecting>
53+
*/
54+
@interface ASCollectionViewCustomLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
55+
56+
@end
57+
58+
/**
59+
* A layout inspector implementation specific for the sizing behavior of UICollectionViewFlowLayouts
60+
*/
5061
@interface ASCollectionViewFlowLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
5162

5263
@property (nonatomic, weak) UICollectionViewFlowLayout *layout;

AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@
1313
#import "ASAssert.h"
1414
#import "ASEqualityHelpers.h"
1515

16+
17+
18+
@implementation ASCollectionViewCustomLayoutInspector
19+
20+
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
21+
{
22+
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
23+
}
24+
25+
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
26+
{
27+
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
28+
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
29+
}
30+
31+
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind
32+
{
33+
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
34+
return 0;
35+
}
36+
37+
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
38+
{
39+
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
40+
return 0;
41+
}
42+
43+
@end
44+
1645
@implementation ASCollectionViewFlowLayoutInspector {
1746
BOOL _delegateImplementsReferenceSizeForHeader;
1847
BOOL _delegateImplementsReferenceSizeForFooter;
@@ -50,8 +79,11 @@ - (void)didChangeCollectionViewDelegate:(id<ASCollectionDelegate>)delegate;
5079

5180
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
5281
{
53-
// TODO: Provide constrained size for flow layout item nodes
54-
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
82+
if ([collectionView.asyncDataSource respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)]) {
83+
return [collectionView.asyncDataSource collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
84+
} else {
85+
return ASSizeRangeMake(_layout.itemSize, _layout.itemSize);
86+
}
5587
}
5688

5789
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

AsyncDisplayKitTests/ASCollectionViewTests.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ - (void)testThatItDoesNotSetALayoutInspectorForCustomLayouts
108108
{
109109
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
110110
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
111-
XCTAssert(collectionView.layoutInspector == nil, @"should not set a layout delegate for custom layouts");
111+
XCTAssert(collectionView.layoutInspector != nil, @"should automatically set a layout delegate for custom layouts");
112+
XCTAssert([collectionView.layoutInspector isKindOfClass:[ASCollectionViewCustomLayoutInspector class]], @"should have a internal custom layout inspector by default");
113+
XCTAssert(ASSizeRangeEqualToSizeRange([collectionView.layoutInspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]], ASSizeRangeMake(CGSizeZero, CGSizeZero)), @"should return a zero constrainted size range in internal custom layout inspector");
114+
XCTAssertThrows([collectionView.layoutInspector collectionView:collectionView supplementaryNodesOfKind:UICollectionElementKindSectionHeader inSection:0], @"should throw an exception for <ASCollectionViewLayoutInspecting> methods");
112115
}
113116

114117
- (void)testThatRegisteringASupplementaryNodeStoresItForIntrospection

0 commit comments

Comments
 (0)