Skip to content

Commit

Permalink
fix(ios): load initial tab when onSwitchToTab mode is set.
Browse files Browse the repository at this point in the history
Setting the initial tab index before the initialization doesn't work, since it resets
 to the default value (0) after initializing the tab bar controller.

It also seems that it loads the default child from index 0 regardless of setting it a
 default. I tried to workaround this from different steps in the lifecycle of the
 controller but looks like there's a native issue there.
  • Loading branch information
asafkorem committed Oct 16, 2024
1 parent 73a8564 commit 6455219
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
23 changes: 13 additions & 10 deletions lib/ios/RNNBottomTabsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
_bottomTabPresenter = bottomTabPresenter;
_dotIndicatorPresenter = dotIndicatorPresenter;

if ([options.bottomTabs.currentTabIndex hasValue]) {
_currentTabIndex = [options.bottomTabs.currentTabIndex get];
_previousTabIndex = _currentTabIndex;
self = [super initWithLayoutInfo:layoutInfo
creator:creator
options:options
defaultOptions:defaultOptions
presenter:presenter
eventEmitter:eventEmitter
childViewControllers:childViewControllers];

IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex;
if ([currentTabIndex hasValue]) {
NSUInteger currentTabIndexValue = [currentTabIndex get];
_previousTabIndex = currentTabIndexValue;
_currentTabIndex = currentTabIndexValue;
}

self = [super initWithLayoutInfo:layoutInfo
creator:creator
options:options
defaultOptions:defaultOptions
presenter:presenter
eventEmitter:eventEmitter
childViewControllers:childViewControllers];
if (@available(iOS 13.0, *)) {
self.tabBar.standardAppearance = [UITabBarAppearance new];
}
Expand Down
25 changes: 20 additions & 5 deletions playground/ios/NavigationTests/RNNCommandsHandlerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,10 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTab {
commandId:@""
completion:^(NSString *componentId){
}];

XCTAssertTrue(_vc1.isViewLoaded);
XCTAssertFalse(_vc2.isViewLoaded);

[tabBarController setSelectedIndex:1];
XCTAssertTrue(_vc2.isViewLoaded);
}
Expand All @@ -531,6 +533,7 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {

BottomTabsBaseAttacher *attacher =
[[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];

RNNBottomTabsController *tabBarController =
[[RNNBottomTabsController alloc] initWithLayoutInfo:nil
creator:nil
Expand All @@ -540,19 +543,31 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {
bottomTabPresenter:nil
dotIndicatorPresenter:nil
eventEmitter:_eventEmmiter
childViewControllers:@[ _vc1, _vc2 ]
childViewControllers:@[ _vc1, _vc2, _vc3 ]
bottomTabsAttacher:attacher];

[tabBarController viewWillAppear:YES];
OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);

[self.uut setRoot:@{}
commandId:@""
completion:^(NSString *componentId){
}];
XCTAssertFalse(_vc1.isViewLoaded);
XCTAssertTrue(_vc2.isViewLoaded);
[tabBarController setSelectedIndex:0];
XCTAssertTrue(_vc1.isViewLoaded);

// TODO: for some reason the controller always loads the default controller (index 0), regardless the initial value.
XCTAssertTrue(_vc1.isViewLoaded);
XCTAssertTrue(_vc2.isViewLoaded);
XCTAssertFalse(_vc3.isViewLoaded);

[tabBarController setSelectedIndex:0];
XCTAssertTrue(_vc1.isViewLoaded);
XCTAssertTrue(_vc2.isViewLoaded);
XCTAssertFalse(_vc3.isViewLoaded);

[tabBarController setSelectedIndex:2];
XCTAssertTrue(_vc1.isViewLoaded);
XCTAssertTrue(_vc2.isViewLoaded);
XCTAssertTrue(_vc3.isViewLoaded);
}

- (void)testSetRoot_withBottomTabsAttachModeAfterInitialTab {
Expand Down

0 comments on commit 6455219

Please sign in to comment.