Explorar el Código

exposed JS path params for ReactNativeHosts, refactored ReactGateway

Daniel Zlotin hace 7 años
padre
commit
8fe55fb1f3

+ 28
- 6
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java Ver fichero

@@ -11,7 +11,6 @@ import com.facebook.react.bridge.ReactContext;
11 11
 import com.facebook.react.bridge.WritableMap;
12 12
 import com.reactnativenavigation.react.NavigationReactGateway;
13 13
 import com.reactnativenavigation.react.ReactGateway;
14
-import com.reactnativenavigation.react.ReactGatewayHost;
15 14
 
16 15
 import java.util.List;
17 16
 
@@ -19,18 +18,18 @@ public abstract class NavigationApplication extends Application implements React
19 18
 
20 19
     public static NavigationApplication instance;
21 20
 
22
-    private ReactGatewayHost reactGateway;
21
+    private NavigationReactGateway reactGateway;
23 22
     private Handler handler;
24 23
 
25 24
     @Override
26 25
     public void onCreate() {
27 26
         super.onCreate();
28 27
         instance = this;
29
-        reactGateway = new NavigationReactGateway();
30 28
         handler = new Handler(getMainLooper());
29
+        reactGateway = new NavigationReactGateway();
31 30
     }
32 31
 
33
-    public void startReactContext() {
32
+    public void startReactContextOnceInBackgroundAndExecuteJS() {
34 33
         reactGateway.startReactContextOnceInBackgroundAndExecuteJS();
35 34
     }
36 35
 
@@ -56,7 +55,31 @@ public abstract class NavigationApplication extends Application implements React
56 55
 
57 56
     @Override
58 57
     public ReactNativeHost getReactNativeHost() {
59
-        return reactGateway;
58
+        return reactGateway.getReactNativeHost();
59
+    }
60
+
61
+    /**
62
+     * @see ReactNativeHost#getJSMainModuleName()
63
+     */
64
+    @Nullable
65
+    public String getJSMainModuleName() {
66
+        return null;
67
+    }
68
+
69
+    /**
70
+     * @see ReactNativeHost#getJSBundleFile()
71
+     */
72
+    @Nullable
73
+    public String getJSBundleFile() {
74
+        return null;
75
+    }
76
+
77
+    /**
78
+     * @see ReactNativeHost#getBundleAssetName()
79
+     */
80
+    @Nullable
81
+    public String getBundleAssetName() {
82
+        return null;
60 83
     }
61 84
 
62 85
     public abstract boolean isDebug();
@@ -92,5 +115,4 @@ public abstract class NavigationApplication extends Application implements React
92 115
         }
93 116
         reactGateway.getReactEventEmitter().sendEvent(eventId, arguments);
94 117
     }
95
-
96 118
 }

+ 8
- 11
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java Ver fichero

@@ -9,6 +9,7 @@ import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
9 9
 import com.reactnativenavigation.NavigationApplication;
10 10
 import com.reactnativenavigation.events.Event;
11 11
 import com.reactnativenavigation.events.EventBus;
12
+import com.reactnativenavigation.events.JsDevReloadEvent;
12 13
 import com.reactnativenavigation.events.ModalDismissedEvent;
13 14
 import com.reactnativenavigation.events.Subscriber;
14 15
 import com.reactnativenavigation.layouts.BottomTabsLayout;
@@ -20,12 +21,11 @@ import com.reactnativenavigation.params.SnackbarParams;
20 21
 import com.reactnativenavigation.params.TitleBarButtonParams;
21 22
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
22 23
 import com.reactnativenavigation.react.JsDevReloadHandler;
23
-import com.reactnativenavigation.react.ReactGateway;
24 24
 import com.reactnativenavigation.react.RedboxPermission;
25 25
 
26 26
 import java.util.List;
27 27
 
