Browse Source

layoutFactory

Guy Carmeli 7 years ago
parent
commit
143474c741

+ 10
- 7
android/app/build.gradle View File

@@ -45,20 +45,23 @@ android {
45 45
 }
46 46
 
47 47
 dependencies {
48
-    compile fileTree(dir: "libs", include: ["*.jar"])
49
-
48
+    compile fileTree(include: ['*.jar'], dir: 'libs')
50 49
     compile 'com.android.support:design:25.1.1'
51
-    compile "com.android.support:appcompat-v7:25.1.1"
50
+    compile 'com.android.support:appcompat-v7:25.1.1'
52 51
 
53 52
     // node_modules
54
-    compile "com.facebook.react:react-native:+"
53
+    compile 'com.facebook.react:react-native:+'
55 54
 
56 55
     // third party
57
-    compile "com.aurelhubert:ahbottomnavigation:1.3.3"
56
+    compile 'com.aurelhubert:ahbottomnavigation:1.3.3'
58 57
     compile 'com.balysv.materialmenu:material-menu-toolbar:1.5.4'
59 58
 
60 59
     // tests
61
-    testCompile "junit:junit:4.12"
62
-    testCompile "org.robolectric:robolectric:3.2.2"
60
+    testCompile 'junit:junit:4.12'
61
+    testCompile 'org.robolectric:robolectric:3.2.2'
63 62
     testCompile 'org.assertj:assertj-core:2.5.0'
63
+    // required for robolectric
64
+    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
65
+
66
+//    testCompile 'org.mockito:mockito-core:2.+'
64 67
 }

+ 0
- 5
android/app/src/main/java/com/reactnativenavigation/container/Container.java View File

@@ -1,5 +0,0 @@
1
-package com.reactnativenavigation.container;
2
-
3
-public class Container {
4
-
5
-}

+ 2
- 5
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -6,7 +6,6 @@ import android.support.v7.app.AppCompatActivity;
6 6
 import com.facebook.react.ReactInstanceManager;
7 7
 import com.facebook.react.ReactNativeHost;
8 8
 import com.facebook.react.ReactPackage;
9
-import com.facebook.react.ReactRootView;
10 9
 import com.facebook.react.bridge.ReactContext;
11 10
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
12 11
 import com.facebook.react.shell.MainReactPackage;
@@ -80,9 +79,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
80 79
         onBackPressed();
81 80
     }
82 81
 
83
-    public void setRoot(String name, Bundle args) {
84
-        ReactRootView v = new ReactRootView(getApplicationContext());
85
-        setContentView(v);
86
-        v.startReactApplication(host.getReactInstanceManager(), name, args);
82
+    public ReactNativeHost getHost() {
83
+        return host;
87 84
     }
88 85
 }

+ 11
- 0
android/app/src/main/java/com/reactnativenavigation/layout/Container.java View File

@@ -0,0 +1,11 @@
1
+package com.reactnativenavigation.layout;
2
+
3
+import android.content.Context;
4
+import android.widget.FrameLayout;
5
+
6
+public class Container extends FrameLayout {
7
+    public Container(Context context, LayoutFactory.RootViewCreator rootViewCreator, String id, String name) {
8
+        super(context);
9
+        addView(rootViewCreator.createRootView(id, name));
10
+    }
11
+}

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/layout/ContainerStack.java View File

@@ -0,0 +1,10 @@
1
+package com.reactnativenavigation.layout;
2
+
3
+import android.content.Context;
4
+import android.widget.FrameLayout;
5
+
6
+public class ContainerStack extends FrameLayout {
7
+    public ContainerStack(Context context) {
8
+        super(context);
9
+    }
10
+}

+ 0
- 12
android/app/src/main/java/com/reactnativenavigation/layout/Layout.java View File

@@ -1,12 +0,0 @@
1
-package com.reactnativenavigation.layout;
2
-
3
-import android.content.Context;
4
-import android.graphics.Color;
5
-import android.widget.FrameLayout;
6
-
7
-public class Layout extends FrameLayout {
8
-    public Layout(Context context) {
9
-        super(context);
10
-        setBackgroundColor(Color.RED);
11
-    }
12
-}

+ 43
- 0
android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java View File

