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

Commit

Permalink
[ASViewController] Support optional node (#3021)
Browse files Browse the repository at this point in the history
* ASViewController can be used without a node
- If a node isn't provided by developers via -initWithNode:, a default one will be created and used internally.
- This allows developers to use ASViewController like a normal UIViewController and as a base class for all view controllers among which some use a node hierarchy and some don't.

* Update ASDKgram to use a shared base ASViewController

* Minor fixes in ASViewController:
- If its node isn't provided by users, don't replace the view controller's view with the default node's view because it might be loaded from a nib.
- Init a vanilla ASDisplayNode if a node isn't provided.

* Some smaller cleanup

* Remove dummy node for ASViewController if it’s used without a node
  • Loading branch information
maicki authored Feb 14, 2017
1 parent cd448a1 commit aecd36a
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 207 deletions.
29 changes: 15 additions & 14 deletions AsyncDisplayKit/ASViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,34 @@ NS_ASSUME_NONNULL_BEGIN
typedef ASTraitCollection * _Nonnull (^ASDisplayTraitsForTraitCollectionBlock)(UITraitCollection *traitCollection);
typedef ASTraitCollection * _Nonnull (^ASDisplayTraitsForTraitWindowSizeBlock)(CGSize windowSize);

/**
* ASViewController allows you to have a completely node backed hierarchy. It automatically
* handles @c ASVisibilityDepth, automatic range mode and propogating @c ASDisplayTraits to contained nodes.
*
* You can opt-out of node backed hierarchy and use it like a normal UIViewController.
* More importantly, you can use it as a base class for all of your view controllers among which some use a node hierarchy and some don't.
* See examples/ASDKgram project for actual implementation.
*/
@interface ASViewController<__covariant DisplayNodeType : ASDisplayNode *> : UIViewController <ASVisibilityDepth>

/**
* ASViewController Designated initializer.
*
* @discussion ASViewController allows you to have a completely node backed heirarchy. It automatically
* handles @c ASVisibilityDepth, automatic range mode and propogating @c ASDisplayTraits to contained nodes.
* ASViewController initializer.
*
* @param node An ASDisplayNode which will provide the root view (self.view)
* @return An ASViewController instance whose root view will be backed by the provided ASDisplayNode.
*
* @see ASVisibilityDepth
*/
- (instancetype)initWithNode:(DisplayNodeType)node NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithNode:(DisplayNodeType)node;

NS_ASSUME_NONNULL_END

/**
* @return node Returns the ASDisplayNode which provides the backing view to the view controller.
*/
@property (nonatomic, strong, readonly) DisplayNodeType node;
@property (nonatomic, strong, readonly, null_unspecified) DisplayNodeType node;

NS_ASSUME_NONNULL_BEGIN

/**
* Set this block to customize the ASDisplayTraits returned when the VC transitions to the given traitCollection.
Expand Down Expand Up @@ -91,12 +100,4 @@ typedef ASTraitCollection * _Nonnull (^ASDisplayTraitsForTraitWindowSizeBlock)(C

@end

@interface ASViewController (Unavailable)

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil AS_UNAVAILABLE("ASViewController requires using -initWithNode:");

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder AS_UNAVAILABLE("ASViewController requires using -initWithNode:");

@end

NS_ASSUME_NONNULL_END
53 changes: 38 additions & 15 deletions AsyncDisplayKit/ASViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ @implementation ASViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
ASDisplayNodeAssert(NO, @"ASViewController requires using -initWithNode:");
return [self initWithNode:[[ASDisplayNode alloc] init]];
if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
return nil;
}

[self _initializeInstance];

return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
ASDisplayNodeAssert(NO, @"ASViewController requires using -initWithNode:");
return [self initWithNode:[[ASDisplayNode alloc] init]];
if (!(self = [super initWithCoder:aDecoder])) {
return nil;
}

[self _initializeInstance];

return self;
}

- (instancetype)initWithNode:(ASDisplayNode *)node
Expand All @@ -49,10 +59,18 @@ - (instancetype)initWithNode:(ASDisplayNode *)node
return nil;
}

ASDisplayNodeAssertNotNil(node, @"Node must not be nil");
ASDisplayNodeAssertTrue(!node.layerBacked);
_node = node;
[self _initializeInstance];

return self;
}

- (void)_initializeInstance
{
if (_node == nil) {
return;
}

_selfConformsToRangeModeProtocol = [self conformsToProtocol:@protocol(ASRangeControllerUpdateRangeProtocol)];
_nodeConformsToRangeModeProtocol = [_node conformsToProtocol:@protocol(ASRangeControllerUpdateRangeProtocol)];
_automaticallyAdjustRangeModeBasedOnViewEvents = _selfConformsToRangeModeProtocol || _nodeConformsToRangeModeProtocol;
Expand All @@ -62,7 +80,7 @@ - (instancetype)initWithNode:(ASDisplayNode *)node
// Node already loaded the view
[self view];
} else {
// If the node didn't load yet add ourselves as on did load observer to laod the view in case the node gets loaded
// If the node didn't load yet add ourselves as on did load observer to load the view in case the node gets loaded
// before the view controller
__weak __typeof__(self) weakSelf = self;
[_node onDidLoad:^(__kindof ASDisplayNode * _Nonnull node) {
Expand All @@ -71,8 +89,6 @@ - (instancetype)initWithNode:(ASDisplayNode *)node
}
}];
}

return self;
}

- (void)dealloc
Expand All @@ -82,13 +98,18 @@ - (void)dealloc

- (void)loadView
{
ASDisplayNodeAssertTrue(!_node.layerBacked);

// Apple applies a frame and autoresizing masks we need. Allocating a view is not
// nearly as expensive as adding and removing it from a hierarchy, and fortunately
// we can avoid that here. Enabling layerBacking on a single node in the hierarchy
// will have a greater performance benefit than the impact of this transient view.
[super loadView];

if (_node == nil) {
return;
}

ASDisplayNodeAssertTrue(!_node.layerBacked);

UIView *view = self.view;
CGRect frame = view.frame;
UIViewAutoresizing autoresizingMask = view.autoresizingMask;
Expand Down Expand Up @@ -135,7 +156,7 @@ - (void)viewDidLayoutSubviews
{
if (_ensureDisplayed && self.neverShowPlaceholders) {
_ensureDisplayed = NO;
[self.node recursivelyEnsureDisplaySynchronously:YES];
[_node recursivelyEnsureDisplaySynchronously:YES];
}
[super viewDidLayoutSubviews];
}
Expand Down Expand Up @@ -212,7 +233,9 @@ - (void)setAutomaticallyAdjustRangeModeBasedOnViewEvents:(BOOL)automaticallyAdju

- (void)updateCurrentRangeModeWithModeIfPossible:(ASLayoutRangeMode)rangeMode
{
if (!_automaticallyAdjustRangeModeBasedOnViewEvents) { return; }
if (!_automaticallyAdjustRangeModeBasedOnViewEvents) {
return;
}

if (_selfConformsToRangeModeProtocol) {
id<ASRangeControllerUpdateRangeProtocol> rangeUpdater = (id<ASRangeControllerUpdateRangeProtocol>)self;
Expand Down Expand Up @@ -268,7 +291,7 @@ - (void)propagateNewTraitCollection:(ASPrimitiveTraitCollection)traitCollection
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// Once we've propagated all the traits, layout this node.
// Remeasure the node with the latest constrained size – old constrained size may be incorrect.
[self.node layoutThatFits:[self nodeConstrainedSize]];
[_node layoutThatFits:[self nodeConstrainedSize]];
#pragma clang diagnostic pop
}
}
Expand All @@ -286,7 +309,7 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

ASPrimitiveTraitCollection traitCollection = self.node.primitiveTraitCollection;
ASPrimitiveTraitCollection traitCollection = _node.primitiveTraitCollection;
traitCollection.containerSize = self.view.bounds.size;
[self propagateNewTraitCollection:traitCollection];
}
Expand Down
10 changes: 9 additions & 1 deletion examples/ASDKgram/Sample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
CC5532171E15CC1E0011C01F /* ASCollectionSectionController.m in Sources */ = {isa = PBXBuildFile; fileRef = CC5532161E15CC1E0011C01F /* ASCollectionSectionController.m */; };
CC6350BB1E1C482D002BC613 /* TailLoadingNode.m in Sources */ = {isa = PBXBuildFile; fileRef = CC6350BA1E1C482D002BC613 /* TailLoadingNode.m */; };
CC85250F1E36B392008EABE6 /* FeedHeaderNode.m in Sources */ = {isa = PBXBuildFile; fileRef = CC85250E1E36B392008EABE6 /* FeedHeaderNode.m */; };
E5F128F01E09625400B4335F /* PhotoFeedBaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = E5F128EF1E09625400B4335F /* PhotoFeedBaseController.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0585427F19D4DBE100606EA6 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-568h@2x.png"; path = "../Default-568h@2x.png"; sourceTree = "<group>"; };
05E2128119D4DB510098F589 /* Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sample.app; sourceTree = BUILT_PRODUCTS_DIR; };
3D24B17D1E4A4E7A9566C5E9 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
69CE83D91E515036004AA230 /* PhotoFeedControllerProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhotoFeedControllerProtocol.h; sourceTree = "<group>"; };
6C2C82AA19EE274300767484 /* Default-667h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-667h@2x.png"; sourceTree = SOURCE_ROOT; };
6C2C82AB19EE274300767484 /* Default-736h@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-736h@3x.png"; sourceTree = SOURCE_ROOT; };
76229A761CBB79E000B62CEF /* WindowWithStatusBarUnderlay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WindowWithStatusBarUnderlay.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +99,8 @@
CC85250D1E36B392008EABE6 /* FeedHeaderNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeedHeaderNode.h; sourceTree = "<group>"; };
CC85250E1E36B392008EABE6 /* FeedHeaderNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FeedHeaderNode.m; sourceTree = "<group>"; };
D09B5DF0BFB37583DE8F3142 /* Pods-Sample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Sample.release.xcconfig"; path = "Pods/Target Support Files/Pods-Sample/Pods-Sample.release.xcconfig"; sourceTree = "<group>"; };
E5F128EE1E09612700B4335F /* PhotoFeedBaseController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhotoFeedBaseController.h; sourceTree = "<group>"; };
E5F128EF1E09625400B4335F /* PhotoFeedBaseController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PhotoFeedBaseController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -183,6 +187,9 @@
767A5F141CAA3D8A004CDA8D /* Controller */ = {
isa = PBXGroup;
children = (
69CE83D91E515036004AA230 /* PhotoFeedControllerProtocol.h */,
E5F128EE1E09612700B4335F /* PhotoFeedBaseController.h */,
E5F128EF1E09625400B4335F /* PhotoFeedBaseController.m */,
767A5F161CAA3D96004CDA8D /* UIKit */,
767A5F151CAA3D90004CDA8D /* ASDK */,
CC00D1581E159132004E5502 /* ASDK-ListKit */,
Expand Down Expand Up @@ -380,7 +387,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
F012A6F39E0149F18F564F50 /* [CP] Copy Pods Resources */ = {
Expand Down Expand Up @@ -412,6 +419,7 @@
768843821CAA37EF00D8629E /* CommentModel.m in Sources */,
768843831CAA37EF00D8629E /* CommentsNode.m in Sources */,
768843961CAA37EF00D8629E /* Utilities.m in Sources */,
E5F128F01E09625400B4335F /* PhotoFeedBaseController.m in Sources */,
768843931CAA37EF00D8629E /* UserModel.m in Sources */,
CC5532171E15CC1E0011C01F /* ASCollectionSectionController.m in Sources */,
768843801CAA37EF00D8629E /* AppDelegate.m in Sources */,
Expand Down
4 changes: 0 additions & 4 deletions examples/ASDKgram/Sample/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

@protocol PhotoFeedControllerProtocol <NSObject>
- (void)resetAllData;
@end

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@end
Expand Down
39 changes: 39 additions & 0 deletions examples/ASDKgram/Sample/PhotoFeedBaseController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// PhotoFeedBaseController.h
// Sample
//
// Created by Huy Nguyen on 20/12/16.
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "PhotoFeedControllerProtocol.h"

@protocol PhotoFeedControllerProtocol;
@class PhotoFeedModel;

@interface PhotoFeedBaseController : ASViewController <PhotoFeedControllerProtocol>

@property (nonatomic, strong, readonly) PhotoFeedModel *photoFeed;
@property (nonatomic, strong, readonly) UITableView *tableView;

- (void)refreshFeed;
- (void)insertNewRows:(NSArray *)newPhotos;

#pragma mark - Subclasses must override these methods

- (void)loadPage;
- (void)requestCommentsForPhotos:(NSArray *)newPhotos;

@end
Loading

0 comments on commit aecd36a

Please sign in to comment.