Skip to content

Commit

Permalink
Propagate new defaultOptions to presenters (#5404)
Browse files Browse the repository at this point in the history
When defaultOptions are set after setRoot has been called presenters need to receive the default options as well,
otherwise the outdated default options are regarded.
Fixes #5395
  • Loading branch information
guyca authored Aug 22, 2019
1 parent a003f75 commit 338b096
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 109 deletions.
4 changes: 2 additions & 2 deletions e2e/Options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('Options', () => {
await elementById(TestIDs.HIDE_TOPBAR_DEFAULT_OPTIONS).tap();
await expect(elementById(TestIDs.TOP_BAR)).toBeVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementById(TestIDs.TOP_BAR)).toBeNotVisible();
await expect(elementById(TestIDs.PUSHED_SCREEN_HEADER)).toBeNotVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementById(TestIDs.TOP_BAR)).toBeNotVisible();
await expect(elementById(TestIDs.PUSHED_SCREEN_HEADER)).toBeNotVisible();
});

it('default options should not override static options', async () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNBasePresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ typedef void (^RNNReactViewReadyCompletionBlock)(void);

@property(nonatomic, strong) NSString *boundComponentId;

@property(nonatomic, strong) RNNNavigationOptions * defaultOptions;

- (instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions;

- (void)bindViewController:(UIViewController *)boundViewController;

- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions;

- (void)applyOptionsOnInit:(RNNNavigationOptions *)initialOptions;

- (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options;
Expand Down
5 changes: 4 additions & 1 deletion lib/ios/RNNBasePresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

@interface RNNBasePresenter ()
@property(nonatomic, strong) RNNDotIndicatorPresenter* dotIndicatorPresenter;
@property(nonatomic, strong) RNNNavigationOptions* defaultOptions;
@end
@implementation RNNBasePresenter

Expand All @@ -24,6 +23,10 @@ - (void)bindViewController:(UIViewController <RNNLayoutProtocol> *)boundViewCont
_boundViewController = boundViewController;
}

- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
_defaultOptions = defaultOptions;
}

- (void)applyOptionsOnInit:(RNNNavigationOptions *)initialOptions {

}
Expand Down
9 changes: 3 additions & 6 deletions lib/ios/RNNCommandsHandler.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#import "RNNCommandsHandler.h"
#import "RNNNavigationOptions.h"
#import "RNNRootViewController.h"
#import "RNNSplitViewController.h"
#import "RNNElementFinder.h"
#import "React/RCTUIManager.h"
#import "RNNErrorHandler.h"
#import "RNNDefaultOptionsHelper.h"
#import "UIViewController+RNNOptions.h"
#import "React/RCTI18nUtil.h"
#import "UIViewController+LayoutProtocol.h"
#import "RNNLayoutManager.h"
#import "UIViewController+Utils.h"

static NSString* const setRoot = @"setRoot";
static NSString* const setStackRoot = @"setStackRoot";
Expand Down Expand Up @@ -86,7 +83,7 @@ - (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:
- (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
[self assertReady];

UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[RNNLayoutManager findComponentForId:componentId];
UIViewController<RNNLayoutProtocol>* vc = [RNNLayoutManager findComponentForId:componentId];
RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNRootViewController class]]) {
[CATransaction begin];
Expand All @@ -106,7 +103,7 @@ - (void)setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCo

UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
[RNNDefaultOptionsHelper recrusivelySetDefaultOptions:defaultOptions onRootViewController:rootViewController];

completion();
}

Expand Down
6 changes: 5 additions & 1 deletion lib/ios/RNNNavigationController.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#import "RNNNavigationController.h"
#import "RNNRootViewController.h"
#import "InteractivePopGestureDelegate.h"

const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;

@implementation RNNNavigationController

-(void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
[super setDefaultOptions:defaultOptions];
[self.presenter setDefaultOptions:defaultOptions];
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.presenter applyOptionsOnViewDidLayoutSubviews:self.resolveOptions];
Expand Down
47 changes: 22 additions & 25 deletions lib/ios/RNNNavigationControllerPresenter.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#import "RNNNavigationControllerPresenter.h"
#import "UINavigationController+RNNOptions.h"
#import "RNNNavigationController.h"
#import <React/RCTConvert.h>
#import "RNNCustomTitleView.h"
#import "UIViewController+LayoutProtocol.h"