@@ -0,0 +1,43 @@
1
+package com.reactnativenavigation.layout;
2
+
3
+import android.app.Activity;
4
+import android.view.View;
5
+
6
+import java.util.List;
7
+import java.util.Map;
8
+
9
+public class LayoutFactory {
10
+    public interface RootViewCreator {
11
+        View createRootView(String id, String name);
12
+    }
13
+
14
+    private Activity activity;
15
+    private RootViewCreator rootViewCreator;
16
+
17
+    public LayoutFactory(Activity activity, RootViewCreator rootViewCreator) {
18
+        this.activity = activity;
19
+        this.rootViewCreator = rootViewCreator;
20
+    }
21
+
22
+    public View create(Map<String, Object> node) {
23
+        String id = (String) node.get("id");
24
+        String type = (String) node.get("type");
25
+
26
+        switch (type) {
27
+            case "Container":
28
+                String name = (String) ((Map<String, Object>)node.get("data")).get("name");
29
+                return new Container(activity, rootViewCreator, id, name);
30
+
31
+            case "ContainerStack":
32
+                ContainerStack containerStack = new ContainerStack(activity);
33
+                List<Map<String, Object>> children = (List<Map<String, Object>>) node.get("children");
34
+                Map<String, Object> inner = children.get(0);
35
+                containerStack.addView(create(inner));
36
+                return containerStack;
37
+
38
+        }
39
+
40
+        return null;
41
+//        return rootViewCreator.createRootView(id, name);
42
+    }
43
+}

+ 21
- 3
android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -1,12 +1,15 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3 3
 import android.os.Bundle;
4
+import android.view.View;
4 5
 
6
+import com.facebook.react.ReactRootView;
5 7
 import com.facebook.react.bridge.ReactApplicationContext;
6 8
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
7 9
 import com.facebook.react.bridge.ReactMethod;
8 10
 import com.facebook.react.bridge.ReadableMap;
9 11
 import com.reactnativenavigation.controllers.NavigationActivity;
12
+import com.reactnativenavigation.layout.LayoutFactory;
10 13
 
