From ff0e94b4e16fc0c62f7b3a4a2e32986dade7dfe1 Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Mon, 16 Nov 2015 11:36:10 -0800 Subject: [PATCH] Introduce ASInterfaceState on ASDisplayNode. Later, this will be added to ASViewController. Right now it is not hooked up to drive anything. It will become the codepath that drives calls like -fetchData and -clearContents. --- AsyncDisplayKit/ASDisplayNode.h | 22 +++++++++++++++++++ AsyncDisplayKit/ASDisplayNode.mm | 12 ++++++++++ .../Private/ASDisplayNodeInternal.h | 2 ++ 3 files changed, 36 insertions(+) diff --git a/AsyncDisplayKit/ASDisplayNode.h b/AsyncDisplayKit/ASDisplayNode.h index cf9b7a54f3..62663267ec 100644 --- a/AsyncDisplayKit/ASDisplayNode.h +++ b/AsyncDisplayKit/ASDisplayNode.h @@ -32,6 +32,27 @@ typedef CALayer *(^ASDisplayNodeLayerBlock)(); */ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node); +/** + Interface state is available on ASDisplayNode and ASViewController, and + allows checking whether a node is in an interface situation where it is prudent to trigger certain + actions: measurement, data fetching, display, and visibility (the latter for animations or other onscreen-only effects). + */ + +typedef NS_OPTIONS(NSUInteger, ASInterfaceState) +{ + /** The element is not predicted to be onscreen soon and preloading should not be performed */ + ASInterfaceStateNone = 1 << 0, + /** The element may be added to a view soon that could become visible. Measure the layout, including size calculation. */ + ASInterfaceStateMeasureLayout = 1 << 1, + /** The element is likely enough to come onscreen that disk and/or network data required for display should be fetched. */ + ASInterfaceStateFetchData = 1 << 2, + /** The elemant is very likely to become visible, and concurrent rendering should be executed for any -setNeedsDisplay. */ + ASInterfaceStateDisplay = 1 << 3, + /** The element is physically onscreen by at least 1 pixel. + In practice, all other bit fields should also be set when this flag is set. */ + ASInterfaceStateVisible = 1 << 4, +}; + /** * An `ASDisplayNode` is an abstraction over `UIView` and `CALayer` that allows you to perform calculations about a view * hierarchy off the main thread, and could do rendering off the main thread as well. @@ -160,6 +181,7 @@ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node); */ @property (nonatomic, readonly, retain) CALayer *layer; +@property (nonatomic, readwrite) ASInterfaceState interfaceState; /** @name Managing dimensions */ diff --git a/AsyncDisplayKit/ASDisplayNode.mm b/AsyncDisplayKit/ASDisplayNode.mm index e7c6e54c0b..394c050128 100644 --- a/AsyncDisplayKit/ASDisplayNode.mm +++ b/AsyncDisplayKit/ASDisplayNode.mm @@ -1684,6 +1684,18 @@ void recursivelyEnsureDisplayForLayer(CALayer *layer) [self clearFetchedData]; } +- (ASInterfaceState)interfaceState +{ + return _interfaceState; +} + +- (void)setInterfaceState:(ASInterfaceState)interfaceState +{ + if (interfaceState != _interfaceState) { + _interfaceState = interfaceState; + } +} + - (void)layout { ASDisplayNodeAssertMainThread(); diff --git a/AsyncDisplayKit/Private/ASDisplayNodeInternal.h b/AsyncDisplayKit/Private/ASDisplayNodeInternal.h index cf0681b72b..4953a5b538 100644 --- a/AsyncDisplayKit/Private/ASDisplayNodeInternal.h +++ b/AsyncDisplayKit/Private/ASDisplayNodeInternal.h @@ -72,6 +72,8 @@ typedef NS_OPTIONS(NSUInteger, ASDisplayNodeMethodOverrides) { NSMutableSet *_pendingDisplayNodes; _ASPendingState *_pendingViewState; + + ASInterfaceState _interfaceState; struct ASDisplayNodeFlags { // public properties