Browse Source

Fix back button initialization on setStackRoot (#5246)

Yogev Ben David 5 years ago
parent
commit
c0ad1945a3
No account linked to committer's email address

+ 0
- 9
lib/ios/RNNNavigationControllerPresenter.m View File

46
 	[navigationController rnn_setNavigationBarLargeTitleFontFamily:[options.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[options.topBar.largeTitle.color getWithDefaultValue:nil]];
46
 	[navigationController rnn_setNavigationBarLargeTitleFontFamily:[options.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[options.topBar.largeTitle.color getWithDefaultValue:nil]];
47
 	[navigationController rnn_setNavigationBarFontFamily:[options.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.title.fontSize getWithDefaultValue:nil] color:[options.topBar.title.color getWithDefaultValue:nil]];
47
 	[navigationController rnn_setNavigationBarFontFamily:[options.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.title.fontSize getWithDefaultValue:nil] color:[options.topBar.title.color getWithDefaultValue:nil]];
48
 	[navigationController rnn_setBackButtonColor:[options.topBar.backButton.color getWithDefaultValue:nil]];
48
 	[navigationController rnn_setBackButtonColor:[options.topBar.backButton.color getWithDefaultValue:nil]];
49
-	[navigationController rnn_setBackButtonIcon:[options.topBar.backButton.icon getWithDefaultValue:nil] withColor:[options.topBar.backButton.color getWithDefaultValue:nil] title:[options.topBar.backButton.showTitle getWithDefaultValue:YES] ? [options.topBar.backButton.title getWithDefaultValue:nil] : @""];
50
 }
49
 }
51
 
50
 
52
 - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
51
 - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
53
 	[super applyOptionsOnWillMoveToParentViewController:options];
52
 	[super applyOptionsOnWillMoveToParentViewController:options];
54
-	
55
-	RNNNavigationController* navigationController = self.bindedViewController;
56
-	[navigationController rnn_setBackButtonIcon:[options.topBar.backButton.icon getWithDefaultValue:nil] withColor:[options.topBar.backButton.color getWithDefaultValue:nil] title:[options.topBar.backButton.showTitle getWithDefaultValue:YES] ? [options.topBar.backButton.title getWithDefaultValue:nil] : @""];
57
 }
53
 }
58
 
54
 
59
 - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
55
 - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
122
 		[navigationController rnn_setNavigationBarLargeTitleVisible:newOptions.topBar.largeTitle.visible.get];
118
 		[navigationController rnn_setNavigationBarLargeTitleVisible:newOptions.topBar.largeTitle.visible.get];
123
 	}
119
 	}
124
 	
120
 	
125
-	if (newOptions.topBar.backButton.icon.hasValue || newOptions.topBar.backButton.showTitle.hasValue || newOptions.topBar.backButton.color.hasValue || newOptions.topBar.backButton.title.hasValue) {
126
-		[navigationController rnn_setBackButtonIcon:[newOptions.topBar.backButton.icon getWithDefaultValue:nil] withColor:[newOptions.topBar.backButton.color getWithDefaultValue:nil] title:[newOptions.topBar.backButton.showTitle getWithDefaultValue:YES] ? [newOptions.topBar.backButton.title getWithDefaultValue:nil] : @""];
127
-		
128
-	}
129
-	
130
 	if (newOptions.topBar.backButton.color.hasValue) {
121
 	if (newOptions.topBar.backButton.color.hasValue) {
131
 		[navigationController rnn_setBackButtonColor:newOptions.topBar.backButton.color.get];
122
 		[navigationController rnn_setBackButtonColor:newOptions.topBar.backButton.color.get];
132
 	}
123
 	}

+ 10
- 0
lib/ios/RNNViewControllerPresenter.m View File

28
 	_navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:self.bindedViewController componentRegistry:_componentRegistry];
28
 	_navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:self.bindedViewController componentRegistry:_componentRegistry];
29
 }
29
 }
30
 
30
 
31
+- (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
32
+	[super applyOptionsOnWillMoveToParentViewController:options];
33
+	UIViewController* viewController = self.bindedViewController;
34
+	[viewController rnn_setBackButtonIcon:[options.topBar.backButton.icon getWithDefaultValue:nil] withColor:[options.topBar.backButton.color getWithDefaultValue:nil] title:[options.topBar.backButton.showTitle getWithDefaultValue:YES] ? [options.topBar.backButton.title getWithDefaultValue:nil] : @""];
35
+}
36
+
31
 - (void)applyOptions:(RNNNavigationOptions *)options {
37
 - (void)applyOptions:(RNNNavigationOptions *)options {
32
 	[super applyOptions:options];
38
 	[super applyOptions:options];
33
 	
39
 	
150
 	if (newOptions.topBar.title.component.name.hasValue) {
156
 	if (newOptions.topBar.title.component.name.hasValue) {
151
 		[self setCustomNavigationTitleView:newOptions perform:nil];
157
 		[self setCustomNavigationTitleView:newOptions perform:nil];
152
 	}
158
 	}
159
+	
160
+	if (newOptions.topBar.backButton.icon.hasValue || newOptions.topBar.backButton.showTitle.hasValue || newOptions.topBar.backButton.color.hasValue || newOptions.topBar.backButton.title.hasValue) {
161
+		[viewController rnn_setBackButtonIcon:[newOptions.topBar.backButton.icon getWithDefaultValue:nil] withColor:[newOptions.topBar.backButton.color getWithDefaultValue:nil] title:[newOptions.topBar.backButton.showTitle getWithDefaultValue:YES] ? [newOptions.topBar.backButton.title getWithDefaultValue:nil] : @""];
162
+	}
153
 }
163
 }
