diff --git a/lib/ios/RNNBottomTabsController.m b/lib/ios/RNNBottomTabsController.m index 40f9e115b3b..8a4b9a78623 100644 --- a/lib/ios/RNNBottomTabsController.m +++ b/lib/ios/RNNBottomTabsController.m @@ -9,6 +9,7 @@ @interface RNNBottomTabsController () @implementation RNNBottomTabsController { NSUInteger _currentTabIndex; + NSUInteger _previousTabIndex; BottomTabsBaseAttacher* _bottomTabsAttacher; BOOL _tabBarNeedsRestore; @@ -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]; } @@ -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 { diff --git a/playground/ios/NavigationTests/BottomTabsControllerTest.m b/playground/ios/NavigationTests/BottomTabsControllerTest.m index d1889243b2b..ecf64f89743 100644 --- a/playground/ios/NavigationTests/BottomTabsControllerTest.m +++ b/playground/ios/NavigationTests/BottomTabsControllerTest.m @@ -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]; diff --git a/playground/ios/NavigationTests/RNNBottomTabsController+Helpers.m b/playground/ios/NavigationTests/RNNBottomTabsController+Helpers.m index 4d1b02fc541..1ee54d761e9 100644 --- a/playground/ios/NavigationTests/RNNBottomTabsController+Helpers.m +++ b/playground/ios/NavigationTests/RNNBottomTabsController+Helpers.m @@ -1,6 +1,7 @@ #import "RNNBottomTabsController+Helpers.h" #import "BottomTabsPresenterCreator.h" #import "BottomTabPresenterCreator.h" +#import @implementation RNNBottomTabsController (Helpers) @@ -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