Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Batch Fetching] Add ASBatchFetchingDelegate #281

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@
E5B077FF1E69F4EB00C24B5B /* ASElementMap.h in Headers */ = {isa = PBXBuildFile; fileRef = E5B077FD1E69F4EB00C24B5B /* ASElementMap.h */; settings = {ATTRIBUTES = (Public, ); }; };
E5B078001E69F4EB00C24B5B /* ASElementMap.m in Sources */ = {isa = PBXBuildFile; fileRef = E5B077FE1E69F4EB00C24B5B /* ASElementMap.m */; };
E5B5B9D11E9BAD9800A6B726 /* ASCollectionLayoutContext+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = E5B5B9D01E9BAD9800A6B726 /* ASCollectionLayoutContext+Private.h */; };
E5C347B11ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = E5C347B01ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h */; };
E5C347B31ECB40AA00EC4BE4 /* ASTableNode+Beta.h in Headers */ = {isa = PBXBuildFile; fileRef = E5C347B21ECB40AA00EC4BE4 /* ASTableNode+Beta.h */; };
E5E281741E71C833006B67C2 /* ASCollectionLayoutState.h in Headers */ = {isa = PBXBuildFile; fileRef = E5E281731E71C833006B67C2 /* ASCollectionLayoutState.h */; settings = {ATTRIBUTES = (Public, ); }; };
E5E281761E71C845006B67C2 /* ASCollectionLayoutState.m in Sources */ = {isa = PBXBuildFile; fileRef = E5E281751E71C845006B67C2 /* ASCollectionLayoutState.m */; };
F711994E1D20C21100568860 /* ASDisplayNodeExtrasTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F711994D1D20C21100568860 /* ASDisplayNodeExtrasTests.m */; };
Expand Down Expand Up @@ -883,6 +885,8 @@
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>"; };
E5B5B9D01E9BAD9800A6B726 /* ASCollectionLayoutContext+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASCollectionLayoutContext+Private.h"; sourceTree = "<group>"; };
E5C347B01ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASBatchFetchingDelegate.h; sourceTree = "<group>"; };
E5C347B21ECB40AA00EC4BE4 /* ASTableNode+Beta.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASTableNode+Beta.h"; sourceTree = "<group>"; };
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 @@ -1051,6 +1055,7 @@
68FC85E11CE29B7E00EDD713 /* ASTabBarController.m */,
B0F880581BEAEC7500D17647 /* ASTableNode.h */,
9CFFC6C11CCAC768006A6476 /* ASTableNode.mm */,
E5C347B21ECB40AA00EC4BE4 /* ASTableNode+Beta.h */,
055F1A3219ABD3E3004DAFF1 /* ASTableView.h */,
055F1A3319ABD3E3004DAFF1 /* ASTableView.mm */,
AC7A2C161BDE11DF0093FE1A /* ASTableViewInternal.h */,
Expand Down Expand Up @@ -1187,6 +1192,7 @@
054963481A1EA066000F8E56 /* ASBasicImageDownloader.mm */,
299DA1A71A828D2900162D41 /* ASBatchContext.h */,
299DA1A81A828D2900162D41 /* ASBatchContext.mm */,
E5C347B01ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h */,
68C215561DE10D330019C4BC /* ASCollectionViewLayoutInspector.h */,
68C215571DE10D330019C4BC /* ASCollectionViewLayoutInspector.m */,
205F0E1B1B373A2C007741D0 /* ASCollectionViewLayoutController.h */,
Expand Down Expand Up @@ -1687,6 +1693,7 @@
68EE0DBE1C1B4ED300BA1B99 /* ASMainSerialQueue.h in Headers */,
CCCCCCE11EC3EF060087FE10 /* ASTextUtilities.h in Headers */,
B350624B1B010EFD0018CF92 /* _ASPendingState.h in Headers */,
E5C347B11ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h in Headers */,
CC54A81C1D70079800296A24 /* ASDispatch.h in Headers */,
B350624D1B010EFD0018CF92 /* _ASScopeTimer.h in Headers */,
CC0F88631E4281E700576FED /* ASSupplementaryNodeSource.h in Headers */,
Expand Down Expand Up @@ -1768,6 +1775,7 @@
34EFC76E1B701CF400AD841F /* ASRatioLayoutSpec.h in Headers */,
DB55C2671C641AE4004EDCF5 /* ASContextTransitioning.h in Headers */,
CCA282C41E9EAE630037E8B7 /* ASLayerBackingTipProvider.h in Headers */,
E5C347B31ECB40AA00EC4BE4 /* ASTableNode+Beta.h in Headers */,
6900C5F41E8072DA00BCD75C /* ASImageNode+Private.h in Headers */,
68B0277B1C1A79D60041016B /* ASDisplayNode+Beta.h in Headers */,
B350622D1B010EFD0018CF92 /* ASScrollDirection.h in Headers */,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
- [ASVideoNode] Added error reporing to ASVideoNode and it's delegate [#260](https://github.com/TextureGroup/Texture/pull/260)
- [ASCollectionNode] Fixed conversion of item index paths between node & view. [Adlai Holler](https://github.com/Adlai-Holler) [#262](https://github.com/TextureGroup/Texture/pull/262)
- [Layout] Extract layout implementation code into it's own subcategories [Michael Schneider] (https://github.com/maicki)[#272](https://github.com/TextureGroup/Texture/pull/272)
- [Batch fetching] Add ASBatchFetchingDelegate that takes scroll velocity and remaining time into account [Huy Nguyen](https://github.com/nguyenhuy) [#281](https://github.com/TextureGroup/Texture/pull/281)
4 changes: 3 additions & 1 deletion Source/ASCollectionNode+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#import <AsyncDisplayKit/ASCollectionNode.h>

@protocol ASCollectionViewLayoutFacilitatorProtocol, ASCollectionLayoutDelegate;
@protocol ASCollectionViewLayoutFacilitatorProtocol, ASCollectionLayoutDelegate, ASBatchFetchingDelegate;
@class ASElementMap;

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -38,6 +38,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (strong, readonly, nullable) id<ASCollectionLayoutDelegate> layoutDelegate;

@property (nonatomic, weak) id<ASBatchFetchingDelegate> batchFetchingDelegate;

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout layoutFacilitator:(nullable id<ASCollectionViewLayoutFacilitatorProtocol>)layoutFacilitator;

- (instancetype)initWithLayoutDelegate:(id<ASCollectionLayoutDelegate>)layoutDelegate layoutFacilitator:(nullable id<ASCollectionViewLayoutFacilitatorProtocol>)layoutFacilitator;
Expand Down
11 changes: 11 additions & 0 deletions Source/ASCollectionNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ @interface ASCollectionNode ()
{
ASDN::RecursiveMutex _environmentStateLock;
Class _collectionViewClass;
id<ASBatchFetchingDelegate> _batchFetchingDelegate;
}
@property (nonatomic) _ASCollectionPendingState *pendingState;
@end
Expand Down Expand Up @@ -442,6 +443,16 @@ - (ASElementMap *)visibleElements
return nil;
}

- (void)setBatchFetchingDelegate:(id<ASBatchFetchingDelegate>)batchFetchingDelegate
{
_batchFetchingDelegate = batchFetchingDelegate;
}

- (id<ASBatchFetchingDelegate>)batchFetchingDelegate
{
return _batchFetchingDelegate;
}

#pragma mark - Range Tuning

- (ASRangeTuningParameters)tuningParametersForRangeType:(ASLayoutRangeType)rangeType
Expand Down
13 changes: 9 additions & 4 deletions Source/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASCollectionInternal.h>
#import <AsyncDisplayKit/ASCollectionLayout.h>
#import <AsyncDisplayKit/ASCollectionNode+Beta.h>
#import <AsyncDisplayKit/ASCollectionViewLayoutController.h>
#import <AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h>
#import <AsyncDisplayKit/ASCollectionViewFlowLayoutInspector.h>
Expand Down Expand Up @@ -1347,7 +1348,7 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi

if (targetContentOffset != NULL) {
ASDisplayNodeAssert(_batchContext != nil, @"Batch context should exist");
[self _beginBatchFetchingIfNeededWithContentOffset:*targetContentOffset];
[self _beginBatchFetchingIfNeededWithContentOffset:*targetContentOffset velocity:velocity];
}

if (_asyncDelegateFlags.scrollViewWillEndDragging) {
Expand Down Expand Up @@ -1521,6 +1522,10 @@ - (BOOL)canBatchFetch
}
}

