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

Commit

Permalink
Address other comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy committed Apr 3, 2017
1 parent cb91a60 commit 49d1262
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 129 deletions.
14 changes: 3 additions & 11 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@
E597E3C61E817B2800451596 /* ASCollectionLayout+Subclasses.h in Headers */ = {isa = PBXBuildFile; fileRef = E597E3C51E817B2800451596 /* ASCollectionLayout+Subclasses.h */; };
E597E3C81E817FB500451596 /* ASDataControllerLayoutContext.h in Headers */ = {isa = PBXBuildFile; fileRef = E597E3C71E817FB500451596 /* ASDataControllerLayoutContext.h */; };
E597E3CA1E81800500451596 /* ASDataControllerLayoutContext.m in Sources */ = {isa = PBXBuildFile; fileRef = E597E3C91E81800500451596 /* ASDataControllerLayoutContext.m */; };
E5ABAC761E8551B3007AC15C /* ASCollectionLayoutHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = E5ABAC751E8551B3007AC15C /* ASCollectionLayoutHelpers.h */; };
E5ABAC781E85528A007AC15C /* ASCollectionLayoutHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = E5ABAC771E85528A007AC15C /* ASCollectionLayoutHelpers.m */; };
E5ABAC7B1E8564EE007AC15C /* ASRectTable.h in Headers */ = {isa = PBXBuildFile; fileRef = E5ABAC791E8564EE007AC15C /* ASRectTable.h */; };
E5ABAC7C1E8564EE007AC15C /* ASRectTable.m in Sources */ = {isa = PBXBuildFile; fileRef = E5ABAC7A1E8564EE007AC15C /* ASRectTable.m */; };
E5B077E61E6843A600C24B5B /* ASCollectionLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = E5B077E11E6843A600C24B5B /* ASCollectionLayout.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -786,19 +784,17 @@
E5711A2D1C840C96009619D4 /* ASCollectionElement.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASCollectionElement.mm; sourceTree = "<group>"; };
E579BC961E84203D00E24C78 /* ASCollectionNode+FrameworkPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASCollectionNode+FrameworkPrivate.h"; sourceTree = "<group>"; };
E597E3C31E81748900451596 /* ASDataController+Beta.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASDataController+Beta.h"; sourceTree = "<group>"; };
E597E3C51E817B2800451596 /* ASCollectionLayout+Subclasses.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASCollectionLayout+Subclasses.h"; sourceTree = "<group>"; };
E597E3C51E817B2800451596 /* ASCollectionLayout+Subclasses.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "ASCollectionLayout+Subclasses.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
E597E3C71E817FB500451596 /* ASDataControllerLayoutContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASDataControllerLayoutContext.h; sourceTree = "<group>"; };
E597E3C91E81800500451596 /* ASDataControllerLayoutContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASDataControllerLayoutContext.m; sourceTree = "<group>"; };
E5ABAC751E8551B3007AC15C /* ASCollectionLayoutHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionLayoutHelpers.h; sourceTree = "<group>"; };
E5ABAC771E85528A007AC15C /* ASCollectionLayoutHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASCollectionLayoutHelpers.m; sourceTree = "<group>"; };
E5ABAC791E8564EE007AC15C /* ASRectTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASRectTable.h; sourceTree = "<group>"; };
E5ABAC7A1E8564EE007AC15C /* ASRectTable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASRectTable.m; sourceTree = "<group>"; };
E5B077E11E6843A600C24B5B /* ASCollectionLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionLayout.h; sourceTree = "<group>"; };
E5B077F41E68576B00C24B5B /* ASCollectionLayout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASCollectionLayout.mm; sourceTree = "<group>"; };
E5B077F41E68576B00C24B5B /* ASCollectionLayout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASCollectionLayout.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
E5B077FD1E69F4EB00C24B5B /* ASElementMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASElementMap.h; sourceTree = "<group>"; };
E5B077FE1E69F4EB00C24B5B /* ASElementMap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASElementMap.m; sourceTree = "<group>"; };
E5B0780A1E6A01E200C24B5B /* ASCollectionFlowLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionFlowLayout.h; sourceTree = "<group>"; };
E5B0780B1E6A01E200C24B5B /* ASCollectionFlowLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASCollectionFlowLayout.m; sourceTree = "<group>"; };
E5B0780B1E6A01E200C24B5B /* ASCollectionFlowLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ASCollectionFlowLayout.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
E5E281731E71C833006B67C2 /* ASCollectionLayoutState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASCollectionLayoutState.h; sourceTree = "<group>"; };
E5E281751E71C845006B67C2 /* ASCollectionLayoutState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASCollectionLayoutState.m; sourceTree = "<group>"; };
EFA731F0396842FF8AB635EE /* libPods-AsyncDisplayKitTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AsyncDisplayKitTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -1420,8 +1416,6 @@
E5B077F41E68576B00C24B5B /* ASCollectionLayout.mm */,
E5B0780A1E6A01E200C24B5B /* ASCollectionFlowLayout.h */,
E5B0780B1E6A01E200C24B5B /* ASCollectionFlowLayout.m */,
E5ABAC751E8551B3007AC15C /* ASCollectionLayoutHelpers.h */,
E5ABAC771E85528A007AC15C /* ASCollectionLayoutHelpers.m */,
);
name = "Collection Layout";
sourceTree = "<group>";
Expand Down Expand Up @@ -1548,7 +1542,6 @@
B35062521B010EFD0018CF92 /* ASDisplayNodeInternal.h in Headers */,
AC7A2C181BDE11DF0093FE1A /* ASTableViewInternal.h in Headers */,
B35062531B010EFD0018CF92 /* ASImageNode+CGExtras.h in Headers */,
E5ABAC761E8551B3007AC15C /* ASCollectionLayoutHelpers.h in Headers */,
254C6B7F1BF94DF4003EC431 /* ASTextKitTruncating.h in Headers */,
CC58AA4B1E398E1D002C8CB4 /* ASBlockTypes.h in Headers */,
6977965F1D8AC8D3007E93D7 /* ASLayoutSpec+Subclasses.h in Headers */,
Expand Down Expand Up @@ -1994,7 +1987,6 @@
DB78412E1C6BCE1600A9E2B4 /* _ASTransitionContext.m in Sources */,
B350620B1B010EFD0018CF92 /* ASTableView.mm in Sources */,
B350620E1B010EFD0018CF92 /* ASTextNode.mm in Sources */,
E5ABAC781E85528A007AC15C /* ASCollectionLayoutHelpers.m in Sources */,
6959433F1D70815300B0EE1F /* ASDisplayNodeLayout.mm in Sources */,
68355B3E1CB57A60001D4E68 /* ASPINRemoteImageDownloader.m in Sources */,
CC034A141E649F1300626263 /* AsyncDisplayKit+IGListKitMethods.m in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions Source/Details/ASCollectionFlowLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
#import <AsyncDisplayKit/ASCollectionLayout.h>
#import <AsyncDisplayKit/ASScrollDirection.h>

NS_ASSUME_NONNULL_BEGIN

AS_SUBCLASSING_RESTRICTED

@interface ASCollectionFlowLayout : ASCollectionLayout

- (instancetype)initWithScrollableDirections:(ASScrollDirection)scrollableDirections;

@end

NS_ASSUME_NONNULL_END
37 changes: 2 additions & 35 deletions Source/Details/ASCollectionFlowLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#import <AsyncDisplayKit/ASCellNode.h>
#import <AsyncDisplayKit/ASCollectionLayoutState.h>
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASCollectionLayoutHelpers.h>
#import <AsyncDisplayKit/ASDataControllerLayoutContext.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASLayout.h>
Expand Down Expand Up @@ -53,7 +52,7 @@ - (ASSizeRange)sizeRangeThatFits:(CGSize)viewportSize
return sizeRange;
}

- (ASCollectionLayoutState *)calculateLayoutForLayoutContext:(ASDataControllerLayoutContext *)context
- (ASCollectionLayoutState *)calculateLayoutWithContext:(ASDataControllerLayoutContext *)context
{
ASElementMap *elementMap = context.elementMap;
NSMutableArray<ASCellNode *> *children = ASArrayByFlatMapping(elementMap.itemElements, ASCollectionElement *element, element.node);
Expand All @@ -72,39 +71,7 @@ - (ASCollectionLayoutState *)calculateLayoutForLayoutContext:(ASDataControllerLa
children:children];
stackSpec.concurrent = YES;
ASLayout *layout = [stackSpec layoutThatFits:[self sizeRangeThatFits:context.viewportSize]];
return ASLayoutToCollectionContentAttributes(layout, elementMap);
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributesInRect = [NSMutableArray array];
NSMapTable *attrsMap = self.currentContentAttributes.elementToLayoutArrtibutesMap;
for (ASCollectionElement *element in attrsMap) {
UICollectionViewLayoutAttributes *attrs = [attrsMap objectForKey:element];
if (CGRectIntersectsRect(rect, attrs.frame)) {
[attributesInRect addObject:attrs];
}
}
return attributesInRect;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = self.currentContentAttributes;
ASCollectionElement *element = [state.elementMap elementForItemAtIndexPath:indexPath];
return [state.elementToLayoutArrtibutesMap objectForKey:element];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = self.currentContentAttributes;
ASCollectionElement *element = [state.elementMap supplementaryElementOfKind:elementKind atIndexPath:indexPath];
return [state.elementToLayoutArrtibutesMap objectForKey:element];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
return [self layoutAttributesForSupplementaryViewOfKind:elementKind atIndexPath:indexPath];
return [[ASCollectionLayoutState alloc] initWithElementMap:elementMap layout:layout];
}

@end
5 changes: 1 addition & 4 deletions Source/Details/ASCollectionLayout+Subclasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ NS_ASSUME_NONNULL_BEGIN

@interface ASCollectionLayout () <ASDataControllerLayoutDelegate>

/// The current state of this layout object, if any. This property must be accessed on main thread.
@property (nonatomic, strong, nullable) ASCollectionLayoutState *state;

/**
* @abstract Prepares and returns a new layout for given context.
*
Expand All @@ -32,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
*
* @discussion This method must block its calling thread but can dispatch to other theads to reduce blocking time.
*/
- (ASCollectionLayoutState *)calculateLayoutForLayoutContext:(ASDataControllerLayoutContext *)context;
- (ASCollectionLayoutState *)calculateLayoutWithContext:(ASDataControllerLayoutContext *)context;

