Browse Source

navigationOptions

Daniel Zlotin 7 years ago
parent
commit
f4c39727dd

+ 4
- 1
lib/android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java View File

@@ -76,7 +76,10 @@ public class LayoutFactory {
76 76
 	}
77 77
 
78 78
 	private ViewController createContainer(LayoutNode node) {
79
-		return new ReactRootViewController(activity, node.id, node.data.optString("name"), node.data.optJSONObject("navigationOptions").optString("title"), reactInstanceManager);
79
+		String id = node.id;
80
+		String name = node.data.optString("name");
81
+		NavigationOptions navigationOptions = NavigationOptions.parse(node.data.optJSONObject("navigationOptions"));
82
+		return new ReactRootViewController(activity, id, name, navigationOptions, reactInstanceManager);
80 83
 	}
81 84
 
82 85
 	private ViewController createContainerStack(LayoutNode node) {

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

@@ -0,0 +1,18 @@
1
+package com.reactnativenavigation.layout;
2
+
3
+import android.support.annotation.NonNull;
4
+
5
+import org.json.JSONObject;
6
+
7
+public class NavigationOptions {
8
+
9
+	@NonNull
10
+	public static NavigationOptions parse(JSONObject json) {
11
+		NavigationOptions result = new NavigationOptions();
12
+		if (json == null) return result;
13
+		result.title = json.optString("title");
14
+		return result;
15
+	}
16
+
17
+	public String title = "";
18
+}

+ 9
- 4
lib/android/app/src/main/java/com/reactnativenavigation/layout/ReactRootViewController.java View File

@@ -13,15 +13,19 @@ import com.reactnativenavigation.viewcontrollers.ViewController;
13 13
 public class ReactRootViewController extends ViewController {
14 14
 
15 15
 	private final String name;
16
-	private final String title;
16
+	private final NavigationOptions navigationOptions;
17 17
 	private final ReactInstanceManager reactInstanceManager;
18 18
 	private boolean attachedToReactInstance = false;
19 19
 	private ReactRootView reactRootView;
20 20
 
21
-	public ReactRootViewController(final Activity activity, final String id, final String name, final String title, final ReactInstanceManager reactInstanceManager) {
21
+	public ReactRootViewController(final Activity activity,
22
+	                               final String id,
23
+	                               final String name,
24
+	                               final NavigationOptions navigationOptions,
25
+	                               final ReactInstanceManager reactInstanceManager) {
22 26
 		super(activity, id);
23 27
 		this.name = name;
24
-		this.title = title;
28
+		this.navigationOptions = navigationOptions;
25 29
 		this.reactInstanceManager = reactInstanceManager;
26 30
 	}
27 31
 
@@ -35,7 +39,8 @@ public class ReactRootViewController extends ViewController {
35 39
 	@Override
36 40
 	public void onViewAppeared() {
37 41
 		super.onViewAppeared();
38
-		if (getParentStackController() != null) getParentStackController().setTitle(title);
42
+		if (getParentStackController() != null)
43
+			getParentStackController().setTitle(navigationOptions.title);
39 44
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(getId());
40 45
 	}
41 46
 

+ 32
- 0
lib/android/app/src/test/java/com/reactnativenavigation/parse/NavigationOptionsTest.java View File

@@ -0,0 +1,32 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import com.reactnativenavigation.BaseTest;
4
+import com.reactnativenavigation.layout.NavigationOptions;
5
+
6
+import org.json.JSONObject;
7
+import org.junit.Test;
8
+
9
+import static org.assertj.core.api.Java6Assertions.assertThat;
10
+
11
+public class NavigationOptionsTest extends BaseTest {
12
+
13
+	@Test
14
+	public void parsesNullAsDefaultEmptyOptions() throws Exception {
15
+		assertThat(NavigationOptions.parse(null)).isNotNull();
16
+	}
17
+
18
+	@Test
19
+	public void parsesJson() throws Exception {
20
+		JSONObject json = new JSONObject();
21
+		json.put("title", "the title");
22
+
23
+		NavigationOptions result = NavigationOptions.parse(json);
24
+		assertThat(result.title).isEqualTo("the title");
25
+	}
26
+
27
+	@Test
28
+	public void defaultEmptyOptions() throws Exception {
29
+		NavigationOptions uut = new NavigationOptions();
30
+		assertThat(uut.title).isEmpty();
31
+	}
32
+}