Daniel Zlotin 7 gadus atpakaļ
vecāks
revīzija
864afada5d

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/controllers/CommandsHandler.java Parādīt failu

@@ -29,7 +29,7 @@ public class CommandsHandler {
29 29
 					}
30 30
 				}, new BottomTabsCreator());
31 31
 
32
-		final LayoutNode layoutTreeRoot = LayoutNode.fromTree(layoutTree);
32
+		final LayoutNode layoutTreeRoot = LayoutNode.parse(layoutTree);
33 33
 		final View rootView = factory.create(layoutTreeRoot);
34 34
 		activity.setContentView(rootView);
35 35
 	}
@@ -46,7 +46,7 @@ public class CommandsHandler {
46 46
 						return rootView;
47 47
 					}
48 48
 				}, new BottomTabsCreator());
49
-		final LayoutNode layoutNode = LayoutNode.fromTree(layoutTree);
49
+		final LayoutNode layoutNode = LayoutNode.parse(layoutTree);
50 50
 		final View rootView = factory.create(layoutNode);
51 51
 		((StackLayout) activity.getContentView()).push(rootView);
52 52
 	}

+ 28
- 23
lib/android/app/src/main/java/com/reactnativenavigation/layout/parse/LayoutNode.java Parādīt failu

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.layout.parse;
2 2
 
3
+import android.support.annotation.NonNull;
4
+
3 5
 import org.json.JSONArray;
4 6
 import org.json.JSONObject;
5 7
 
@@ -21,9 +23,35 @@ public class LayoutNode {
21 23
 		}
22 24
 	}
23 25
 
26
+	@SuppressWarnings("unchecked")
27
+	public static LayoutNode parse(JSONObject layoutTree) {
28
+		String id = layoutTree.optString("id");
29
+		LayoutNode.Type type = LayoutNode.Type.fromString(layoutTree.optString("type"));
30
+		JSONObject data = parseData(layoutTree);
31
+		List<LayoutNode> children = parseChildren(layoutTree);
32
+		return new LayoutNode(id, type, data, children);
33
+	}
34
+
35
+	@NonNull
36
+	private static List<LayoutNode> parseChildren(JSONObject layoutTree) {
37
+		List<LayoutNode> children = new ArrayList<>();
38
+		if (layoutTree.has("children")) {
39
+			JSONArray rawChildren = layoutTree.optJSONArray("children");
40
+			for (int i = 0; i < rawChildren.length(); i++) {
41
+				children.add(LayoutNode.parse(rawChildren.optJSONObject(i)));
42
+			}
43
+		}
44
+		return children;
45
+	}
46
+
47
+	private static JSONObject parseData(JSONObject layoutTree) {
48
+		return layoutTree.has("data") ? layoutTree.optJSONObject("data") : new JSONObject();
49
+	}
50
+
24 51
 	public final String id;
25 52
 	public final Type type;
26 53
 	public final JSONObject data;
54
+
27 55
 	public final List<LayoutNode> children;
28 56
 
29 57
 	public LayoutNode(String id, Type type) {
@@ -36,27 +64,4 @@ public class LayoutNode {
36 64
 		this.data = data;
37 65
 		this.children = children;
38 66
 	}
39
-
40
-	@SuppressWarnings("unchecked")
41
-	public static LayoutNode fromTree(JSONObject layoutTree) {
42
-		String id = layoutTree.optString("id");
43
-		LayoutNode.Type type = LayoutNode.Type.fromString(layoutTree.optString("type"));
44
-
45
-		JSONObject data;
46
-		if (layoutTree.has("data")) {
47
-			data = layoutTree.optJSONObject("data");
48
-		} else {
49
-			data = new JSONObject();
50
-		}
51
-
52
-		List<LayoutNode> children = new ArrayList<>();
53
-		if (layoutTree.has("children")) {
54
-			JSONArray rawChildren = layoutTree.optJSONArray("children");
55
-			for (int i = 0; i < rawChildren.length(); i++) {
56
-				children.add(LayoutNode.fromTree(rawChildren.optJSONObject(i)));
57
-			}
58
-		}
59
-
60
-		return new LayoutNode(id, type, data, children);
61
-	}
62 67
 }

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/layout/parse/LayoutNodeTest.java Parādīt failu

@@ -34,7 +34,7 @@ public class LayoutNodeTest extends BaseTest {
34 34
 				"data: {dataKey: dataValue}, " +
35 35
 				"children: [{id: childId1, type: Container}]}");
36 36
 
37
-		LayoutNode result = LayoutNode.fromTree(tree);
37
+		LayoutNode result = LayoutNode.parse(tree);
38 38
 
39 39
 		assertThat(result).isNotNull();
40 40
 		assertThat(result.id).isEqualTo("node1");