Daniel Zlotin 7 years ago
parent
commit
864afada5d

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/controllers/CommandsHandler.java View File

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

+ 28
- 23
lib/android/app/src/main/java/com/reactnativenavigation/layout/parse/LayoutNode.java View File

1
 package com.reactnativenavigation.layout.parse;
1
 package com.reactnativenavigation.layout.parse;
2
 
2
 
3
+import android.support.annotation.NonNull;
4
+
3
 import org.json.JSONArray;
5
 import org.json.JSONArray;
4
 import org.json.JSONObject;
6
 import org.json.JSONObject;
5
 
7
 
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
 	public final String id;
51
 	public final String id;
25
 	public final Type type;
52
 	public final Type type;
26
 	public final JSONObject data;
53
 	public final JSONObject data;
54
+
27
 	public final List<LayoutNode> children;
55
 	public final List<LayoutNode> children;
28
 
56
 
29
 	public LayoutNode(String id, Type type) {
57
 	public LayoutNode(String id, Type type) {
36
 		this.data = data;
64
 		this.data = data;
37
 		this.children = children;
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 View File

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