Skip to content

Commit

Permalink
Model splitView after the other presenter options (#4647)
Browse files Browse the repository at this point in the history
Seems like RNNSplitViewOptions wasn't updated to use the presenters, so
the options were going nowhere.
  • Loading branch information
zzorba authored and guyca committed Feb 4, 2019
1 parent 9c48a78 commit 00d5e31
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 56 deletions.
3 changes: 2 additions & 1 deletion lib/ios/RNNControllerFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "RNNNavigationControllerPresenter.h"
#import "RNNTabBarPresenter.h"
#import "RNNSideMenuPresenter.h"
#import "RNNSplitViewControllerPresenter.h"

@implementation RNNControllerFactory {
id<RNNRootViewCreator> _creator;
Expand Down Expand Up @@ -204,7 +205,7 @@ - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
- (UIViewController<RNNParentProtocol> *)createSplitView:(RNNLayoutNode*)node {
RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] init];

NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];

Expand Down
12 changes: 3 additions & 9 deletions lib/ios/RNNSplitViewController.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RNNLayoutNode.h"
#import "RNNRootViewCreator.h"
#import "RNNEventEmitter.h"
#import "RNNNavigationOptions.h"
#import "RNNSplitViewOptions.h"
#import "RNNAnimator.h"
#import "RNNTopTabsViewController.h"
#import "RNNParentProtocol.h"
#import "RNNSplitViewControllerPresenter.h"
#import "UISplitViewController+RNNOptions.h"

@interface RNNSplitViewController : UISplitViewController <RNNParentProtocol>

@property (nonatomic, strong) RNNNavigationOptions* options;
@property (nonatomic, strong) RNNNavigationOptions* defaultOptions;
@property (nonatomic, retain) RNNLayoutInfo* layoutInfo;
@property (nonatomic, retain) RNNViewControllerPresenter* presenter;
@property (nonatomic, retain) RNNSplitViewControllerPresenter* presenter;

@end
19 changes: 11 additions & 8 deletions lib/ios/RNNSplitViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

@implementation RNNSplitViewController

