Skip to content

Commit

Permalink
Fix registerBottomTabSelectedListener unselected value (#6326)
Browse files Browse the repository at this point in the history
Closes #6318
  • Loading branch information
yogevbd authored Jun 22, 2020
1 parent a1578cd commit fbb72f2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
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

0 comments on commit fbb72f2

Please sign in to comment.