Преглед изворни кода

setting topbarBackgroundColor

Daniel Zlotin пре 7 година
родитељ
комит
f04a273edd

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationOptions.java Прегледај датотеку

@@ -22,5 +22,6 @@ public class NavigationOptions {
22 22
 
23 23
 	public void mergeWith(final NavigationOptions other) {
24 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 Прегледај датотеку

@@ -54,6 +54,8 @@ public class ContainerViewController extends ViewController {
54 54
 	@Override
55 55
 	public void onViewAppeared() {
56 56
 		super.onViewAppeared();
57
+		ensureViewIsCreated();
58
+		applyNavigationOptions();
57 59
 		containerView.sendContainerStart();
58 60
 	}
59 61
 
@@ -75,14 +77,15 @@ public class ContainerViewController extends ViewController {
75 77
 		return containerView.asView();
76 78
 	}
77 79
 
78
-	public void applyNavigationOptions(final NavigationOptions options) {
80
+	public void mergeNavigationOptions(final NavigationOptions options) {
79 81
 		navigationOptions.mergeWith(options);
80 82
 		applyNavigationOptions();
81 83
 	}
82 84
 
83 85
 	private void applyNavigationOptions() {
84 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 Прегледај датотеку

@@ -59,7 +59,7 @@ public class Navigator extends ParentController {
59 59
 	public void setOptions(final String containerId, NavigationOptions options) {
60 60
 		ViewController target = findControllerById(containerId);
61 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 Прегледај датотеку

@@ -132,10 +132,6 @@ public class StackController extends ParentController {
132 132
 		return stack.values();
133 133
 	}
134 134
 
135
-	public void setTitle(final String title) {
136
-		topBar.setTitle(title);
137
-	}
138
-
139 135
 	public TopBar getTopBar() {
140 136
 		ensureViewIsCreated();
141 137
 		return topBar;

+ 0
- 3
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestContainerView.java Прегледај датотеку

@@ -22,16 +22,13 @@ public class TestContainerView extends View implements ContainerViewController.C
22 22
 
23 23
 	@Override
24 24
 	public void destroy() {
25
-
26 25
 	}
27 26
 
28 27
 	@Override
29 28
 	public void sendContainerStart() {
30
-
31 29
 	}
32 30
 
33 31
 	@Override
34 32
 	public void sendContainerStop() {
35
-
36 33
 	}
37 34
 }

+ 93
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ContainerViewControllerTest.java Прегледај датотеку

@@ -1,6 +1,8 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.graphics.Color;
5
+import android.graphics.drawable.ColorDrawable;
4 6
 
5 7
 import com.reactnativenavigation.BaseTest;
6 8
 import com.reactnativenavigation.mocks.TestContainerView;
@@ -12,42 +14,116 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
12 14
 import static org.mockito.Mockito.spy;
13 15
 import static org.mockito.Mockito.times;
14 16
 import static org.mockito.Mockito.verify;
17
+import static org.mockito.Mockito.when;
15 18
 
16 19
 public class ContainerViewControllerTest extends BaseTest {
17
-	private ContainerViewController uut;
18 20
 	private Activity activity;
21
+	private ContainerViewController uut;
22
+	private ContainerViewController.ContainerView view;
23
+	private NavigationOptions initialNavigationOptions;
19 24
 
20 25
 	@Override
21 26
 	public void beforeEach() {
22 27
 		super.beforeEach();
23 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 32
 			@Override
31 33
 			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
32 34
 				return view;
33 35
 			}
34
-		}, new NavigationOptions());
36
+		}, initialNavigationOptions);
37
+	}
38
+
39
+	@Test
40
+	public void createsViewFromContainerViewCreator() throws Exception {
35 41
 		assertThat(uut.getView()).isSameAs(view);
36 42
 	}
37 43
 
38 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 46
 		uut.ensureViewIsCreated();
48
-
49 47
 		verify(view, times(0)).destroy();
50 48
 		uut.destroy();
51 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
 }