Browse Source

V2 options applying refactoring (#1881)

* presenter skeleton

* test fix

* rm presentation

* options presenter

* test refactoring
Roman Kozlov 7 years ago
parent
commit
5d28ee4d9d

+ 26
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java View File

1
+package com.reactnativenavigation.presentation;
2
+
3
+import com.reactnativenavigation.parse.NavigationOptions;
4
+import com.reactnativenavigation.viewcontrollers.StackController;
5
+
6
+/**
7
+ * Created by romanko on 9/14/17.
8
+ */
9
+
10
+public class OptionsPresenter {
11
+
12
+	private StackController controller;
13
+
14
+	public OptionsPresenter(StackController controller) {
15
+		this.controller = controller;
16
+	}
17
+
18
+	public void applyOptions(NavigationOptions options) {
19
+		if (controller != null) {
20
+			controller.getTopBar().setTitle(options.title);
21
+			controller.getTopBar().setBackgroundColor(options.topBarBackgroundColor);
22
+			controller.getTopBar().setTitleTextColor(options.topBarTextColor);
23
+			controller.getTopBar().setTitleFontSize(options.topBarTextFontSize);
24
+		}
25
+	}
26
+}

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

5
 import android.view.View;
5
 import android.view.View;
6
 
6
 
7
 import com.reactnativenavigation.parse.NavigationOptions;
7
 import com.reactnativenavigation.parse.NavigationOptions;
8
+import com.reactnativenavigation.presentation.OptionsPresenter;
8
 
9
 
9
 public class ContainerViewController extends ViewController {
10
 public class ContainerViewController extends ViewController {
10
 
11
 
28
 	}
29
 	}
29
 
30
 
30
 	private final String containerName;
31
 	private final String containerName;
32
+	private OptionsPresenter optionsPresenter;
31
 
33
 
32
 	private final ContainerViewCreator viewCreator;
34
 	private final ContainerViewCreator viewCreator;
33
 	private final NavigationOptions navigationOptions;
35
 	private final NavigationOptions navigationOptions;
34
 	private ContainerView containerView;
36
 	private ContainerView containerView;
35
 
37
 
36
 	public ContainerViewController(final Activity activity,
38
 	public ContainerViewController(final Activity activity,
37
-	                               final String id,
38
-	                               final String containerName,
39
-	                               final ContainerViewCreator viewCreator,
40
-	                               final NavigationOptions initialNavigationOptions) {
39
+								   final String id,
40
+								   final String containerName,
41
+								   final ContainerViewCreator viewCreator,
42
+								   final NavigationOptions initialNavigationOptions) {
41
 		super(activity, id);
43
 		super(activity, id);
42
 		this.containerName = containerName;
44
 		this.containerName = containerName;
43
 		this.viewCreator = viewCreator;
45
 		this.viewCreator = viewCreator;
55
 	public void onViewAppeared() {
57
 	public void onViewAppeared() {
56
 		super.onViewAppeared();
58
 		super.onViewAppeared();
57
 		ensureViewIsCreated();
59
 		ensureViewIsCreated();
58
-		applyNavigationOptions();
60
+		applyOptions();
59
 		containerView.sendContainerStart();
61
 		containerView.sendContainerStart();
60
 	}
62
 	}
61
 
63
 
79
 
81
 
80
 	public void mergeNavigationOptions(final NavigationOptions options) {
82
 	public void mergeNavigationOptions(final NavigationOptions options) {
81
 		navigationOptions.mergeWith(options);
83
 		navigationOptions.mergeWith(options);
82
-		applyNavigationOptions();
83
-	}
84
-
85
-	private void applyNavigationOptions() {
86
-		if (getParentStackController() != null) {
87
-			getParentStackController().getTopBar().setTitle(navigationOptions.title);
88
-			getParentStackController().getTopBar().setBackgroundColor(navigationOptions.topBarBackgroundColor);
89
-			getParentStackController().getTopBar().setTitleTextColor(navigationOptions.topBarTextColor);
90
-			getParentStackController().getTopBar().setTitleFontSize(navigationOptions.topBarTextFontSize);
91
-		}
84
+		applyOptions();
92
 	}
85
 	}
93
 
86
 
94
 	public NavigationOptions getNavigationOptions() {
87
 	public NavigationOptions getNavigationOptions() {
95
 		return navigationOptions;
88
 		return navigationOptions;
96
 	}
89
 	}
90
+
91
+	private void applyOptions() {
92
+		OptionsPresenter presenter = new OptionsPresenter(getParentStackController());
93
+		presenter.applyOptions(navigationOptions);
94
+	}
97
 }
95
 }

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

7
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
8
 import com.reactnativenavigation.mocks.TestContainerView;
8
 import com.reactnativenavigation.mocks.TestContainerView;
9
 import com.reactnativenavigation.parse.NavigationOptions;
9
 import com.reactnativenavigation.parse.NavigationOptions;
10
+import com.reactnativenavigation.presentation.OptionsPresenter;
10
 
11
 
11
 import org.junit.Test;
12
 import org.junit.Test;
12
 
13
 
17
 import static org.mockito.Mockito.when;
18
 import static org.mockito.Mockito.when;
18
 
19
 
19
 public class ContainerViewControllerTest extends BaseTest {
20
 public class ContainerViewControllerTest extends BaseTest {
20
-	private Activity activity;
21
 	private ContainerViewController uut;
21
 	private ContainerViewController uut;
22
 	private ContainerViewController.ContainerView view;
22
 	private ContainerViewController.ContainerView view;
23
-	private NavigationOptions initialNavigationOptions;
24
 
23
 
25
 	@Override
24
 	@Override
26
 	public void beforeEach() {
25
 	public void beforeEach() {
27
 		super.beforeEach();
26
 		super.beforeEach();
28
-		activity = newActivity();
29
-		initialNavigationOptions = new NavigationOptions();
27
+		Activity activity = newActivity();
30
 		view = spy(new TestContainerView(activity));
28
 		view = spy(new TestContainerView(activity));
31
 		uut = new ContainerViewController(activity, "containerId1", "containerName", new ContainerViewController.ContainerViewCreator() {
29
 		uut = new ContainerViewController(activity, "containerId1", "containerName", new ContainerViewController.ContainerViewCreator() {
32
 			@Override
30
 			@Override
33
 			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
31
 			public ContainerViewController.ContainerView create(final Activity activity1, final String containerId, final String containerName) {
34
 				return view;
32
 				return view;
35
 			}
33
 			}
36
-		}, initialNavigationOptions);
34
+		}, new NavigationOptions());
37
 	}
35
 	}
38
 
36
 
39
 	@Test
37
 	@Test
71
 		when(view.isReady()).thenReturn(true);
69
 		when(view.isReady()).thenReturn(true);
72
 		assertThat(uut.isViewShown()).isTrue();
70
 		assertThat(uut.isViewShown()).isTrue();
73
 	}
71
 	}
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
-	}
129
-
130
-	@Test
131
-	public void appliesTopBarTextColor() throws Exception {
132
-		assertThat(uut.getNavigationOptions()).isSameAs(initialNavigationOptions);
133
-		initialNavigationOptions.title = "the title";
134
-		StackController stackController = new StackController(activity, "stackId");
135
-		stackController.push(uut);
136
-		uut.onViewAppeared();
137
-		assertThat(stackController.getTopBar().getTitleTextView().getCurrentTextColor()).isNotEqualTo(Color.RED);
138
-
139
-		NavigationOptions opts = new NavigationOptions();
140
-		opts.title = "the title";
141
-		opts.topBarTextColor = Color.RED;
142
-		uut.mergeNavigationOptions(opts);
143
-
144
-		assertThat(stackController.getTopBar().getTitleTextView()).isNotEqualTo(null);
145
-		assertThat(stackController.getTopBar().getTitleTextView().getCurrentTextColor()).isEqualTo(Color.RED);
146
-	}
147
-
148
-	@Test
149
-	public void appliesTopBarTextSize() throws Exception {
150
-		assertThat(uut.getNavigationOptions()).isSameAs(initialNavigationOptions);
151
-		initialNavigationOptions.title = "the title";
152
-		StackController stackController = new StackController(activity, "stackId");
153
-		stackController.push(uut);
154
-		uut.onViewAppeared();
155
-		assertThat(stackController.getTopBar().getTitleTextView().getTextSize()).isNotEqualTo(18);
156
-
157
-		NavigationOptions opts = new NavigationOptions();
158
-		opts.title = "the title";
159
-		opts.topBarTextFontSize = 18;
160
-		uut.mergeNavigationOptions(opts);
161
-
162
-		assertThat(stackController.getTopBar().getTitleTextView()).isNotEqualTo(null);
163
-		assertThat(stackController.getTopBar().getTitleTextView().getTextSize()).isEqualTo(18);
164
-	}
165
 }
72
 }

+ 129
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsApplyingTest.java View File

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