@end

Expand Down
2 changes: 0 additions & 2 deletions Source/Details/ASCollectionLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, weak) ASCollectionNode *collectionNode;

- (instancetype)init NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithCoder:(NSCoder *)aDecoder __unavailable;

@end
Expand Down
53 changes: 31 additions & 22 deletions Source/Details/ASCollectionLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,6 @@ @interface ASCollectionLayout () {

@implementation ASCollectionLayout

- (instancetype)init
{
return [super init];
}

- (ASCollectionLayoutState *)state
{
ASDisplayNodeAssertMainThread();
return _state;
}

- (void)setState:(ASCollectionLayoutState *)newState
{
ASDisplayNodeAssertMainThread();
if (! ASObjectIsEqual(_state, newState)) {
_state = newState;
}
}

#pragma mark - ASDataControllerLayoutDelegate

- (ASDataControllerLayoutContext *)layoutContextWithElementMap:(ASElementMap *)map
Expand All @@ -64,7 +45,7 @@ - (ASDataControllerLayoutContext *)layoutContextWithElementMap:(ASElementMap *)m

- (void)prepareLayoutForLayoutContext:(ASDataControllerLayoutContext *)context
{
ASCollectionLayoutState *state = [self calculateLayoutForLayoutContext:context];
ASCollectionLayoutState *state = [self calculateLayoutWithContext:context];

ASDN::MutexLocker l(__instanceLock__);
_pendingState = state;
Expand All @@ -90,7 +71,7 @@ - (void)prepareLayout
}

if (state == nil) {
state = [self calculateLayoutForLayoutContext:context];
state = [self calculateLayoutWithContext:context];
}

_state = state;
Expand All @@ -106,12 +87,40 @@ - (void)invalidateLayout
- (CGSize)collectionViewContentSize
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssertNotNil(_state, @"Collection layout state should not be nil at this point");
return _state.contentSize;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributesInRect = [NSMutableArray array];
NSMapTable *attrsMap = _state.elementToLayoutArrtibutesMap;
for (ASCollectionElement *element in attrsMap) {
UICollectionViewLayoutAttributes *attrs = [attrsMap objectForKey:element];
if (CGRectIntersectsRect(rect, attrs.frame)) {
[attributesInRect addObject:attrs];
}
}
return attributesInRect;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = _state;
ASCollectionElement *element = [state.elementMap elementForItemAtIndexPath:indexPath];
return [state.elementToLayoutArrtibutesMap objectForKey:element];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
ASCollectionLayoutState *state = _state;
ASCollectionElement *element = [state.elementMap supplementaryElementOfKind:elementKind atIndexPath:indexPath];
return [state.elementToLayoutArrtibutesMap objectForKey:element];
}