11 14
 public class NavigationModule extends ReactContextBaseJavaModule {
12 15
     public static final String NAME = "RNNBridgeModule";
@@ -25,9 +28,24 @@ public class NavigationModule extends ReactContextBaseJavaModule {
25 28
         NavigationActivity.instance.runOnUiThread(new Runnable() {
26 29
             @Override
27 30
             public void run() {
28
-                Bundle args = new Bundle();
29
-                args.putString("id", layoutTree.getArray("children").getMap(0).getString("id"));
30
-                NavigationActivity.instance.setRoot(layoutTree.getArray("children").getMap(0).getMap("data").getString("name"), args);
31
+                LayoutFactory factory = new LayoutFactory(NavigationActivity.instance, new LayoutFactory.RootViewCreator() {
32
+                    @Override
33
+                    public View createRootView(String id, String name) {
34
+                        ReactRootView rootView = new ReactRootView(NavigationActivity.instance);
35
+                        Bundle opts = new Bundle();
36
+                        opts.putString("id", id);
37
+                        rootView.startReactApplication(NavigationActivity.instance.getHost().getReactInstanceManager(), name, opts);
38
+                        return rootView;
39
+                    }
40
+                });
41
+
42
+//                Map<String, Object> node = new HashMap<String, Object>();
43
+//                node.put("id", container.getString("id"));
44
+//                HashMap<String, Object> data = new HashMap<>();
45
+//                data.put("name", container.getMap("data").getString("name"));
46
+//                node.put("data", data);
47
+//                View rootView = factory.create(node);
48
+//                NavigationActivity.instance.setContentView(rootView);
31 49
             }
32 50
         });
33 51
     }

+ 103
- 0
android/app/src/test/java/com/reactnativenavigation/LayoutFactoryTest.java View File

@@ -0,0 +1,103 @@
1
+package com.reactnativenavigation;
2
+
3
+import android.app.Activity;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+
7
+import com.reactnativenavigation.layout.Container;
8
+import com.reactnativenavigation.layout.ContainerStack;
9
+import com.reactnativenavigation.layout.LayoutFactory;
10
+
11
+import org.junit.Before;
12
+import org.junit.Test;
13
+import org.junit.runner.RunWith;
14
+import org.robolectric.Robolectric;
15
+import org.robolectric.RobolectricTestRunner;
16
+
17
+import java.util.ArrayList;
18
+import java.util.HashMap;
19
+import java.util.List;
20
+import java.util.Map;
21
+import java.util.concurrent.atomic.AtomicReference;
22
+
23
+import static org.assertj.core.api.Java6Assertions.assertThat;
24
+
25
+
26
+@RunWith(RobolectricTestRunner.class)
27
+public class LayoutFactoryTest {
28
+    @Before
29
+    public void setUp() {
30
+
31
+    }
32
+
33
+    @Test
34
+    public void returnsContainerThatHoldsTheRootView() {
35
+        final AtomicReference<String> idRef = new AtomicReference<>();
36
+        final AtomicReference<String> nameRef = new AtomicReference<>();
37
+        final AtomicReference<View> viewRef = new AtomicReference<>();
38
+
39
+        LayoutFactory.RootViewCreator rootViewCreator = new LayoutFactory.RootViewCreator() {
40
+            @Override
41
+            public View createRootView(String id, String name) {
42
+                idRef.set(id);
43
+                nameRef.set(name);
44
+                viewRef.set(new View(Robolectric.setupActivity(Activity.class)));
45
+                return viewRef.get();
46
+            }
47
+        };
48
+
49
+        Map<String, Object> node = new HashMap() {{
50
+            Map<String, Object> data = new HashMap<>();
51
+            data.put("name", "MyName");
52
+            put("id", "myUniqueId");
53
+            put("data", data);
54
+            put("type", "Container");
55
+        }};
56
+
57
+        ViewGroup result =
58
+                (ViewGroup) new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator).create(node);
59
+        assertThat(result).isInstanceOf(Container.class);
60
+        assertThat(result.getChildCount()).isEqualTo(1);
61
+        assertThat(result.getChildAt(0)).isEqualTo(viewRef.get());
62
+    }
63
+
64
+
65
+    @Test
66
+    public void returnsContainerStack() {
67
+        final AtomicReference<String> idRef = new AtomicReference<>();
68
+        final AtomicReference<String> nameRef = new AtomicReference<>();
69
+        final AtomicReference<View> viewRef = new AtomicReference<>();
70
+
71
+        LayoutFactory.RootViewCreator rootViewCreator = new LayoutFactory.RootViewCreator() {
72
+            @Override
73
+            public View createRootView(String id, String name) {
74
+                idRef.set(id);
75
+                nameRef.set(name);
76
+                viewRef.set(new View(Robolectric.setupActivity(Activity.class)));
77
+                return viewRef.get();
78
+            }
79
+        };
80
+
81
+        Map<String, Object> node = new HashMap() {{
82
+            Map<String, Object> data = new HashMap<>();
83
+            data.put("name", "MyName");
84
+            put("id", "myUniqueId");
85
+            put("data", data);
86
+            put("type", "Container");
87
+        }};
88
+
89
+        HashMap<String, Object> outerNode = new HashMap<>();
90
+        outerNode.put("type", "ContainerStack");
91
+        List<Map<String, Object>> children = new ArrayList<>();
92
+        children.add(node);
93
+        outerNode.put("children", children);
94
+
95
+        ViewGroup result =
96
+                (ViewGroup) new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator).create(outerNode);
97
+        assertThat(result).isInstanceOf(ContainerStack.class);
98
+        assertThat(result.getChildCount()).isEqualTo(1);
99
+        ViewGroup container = (ViewGroup) result.getChildAt(0);
100
+        assertThat(container.getChildCount()).isEqualTo(1);
101
+        assertThat(container.getChildAt(0)).isEqualTo(viewRef.get());
102
+    }
103
+}

+ 2
- 0
playground/android/app/build.gradle View File

@@ -17,5 +17,7 @@ android {
17 17
 dependencies {
18 18
     compile fileTree(dir: 'libs', include: ['*.jar'])
19 19
     compile 'com.facebook.react:react-native:+'
20
+    compile 'com.android.support:design:25.1.1'
21
+    compile "com.android.support:appcompat-v7:25.1.1"
20 22
     compile project(':react-native-navigation')
21 23
 }

+ 1
- 1
src/adapters/NativeCommandsSender.js View File

@@ -6,7 +6,7 @@ export default class NativeCommandsSender {
6 6
   }
7 7
 
8 8
   setRoot(layoutTree) {
9
-    console.log(layoutTree);
9
+    console.log('layoutTree: ', JSON.stringify(layoutTree));
10 10
     this.nativeCommandsModule.setRoot(layoutTree);
11 11
     return Promise.resolve(layoutTree);
12 12
   }