Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix registerBottomTabSelectedListener unselected value #6326

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/ios/RNNBottomTabsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ @interface RNNBottomTabsController ()

@implementation RNNBottomTabsController {
NSUInteger _currentTabIndex;
NSUInteger _previousTabIndex;
BottomTabsBaseAttacher* _bottomTabsAttacher;
BOOL _tabBarNeedsRestore;

Expand Down Expand Up @@ -106,6 +107,7 @@ - (UIViewController *)selectedViewController {

- (void)setSelectedViewController:(__kindof UIViewController *)selectedViewController {
NSArray* children = self.pendingChildViewControllers ?: self.childViewControllers;
_previousTabIndex = _currentTabIndex;
_currentTabIndex = [children indexOfObject:selectedViewController];
[super setSelectedViewController:selectedViewController];
}
Expand All @@ -132,8 +134,7 @@ - (void)setTabBarVisible:(BOOL)visible {
#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
_currentTabIndex = tabBarController.selectedIndex;
[self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_previousTabIndex)];
}

- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
Expand Down
12 changes: 12 additions & 0 deletions playground/ios/NavigationTests/BottomTabsControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ - (void)testSetSelectedIndex_ShouldSetSelectedIndexWithCurrentTabIndex {
XCTAssertTrue(uut.selectedIndex == 1);
}

- (void)testDidSelectViewController_emitEventOnTabPress {
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initEmptyOptions];
RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
RNNBottomTabsController *uut = [RNNBottomTabsController createWithChildren:@[[UIViewController new], vc] options:options];
[uut viewWillAppear:YES];

[[(id)uut.eventEmitter expect] sendBottomTabSelected:@(1) unselected:@(0)];
[uut setSelectedViewController:vc];
[uut tabBarController:uut didSelectViewController:vc];
[(id)uut.eventEmitter verify];
}

- (void)testOnViewDidLayoutSubviews_ShouldUpdateDotIndicatorForChildren {
id dotIndicator = [OCMockObject partialMockForObject:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil]];
RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "RNNBottomTabsController+Helpers.h"
#import "BottomTabsPresenterCreator.h"
#import "BottomTabPresenterCreator.h"
#import <OCMock/OCMock.h>

@implementation RNNBottomTabsController (Helpers)

Expand All @@ -14,7 +15,7 @@ + (RNNBottomTabsController *)createWithChildren:(NSArray *)children {

+ (RNNBottomTabsController *)createWithChildren:(NSArray *)children options:(RNNNavigationOptions *)options {
RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
return [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:defaultOptions presenter:[BottomTabsPresenterCreator createWithDefaultOptions:defaultOptions] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:defaultOptions] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:defaultOptions] eventEmitter:nil childViewControllers:children bottomTabsAttacher:nil];
return [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:defaultOptions presenter:[BottomTabsPresenterCreator createWithDefaultOptions:defaultOptions] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:defaultOptions] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:defaultOptions] eventEmitter:[OCMockObject partialMockForObject:RNNEventEmitter.new] childViewControllers:children bottomTabsAttacher:nil];
}

@end