|
@@ -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
|
}
|