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

Commit 4804f42

Browse files
committed
Revert "[ASCollectionView] Add a "Null Object" for a layout inspector if custom layout is given but no layout inspector" (#1713)
1 parent 5289b1e commit 4804f42

4 files changed

Lines changed: 40 additions & 73 deletions

File tree

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 37 additions & 24 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-
id<ASCollectionViewLayoutInspecting> _defaultLayoutInspector;
101+
ASCollectionViewFlowLayoutInspector *_flowLayoutInspector;
102102
NSMutableSet *_cellsForVisibilityUpdates;
103103
id<ASCollectionViewLayoutFacilitatorProtocol> _layoutFacilitator;
104104

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

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

264256
_proxyDelegate = [[ASCollectionViewProxy alloc] initWithTarget:nil interceptor:self];
@@ -285,6 +277,19 @@ - (void)dealloc
285277
[self setAsyncDataSource:nil];
286278
}
287279

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

@@ -404,9 +409,7 @@ - (void)setAsyncDelegate:(id<ASCollectionViewDelegate>)asyncDelegate
404409

405410
super.delegate = (id<UICollectionViewDelegate>)_proxyDelegate;
406411

407-
if ([_layoutInspector respondsToSelector:@selector(didChangeCollectionViewDelegate:)]) {
408-
[_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
409-
}
412+
[_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
410413
}
411414

412415
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeType:(ASLayoutRangeType)rangeType
@@ -888,21 +891,28 @@ - (ASCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAt
888891

889892
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
890893
{
891-
ASSizeRange constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
894+
ASSizeRange constrainedSize = kInvalidSizeRange;
895+
if (_layoutInspector) {
896+
constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
897+
}
898+
892899
if (!ASSizeRangeEqualToSizeRange(constrainedSize, kInvalidSizeRange)) {
893900
return constrainedSize;
894901
}
895902

896-
CGSize maxSize = _maxSizeForNodesConstrainedSize;
897-
if (CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero)) {
898-
maxSize = self.bounds.size;
899-
}
900-
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
901-
maxSize.width = FLT_MAX;
903+
// TODO: Move this logic into the flow layout inspector. Create a simple inspector for non-flow layouts that don't
904+
// implement a custom inspector.
905+
if (_asyncDataSourceFlags.asyncDataSourceConstrainedSizeForNode) {
906+
constrainedSize = [_asyncDataSource collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
902907
} else {
903-
maxSize.height = FLT_MAX;
908+
CGSize maxSize = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero) ? self.bounds.size : _maxSizeForNodesConstrainedSize;
909+
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
910+
maxSize.width = FLT_MAX;
911+
} else {
912+
maxSize.height = FLT_MAX;
913+
}
914+
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
904915
}
905-
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
906916

907917
return constrainedSize;
908918
}
@@ -964,16 +974,19 @@ - (NSArray *)supplementaryNodeKindsInDataController:(ASCollectionDataController
964974

965975
- (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
966976
{
977+
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
967978
return [_layoutInspector collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
968979
}
969980

970981
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
971982
{
983+
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
972984
return [_layoutInspector collectionView:self supplementaryNodesOfKind:kind inSection:section];
973985
}
974986

975987
- (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind;
976988
{
989+
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
977990
return [_layoutInspector collectionView:self numberOfSectionsForSupplementaryNodeOfKind:kind];
978991
}
979992

AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@
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-
*/
6150
@interface ASCollectionViewFlowLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
6251

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

AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,6 @@
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-
4516
@implementation ASCollectionViewFlowLayoutInspector {
4617
BOOL _delegateImplementsReferenceSizeForHeader;
4718
BOOL _delegateImplementsReferenceSizeForFooter;
@@ -79,11 +50,8 @@ - (void)didChangeCollectionViewDelegate:(id<ASCollectionDelegate>)delegate;
7950

8051
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
8152
{
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-
}
53+
// TODO: Provide constrained size for flow layout item nodes
54+
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
8755
}
8856

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

AsyncDisplayKitTests/ASCollectionViewTests.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ - (void)testThatItDoesNotSetALayoutInspectorForCustomLayouts
108108
{
109109
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
110110
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
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");
111+
XCTAssert(collectionView.layoutInspector == nil, @"should not set a layout delegate for custom layouts");
115112
}
116113

117114
- (void)testThatRegisteringASupplementaryNodeStoresItForIntrospection

0 commit comments

Comments
 (0)