From d4b1f625aaa3bf85989f30aee809f116a92db57e Mon Sep 17 00:00:00 2001 From: Huy Nguyen Date: Fri, 8 Sep 2017 17:05:06 +0100 Subject: [PATCH] [Gallery layout] Include the caller in properties providing methods (#533) * Include the caller in ASCollectionGalleryLayoutPropertiesProviding's methods * Update CHANGELOG.md --- CHANGELOG.md | 1 + Source/ASPagerNode.m | 2 +- .../Details/ASCollectionGalleryLayoutDelegate.h | 17 +++++++++++++---- .../ASCollectionGalleryLayoutDelegate.mm | 14 +++++++------- .../ASCollectionView/Sample/ViewController.m | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f38cdc875..1553fc74cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - [Breaking] Remove APIs that have been deprecated since 2.0 and/or for at least 6 months [Huy Nguyen](https://github.com/nguyenhuy) [#529](https://github.com/TextureGroup/Texture/pull/529) - [ASDisplayNode] Ensure `-displayWillStartAsynchronously:` and `-displayDidFinish` are invoked on rasterized subnodes. [Eric Scheers](https://github.com/smeis) [#532](https://github.com/TextureGroup/Texture/pull/532) - Fixed a memory corruption issue in the ASImageNode display system. [Adlai Holler](https://github.com/Adlai-Holler) [#555](https://github.com/TextureGroup/Texture/pull/555) +- [Breaking] Rename ASCollectionGalleryLayoutSizeProviding to ASCollectionGalleryLayoutPropertiesProviding. Besides a fixed item size, it now can provide interitem and line spacings, as well as section inset [Huy Nguyen](https://github.com/nguyenhuy) [#496](https://github.com/TextureGroup/Texture/pull/496) [#533](https://github.com/TextureGroup/Texture/pull/533) ##2.4 - Fix an issue where inserting/deleting sections could lead to inconsistent supplementary element behavior. [Adlai Holler](https://github.com/Adlai-Holler) diff --git a/Source/ASPagerNode.m b/Source/ASPagerNode.m index 1ef893d9d6..4d8195a179 100644 --- a/Source/ASPagerNode.m +++ b/Source/ASPagerNode.m @@ -145,7 +145,7 @@ #pragma mark - ASCollectionGalleryLayoutPropertiesProviding -- (CGSize)sizeForElements:(ASElementMap *)elements +- (CGSize)galleryLayoutDelegate:(nonnull ASCollectionGalleryLayoutDelegate *)delegate sizeForElements:(nonnull ASElementMap *)elements { ASDisplayNodeAssertMainThread(); return [self pageSize]; diff --git a/Source/Details/ASCollectionGalleryLayoutDelegate.h b/Source/Details/ASCollectionGalleryLayoutDelegate.h index dfd0f5c87d..99958d1c97 100644 --- a/Source/Details/ASCollectionGalleryLayoutDelegate.h +++ b/Source/Details/ASCollectionGalleryLayoutDelegate.h @@ -14,6 +14,7 @@ #import @class ASElementMap; +@class ASCollectionGalleryLayoutDelegate; NS_ASSUME_NONNULL_BEGIN @@ -24,11 +25,13 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion This method will only be called on main thread. * + * @param delegate The calling object. + * * @param elements All elements to be sized. * * @return The elements' size */ -- (CGSize)sizeForElements:(ASElementMap *)elements; +- (CGSize)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate sizeForElements:(ASElementMap *)elements; @optional @@ -42,11 +45,13 @@ NS_ASSUME_NONNULL_BEGIN * It is not applied between the first line and the header, or between the last line and the footer. * This is the same behavior as UICollectionViewFlowLayout's minimumLineSpacing. * + * @param delegate The calling object. + * * @param elements All elements in the layout. * * @return The interitem spacing */ -- (CGFloat)minimumLineSpacingForElements:(ASElementMap *)elements; +- (CGFloat)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate minimumLineSpacingForElements:(ASElementMap *)elements; /** * Returns the minumum spacing to use between items in the same row or column, depending on the scroll directions. @@ -58,22 +63,26 @@ NS_ASSUME_NONNULL_BEGIN * It is considered while fitting items into lines, but the actual final spacing between some items might be larger. * This is the same behavior as UICollectionViewFlowLayout's minimumInteritemSpacing. * + * @param delegate The calling object. + * * @param elements All elements in the layout. * * @return The interitem spacing */ -- (CGFloat)minimumInteritemSpacingForElements:(ASElementMap *)elements; +- (CGFloat)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate minimumInteritemSpacingForElements:(ASElementMap *)elements; /** * Returns the margins of each section. * * @discussion This method will only be called on main thread. * + * @param delegate The calling object. + * * @param elements All elements in the layout. * * @return The margins used to layout content in a section */ -- (UIEdgeInsets)sectionInsetForElements:(ASElementMap *)elements; +- (UIEdgeInsets)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate sectionInsetForElements:(ASElementMap *)elements; @end diff --git a/Source/Details/ASCollectionGalleryLayoutDelegate.mm b/Source/Details/ASCollectionGalleryLayoutDelegate.mm index a01fdf7f76..2734e9649a 100644 --- a/Source/Details/ASCollectionGalleryLayoutDelegate.mm +++ b/Source/Details/ASCollectionGalleryLayoutDelegate.mm @@ -66,9 +66,9 @@ _propertiesProviderFlags = {}; } else { _propertiesProvider = propertiesProvider; - _propertiesProviderFlags.minimumLineSpacingForElements = [_propertiesProvider respondsToSelector:@selector(minimumLineSpacingForElements:)]; - _propertiesProviderFlags.minimumInteritemSpacingForElements = [_propertiesProvider respondsToSelector:@selector(minimumInteritemSpacingForElements:)]; - _propertiesProviderFlags.sectionInsetForElements = [_propertiesProvider respondsToSelector:@selector(sectionInsetForElements:)]; + _propertiesProviderFlags.minimumLineSpacingForElements = [_propertiesProvider respondsToSelector:@selector(galleryLayoutDelegate:minimumLineSpacingForElements:)]; + _propertiesProviderFlags.minimumInteritemSpacingForElements = [_propertiesProvider respondsToSelector:@selector(galleryLayoutDelegate:minimumInteritemSpacingForElements:)]; + _propertiesProviderFlags.sectionInsetForElements = [_propertiesProvider respondsToSelector:@selector(galleryLayoutDelegate:sectionInsetForElements:)]; } } @@ -80,10 +80,10 @@ return nil; } - CGSize itemSize = [propertiesProvider sizeForElements:elements]; - UIEdgeInsets sectionInset = _propertiesProviderFlags.sectionInsetForElements ? [propertiesProvider sectionInsetForElements:elements] : UIEdgeInsetsZero; - CGFloat lineSpacing = _propertiesProviderFlags.minimumLineSpacingForElements ? [propertiesProvider minimumLineSpacingForElements:elements] : 0.0; - CGFloat interitemSpacing = _propertiesProviderFlags.minimumInteritemSpacingForElements ? [propertiesProvider minimumInteritemSpacingForElements:elements] : 0.0; + CGSize itemSize = [propertiesProvider galleryLayoutDelegate:self sizeForElements:elements]; + UIEdgeInsets sectionInset = _propertiesProviderFlags.sectionInsetForElements ? [propertiesProvider galleryLayoutDelegate:self sectionInsetForElements:elements] : UIEdgeInsetsZero; + CGFloat lineSpacing = _propertiesProviderFlags.minimumLineSpacingForElements ? [propertiesProvider galleryLayoutDelegate:self minimumLineSpacingForElements:elements] : 0.0; + CGFloat interitemSpacing = _propertiesProviderFlags.minimumInteritemSpacingForElements ? [propertiesProvider galleryLayoutDelegate:self minimumInteritemSpacingForElements:elements] : 0.0; return [[_ASCollectionGalleryLayoutInfo alloc] initWithItemSize:itemSize minimumLineSpacing:lineSpacing minimumInteritemSpacing:interitemSpacing diff --git a/examples/ASCollectionView/Sample/ViewController.m b/examples/ASCollectionView/Sample/ViewController.m index fa093f80ad..3755b01d69 100644 --- a/examples/ASCollectionView/Sample/ViewController.m +++ b/examples/ASCollectionView/Sample/ViewController.m @@ -112,7 +112,7 @@ #pragma mark - ASCollectionGalleryLayoutPropertiesProviding -- (CGSize)sizeForElements:(ASElementMap *)elements +- (CGSize)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate sizeForElements:(ASElementMap *)elements { ASDisplayNodeAssertMainThread(); return CGSizeMake(180, 90);