Browse Source

setting topbarBackgroundColor

Daniel Zlotin 7 years ago
parent
commit
f04a273edd

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationOptions.java View File

22
 
22
 
23
 	public void mergeWith(final NavigationOptions other) {
23
 	public void mergeWith(final NavigationOptions other) {
24
 		title = other.title;
24
 		title = other.title;
25
+		topBarBackgroundColor = other.topBarBackgroundColor;
25
 	}
26
 	}
26
 }
27
 }

+ 5
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ContainerViewController.java View File

54
 	@Override
54
 	@Override
55
 	public void onViewAppeared() {
55
 	public void onViewAppeared() {
56
 		super.onViewAppeared();
56
 		super.onViewAppeared();
57
+		ensureViewIsCreated();
58
+		applyNavigationOptions();
57
 		containerView.sendContainerStart();
59
 		containerView.sendContainerStart();
58
 	}
60
 	}
59
 
61
 
75
 		return containerView.asView();
77
 		return containerView.asView();
76
 	}
78
 	}
77
 
79
 
78
-	public void applyNavigationOptions(final NavigationOptions options) {
80
+	public void mergeNavigationOptions(final NavigationOptions options) {
79
 		navigationOptions.mergeWith(options);
81
 		navigationOptions.mergeWith(options);
80
 		applyNavigationOptions();
82
 		applyNavigationOptions();
81
 	}
83
 	}
82
 
84
 
83
 	private void applyNavigationOptions() {
85
 	private void applyNavigationOptions() {
84
 		if (getParentStackController() != null) {
86
 		if (getParentStackController() != null) {
85
-			getParentStackController().setTitle(navigationOptions.title);
87
+			getParentStackController().getTopBar().setTitle(navigationOptions.title);
88
+			getParentStackController().getTopBar().setBackgroundColor(navigationOptions.topBarBackgroundColor);
86
 		}
89
 		}
87
 	}
90
 	}
88
 
91
 

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

59
 	public void setOptions(final String containerId, NavigationOptions options) {
59
 	public void setOptions(final String containerId, NavigationOptions options) {
60
 		ViewController target = findControllerById(containerId);
60
 		ViewController target = findControllerById(containerId);
61
 		if (target instanceof ContainerViewController) {
61
 		if (target instanceof ContainerViewController) {
62
-			((ContainerViewController) target).applyNavigationOptions(options);
62
+			((ContainerViewController) target).mergeNavigationOptions(options);
63
 		}
63
 		}
64
 	}
64
 	}
65
 
65
 

+ 0
- 4
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

132
 		return stack.values();
132
 		return stack.values();
133
 	}
133
 	}
134
 
134
 
135
-	public void setTitle(final String title) {
136
-		topBar.setTitle(title);
137
-	}
138
-
139
 	public TopBar getTopBar() {
135
 	public TopBar getTopBar() {
140
 		ensureViewIsCreated();
136
 		ensureViewIsCreated();
141
 		return topBar;
137
 		return topBar;

+ 0
- 3
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestContainerView.java View File

22
 
22
 
23
 	@Override
23
 	@Override
24
 	public void destroy() {
24
 	public void destroy() {
25
-
26
 	}
25
 	}
27
 
26
 
28
 	@Override
27
 	@Override
29
 	public void sendContainerStart() {
28
 	public void sendContainerStart() {
30
-
31
 	}
29
 	}
32
 
30
 
33
 	@Override
31
 	@Override
34
 	public void sendContainerStop() {
32
 	public void sendContainerStop() {
35
-
36
 	}
33
 	}
37
 }
34
 }

+ 93
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ContainerViewControllerTest.java View File

1
 package com.reactnativenavigation.viewcontrollers;
1
 package com.reactnativenavigation.viewcontrollers;
2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
+import android.graphics.Color;
5
+import android.graphics.drawable.ColorDrawable;
4
 
6
 
5
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
6
 import com.reactnativenavigation.mocks.TestContainerView;
8
 import com.reactnativenavigation.mocks.TestContainerView;
12
 import static org.mockito.Mockito.spy;
14
 import static org.mockito.Mockito.spy;
13
 import static org.mockito.Mockito.times;
15
 import static org.mockito.Mockito.times;
14
 import static org.mockito.Mockito.verify;
16
 import static org.mockito.Mockito.verify;
17
+import static org.mockito.Mockito.when;
15
 
18
 
16
 public class ContainerViewControllerTest extends BaseTest {
19
 public class ContainerViewControllerTest extends BaseTest {
17
-	private ContainerViewController uut;
18
 	private Activity activity;
20
 	private Activity activity;
21
+	private ContainerViewController uut;
22
+	private ContainerViewController.ContainerView view;
23
+	private NavigationOptions initialNavigationOptions;
19
 
24
 
20
 	@Override
25
 	@Override
21
 	public void beforeEach() {
26
 	public void beforeEach() {
22
 		super.beforeEach();
27
 		super.beforeEach();
23
 		activity = newActivity();
28
 		activity = newActivity();
24
-	}
25
-
26
-	@Test
27
-	public void createsViewFromContainerViewCreator() throws Exception {
28
-		final ContainerViewController.ContainerView view = new TestContainerView(activity);
29
-		uut = new ContainerViewController(newActivity(), "id1", "containerName", new ContainerViewController.ContainerViewCreator() {
29
+		initialNavigationOptions = new NavigationOptions();
30
+		view = spy(new TestContainerView(activity));
31
+		uut = new ContainerViewController(activity, "containerId1", "containerName", new ContainerViewController.ContainerViewCreator() {
30
 			@Override
32
 			@Override
31
 			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
33
 			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
32
 				return view;
34
 				return view;
33
 			}
35
 			}
34
-		}, new NavigationOptions());
36
+		}, initialNavigationOptions);
37
+	}
38
+
39
+	@Test
40
+	public void createsViewFromContainerViewCreator() throws Exception {
35
 		assertThat(uut.getView()).isSameAs(view);
41
 		assertThat(uut.getView()).isSameAs(view);
36
 	}
42
 	}
