Skip to content

Commit 3757ff7

Browse files
authoredMar 9, 2020
Create new UITabBarItem instance on each bottomTab update (#6018)
Fix an issue where bottomTab.testID doesn't get updated on mergeOptions unless a new UITabBarItem is created. Look like an issue in iOS where tabBarItem.accessibilityIdentifier doesn't get updated unless a new tabBarItem is created.
1 parent 6d61ec0 commit 3757ff7

16 files changed

+42
-26
lines changed
 

Diff for: ‎lib/ios/BottomTabAppearancePresenter.h

-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
API_AVAILABLE(ios(13.0))
44
@interface BottomTabAppearancePresenter : BottomTabPresenter
55

6-
- (instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions children:(NSArray<UIViewController *> *)children;
7-
86
@end

Diff for: ‎lib/ios/BottomTabAppearancePresenter.m

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@
33

44
@implementation BottomTabAppearancePresenter
55

6-
- (instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions children:(NSArray<UIViewController *> *)children {
7-
self = [super initWithDefaultOptions:defaultOptions];
8-
for (UIViewController* child in children) {
9-
child.tabBarItem.standardAppearance = [[UITabBarAppearance alloc] init];
10-
}
11-
return self;
12-
}
13-
146
- (void)createTabBarItem:(UIViewController *)child bottomTabOptions:(RNNBottomTabOptions *)bottomTabOptions {
15-
child.tabBarItem = [TabBarItemAppearanceCreator updateTabBarItem:child.tabBarItem bottomTabOptions:bottomTabOptions];
7+
child.tabBarItem = [TabBarItemAppearanceCreator createTabBarItem:bottomTabOptions mergeItem:child.tabBarItem];
168
}
179

1810
@end

Diff for: ‎lib/ios/BottomTabPresenter.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ - (void)applyOptions:(RNNNavigationOptions *)options child:(UIViewController *)c
1515
- (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options child:(UIViewController *)child {
1616
RNNNavigationOptions * withDefault = [options withDefault:self.defaultOptions];
1717

18+
[self createTabBarItem:child bottomTabOptions:withDefault.bottomTab];
1819
[child setTabBarItemBadge:[withDefault.bottomTab.badge getWithDefaultValue:[NSNull null]]];
1920
[child setTabBarItemBadgeColor:[withDefault.bottomTab.badgeColor getWithDefaultValue:nil]];
20-
[self createTabBarItem:child bottomTabOptions:withDefault.bottomTab];
2121
}
2222

2323
- (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions child:(UIViewController *)child {
2424
RNNNavigationOptions* withDefault = (RNNNavigationOptions *) [[resolvedOptions withDefault:self.defaultOptions] overrideOptions:options];
2525

26+
if (options.bottomTab.hasValue) [self createTabBarItem:child bottomTabOptions:withDefault.bottomTab];
2627
if (options.bottomTab.badge.hasValue) [child setTabBarItemBadge:options.bottomTab.badge.get];
2728
if (options.bottomTab.badgeColor.hasValue) [child setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
28-
if (options.bottomTab.hasValue) [self createTabBarItem:child bottomTabOptions:withDefault.bottomTab];
2929
}
3030

3131
- (void)createTabBarItem:(UIViewController *)child bottomTabOptions:(RNNBottomTabOptions *)bottomTabOptions {
32-
child.tabBarItem = [RNNTabBarItemCreator updateTabBarItem:child.tabBarItem bottomTabOptions:bottomTabOptions];
32+
child.tabBarItem = [RNNTabBarItemCreator createTabBarItem:bottomTabOptions mergeItem:child.tabBarItem];
3333
}
3434

3535
@end

Diff for: ‎lib/ios/BottomTabPresenterCreator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
@interface BottomTabPresenterCreator : NSObject
55

6-
+ (BottomTabPresenter *)createWithDefaultOptions:(RNNNavigationOptions *)defaultOptions children:(NSArray<UIViewController *> *)children;
6+
+ (BottomTabPresenter *)createWithDefaultOptions:(RNNNavigationOptions *)defaultOptions;
77

88
@end

Diff for: ‎lib/ios/BottomTabPresenterCreator.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
@implementation BottomTabPresenterCreator
55

6-
+ (BottomTabPresenter *)createWithDefaultOptions:(RNNNavigationOptions *)defaultOptions children:(NSArray<UIViewController *> *)children {
6+
+ (BottomTabPresenter *)createWithDefaultOptions:(RNNNavigationOptions *)defaultOptions {
77
if (@available(iOS 13.0, *)) {
8-
return [[BottomTabAppearancePresenter alloc] initWithDefaultOptions:defaultOptions children:children];
8+
return [[BottomTabAppearancePresenter alloc] initWithDefaultOptions:defaultOptions];
99
} else {
1010
return [[BottomTabPresenter alloc] initWithDefaultOptions:defaultOptions];
1111
}

Diff for: ‎lib/ios/RNNControllerFactory.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ - (UIViewController *)createBottomTabs:(RNNLayoutNode*)node {
152152
RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
153153
RNNBottomTabsPresenter* presenter = [BottomTabsPresenterCreator createWithDefaultOptions:_defaultOptions];
154154
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
155-
BottomTabPresenter* bottomTabPresenter = [BottomTabPresenterCreator createWithDefaultOptions:_defaultOptions children:childViewControllers];;
155+
BottomTabPresenter* bottomTabPresenter = [BottomTabPresenterCreator createWithDefaultOptions:_defaultOptions];
156156
RNNDotIndicatorPresenter* dotIndicatorPresenter = [[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:_defaultOptions];
157157
BottomTabsBaseAttacher* bottomTabsAttacher = [_bottomTabsAttachModeFactory fromOptions:options];
158158

Diff for: ‎lib/ios/RNNTabBarItemCreator.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
@interface RNNTabBarItemCreator : NSObject
66

7-
+ (UITabBarItem *)updateTabBarItem:(UITabBarItem *)tabItem bottomTabOptions:(RNNBottomTabOptions *)bottomTabOptions;
7+
+ (UITabBarItem *)createTabBarItem:(RNNBottomTabOptions *)bottomTabOptions mergeItem:(UITabBarItem *)mergeItem;
8+
9+
+ (UITabBarItem *)createTabBarItem:(UITabBarItem *)mergeItem;
810

911
+ (void)setTitleAttributes:(UITabBarItem *)tabItem titleAttributes:(NSDictionary *)titleAttributes;
1012

Diff for: ‎lib/ios/RNNTabBarItemCreator.m

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
@implementation RNNTabBarItemCreator
66

7-
+ (UITabBarItem *)updateTabBarItem:(UITabBarItem *)tabItem bottomTabOptions:(RNNBottomTabOptions *)bottomTabOptions {
7+
+ (UITabBarItem *)createTabBarItem:(UITabBarItem *)mergeItem {
8+
return [UITabBarItem new];
9+
}
10+
11+
+ (UITabBarItem *)createTabBarItem:(RNNBottomTabOptions *)bottomTabOptions mergeItem:(UITabBarItem *)mergeItem {
12+
UITabBarItem* tabItem = [self createTabBarItem:mergeItem];
813
UIImage* icon = [bottomTabOptions.icon getWithDefaultValue:nil];
914
UIImage* selectedIcon = [bottomTabOptions.selectedIcon getWithDefaultValue:icon];
1015
UIColor* iconColor = [bottomTabOptions.iconColor getWithDefaultValue:nil];

Diff for: ‎lib/ios/TabBarItemAppearanceCreator.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "RNNTabBarItemCreator.h"
22

3+
API_AVAILABLE(ios(13.0))
34
@interface TabBarItemAppearanceCreator : RNNTabBarItemCreator
45

56
@end

Diff for: ‎lib/ios/TabBarItemAppearanceCreator.m

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
@implementation TabBarItemAppearanceCreator
44

5+
+ (UITabBarItem *)createTabBarItem:(UITabBarItem *)mergeItem {
6+
UITabBarItem* tabBarItem = [super createTabBarItem:mergeItem];
7+
tabBarItem.standardAppearance = mergeItem.standardAppearance ?: [[UITabBarAppearance alloc] init];
8+
return tabBarItem;
9+
}
10+
511
+ (void)setTitleAttributes:(UITabBarItem *)tabItem titleAttributes:(NSDictionary *)titleAttributes {
612
tabItem.standardAppearance.stackedLayoutAppearance.normal.titleTextAttributes = titleAttributes;
713
}

Diff for: ‎playground/ios/NavigationTests/BottomTabPresenterTest.m

+14
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ - (void)testMergeOptions_shouldSetTabBarItemColorWithDefaultOptions {
5252
XCTAssertEqual(self.componentViewController.tabBarItem.title, @"title");
5353
}
5454

55+
- (void)testMergeOptions_shouldCreateNewTabBarItemInstance {
56+
RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
57+
defaultOptions.bottomTab.selectedIconColor = [Color withColor:UIColor.greenColor];
58+
self.uut.defaultOptions = defaultOptions;
59+
60+
RNNNavigationOptions* mergeOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
61+
mergeOptions.bottomTab.text = [[Text alloc] initWithValue:@"title"];
62+
63+
UITabBarItem* currentTabBarItem = self.componentViewController.tabBarItem;
64+
[self.uut mergeOptions:mergeOptions resolvedOptions:self.options child:self.componentViewController];
65+
UITabBarItem* newTabBarItem = self.componentViewController.tabBarItem;
66+
XCTAssertNotEqual(currentTabBarItem, newTabBarItem);
67+
}
68+
5569
@end

Diff for: ‎playground/ios/NavigationTests/RNNBottomTabsAppearancePresenterTest.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ - (void)setUp {
2525
self.children = @[[[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:[[RNNComponentPresenter alloc] initWithDefaultOptions:nil] options:nil defaultOptions:nil]];
2626
self.dotIndicatorPresenter = [OCMockObject partialMockForObject:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil]];
2727
self.uut = [OCMockObject partialMockForObject:[BottomTabsPresenterCreator createWithDefaultOptions:nil]];
28-
self.boundViewController = [OCMockObject partialMockForObject:[[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:self.uut bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:nil children:self.children] dotIndicatorPresenter:self.dotIndicatorPresenter eventEmitter:nil childViewControllers:self.children bottomTabsAttacher:nil]];
28+
self.boundViewController = [OCMockObject partialMockForObject:[[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:self.uut bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:nil] dotIndicatorPresenter:self.dotIndicatorPresenter eventEmitter:nil childViewControllers:self.children bottomTabsAttacher:nil]];
2929
[self.uut bindViewController:self.boundViewController];
3030
self.options = [[RNNNavigationOptions alloc] initEmptyOptions];
3131
}

Diff for: ‎playground/ios/NavigationTests/RNNBottomTabsController+Helpers.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ + (RNNBottomTabsController *)create {
1111

1212
+ (RNNBottomTabsController *)createWithChildren:(NSArray *)children {
1313
RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
14-
return [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:defaultOptions presenter:[BottomTabsPresenterCreator createWithDefaultOptions:defaultOptions] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:defaultOptions children:children] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:defaultOptions] eventEmitter:nil childViewControllers:children bottomTabsAttacher:nil];
14+
return [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:defaultOptions presenter:[BottomTabsPresenterCreator createWithDefaultOptions:defaultOptions] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:defaultOptions] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:defaultOptions] eventEmitter:nil childViewControllers:children bottomTabsAttacher:nil];
1515
}
1616

1717
@end

Diff for: ‎playground/ios/NavigationTests/RNNDotIndicatorPresenterTest.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ @implementation RNNDotIndicatorPresenterTest
2020
- (void)setUp {
2121
[super setUp];
2222
self.child = [self createChild];
23-
self.bottomTabPresenter = [BottomTabPresenterCreator createWithDefaultOptions:nil children:@[self.child]];
23+
self.bottomTabPresenter = [BottomTabPresenterCreator createWithDefaultOptions:nil];
2424
self.uut = [OCMockObject partialMockForObject:[RNNDotIndicatorPresenter new]];
2525
self.bottomTabs = [OCMockObject partialMockForObject:[RNNBottomTabsController createWithChildren:@[self.child]]];
2626

Diff for: ‎playground/ios/NavigationTests/RNNRootViewControllerTest.m

-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ - (void)testTabBadge {
180180
NSString* tabBadgeInput = @"5";
181181
self.options.bottomTab.badge = [[Text alloc] initWithValue:tabBadgeInput];
182182
NSMutableArray* controllers = [NSMutableArray new];
183-
UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
184-
[self.uut setTabBarItem:item];
185183
[controllers addObject:self.uut];
186184
__unused RNNBottomTabsController* vc = [RNNBottomTabsController createWithChildren:controllers];
187185
[self.uut willMoveToParentViewController:vc];

Diff for: ‎playground/ios/NavigationTests/RNNTabBarControllerTest.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (void)setUp {
2828
self.mockTabBarPresenter = [OCMockObject partialMockForObject:[[RNNBottomTabsPresenter alloc] init]];
2929
self.mockChildViewController = [OCMockObject partialMockForObject:[RNNComponentViewController new]];
3030
self.mockEventEmitter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
31-
self.originalUut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:self.mockTabBarPresenter bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:nil children:children] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil] eventEmitter:self.mockEventEmitter childViewControllers:children bottomTabsAttacher:nil];
31+
self.originalUut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:self.mockTabBarPresenter bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:nil] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil] eventEmitter:self.mockEventEmitter childViewControllers:children bottomTabsAttacher:nil];
3232
self.uut = [OCMockObject partialMockForObject:self.originalUut];
3333
OCMStub([self.uut selectedViewController]).andReturn(self.mockChildViewController);
3434
}

0 commit comments

Comments
 (0)
Please sign in to comment.