154
 
164
 
155
 - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
165
 - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {

+ 0
- 57
lib/ios/ReactNativeNavigationTests/RNNNavigationControllerPresenterTest.m View File

35
 	[_bindedViewController verify];
35
 	[_bindedViewController verify];
36
 }
36
 }
37
 
37
 
38
-
39
-- (void)testApplyOptions_shouldSetBackButtonOnBindedViewController_withTitle {
40
-	Text* title = [[Text alloc] initWithValue:@"Title"];
41
-	self.options.topBar.backButton.title = title;
42
-	[[_bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:title.get];
43
-	[self.uut applyOptions:self.options];
44
-	[_bindedViewController verify];
45
-}
46
-
47
-- (void)testApplyOptions_shouldSetBackButtonOnBindedViewController_withHideTitle {
48
-	Text* title = [[Text alloc] initWithValue:@"Title"];
49
-	self.options.topBar.backButton.title = title;
50
-	self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
51
-	[[_bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:@""];
52
-	[self.uut applyOptions:self.options];
53
-	[_bindedViewController verify];
54
-}
55
-
56
-- (void)testApplyOptions_shouldSetBackButtonOnBindedViewController_withIcon {
57
-	Image* image = [[Image alloc] initWithValue:[UIImage new]];
58
-	self.options.topBar.backButton.icon = image;
59
-	[[_bindedViewController expect] rnn_setBackButtonIcon:image.get withColor:nil title:nil];
60
-	[self.uut applyOptions:self.options];
61
-	[_bindedViewController verify];
62
-}
63
-
64
-- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withTitle {
65
-	Text* title = [[Text alloc] initWithValue:@"Title"];
66
-	self.options.topBar.backButton.title = title;
67
-	[[_bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:title.get];
68
-	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
69
-	[_bindedViewController verify];
70
-}
71
-
72
-- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withHideTitle {
73
-	Text* title = [[Text alloc] initWithValue:@"Title"];
74
-	self.options.topBar.backButton.title = title;
75
-	self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
76
-	[[_bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:@""];
77
-	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
78
-	[_bindedViewController verify];
79
-}
80
-
81
-- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withIcon {
82
-	Image* image = [[Image alloc] initWithValue:[UIImage new]];
83
-	self.options.topBar.backButton.icon = image;
84
-	[[_bindedViewController expect] rnn_setBackButtonIcon:image.get withColor:nil title:nil];
85
-	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
86
-	[_bindedViewController verify];
87
-}
88
-
89
-- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withDefaultValues {
90
-	[[_bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:nil];
91
-	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
92
-	[_bindedViewController verify];
93
-}
94
-
95
 - (void)testApplyOptionsBeforePoppingShouldSetTopBarBackgroundForPoppingViewController {
38
 - (void)testApplyOptionsBeforePoppingShouldSetTopBarBackgroundForPoppingViewController {
96
 	_options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
39
 	_options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
97
 	
40
 	

+ 30
- 0
lib/ios/ReactNativeNavigationTests/RNNViewControllerPresenterTest.m View File

173
 	XCTAssertEqual(self.uut.bindedComponentId, @"componentId");
173
 	XCTAssertEqual(self.uut.bindedComponentId, @"componentId");
174
 }
174
 }
175
 
175
 
176
+- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withTitle {
177
+	Text* title = [[Text alloc] initWithValue:@"Title"];
178
+	self.options.topBar.backButton.title = title;
179
+	[[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:title.get];
180
+	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
181
+	[(id)self.bindedViewController verify];
182
+}
183
+
184
+- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withHideTitle {
185
+	Text* title = [[Text alloc] initWithValue:@"Title"];
186
+	self.options.topBar.backButton.title = title;
187
+	self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
188
+	[[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:@""];
189
+	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
190
+	[(id)self.bindedViewController verify];
191
+}
192
+
193
+- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withIcon {
194
+	Image* image = [[Image alloc] initWithValue:[UIImage new]];
195
+	self.options.topBar.backButton.icon = image;
196
+	[[(id)self.bindedViewController expect] rnn_setBackButtonIcon:image.get withColor:nil title:nil];
197
+	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
198
+	[(id)self.bindedViewController verify];
199
+}
200
+
201
+- (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withDefaultValues {
202
+	[[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:nil];
203
+	[self.uut applyOptionsOnWillMoveToParentViewController:self.options];
204
+	[(id)self.bindedViewController verify];
205
+}
176
 
206
 
177
 
207
 
178
 - (RNNLayoutInfo *)createLayoutInfoWithComponentId:(NSString *)componentId {
208
 - (RNNLayoutInfo *)createLayoutInfoWithComponentId:(NSString *)componentId {

+ 0
- 39
lib/ios/ReactNativeNavigationTests/UINavigationController+RNNOptionsTest.m View File

11
     [super setUp];
11
     [super setUp];
12
 }
12
 }
13
 
13
 
14
-- (void)testSetBackButtonIcon_withColor_shouldSetColor {
15
-	UIViewController* viewController = [UIViewController new];
16
-	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
17
-	UIColor* color = [UIColor blackColor];
18
-	
19
-	[nav rnn_setBackButtonIcon:nil withColor:color title:nil];
20
-	XCTAssertEqual(color, viewController.navigationItem.backBarButtonItem.tintColor);
21
-}
22
-
23
-- (void)testSetBackButtonIcon_withColor_shouldSetTitle {
24
-	UIViewController* viewController = [UIViewController new];
25
-	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
26
-	NSString* title = @"Title";
27
-	
28
-	[nav rnn_setBackButtonIcon:nil withColor:nil title:title];
29
-	XCTAssertEqual(title, viewController.navigationItem.backBarButtonItem.title);
30
-}
31
-
32
-- (void)testSetBackButtonIcon_withColor_shouldSetIcon {
33
-	UIViewController* viewController = [UIViewController new];
34
-	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
35
-	UIImage* icon = [UIImage new];
36
-	
37
-	[nav rnn_setBackButtonIcon:icon withColor:nil title:nil];
38
-	XCTAssertEqual(icon, viewController.navigationItem.backBarButtonItem.image);
39
-}
40
-
41
-- (void)testSetBackButtonIcon_shouldSetTitleOnPreviousViewControllerIfExists {
42
-	UIViewController* viewController = [UIViewController new];
43
-	UIViewController* viewController2 = [UIViewController new];
44
-	UINavigationController* nav = [[UINavigationController alloc] init];
45
-	[nav setViewControllers:@[viewController, viewController2]];
46
-	NSString* title = @"Title";
47
-
48
-	[nav rnn_setBackButtonIcon:nil withColor:nil title:title];
49
-	XCTAssertEqual(title, viewController.navigationItem.backBarButtonItem.title);
50
-}
51
-
52
-
53
 @end
14
 @end

+ 38
- 0
lib/ios/ReactNativeNavigationTests/UIViewController+LayoutProtocolTest.m View File

1
 #import <XCTest/XCTest.h>
1
 #import <XCTest/XCTest.h>
2
 #import <OCMock/OCMock.h>
2
 #import <OCMock/OCMock.h>
3
 #import "UIViewController+LayoutProtocol.h"
3
 #import "UIViewController+LayoutProtocol.h"
4
+#import "UIViewController+RNNOptions.h"
4
 #import "RNNViewControllerPresenter.h"
5
 #import "RNNViewControllerPresenter.h"
5
 #import "RCTConvert+Modal.h"
6
 #import "RCTConvert+Modal.h"
6
 
7
 
39
 	XCTAssertEqual(uut.viewControllers[1], child2);
40
 	XCTAssertEqual(uut.viewControllers[1], child2);
40
 }
41
 }
41
 
42
 
43
+- (void)testSetBackButtonIcon_withColor_shouldSetColor {
44
+	UIViewController* uut = [UIViewController new];
45
+	[[UINavigationController alloc] initWithRootViewController:uut];
46
+	UIColor* color = [UIColor blackColor];
47
+	
48
+	[uut rnn_setBackButtonIcon:nil withColor:color title:nil];
49
+	XCTAssertEqual(color, uut.navigationItem.backBarButtonItem.tintColor);
50
+}
51
+
52
+- (void)testSetBackButtonIcon_withColor_shouldSetTitle {
53
+	UIViewController* uut = [UIViewController new];
54
+	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:uut];
55
+	NSString* title = @"Title";
56
+	
57
+	[uut rnn_setBackButtonIcon:nil withColor:nil title:title];
58
+	XCTAssertEqual(title, uut.navigationItem.backBarButtonItem.title);
59
+}
60
+
61
+- (void)testSetBackButtonIcon_withColor_shouldSetIcon {
62
+	UIViewController* uut = [UIViewController new];
63
+	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:uut];
64
+	UIImage* icon = [UIImage new];
65
+	
66
+	[uut rnn_setBackButtonIcon:icon withColor:nil title:nil];
67
+	XCTAssertEqual(icon, uut.navigationItem.backBarButtonItem.image);
68
+}
69
+
70
+- (void)testSetBackButtonIcon_shouldSetTitleOnPreviousViewControllerIfExists {
71
+	UIViewController* uut = [UIViewController new];
72
+	UIViewController* viewController2 = [UIViewController new];
73
+	UINavigationController* nav = [[UINavigationController alloc] init];
74
+	[nav setViewControllers:@[uut, viewController2]];
75
+	NSString* title = @"Title";
76
+	
77
+	[uut rnn_setBackButtonIcon:nil withColor:nil title:title];
78
+	XCTAssertEqual(title, uut.navigationItem.backBarButtonItem.title);
79
+}
42
 
80
 
43
 @end
81
 @end

+ 0
- 2
lib/ios/UINavigationController+RNNOptions.h View File

24
 
24
 
25
 - (void)rnn_setNavigationBarClipsToBounds:(BOOL)clipsToBounds;
25
 - (void)rnn_setNavigationBarClipsToBounds:(BOOL)clipsToBounds;
26
 
26
 
27
-- (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title;
28
-
29
 - (void)rnn_setNavigationBarLargeTitleVisible:(BOOL)visible;
27
 - (void)rnn_setNavigationBarLargeTitleVisible:(BOOL)visible;
30
 
28
 
31
 - (void)rnn_setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize color:(UIColor *)color;
29
 - (void)rnn_setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize color:(UIColor *)color;

+ 0
- 20
lib/ios/UINavigationController+RNNOptions.m View File

1
 #import "UINavigationController+RNNOptions.h"
1
 #import "UINavigationController+RNNOptions.h"
2
 #import "RNNFontAttributesCreator.h"
2
 #import "RNNFontAttributesCreator.h"
3
-#import "UIImage+tint.h"
4
 
3
 
5
 const NSInteger BLUR_TOPBAR_TAG = 78264802;
4
 const NSInteger BLUR_TOPBAR_TAG = 78264802;
6
 
5
 
96
 	}
95
 	}
97
 }
96
 }
98
 
97
 
99
-- (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title {
100
-	UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
101
-	if (icon) {
102
-		backItem.image = color
103
-		? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
104
-		: icon;
105
-		
106
-		[self.navigationBar setBackIndicatorImage:[UIImage new]];
107
-		[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
108
-	}
109
-	
110
-	UIViewController *lastViewControllerInStack = self.viewControllers.count > 1 ? [self.viewControllers objectAtIndex:self.viewControllers.count-2] : self.topViewController;
111
-	
112
-	backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
113
-	backItem.tintColor = color;
114
-	
115
-	lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
116
-}
117
-
118
 - (void)rnn_setBackButtonColor:(UIColor *)color {
98
 - (void)rnn_setBackButtonColor:(UIColor *)color {
119
 	self.navigationBar.tintColor = color;
99
 	self.navigationBar.tintColor = color;
120
 }
100
 }

+ 2
- 0
lib/ios/UIViewController+RNNOptions.h View File

34
 
34
 
35
 - (void)rnn_setInterceptTouchOutside:(BOOL)interceptTouchOutside;
35
 - (void)rnn_setInterceptTouchOutside:(BOOL)interceptTouchOutside;
36
 
36
 
37
+- (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title;
38
+
37
 - (BOOL)isModal;
39
 - (BOOL)isModal;
38
 
40
 
39
 @end
41
 @end

+ 20
- 0
lib/ios/UIViewController+RNNOptions.m View File

1
 #import "UIViewController+RNNOptions.h"
1
 #import "UIViewController+RNNOptions.h"
2
 #import <React/RCTRootView.h>
2
 #import <React/RCTRootView.h>
3
+#import "UIImage+tint.h"
3
 
4
 
4
 #define kStatusBarAnimationDuration 0.35
5
 #define kStatusBarAnimationDuration 0.35
5
 const NSInteger BLUR_STATUS_TAG = 78264801;
6
 const NSInteger BLUR_STATUS_TAG = 78264801;
162
 	}
163
 	}
163
 }
164
 }
164
 
165
 
166
+- (void)rnn_setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title {
167
+	UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
168
+	if (icon) {
169
+		backItem.image = color
170
+		? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
171
+		: icon;
172
+		
173
+		[self.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
174
+		[self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
175
+	}
176
+	
177
+	UIViewController *lastViewControllerInStack = self.navigationController.viewControllers.count > 1 ? [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] : self.navigationController.topViewController;
178
+	
179
+	backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
180
+	backItem.tintColor = color;
181
+	
182
+	lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
183
+}
184
+
165
 @end
185
 @end