37
 
43
 
38
 	@Test
44
 	@Test
39
-	public void containerViewDestroyedOnDestory() throws Exception {
40
-		final ContainerViewController.ContainerView view = spy(new TestContainerView(activity));
41
-		uut = new ContainerViewController(newActivity(), "id1", "containerName", new ContainerViewController.ContainerViewCreator() {
42
-			@Override
43
-			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
44
-				return view;
45
-			}
46
-		}, new NavigationOptions());
45
+	public void containerViewDestroyedOnDestroy() throws Exception {
47
 		uut.ensureViewIsCreated();
46
 		uut.ensureViewIsCreated();
48
-
49
 		verify(view, times(0)).destroy();
47
 		verify(view, times(0)).destroy();
50
 		uut.destroy();
48
 		uut.destroy();
51
 		verify(view, times(1)).destroy();
49
 		verify(view, times(1)).destroy();
52
 	}
50
 	}
51
+
52
+	@Test
53
+	public void lifecycleMethodsSentToContainerView() throws Exception {
54
+		uut.ensureViewIsCreated();
55
+		verify(view, times(0)).sendContainerStart();
56
+		verify(view, times(0)).sendContainerStop();
57
+		uut.onViewAppeared();
58
+		verify(view, times(1)).sendContainerStart();
59
+		verify(view, times(0)).sendContainerStop();
60
+		uut.onViewDisappear();
61
+		verify(view, times(1)).sendContainerStart();
62
+		verify(view, times(1)).sendContainerStop();
63
+	}
64
+
65
+	@Test
66
+	public void isViewShownOnlyIfContainerViewIsReady() throws Exception {
67
+		assertThat(uut.isViewShown()).isFalse();
68
+		uut.ensureViewIsCreated();
69
+		when(view.asView().isShown()).thenReturn(true);
70
+		assertThat(uut.isViewShown()).isFalse();
71
+		when(view.isReady()).thenReturn(true);
72
+		assertThat(uut.isViewShown()).isTrue();
73
+	}
74
+
75
+	@Test
76
+	public void applyNavigationOptionsHandlesNoParentStack() throws Exception {
77
+		assertThat(uut.getParentStackController()).isNull();
78
+		uut.onViewAppeared();
79
+		assertThat(uut.getParentStackController()).isNull();
80
+	}
81
+
82
+	@Test
83
+	public void initialOptionsAppliedOnAppear() throws Exception {
84
+		assertThat(uut.getNavigationOptions()).isSameAs(initialNavigationOptions);
85
+		initialNavigationOptions.title = "the title";
86
+		StackController stackController = new StackController(activity, "stackId");
87
+		stackController.push(uut);
88
+		assertThat(stackController.getTopBar().getTitle()).isEmpty();
89
+
90
+		uut.onViewAppeared();
91
+		assertThat(stackController.getTopBar().getTitle()).isEqualTo("the title");
92
+	}
93
+
94
+	@Test
95
+	public void mergeNavigationOptionsUpdatesCurrentOptions() throws Exception {
96
+		assertThat(uut.getNavigationOptions().title).isEmpty();
97
+		NavigationOptions options = new NavigationOptions();
98
+		options.title = "new title";
99
+		uut.mergeNavigationOptions(options);
100
+		assertThat(uut.getNavigationOptions().title).isEqualTo("new title");
101
+		assertThat(uut.getNavigationOptions()).isSameAs(initialNavigationOptions);
102
+	}
103
+
104
+	@Test
105
+	public void reappliesOptionsOnMerge() throws Exception {
106
+		StackController stackController = new StackController(activity, "stackId");
107
+		stackController.push(uut);
108
+		assertThat(stackController.getTopBar().getTitle()).isEmpty();
109
+
110
+		NavigationOptions opts = new NavigationOptions();
111
+		opts.title = "the new title";
112
+		uut.mergeNavigationOptions(opts);
113
+
114
+		assertThat(stackController.getTopBar().getTitle()).isEqualTo("the new title");
115
+	}
116
+
117
+	@Test
118
+	public void appliesTopBackBackgroundColor() throws Exception {
119
+		StackController stackController = new StackController(activity, "stackId");
120
+		stackController.push(uut);
121
+		assertThat(((ColorDrawable) stackController.getTopBar().getBackground()).getColor()).isNotEqualTo(0xffff0000);
122
+
123
+		NavigationOptions opts = new NavigationOptions();
124
+		opts.topBarBackgroundColor = Color.RED;
125
+		uut.mergeNavigationOptions(opts);
126
+
127
+		assertThat(((ColorDrawable) stackController.getTopBar().getBackground()).getColor()).isEqualTo(0xffff0000);
128
+	}
53
 }
129
 }