- (id<ASBatchFetchingDelegate>)batchFetchingDelegate{
return self.collectionNode.batchFetchingDelegate;
}

- (void)_scheduleCheckForBatchFetchingForNumberOfChanges:(NSUInteger)changes
{
// Prevent fetching will continually trigger in a loop after reaching end of content and no new content was provided
Expand All @@ -1542,12 +1547,12 @@ - (void)_checkForBatchFetching
return;
}

[self _beginBatchFetchingIfNeededWithContentOffset:self.contentOffset];
[self _beginBatchFetchingIfNeededWithContentOffset:self.contentOffset velocity:CGPointZero];
}

- (void)_beginBatchFetchingIfNeededWithContentOffset:(CGPoint)contentOffset
- (void)_beginBatchFetchingIfNeededWithContentOffset:(CGPoint)contentOffset velocity:(CGPoint)velocity
{
if (ASDisplayShouldFetchBatchForScrollView(self, self.scrollDirection, self.scrollableDirections, contentOffset)) {
if (ASDisplayShouldFetchBatchForScrollView(self, self.scrollDirection, self.scrollableDirections, contentOffset, velocity)) {
[self _beginBatchFetching];
}
}
Expand Down
25 changes: 25 additions & 0 deletions Source/ASTableNode+Beta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// ASTableNode+Beta.h
// Texture
//
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//

#import <AsyncDisplayKit/ASTableNode.h>

@protocol ASBatchFetchingDelegate;

NS_ASSUME_NONNULL_BEGIN

@interface ASTableNode (Beta)

@property (nonatomic, weak) id<ASBatchFetchingDelegate> batchFetchingDelegate;

@end

NS_ASSUME_NONNULL_END
13 changes: 13 additions & 0 deletions Source/ASTableNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//

#import <AsyncDisplayKit/ASTableNode.h>
#import <AsyncDisplayKit/ASTableNode+Beta.h>

#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASTableViewInternal.h>
Expand Down Expand Up @@ -68,6 +70,7 @@ - (instancetype)init
@interface ASTableNode ()
{
ASDN::RecursiveMutex _environmentStateLock;
id<ASBatchFetchingDelegate> _batchFetchingDelegate;
}

@property (nonatomic, strong) _ASTablePendingState *pendingState;
Expand Down Expand Up @@ -381,6 +384,16 @@ - (BOOL)allowsMultipleSelectionDuringEditing
}
}

