Skip to content

Commit

Permalink
Apply bottomTabs options after children added (#4994)
Browse files Browse the repository at this point in the history
* Apply bottomTabs options after children added

* Fix unit tests
  • Loading branch information
yogevbd authored Apr 17, 2019
1 parent 81dc07d commit 2bddff3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/ios/RNNBasePresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ typedef void (^RNNReactViewReadyCompletionBlock)(void);

- (void)applyOptions:(RNNNavigationOptions *)options;

- (void)applyOptionsOnSetViewControllers:(RNNNavigationOptions *)options;

- (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options;

- (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions defaultOptions:(RNNNavigationOptions *)defaultOptions;
Expand Down
40 changes: 22 additions & 18 deletions lib/ios/RNNBasePresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,51 @@ - (void)bindViewController:(UIViewController<RNNLayoutProtocol> *)bindedViewCont
}

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

}

- (void)applyOptionsOnSetViewControllers:(RNNNavigationOptions *)options {

}

- (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
UIViewController* viewController = self.bindedViewController;

if (options.bottomTab.text.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.icon.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.selectedIcon.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.badgeColor.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.textColor.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.iconColor.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.selectedTextColor.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
}

if (options.bottomTab.selectedIconColor.hasValue) {
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:options.bottomTab];
viewController.tabBarItem = tabItem;
Expand All @@ -72,11 +76,11 @@ - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)opt

- (void)applyOptions:(RNNNavigationOptions *)options {
UIViewController* viewController = self.bindedViewController;

if (options.bottomTab.badge.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
[viewController rnn_setTabBarItemBadge:options.bottomTab.badge.get];
}

if (options.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
[viewController rnn_setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
}
Expand All @@ -87,47 +91,47 @@ - (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavig
if (newOptions.bottomTab.badge.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
[viewController rnn_setTabBarItemBadge:newOptions.bottomTab.badge.get];
}

if (newOptions.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
[viewController rnn_setTabBarItemBadgeColor:newOptions.bottomTab.badgeColor.get];
}

if (newOptions.bottomTab.text.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.icon.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.selectedIcon.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.textColor.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.selectedTextColor.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.iconColor.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
viewController.tabBarItem = tabItem;
}

if (newOptions.bottomTab.selectedIconColor.hasValue) {
RNNNavigationOptions* buttonsResolvedOptions = [(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions] withDefault:defaultOptions];
UITabBarItem* tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
Expand Down
5 changes: 5 additions & 0 deletions lib/ios/RNNTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ @implementation RNNTabBarController {
NSUInteger _currentTabIndex;
}

- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
[super setViewControllers:viewControllers];
[self.presenter applyOptionsOnSetViewControllers:self.resolveOptions];
}

- (id<UITabBarControllerDelegate>)delegate {
return self;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/RNNTabBarPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

@implementation RNNTabBarPresenter

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

- (void)applyOptions:(RNNNavigationOptions *)options {
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/ReactNativeNavigationTests/RNNTabBarPresenterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ - (void)testApplyOptions_shouldApplyOptions {
[self.bindedViewController verify];
}

- (void)testApplyOptions_shouldApplyOptionsOnInit {
- (void)testApplyOptions_shouldApplyOptionsOnSetViewControllers {
RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
initialOptions.bottomTabs.currentTabIndex = [[IntNumber alloc] initWithValue:@(1)];
[[self.bindedViewController expect] rnn_setCurrentTabIndex:1];

[self.uut applyOptionsOnInit:initialOptions];
[self.uut applyOptionsOnSetViewControllers:initialOptions];
[self.bindedViewController verify];
}

Expand Down

0 comments on commit 2bddff3

Please sign in to comment.