Revert "[ASCollectionView] Add a "Null Object" for a layout inspector if custom layout is given but no layout inspector" (#1713)

This commit is contained in:
appleguy
2016-06-04 22:49:10 -07:00
parent 5289b1e11e
commit 4804f429b9
4 changed files with 40 additions and 73 deletions

View File

@@ -98,7 +98,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
ASCollectionDataController *_dataController; ASCollectionDataController *_dataController;
ASRangeController *_rangeController; ASRangeController *_rangeController;
ASCollectionViewLayoutController *_layoutController; ASCollectionViewLayoutController *_layoutController;
id<ASCollectionViewLayoutInspecting> _defaultLayoutInspector; ASCollectionViewFlowLayoutInspector *_flowLayoutInspector;
NSMutableSet *_cellsForVisibilityUpdates; NSMutableSet *_cellsForVisibilityUpdates;
id<ASCollectionViewLayoutFacilitatorProtocol> _layoutFacilitator; id<ASCollectionViewLayoutFacilitatorProtocol> _layoutFacilitator;
@@ -246,19 +246,11 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
// and should not trigger a relayout. // and should not trigger a relayout.
_ignoreMaxSizeChange = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero); _ignoreMaxSizeChange = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero);
// Register the default layout inspector delegate for flow layouts only, custom layouts
// will need to roll their own ASCollectionViewLayoutInspecting implementation and set a layout delegate
if ([layout asdk_isFlowLayout]) { if ([layout asdk_isFlowLayout]) {
// Register the default layout inspector delegate for flow layouts only _layoutInspector = [self flowLayoutInspector];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
ASDisplayNodeAssertNotNil(layout, @"Collection view layout must be a flow layout to use the built-in inspector");
_defaultLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] initWithCollectionView:self flowLayout:layout];
} else {
// Custom layouts will need to roll their own ASCollectionViewLayoutInspecting implementation and set a layout
// delegate. In the meantime ASDK provides a custom layout inspector that does not provide any implementation
// and asserts for methods that should be implemented in the <ASCollectionViewLayoutInspecting>
_defaultLayoutInspector = [[ASCollectionViewCustomLayoutInspector alloc] init];
} }
_layoutInspector = _defaultLayoutInspector;
_layoutFacilitator = layoutFacilitator; _layoutFacilitator = layoutFacilitator;
_proxyDelegate = [[ASCollectionViewProxy alloc] initWithTarget:nil interceptor:self]; _proxyDelegate = [[ASCollectionViewProxy alloc] initWithTarget:nil interceptor:self];
@@ -285,6 +277,19 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
[self setAsyncDataSource:nil]; [self setAsyncDataSource:nil];
} }
/**
* A layout inspector implementation specific for the sizing behavior of UICollectionViewFlowLayouts
*/
- (ASCollectionViewFlowLayoutInspector *)flowLayoutInspector
{
if (_flowLayoutInspector == nil) {
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
ASDisplayNodeAssertNotNil(layout, @"Collection view layout must be a flow layout to use the built-in inspector");
_flowLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] initWithCollectionView:self flowLayout:layout];
}
return _flowLayoutInspector;
}
#pragma mark - #pragma mark -
#pragma mark Overrides. #pragma mark Overrides.
@@ -404,9 +409,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
super.delegate = (id<UICollectionViewDelegate>)_proxyDelegate; super.delegate = (id<UICollectionViewDelegate>)_proxyDelegate;
if ([_layoutInspector respondsToSelector:@selector(didChangeCollectionViewDelegate:)]) { [_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
[_layoutInspector didChangeCollectionViewDelegate:asyncDelegate];
}
} }
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeType:(ASLayoutRangeType)rangeType - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeType:(ASLayoutRangeType)rangeType
@@ -888,21 +891,28 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{ {
ASSizeRange constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath]; ASSizeRange constrainedSize = kInvalidSizeRange;
if (_layoutInspector) {
constrainedSize = [_layoutInspector collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
}
if (!ASSizeRangeEqualToSizeRange(constrainedSize, kInvalidSizeRange)) { if (!ASSizeRangeEqualToSizeRange(constrainedSize, kInvalidSizeRange)) {
return constrainedSize; return constrainedSize;
} }
CGSize maxSize = _maxSizeForNodesConstrainedSize; // TODO: Move this logic into the flow layout inspector. Create a simple inspector for non-flow layouts that don't
if (CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero)) { // implement a custom inspector.
maxSize = self.bounds.size; if (_asyncDataSourceFlags.asyncDataSourceConstrainedSizeForNode) {
} constrainedSize = [_asyncDataSource collectionView:self constrainedSizeForNodeAtIndexPath:indexPath];
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
maxSize.width = FLT_MAX;
} else { } else {
maxSize.height = FLT_MAX; CGSize maxSize = CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, CGSizeZero) ? self.bounds.size : _maxSizeForNodesConstrainedSize;
if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) {
maxSize.width = FLT_MAX;
} else {
maxSize.height = FLT_MAX;
}
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
} }
constrainedSize = ASSizeRangeMake(CGSizeZero, maxSize);
return constrainedSize; return constrainedSize;
} }
@@ -964,16 +974,19 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{ {
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return [_layoutInspector collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath]; return [_layoutInspector collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
} }
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section - (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
{ {
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return [_layoutInspector collectionView:self supplementaryNodesOfKind:kind inSection:section]; return [_layoutInspector collectionView:self supplementaryNodesOfKind:kind inSection:section];
} }
- (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind; - (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind;
{ {
ASDisplayNodeAssert(_layoutInspector != nil, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return [_layoutInspector collectionView:self numberOfSectionsForSupplementaryNodeOfKind:kind]; return [_layoutInspector collectionView:self numberOfSectionsForSupplementaryNodeOfKind:kind];
} }

View File

@@ -47,17 +47,6 @@
@end @end
/**
* Simple "Null Object" inspector for non-flow layouts that does not implement a custom inspector, provides a zero
* constrained size and throws an exception if methods are called from <ASCollectionViewLayoutInspecting>
*/
@interface ASCollectionViewCustomLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
@end
/**
* A layout inspector implementation specific for the sizing behavior of UICollectionViewFlowLayouts
*/
@interface ASCollectionViewFlowLayoutInspector : NSObject <ASCollectionViewLayoutInspecting> @interface ASCollectionViewFlowLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
@property (nonatomic, weak) UICollectionViewFlowLayout *layout; @property (nonatomic, weak) UICollectionViewFlowLayout *layout;

View File

@@ -13,35 +13,6 @@
#import "ASAssert.h" #import "ASAssert.h"
#import "ASEqualityHelpers.h" #import "ASEqualityHelpers.h"
@implementation ASCollectionViewCustomLayoutInspector
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
}
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
}
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind
{
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return 0;
}
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
{
ASDisplayNodeAssert(NO, @"To support supplementary nodes in ASCollectionView, it must have a layoutDelegate for layout inspection. (See ASCollectionViewFlowLayoutInspector for an example.)");
return 0;
}
@end
@implementation ASCollectionViewFlowLayoutInspector { @implementation ASCollectionViewFlowLayoutInspector {
BOOL _delegateImplementsReferenceSizeForHeader; BOOL _delegateImplementsReferenceSizeForHeader;
BOOL _delegateImplementsReferenceSizeForFooter; BOOL _delegateImplementsReferenceSizeForFooter;
@@ -79,11 +50,8 @@
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{ {
if ([collectionView.asyncDataSource respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)]) { // TODO: Provide constrained size for flow layout item nodes
return [collectionView.asyncDataSource collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]; return ASSizeRangeMake(CGSizeZero, CGSizeZero);
} else {
return ASSizeRangeMake(_layout.itemSize, _layout.itemSize);
}
} }
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

View File

@@ -108,10 +108,7 @@
{ {
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init]; UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
XCTAssert(collectionView.layoutInspector != nil, @"should automatically set a layout delegate for custom layouts"); XCTAssert(collectionView.layoutInspector == nil, @"should not set a layout delegate for custom layouts");
XCTAssert([collectionView.layoutInspector isKindOfClass:[ASCollectionViewCustomLayoutInspector class]], @"should have a internal custom layout inspector by default");
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");
XCTAssertThrows([collectionView.layoutInspector collectionView:collectionView supplementaryNodesOfKind:UICollectionElementKindSectionHeader inSection:0], @"should throw an exception for <ASCollectionViewLayoutInspecting> methods");
} }
- (void)testThatRegisteringASupplementaryNodeStoresItForIntrospection - (void)testThatRegisteringASupplementaryNodeStoresItForIntrospection