- (void)setBatchFetchingDelegate:(id<ASBatchFetchingDelegate>)batchFetchingDelegate
{
_batchFetchingDelegate = batchFetchingDelegate;
}

- (id<ASBatchFetchingDelegate>)batchFetchingDelegate
{
return _batchFetchingDelegate;
}

#pragma mark ASRangeControllerUpdateRangeProtocol

- (void)updateCurrentRangeWithMode:(ASLayoutRangeMode)rangeMode
Expand Down
15 changes: 10 additions & 5 deletions Source/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#import <AsyncDisplayKit/ASElementMap.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASTableNode.h>
#import <AsyncDisplayKit/ASTableNode+Beta.h>
#import <AsyncDisplayKit/ASRangeController.h>
#import <AsyncDisplayKit/ASEqualityHelpers.h>
#import <AsyncDisplayKit/ASTableLayoutController.h>
Expand Down Expand Up @@ -1225,7 +1225,7 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi

if (targetContentOffset != NULL) {
ASDisplayNodeAssert(_batchContext != nil, @"Batch context should exist");
[self _beginBatchFetchingIfNeededWithContentOffset:*targetContentOffset];
[self _beginBatchFetchingIfNeededWithContentOffset:*targetContentOffset velocity:velocity];
}

if (_asyncDelegateFlags.scrollViewWillEndDragging) {
Expand Down Expand Up @@ -1381,6 +1381,11 @@ - (BOOL)canBatchFetch
}
}

- (id<ASBatchFetchingDelegate>)batchFetchingDelegate
{
return self.tableNode.batchFetchingDelegate;
}