- (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNViewControllerPresenter *)presenter {
- (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNSplitViewControllerPresenter *)presenter {
self = [super init];

self.presenter = presenter;
[self.presenter bindViewController:self];


self.defaultOptions = defaultOptions;
self.options = options;

self.presenter = presenter;
[self.presenter bindViewController:self];
[self.presenter applyOptionsOnInit:self.options];

self.layoutInfo = layoutInfo;

self.navigationController.delegate = self;

[self bindChildViewControllers:childViewControllers];

return self;
}

Expand All @@ -27,7 +29,7 @@ - (void)willMoveToParentViewController:(UIViewController *)parent {

- (void)onChildWillAppear {
[_presenter applyOptions:self.resolveOptions];
[((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
[((UISplitViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
}

- (RNNNavigationOptions *)resolveOptions {
Expand All @@ -36,7 +38,7 @@ - (RNNNavigationOptions *)resolveOptions {

- (void)mergeOptions:(RNNNavigationOptions *)options {
[_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
[((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
[((UISplitViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
}

- (void)overrideOptions:(RNNNavigationOptions *)options {
Expand All @@ -61,4 +63,5 @@ - (UIViewController *)getCurrentChild {
return [[self getCurrentChild] getCurrentLeaf];
}


@end
5 changes: 5 additions & 0 deletions lib/ios/RNNSplitViewControllerPresenter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import "RNNViewControllerPresenter.h"

@interface RNNSplitViewControllerPresenter : RNNViewControllerPresenter

@end
52 changes: 52 additions & 0 deletions lib/ios/RNNSplitViewControllerPresenter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#import "RNNSplitViewControllerPresenter.h"
#import "UISplitViewController+RNNOptions.h"
#import "RCTConvert+Modal.h"
#import "RNNSplitViewController.h"

@implementation RNNSplitViewControllerPresenter

- (void)bindViewController:(UISplitViewController *)bindedViewController viewCreator:(id<RNNRootViewCreator>)creator {
self.bindedViewController = bindedViewController;
}

- (void)applyOptions:(RNNNavigationOptions *)options {
[super applyOptions:options];

UISplitViewController* splitViewController = self.bindedViewController;
[splitViewController rnn_setDisplayMode:options.splitView.displayMode];
[splitViewController rnn_setPrimaryEdge:options.splitView.primaryEdge];
[splitViewController rnn_setMinWidth:options.splitView.minWidth];
[splitViewController rnn_setMaxWidth:options.splitView.maxWidth];
}


- (void)applyOptionsOnInit:(RNNNavigationOptions *)initialOptions {
[super applyOptionsOnInit:initialOptions];

UISplitViewController* splitViewController = self.bindedViewController;
[splitViewController rnn_setDisplayMode:initialOptions.splitView.displayMode];
[splitViewController rnn_setPrimaryEdge:initialOptions.splitView.primaryEdge];
[splitViewController rnn_setMinWidth:initialOptions.splitView.minWidth];
[splitViewController rnn_setMaxWidth:initialOptions.splitView.maxWidth];
}

- (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions defaultOptions:(RNNNavigationOptions *)defaultOptions {
[super mergeOptions:newOptions currentOptions:currentOptions defaultOptions:defaultOptions];

UISplitViewController* splitViewController = self.bindedViewController;

if (newOptions.splitView.displayMode) {
[splitViewController rnn_setDisplayMode:newOptions.splitView.displayMode];
}
if (newOptions.splitView.primaryEdge) {
[splitViewController rnn_setPrimaryEdge:newOptions.splitView.primaryEdge];
}
if (newOptions.splitView.minWidth) {
[splitViewController rnn_setMinWidth:newOptions.splitView.minWidth];
}
if (newOptions.splitView.maxWidth) {
[splitViewController rnn_setMaxWidth:newOptions.splitView.maxWidth];
}
}

@end
4 changes: 2 additions & 2 deletions lib/ios/RNNSplitViewOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@property (nonatomic, strong) NSString* displayMode;
@property (nonatomic, strong) NSString* primaryEdge;
@property (nonatomic, strong) NSNumber* minWidth;
@property (nonatomic, strong) NSNumber* maxWidth;
@property (nonatomic, strong) Number* minWidth;
@property (nonatomic, strong) Number* maxWidth;

@end
38 changes: 2 additions & 36 deletions lib/ios/RNNSplitViewOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,9 @@ - (instancetype)initWithDict:(NSDictionary *)dict {

self.displayMode = dict[@"displayMode"];
self.primaryEdge = dict[@"primaryEdge"];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
self.minWidth = [f numberFromString:dict[@"minWidth"]];
self.maxWidth = [f numberFromString:dict[@"maxWidth"]];

self.minWidth = [NumberParser parse:dict key:@"minWidth"];
self.maxWidth = [NumberParser parse:dict key:@"maxWidth"];
return self;
}

-(void)applyOn:(UIViewController<RNNParentProtocol> *)viewController {

UISplitViewController *svc = (UISplitViewController*) viewController;

if (@available(iOS 11.0, *)) {
if ([self.primaryEdge isEqualToString:@"trailing"]) {
[svc setPrimaryEdge:UISplitViewControllerPrimaryEdgeTrailing];
} else {
[svc setPrimaryEdge:UISplitViewControllerPrimaryEdgeLeading];
}
}

if ([self.displayMode isEqualToString:@"visible"]) {
[svc setPreferredDisplayMode:UISplitViewControllerDisplayModeAllVisible];
} else if ([self.displayMode isEqualToString:@"hidden"]) {
[svc setPreferredDisplayMode:UISplitViewControllerDisplayModePrimaryHidden];
} else if ([self.displayMode isEqualToString:@"overlay"]) {
[svc setPreferredDisplayMode:UISplitViewControllerDisplayModePrimaryOverlay];
} else {
[svc setPreferredDisplayMode:UISplitViewControllerDisplayModeAutomatic];
}

if (self.minWidth) {
[svc setMinimumPrimaryColumnWidth:[self.minWidth doubleValue]];
}

if (self.maxWidth) {
[svc setMaximumPrimaryColumnWidth:[self.maxWidth doubleValue]];
}
}

@end
12 changes: 12 additions & 0 deletions lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
50F5DFC21F407A8C001A00BC /* RNNTabBarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50F5DFC01F407A8C001A00BC /* RNNTabBarController.m */; };
50F5DFC51F407AA0001A00BC /* RNNNavigationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 50F5DFC31F407AA0001A00BC /* RNNNavigationController.h */; };
50F5DFC61F407AA0001A00BC /* RNNNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50F5DFC41F407AA0001A00BC /* RNNNavigationController.m */; };
651E1F8A21FD624600DFEA19 /* UISplitViewController+RNNOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 651E1F8921FD624600DFEA19 /* UISplitViewController+RNNOptions.m */; };
651E1F8D21FD642100DFEA19 /* RNNSplitViewControllerPresenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 651E1F8C21FD642100DFEA19 /* RNNSplitViewControllerPresenter.m */; };
7365071121E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 7365070F21E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h */; };
7365071221E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 7365071021E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m */; };
7B1126A01E2D263F00F9B03B /* RNNEventEmitter.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B11269F1E2D263F00F9B03B /* RNNEventEmitter.m */; };
Expand Down Expand Up @@ -560,6 +562,10 @@
50F5DFC01F407A8C001A00BC /* RNNTabBarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNTabBarController.m; sourceTree = "<group>"; };
50F5DFC31F407AA0001A00BC /* RNNNavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNNavigationController.h; sourceTree = "<group>"; };
50F5DFC41F407AA0001A00BC /* RNNNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNNavigationController.m; sourceTree = "<group>"; };
651E1F8821FD611600DFEA19 /* UISplitViewController+RNNOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UISplitViewController+RNNOptions.h"; sourceTree = "<group>"; };
651E1F8921FD624600DFEA19 /* UISplitViewController+RNNOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UISplitViewController+RNNOptions.m"; sourceTree = "<group>"; };
651E1F8B21FD63F400DFEA19 /* RNNSplitViewControllerPresenter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNSplitViewControllerPresenter.h; sourceTree = "<group>"; };
651E1F8C21FD642100DFEA19 /* RNNSplitViewControllerPresenter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNSplitViewControllerPresenter.m; sourceTree = "<group>"; };
7365070F21E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+UIBarButtonSystemItem.h"; sourceTree = "<group>"; };
7365071021E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+UIBarButtonSystemItem.m"; sourceTree = "<group>"; };
7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNEventEmitter.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -942,6 +948,8 @@
501224052173592D000F5F98 /* RNNTabBarPresenter.m */,
5012240821735959000F5F98 /* RNNSideMenuPresenter.h */,
5012240921735959000F5F98 /* RNNSideMenuPresenter.m */,
651E1F8B21FD63F400DFEA19 /* RNNSplitViewControllerPresenter.h */,
651E1F8C21FD642100DFEA19 /* RNNSplitViewControllerPresenter.m */,
);
name = Presenters;
sourceTree = "<group>";
Expand Down Expand Up @@ -1094,6 +1102,8 @@
5038A3B4216DF602009280BC /* UINavigationController+RNNOptions.m */,
5038A3B7216DFCFD009280BC /* UITabBarController+RNNOptions.h */,
5038A3B8216DFCFD009280BC /* UITabBarController+RNNOptions.m */,
651E1F8821FD611600DFEA19 /* UISplitViewController+RNNOptions.h */,
651E1F8921FD624600DFEA19 /* UISplitViewController+RNNOptions.m */,
7BD721F31E2D3AA100724059 /* Bridge */,
7B1E4C4B1E2D173700C3A525 /* Controllers */,
263905881E4C6F440023D7D3 /* RNNSideMenu */,
Expand Down Expand Up @@ -1445,6 +1455,7 @@
50570B272061473D006A1B5C /* RNNTitleOptions.m in Sources */,
263905BF1E4C6F440023D7D3 /* RCCTheSideBarManagerViewController.m in Sources */,
5012241F217366D4000F5F98 /* ColorParser.m in Sources */,
651E1F8D21FD642100DFEA19 /* RNNSplitViewControllerPresenter.m in Sources */,
501E0218213E7EA3003365C5 /* RNNReactView.m in Sources */,
50BE951220B5A787004F5DF5 /* RNNStatusBarOptions.m in Sources */,
50495953216F62BD006D2B81 /* NullNumber.m in Sources */,
Expand Down Expand Up @@ -1473,6 +1484,7 @@
50395590217482FE00B0A663 /* NullIntNumber.m in Sources */,
E8A5CD631F49114F00E89D0D /* RNNElement.m in Sources */,
5038A3CB216E328A009280BC /* Param.m in Sources */,
651E1F8A21FD624600DFEA19 /* UISplitViewController+RNNOptions.m in Sources */,
50395594217485B000B0A663 /* Double.m in Sources */,
504AFE751FFFF0540076E904 /* RNNTopTabsOptions.m in Sources */,
50175CD2207A2AA1004FE91B /* RNNComponentOptions.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import "RNNSideMenuChildVC.h"
#import "RNNNavigationController.h"
#import "RNNTabBarController.h"
#import "RNNTopTabsViewController.h"
#import "RNNSplitViewController.h"

@interface RNNControllerFactoryTest : XCTestCase
Expand Down
14 changes: 14 additions & 0 deletions lib/ios/UISplitViewController+RNNOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <UIKit/UIKit.h>
#import "Number.h"

@interface UISplitViewController (RNNOptions)

- (void)rnn_setDisplayMode:(NSString *)displayMode;

- (void)rnn_setPrimaryEdge:(NSString *)primaryEdge;

- (void)rnn_setMinWidth:(Number *)minWidth;

- (void)rnn_setMaxWidth:(Number *)maxWidth;

@end
41 changes: 41 additions & 0 deletions lib/ios/UISplitViewController+RNNOptions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#import "UISplitViewController+RNNOptions.h"
#import "RNNSplitViewController.h"

@implementation UISplitViewController (RNNOptions)

- (void)rnn_setDisplayMode:(NSString *)displayMode {
if ([displayMode isEqualToString:@"visible"]) {
self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
} else if ([displayMode isEqualToString:@"hidden"]) {
self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
} else if ([displayMode isEqualToString:@"overlay"]) {
self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
} else {
self.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
}
}

- (void)rnn_setPrimaryEdge:(NSString *)primaryEdge {
if (@available(iOS 11.0, *)) {
if ([primaryEdge isEqualToString:@"trailing"]) {
self.primaryEdge = UISplitViewControllerPrimaryEdgeTrailing;
} else {
self.primaryEdge = UISplitViewControllerPrimaryEdgeLeading;
}
}
}

- (void)rnn_setMinWidth:(Number *)minWidth {
if (minWidth.hasValue) {
[self setMinimumPrimaryColumnWidth:[[minWidth get] doubleValue]];
}
}

- (void)rnn_setMaxWidth:(Number *)maxWidth {
if (maxWidth.hasValue) {
[self setMaximumPrimaryColumnWidth:[[maxWidth get] doubleValue]];
}
}

@end

0 comments on commit 00d5e31

Please sign in to comment.