28
-public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, ReactGateway.OnJsDevReloadListener, Subscriber {
28
+public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, Subscriber {
29 29
 
30 30
     /**
31 31
      * Although we start multiple activities, we make sure to pass Intent.CLEAR_TASK | Intent.NEW_TASK
@@ -46,7 +46,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
46 46
         super.onCreate(savedInstanceState);
47 47
 
48 48
         if (!NavigationApplication.instance.isReactContextInitialized()) {
49
-            NavigationApplication.instance.startReactContext();
49
+            NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
50 50
             return;
51 51
         }
52 52
 
@@ -82,7 +82,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
82 82
         }
83 83
 
84 84
         currentActivity = this;
85
-        NavigationApplication.instance.getReactGateway().onResumeActivity(this, this, this);
85
+        NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
86 86
         EventBus.instance.register(this);
87 87
     }
88 88
 
@@ -116,12 +116,6 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
116 116
         }
117 117
     }
118 118
 
119
-    @Override
120
-    public void onJsDevReload() {
121
-        modalController.destroy();
122
-        layout.destroy();
123
-    }
124
-
125 119
     @Override
126 120
     public void invokeDefaultOnBackPressed() {
127 121
         super.onBackPressed();
@@ -258,8 +252,11 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
258 252
 
259 253
     @Override
260 254
     public void onEvent(Event event) {
261
-        if (event.getType() == ModalDismissedEvent.TYPE) {
255
+        if (event.getType().equals(ModalDismissedEvent.TYPE)) {
262 256
             layout.onModalDismissed();
257
+        } else if (event.getType().equals(JsDevReloadEvent.TYPE)) {
258
+            modalController.destroy();
259
+            layout.destroy();
263 260
         }
264 261
     }
265 262
 }

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/SplashActivity.java Ver fichero

@@ -15,7 +15,7 @@ public abstract class SplashActivity extends AppCompatActivity {
15 15
     protected void onCreate(@Nullable Bundle savedInstanceState) {
16 16
         super.onCreate(savedInstanceState);
17 17
         setSplashLayout();
18
-        NavigationApplication.instance.startReactContext();
18
+        NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
19 19
     }
20 20
 
21 21
     @Override

+ 11
- 0
android/app/src/main/java/com/reactnativenavigation/events/JsDevReloadEvent.java Ver fichero

@@ -0,0 +1,11 @@
1
+package com.reactnativenavigation.events;
2
+
3
+public class JsDevReloadEvent implements Event {
4
+
5
+    public static final String TYPE = "JsDevReloadEvent";
6
+
7
+    @Override
8
+    public String getType() {
9
+        return TYPE;
10
+    }
11
+}

+ 103
- 55
android/app/src/main/java/com/reactnativenavigation/react/NavigationReactGateway.java Ver fichero

@@ -4,6 +4,7 @@ import android.app.Activity;
4 4
 import android.content.Intent;
5 5
 
6 6
 import com.facebook.react.ReactInstanceManager;
7
+import com.facebook.react.ReactNativeHost;
7 8
 import com.facebook.react.ReactPackage;
8 9
 import com.facebook.react.bridge.ReactContext;
9 10
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
@@ -11,17 +12,21 @@ import com.facebook.react.shell.MainReactPackage;
11 12
 import com.reactnativenavigation.NavigationApplication;
12 13
 import com.reactnativenavigation.bridge.NavigationReactEventEmitter;
13 14
 import com.reactnativenavigation.bridge.NavigationReactPackage;
15
+import com.reactnativenavigation.events.EventBus;
16
+import com.reactnativenavigation.events.JsDevReloadEvent;
14 17
 
15 18
 import java.util.ArrayList;
16 19
 import java.util.List;
17 20
 
18
-public class NavigationReactGateway extends ReactGatewayHost implements ReactInstanceManager.ReactInstanceEventListener {
21
+import javax.annotation.Nullable;
19 22
 
20
-    private OnJsDevReloadListener onJsDevReloadListener;
23
+public class NavigationReactGateway implements ReactGateway {
24
+
25
+    private final ReactNativeHost host;
21 26
     private NavigationReactEventEmitter reactEventEmitter;
22 27
 
23 28
     public NavigationReactGateway() {
24
-        super(NavigationApplication.instance);
29
+        host = new ReactNativeHostImpl();
25 30
     }
26 31
 
27 32
     @Override
@@ -30,7 +35,7 @@ public class NavigationReactGateway extends ReactGatewayHost implements ReactIns
30 35
     }
31 36
 
32 37
     public boolean isInitialized() {
33
-        return hasInstance() && getReactInstanceManager().getCurrentReactContext() != null;
38
+        return host.hasInstance() && getReactInstanceManager().getCurrentReactContext() != null;
34 39
     }
35 40
 
36 41
     public ReactContext getReactContext() {
@@ -38,26 +43,31 @@ public class NavigationReactGateway extends ReactGatewayHost implements ReactIns
38 43
     }
39 44
 
40 45
     public NavigationReactEventEmitter getReactEventEmitter() {
46
+        if (reactEventEmitter == null && isInitialized()) {
47
+            reactEventEmitter = new NavigationReactEventEmitter(getReactContext());
48
+        }
41 49
         return reactEventEmitter;
42 50
     }
43 51
 
52
+    @Override
53
+    public ReactInstanceManager getReactInstanceManager() {
54
+        return host.getReactInstanceManager();
55
+    }
56
+
44 57
     public void onBackPressed() {
45 58
         getReactInstanceManager().onBackPressed();
46 59
     }
47 60
 
48 61
     public void onDestroyApp() {
49 62
         getReactInstanceManager().onHostDestroy();
50
-        getReactInstanceManager().removeReactInstanceEventListener(this);
51
-        clear();
63
+        host.clear();
52 64
     }
53 65
 
54 66
     public void onPauseActivity() {
55 67
         getReactInstanceManager().onHostPause();
56
-        onJsDevReloadListener = null;
57 68
     }
58 69
 
59
-    public void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, OnJsDevReloadListener onJsDevReloadListener) {
60
-        this.onJsDevReloadListener = onJsDevReloadListener;
70
+    public void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler) {
61 71
         getReactInstanceManager().onHostResume(activity, defaultHardwareBackBtnHandler);
62 72
     }
63 73
 
@@ -65,63 +75,101 @@ public class NavigationReactGateway extends ReactGatewayHost implements ReactIns
65 75
         getReactInstanceManager().onActivityResult(requestCode, resultCode, data);
66 76
     }
67 77
 
68
-    private void replaceJsDevReloadListener(ReactInstanceManager manager) {
69
-        new JsDevReloadListenerReplacer(manager, new JsDevReloadListenerReplacer.Listener() {
70
-            @Override
71
-            public void onJsDevReload() {
72
-                if (onJsDevReloadListener != null)
73
-                    onJsDevReloadListener.onJsDevReload();
74
-            }
75
-        }).replace();
78
+    public ReactNativeHost getReactNativeHost() {
79
+        return host;
76 80
     }
77 81
 
78
-    @Override
79
-    protected ReactInstanceManager createReactInstanceManager() {
80
-        ReactInstanceManager manager = super.createReactInstanceManager();
81
-        if (NavigationApplication.instance.isDebug()) {
82
-            replaceJsDevReloadListener(manager);
82
+
83
+    private static class ReactNativeHostImpl extends ReactNativeHost implements ReactInstanceManager.ReactInstanceEventListener {
84
+
85
+        public ReactNativeHostImpl() {
86
+            super(NavigationApplication.instance);
83 87
         }
84
-        manager.addReactInstanceEventListener(this);
85
-        return manager;
86
-    }
87 88
 
88
-    @Override
89
-    protected boolean getUseDeveloperSupport() {
90
-        return NavigationApplication.instance.isDebug();
91
-    }
89
+        @Override
90
+        protected boolean getUseDeveloperSupport() {
91
+            return NavigationApplication.instance.isDebug();
92
+        }
92 93
 
93
-    @Override
94
-    protected List<ReactPackage> getPackages() {
95
-        return createReactPackages();
96
-    }
94
+        @Override
95
+        protected List<ReactPackage> getPackages() {
96
+            List<ReactPackage> list = new ArrayList<>();
97
+            list.add(new MainReactPackage());
98
+            list.add(new NavigationReactPackage());
99
+            addAdditionalReactPackagesIfNeeded(list);
100
+            return list;
101
+        }
97 102
 
98
-    private List<ReactPackage> createReactPackages() {
99
-        List<ReactPackage> list = new ArrayList<>();
100
-        list.add(new MainReactPackage());
101
-        list.add(new NavigationReactPackage());
102
-        addAdditionalReactPackagesIfNeeded(list);
103
-        return list;
104
-    }
103
+        private void addAdditionalReactPackagesIfNeeded(List<ReactPackage> list) {
104
+            List<ReactPackage> additionalReactPackages = NavigationApplication.instance.createAdditionalReactPackages();
105
+            if (additionalReactPackages == null) {
106
+                return;
107
+            }
108
+
109
+            for (ReactPackage reactPackage : additionalReactPackages) {
110
+                if (reactPackage instanceof MainReactPackage)
111
+                    throw new RuntimeException("Do not create a new MainReactPackage. This is created for you.");
112
+                if (reactPackage instanceof NavigationReactPackage)
113
+                    throw new RuntimeException("Do not create a new NavigationReactPackage. This is created for you.");
114
+            }
105 115
 
106
-    private void addAdditionalReactPackagesIfNeeded(List<ReactPackage> list) {
107
-        List<ReactPackage> additionalReactPackages = NavigationApplication.instance.createAdditionalReactPackages();
108
-        if (additionalReactPackages == null) {
109
-            return;
116
+            list.addAll(additionalReactPackages);
110 117
         }
111 118
 
112
-        for (ReactPackage reactPackage : additionalReactPackages) {
113
-            if (reactPackage instanceof MainReactPackage)
114
-                throw new RuntimeException("Do not create a new MainReactPackage. This is created for you.");
115
-            if (reactPackage instanceof NavigationReactPackage)
116
-                throw new RuntimeException("Do not create a new NavigationReactPackage. This is created for you.");
119
+        @Override
120
+        protected ReactInstanceManager createReactInstanceManager() {
121
+            ReactInstanceManager manager = super.createReactInstanceManager();
122
+            if (NavigationApplication.instance.isDebug()) {
123
+                replaceJsDevReloadListener(manager);
124
+            }
125
+            manager.addReactInstanceEventListener(this);
126
+            return manager;
117 127
         }
118 128
 
119
-        list.addAll(additionalReactPackages);
120
-    }
129
+        private void replaceJsDevReloadListener(ReactInstanceManager manager) {
130
+            new JsDevReloadListenerReplacer(manager, new JsDevReloadListenerReplacer.Listener() {
131
+                @Override
132
+                public void onJsDevReload() {
133
+                    EventBus.instance.post(new JsDevReloadEvent());
134
+                }
135
+            }).replace();
136
+        }
121 137
 
122
-    @Override
123
-    public void onReactContextInitialized(ReactContext context) {
124
-        reactEventEmitter = new NavigationReactEventEmitter(context);
125
-        NavigationApplication.instance.onReactInitialized(context);
138
+        @Override
139
+        public void onReactContextInitialized(ReactContext context) {
140
+            NavigationApplication.instance.onReactInitialized(context);
141
+        }
142
+
143
+        @Override
144
+        public void clear() {
145
+            getReactInstanceManager().removeReactInstanceEventListener(this);
146
+            super.clear();
147
+        }
148
+
149
+        @Override
150
+        protected String getJSMainModuleName() {
151
+            String jsMainModuleName = NavigationApplication.instance.getJSMainModuleName();
152
+            if (jsMainModuleName != null)
153
+                return jsMainModuleName;
154
+            return super.getJSMainModuleName();
155
+        }
156
+
157
+        @Nullable
158
+        @Override
159
+        protected String getJSBundleFile() {
160
+            String jsBundleFile = NavigationApplication.instance.getJSBundleFile();
161
+            if (jsBundleFile != null)
162
+                return jsBundleFile;
163
+            return super.getJSBundleFile();
164
+        }
165
+
166
+        @Nullable
167
+        @Override
168
+        protected String getBundleAssetName() {
169
+            String bundleAssetName = NavigationApplication.instance.getBundleAssetName();
170
+            if (bundleAssetName != null)
171
+                return bundleAssetName;
172
+            return super.getBundleAssetName();
173
+        }
126 174
     }
127 175
 }

+ 1
- 5
android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java Ver fichero

@@ -10,10 +10,6 @@ import com.reactnativenavigation.bridge.NavigationReactEventEmitter;
10 10
 
11 11
 public interface ReactGateway {
12 12
 
13
-    interface OnJsDevReloadListener {
14
-        void onJsDevReload();
15
-    }
16
-
17 13
     void startReactContextOnceInBackgroundAndExecuteJS();
18 14
 
19 15
     boolean isInitialized();
@@ -24,7 +20,7 @@ public interface ReactGateway {
24 20
 
25 21
     ReactInstanceManager getReactInstanceManager();
26 22
 
27
-    void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, OnJsDevReloadListener onJsDevReloadListener);
23
+    void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler);
28 24
 
29 25
     void onPauseActivity();
30 26
 

+ 0
- 11
android/app/src/main/java/com/reactnativenavigation/react/ReactGatewayHost.java Ver fichero

@@ -1,11 +0,0 @@
1
-package com.reactnativenavigation.react;
2
-
3
-import android.app.Application;
4
-
5
-import com.facebook.react.ReactNativeHost;
6
-
7
-public abstract class ReactGatewayHost extends ReactNativeHost implements ReactGateway {
8
-    protected ReactGatewayHost(Application application) {
9
-        super(application);
10
-    }
11
-}

+ 0
- 1
example-redux/android/app/src/main/java/com/exampleredux/MainApplication.java Ver fichero

@@ -9,7 +9,6 @@ import java.util.List;
9 9
 
10 10
 public class MainApplication extends NavigationApplication {
11 11
 
12
-
13 12
     @Override
14 13
     public boolean isDebug() {
15 14
         return BuildConfig.DEBUG;