- (void)_scheduleCheckForBatchFetchingForNumberOfChanges:(NSUInteger)changes
{
// Prevent fetching will continually trigger in a loop after reaching end of content and no new content was provided
Expand All @@ -1402,12 +1407,12 @@ - (void)_checkForBatchFetching
return;
}

[self _beginBatchFetchingIfNeededWithContentOffset:self.contentOffset];
[self _beginBatchFetchingIfNeededWithContentOffset:self.contentOffset velocity:CGPointZero];
}

- (void)_beginBatchFetchingIfNeededWithContentOffset:(CGPoint)contentOffset
- (void)_beginBatchFetchingIfNeededWithContentOffset:(CGPoint)contentOffset velocity:(CGPoint)velocity
{
if (ASDisplayShouldFetchBatchForScrollView(self, self.scrollDirection, ASScrollDirectionVerticalDirections, contentOffset)) {
if (ASDisplayShouldFetchBatchForScrollView(self, self.scrollDirection, ASScrollDirectionVerticalDirections, contentOffset, velocity)) {
[self _beginBatchFetching];
}
}
Expand Down
27 changes: 27 additions & 0 deletions Source/Details/ASBatchFetchingDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ASBatchFetchingDelegate.h
// Texture
//
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//

#import <UIKit/UIKit.h>

@protocol ASBatchFetchingDelegate <NSObject>

/**
* @abstract Determine if batch fetching should begin based on the remaining time.
* If the delegate doesn't have enough information to confidently decide, it can take the given hint.
*
* @param remainingTime The amount of time left for user to reach the end of the scroll view's content.
*
* @param hint A hint for the delegate to fallback to.
*/
- (BOOL)shouldFetchBatchWithRemainingTime:(NSTimeInterval)remainingTime hint:(BOOL)hint;

@end
18 changes: 16 additions & 2 deletions Source/Private/ASBatchFetching.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@

ASDISPLAYNODE_EXTERN_C_BEGIN

NS_ASSUME_NONNULL_BEGIN

@class ASBatchContext;
@protocol ASBatchFetchingDelegate;

@protocol ASBatchFetchingScrollView <NSObject>

- (BOOL)canBatchFetch;
- (ASBatchContext *)batchContext;
- (CGFloat)leadingScreensForBatching;
- (nullable id<ASBatchFetchingDelegate>)batchFetchingDelegate;

@end

Expand All @@ -39,9 +43,14 @@ ASDISPLAYNODE_EXTERN_C_BEGIN
@param scrollDirection The current scrolling direction of the scroll view.
@param scrollableDirections The possible scrolling directions of the scroll view.
@param contentOffset The offset that the scrollview will scroll to.
@param velocity The velocity of the scroll view (in points) at the moment the touch was released.
@return Whether or not the current state should proceed with batch fetching.
*/
BOOL ASDisplayShouldFetchBatchForScrollView(UIScrollView<ASBatchFetchingScrollView> *scrollView, ASScrollDirection scrollDirection, ASScrollDirection scrollableDirections, CGPoint contentOffset);
BOOL ASDisplayShouldFetchBatchForScrollView(UIScrollView<ASBatchFetchingScrollView> *scrollView,
ASScrollDirection scrollDirection,
ASScrollDirection scrollableDirections,
CGPoint contentOffset,
CGPoint velocity);


/**
Expand All @@ -54,6 +63,8 @@ BOOL ASDisplayShouldFetchBatchForScrollView(UIScrollView<ASBatchFetchingScrollVi
@param targetOffset The offset that the scrollview will scroll to.
@param leadingScreens How many screens in the remaining distance will trigger batch fetching.
@param visible Whether the view is visible or not.
@param velocity The velocity of the scroll view (in points) at the moment the touch was released.
@param delegate The delegate to be consulted if needed.
@return Whether or not the current state should proceed with batch fetching.
@discussion This method is broken into a category for unit testing purposes and should be used with the ASTableView and
* ASCollectionView batch fetching API.
Expand All @@ -65,6 +76,9 @@ extern BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context,
CGSize contentSize,
CGPoint targetOffset,
CGFloat leadingScreens,
BOOL visible);
BOOL visible,
CGPoint velocity,
_Nullable id<ASBatchFetchingDelegate> delegate);

NS_ASSUME_NONNULL_END
ASDISPLAYNODE_EXTERN_C_END
Loading