Skip to content

Commit

Permalink
fix(ios): avoid setting selected-index before setup has finished. (#7925
Browse files Browse the repository at this point in the history
)

This fixes the issue mentioned on #7921, #7922 and #7924.

Changing the `selectedIndex` will not have any effect before the setup has finished,
 the original issue was the method `setSelectedIndex` was called immediately when calling
 to the `super` initializer with a `0` value regardless of the initial `currentTab`.

This prevents the loading of the tab from index `0` regardless of the custom initial rendered tab.
  • Loading branch information
asafkorem authored Oct 16, 2024
1 parent 292eee5 commit ac26b03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 21 additions & 9 deletions lib/ios/RNNBottomTabsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ @implementation RNNBottomTabsController {
NSUInteger _previousTabIndex;
BottomTabsBaseAttacher *_bottomTabsAttacher;
BOOL _tabBarNeedsRestore;
RNNNavigationOptions *_options;
BOOL _didFinishSetup;
}

- (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
Expand All @@ -28,6 +30,15 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
_bottomTabsAttacher = bottomTabsAttacher;
_bottomTabPresenter = bottomTabPresenter;
_dotIndicatorPresenter = dotIndicatorPresenter;
_options = options;
_didFinishSetup = NO;

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

self = [super initWithLayoutInfo:layoutInfo
creator:creator
Expand All @@ -37,13 +48,6 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
eventEmitter:eventEmitter
childViewControllers:childViewControllers];

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

if (@available(iOS 13.0, *)) {
self.tabBar.standardAppearance = [UITabBarAppearance new];
}
Expand All @@ -60,6 +64,7 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
action:@selector(handleLongPressGesture:)];
[self.tabBar addGestureRecognizer:self.longPressRecognizer];

_didFinishSetup = YES;
return self;
}

Expand Down Expand Up @@ -132,8 +137,15 @@ - (void)setSelectedIndexByComponentID:(NSString *)componentID {
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
_currentTabIndex = selectedIndex;
[super setSelectedIndex:selectedIndex];
IntNumber *currentTabIndex = _options.bottomTabs.currentTabIndex;
if ([currentTabIndex hasValue] && !_didFinishSetup) {
NSUInteger currentTabIndexValue = [currentTabIndex get];
_currentTabIndex = currentTabIndexValue;
} else {
_currentTabIndex = selectedIndex;
}

[super setSelectedIndex:_currentTabIndex];
}

- (UIViewController *)selectedViewController {
Expand Down
3 changes: 1 addition & 2 deletions playground/ios/NavigationTests/RNNCommandsHandlerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {
completion:^(NSString *componentId){
}];

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

Expand Down

0 comments on commit ac26b03

Please sign in to comment.