#pragma mark - Subclass hooks

- (ASCollectionLayoutState *)calculateLayoutForLayoutContext:(ASDataControllerLayoutContext *)context
- (ASCollectionLayoutState *)calculateLayoutWithContext:(ASDataControllerLayoutContext *)context
{
// Subclass hooks
ASDisplayNodeAssertLockUnownedByCurrentThread(__instanceLock__);
Expand Down
13 changes: 0 additions & 13 deletions Source/Details/ASCollectionLayoutHelpers.h

This file was deleted.

37 changes: 0 additions & 37 deletions Source/Details/ASCollectionLayoutHelpers.m

This file was deleted.

16 changes: 14 additions & 2 deletions Source/Details/ASCollectionLayoutState.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import <UIKit/UIKit.h>
#import <AsyncDisplayKit/ASBaseDefines.h>

@class ASElementMap, ASCollectionElement;
@class ASElementMap, ASCollectionElement, ASLayout;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -33,10 +33,22 @@ AS_SUBCLASSING_RESTRICTED
*
* @param contentSize The content size of the collection's layout
*
* @param elementToLayoutArrtibutesMap Map between elements to their layout attributes. The map may contain all elements, or a subset of them and to be updated later. Should use weak pointers for elements.
* @param elementToLayoutArrtibutesMap Map between elements to their layout attributes. The map may contain all elements, or a subset of them and will be updated later.
* Also, it should have NSMapTableObjectPointerPersonality and NSMapTableWeakMemory as key options.
*/
- (instancetype)initWithElementMap:(ASElementMap *)elementMap contentSize:(CGSize)contentSize elementToLayoutArrtibutesMap:(NSMapTable<ASCollectionElement *, UICollectionViewLayoutAttributes *> *)attrsMap NS_DESIGNATED_INITIALIZER;

/**
* Convenience initializer.
*
* @param elementMap The element map used to calculate this object
*
* @param layout The layout describes size and position of all elements, or a subset of them and will be updated later.
*
* @discussion The sublayouts that describe position of elements must be direct children of the root layout object parameter.
*/
- (instancetype)initWithElementMap:(ASElementMap *)elementMap layout:(ASLayout *)layout;

@end

NS_ASSUME_NONNULL_END
26 changes: 26 additions & 0 deletions Source/Details/ASCollectionLayoutState.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,37 @@
//

#import <AsyncDisplayKit/ASCollectionLayoutState.h>

#import <AsyncDisplayKit/ASAssert.h>
#import <AsyncDisplayKit/ASCellNode+Internal.h>
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASLayout.h>

@implementation ASCollectionLayoutState

- (instancetype)initWithElementMap:(ASElementMap *)elementMap layout:(ASLayout *)layout
{
NSMapTable<ASCollectionElement *, UICollectionViewLayoutAttributes *> *attrsMap = [NSMapTable mapTableWithKeyOptions:(NSMapTableObjectPointerPersonality | NSMapTableWeakMemory) valueOptions:NSMapTableStrongMemory];
for (ASLayout *sublayout in layout.sublayouts) {
ASCollectionElement *element = ((ASCellNode *)sublayout.layoutElement).collectionElement;
NSIndexPath *indexPath = [elementMap indexPathForElement:element];
NSString *supplementaryElementKind = element.supplementaryElementKind;

UICollectionViewLayoutAttributes *attrs;
if (supplementaryElementKind == nil) {
attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
} else {
attrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:supplementaryElementKind withIndexPath:indexPath];
}

attrs.frame = sublayout.frame;
[attrsMap setObject:attrs forKey:element];
}

return [self initWithElementMap:elementMap contentSize:layout.size elementToLayoutArrtibutesMap:attrsMap];
}

- (instancetype)initWithElementMap:(ASElementMap *)elementMap contentSize:(CGSize)contentSize elementToLayoutArrtibutesMap:(NSMapTable<ASCollectionElement *,UICollectionViewLayoutAttributes *> *)attrsMap
{
self = [super init];
Expand Down
Loading

0 comments on commit 49d1262

Please sign in to comment.