@interface RNNNavigationControllerPresenter() {
RNNReactComponentRegistry* _componentRegistry;
Expand All @@ -25,44 +23,43 @@ - (void)applyOptions:(RNNNavigationOptions *)options {
[super applyOptions:options];

RNNNavigationController* navigationController = self.boundViewController;
RNNNavigationOptions * withDefault = [options withDefault:[self defaultOptions]];

self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new];
self.interactivePopGestureDelegate.navigationController = navigationController;
self.interactivePopGestureDelegate.originalDelegate = navigationController.interactivePopGestureRecognizer.delegate;
navigationController.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate;

[navigationController rnn_setInteractivePopGestureEnabled:[options.popGesture getWithDefaultValue:YES]];
[navigationController rnn_setRootBackgroundImage:[options.rootBackgroundImage getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarTestID:[options.topBar.testID getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarVisible:[options.topBar.visible getWithDefaultValue:YES] animated:[options.topBar.animate getWithDefaultValue:YES]];
[navigationController rnn_hideBarsOnScroll:[options.topBar.hideOnScroll getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarNoBorder:[options.topBar.noBorder getWithDefaultValue:NO]];
[navigationController rnn_setBarStyle:[RCTConvert UIBarStyle:[options.topBar.barStyle getWithDefaultValue:@"default"]]];
[navigationController rnn_setNavigationBarTranslucent:[options.topBar.background.translucent getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarClipsToBounds:[options.topBar.background.clipToBounds getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarBlur:[options.topBar.background.blur getWithDefaultValue:NO]];
[navigationController setTopBarBackgroundColor:[options.topBar.background.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarLargeTitleVisible:[options.topBar.largeTitle.visible getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarLargeTitleFontFamily:[options.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[options.topBar.largeTitle.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarFontFamily:[options.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.title.fontSize getWithDefaultValue:nil] color:[options.topBar.title.color getWithDefaultValue:nil]];
[navigationController rnn_setBackButtonColor:[options.topBar.backButton.color getWithDefaultValue:nil]];
}

- (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
[super applyOptionsOnWillMoveToParentViewController:options];
[navigationController rnn_setInteractivePopGestureEnabled:[withDefault.popGesture getWithDefaultValue:YES]];
[navigationController rnn_setRootBackgroundImage:[withDefault.rootBackgroundImage getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarTestID:[withDefault.topBar.testID getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarVisible:[withDefault.topBar.visible getWithDefaultValue:YES] animated:[withDefault.topBar.animate getWithDefaultValue:YES]];
[navigationController rnn_hideBarsOnScroll:[withDefault.topBar.hideOnScroll getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarNoBorder:[withDefault.topBar.noBorder getWithDefaultValue:NO]];
[navigationController rnn_setBarStyle:[RCTConvert UIBarStyle:[withDefault.topBar.barStyle getWithDefaultValue:@"default"]]];
[navigationController rnn_setNavigationBarTranslucent:[withDefault.topBar.background.translucent getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
[navigationController setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
[navigationController rnn_setNavigationBarLargeTitleFontFamily:[withDefault.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.largeTitle.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:nil]];
[navigationController rnn_setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
}

- (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
if (options.topBar.background.component.name.hasValue) {
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
if (withDefault.topBar.background.component.name.hasValue) {
[self presentBackgroundComponent];
}
}

- (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
RNNNavigationController* navigationController = self.boundViewController;
[navigationController setTopBarBackgroundColor:[options.topBar.background.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarFontFamily:[options.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.title.fontSize getWithDefaultValue:nil] color:[options.topBar.title.color getWithDefaultValue:[UIColor blackColor]]];
[navigationController rnn_setNavigationBarLargeTitleVisible:[options.topBar.largeTitle.visible getWithDefaultValue:NO]];
[navigationController setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
[navigationController rnn_setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:[UIColor blackColor]]];
[navigationController rnn_setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
}

- (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions {
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ - (void)bindViewController:(UIViewController *)viewController {
[viewController didMoveToParentViewController:self];
}

- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
[_presenter setDefaultOptions:defaultOptions];
}

- (void)mergeOptions:(RNNNavigationOptions *)options {
[_presenter mergeOptions:options currentOptions:self.options];
[self.parentViewController mergeOptions:options];
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNSideMenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo creator:(id<RNNRo
return self;
}

- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
[self.presenter setDefaultOptions:defaultOptions];
}

- (void)setAnimationType:(NSString *)animationType {
MMDrawerControllerDrawerVisualStateBlock animationTypeStateBlock = nil;
if ([animationType isEqualToString:@"door"]) animationTypeStateBlock = [MMDrawerVisualState swingingDoorVisualStateBlock];
Expand Down
51 changes: 26 additions & 25 deletions lib/ios/RNNSideMenuPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,53 @@ -(instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions {

- (void)applyOptions:(RNNNavigationOptions *)options {
[super applyOptions:options];
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
RNNSideMenuController* sideMenuController = self.boundViewController;

[sideMenuController side:MMDrawerSideLeft enabled:[withDefault.sideMenu.left.enabled getWithDefaultValue:YES]];
[sideMenuController side:MMDrawerSideRight enabled:[withDefault.sideMenu.right.enabled getWithDefaultValue:YES]];

[sideMenuController side:MMDrawerSideLeft enabled:[options.sideMenu.left.enabled getWithDefaultValue:YES]];
[sideMenuController side:MMDrawerSideRight enabled:[options.sideMenu.right.enabled getWithDefaultValue:YES]];

[sideMenuController setShouldStretchLeftDrawer:[options.sideMenu.left.shouldStretchDrawer getWithDefaultValue:YES]];
[sideMenuController setShouldStretchRightDrawer:[options.sideMenu.right.shouldStretchDrawer getWithDefaultValue:YES]];
[sideMenuController setShouldStretchLeftDrawer:[withDefault.sideMenu.left.shouldStretchDrawer getWithDefaultValue:YES]];
[sideMenuController setShouldStretchRightDrawer:[withDefault.sideMenu.right.shouldStretchDrawer getWithDefaultValue:YES]];

[sideMenuController setAnimationVelocityLeft:[options.sideMenu.left.animationVelocity getWithDefaultValue:840.0f]];
[sideMenuController setAnimationVelocityRight:[options.sideMenu.right.animationVelocity getWithDefaultValue:840.0f]];
[sideMenuController setAnimationVelocityLeft:[withDefault.sideMenu.left.animationVelocity getWithDefaultValue:840.0f]];
[sideMenuController setAnimationVelocityRight:[withDefault.sideMenu.right.animationVelocity getWithDefaultValue:840.0f]];

[sideMenuController setAnimationType:[options.sideMenu.animationType getWithDefaultValue:nil]];
[sideMenuController setAnimationType:[withDefault.sideMenu.animationType getWithDefaultValue:nil]];

if (options.sideMenu.left.width.hasValue) {
[sideMenuController side:MMDrawerSideLeft width:options.sideMenu.left.width.get];
if (withDefault.sideMenu.left.width.hasValue) {
[sideMenuController side:MMDrawerSideLeft width:withDefault.sideMenu.left.width.get];
}

if (options.sideMenu.right.width.hasValue) {
[sideMenuController side:MMDrawerSideRight width:options.sideMenu.right.width.get];
if (withDefault.sideMenu.right.width.hasValue) {
[sideMenuController side:MMDrawerSideRight width:withDefault.sideMenu.right.width.get];
}

if (options.sideMenu.left.visible.hasValue) {
[sideMenuController side:MMDrawerSideLeft visible:options.sideMenu.left.visible.get];
[options.sideMenu.left.visible consume];
if (withDefault.sideMenu.left.visible.hasValue) {
[sideMenuController side:MMDrawerSideLeft visible:withDefault.sideMenu.left.visible.get];
[withDefault.sideMenu.left.visible consume];
}

if (options.sideMenu.right.visible.hasValue) {
[sideMenuController side:MMDrawerSideRight visible:options.sideMenu.right.visible.get];
[options.sideMenu.right.visible consume];
if (withDefault.sideMenu.right.visible.hasValue) {
[sideMenuController side:MMDrawerSideRight visible:withDefault.sideMenu.right.visible.get];
[withDefault.sideMenu.right.visible consume];
}
}

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


RNNNavigationOptions *withDefault = [initialOptions withDefault:[self defaultOptions]];
RNNSideMenuController* sideMenuController = self.boundViewController;
if (initialOptions.sideMenu.left.width.hasValue) {
[sideMenuController side:MMDrawerSideLeft width:initialOptions.sideMenu.left.width.get];
if (withDefault.sideMenu.left.width.hasValue) {
[sideMenuController side:MMDrawerSideLeft width:withDefault.sideMenu.left.width.get];
}

if (initialOptions.sideMenu.right.width.hasValue) {
[sideMenuController side:MMDrawerSideRight width:initialOptions.sideMenu.right.width.get];
if (withDefault.sideMenu.right.width.hasValue) {
[sideMenuController side:MMDrawerSideRight width:withDefault.sideMenu.right.width.get];
}

[sideMenuController setOpenDrawerGestureModeMask:[[initialOptions.sideMenu.openGestureMode getWithDefaultValue:@(MMOpenDrawerGestureModeAll)] integerValue]];
[sideMenuController setOpenDrawerGestureModeMask:[[withDefault.sideMenu.openGestureMode getWithDefaultValue:@(MMOpenDrawerGestureModeAll)] integerValue]];
}

- (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions {
Expand Down
23 changes: 10 additions & 13 deletions lib/ios/RNNTabBarPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@

@implementation RNNTabBarPresenter

-(instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions {
self = [super initWithDefaultOptions:defaultOptions];
return self;
}

- (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
UITabBarController *tabBarController = self.boundViewController;
[tabBarController rnn_setCurrentTabIndex:[options.bottomTabs.currentTabIndex getWithDefaultValue:0]];
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
[tabBarController rnn_setCurrentTabIndex:[withDefault.bottomTabs.currentTabIndex getWithDefaultValue:0]];
}

- (void)applyOptions:(RNNNavigationOptions *)options {
UITabBarController *tabBarController = self.boundViewController;

[tabBarController rnn_setTabBarTestID:[options.bottomTabs.testID getWithDefaultValue:nil]];
[tabBarController rnn_setTabBarBackgroundColor:[options.bottomTabs.backgroundColor getWithDefaultValue:nil]];
[tabBarController rnn_setTabBarTranslucent:[options.bottomTabs.translucent getWithDefaultValue:NO]];
[tabBarController rnn_setTabBarHideShadow:[options.bottomTabs.hideShadow getWithDefaultValue:NO]];
[tabBarController rnn_setTabBarStyle:[RCTConvert UIBarStyle:[options.bottomTabs.barStyle getWithDefaultValue:@"default"]]];
[tabBarController rnn_setTabBarVisible:[options.bottomTabs.visible getWithDefaultValue:YES] animated:[options.bottomTabs.animate getWithDefaultValue:NO]];
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];

[tabBarController rnn_setTabBarTestID:[withDefault.bottomTabs.testID getWithDefaultValue:nil]];
[tabBarController rnn_setTabBarBackgroundColor:[withDefault.bottomTabs.backgroundColor getWithDefaultValue:nil]];
[tabBarController rnn_setTabBarTranslucent:[withDefault.bottomTabs.translucent getWithDefaultValue:NO]];
[tabBarController rnn_setTabBarHideShadow:[withDefault.bottomTabs.hideShadow getWithDefaultValue:NO]];
[tabBarController rnn_setTabBarStyle:[RCTConvert UIBarStyle:[withDefault.bottomTabs.barStyle getWithDefaultValue:@"default"]]];
[tabBarController rnn_setTabBarVisible:[withDefault.bottomTabs.visible getWithDefaultValue:YES] animated:[withDefault.bottomTabs.animate getWithDefaultValue:NO]];
}

- (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions {
Expand Down
Loading

0 comments on commit 338b096

Please sign in to comment.