|
@@ -0,0 +1,107 @@
|
|
1
|
+package com.reactnativenavigation.react;
|
|
2
|
+
|
|
3
|
+import android.app.Application;
|
|
4
|
+import androidx.annotation.NonNull;
|
|
5
|
+import androidx.annotation.Nullable;
|
|
6
|
+
|
|
7
|
+import com.facebook.infer.annotation.Assertions;
|
|
8
|
+import com.facebook.react.ReactInstanceManager;
|
|
9
|
+import com.facebook.react.ReactInstanceManagerBuilder;
|
|
10
|
+import com.facebook.react.ReactNativeHost;
|
|
11
|
+import com.facebook.react.ReactPackage;
|
|
12
|
+import com.facebook.react.common.LifecycleState;
|
|
13
|
+import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
|
|
14
|
+import com.facebook.react.shell.MainReactPackage;
|
|
15
|
+import com.reactnativenavigation.NavigationApplication;
|
|
16
|
+
|
|
17
|
+import java.util.ArrayList;
|
|
18
|
+import java.util.List;
|
|
19
|
+
|
|
20
|
+/**
|
|
21
|
+ * Default implementation of {@link ReactNativeHost} that includes {@link NavigationPackage}
|
|
22
|
+ * and user-defined additional packages.
|
|
23
|
+ */
|
|
24
|
+public class NavigationReactNativeHost extends ReactNativeHost implements BundleDownloadListenerProvider {
|
|
25
|
+
|
|
26
|
+ private final boolean isDebug;
|
|
27
|
+ private final List<ReactPackage> additionalReactPackages;
|
|
28
|
+ private @Nullable NavigationDevBundleDownloadListener bundleListener;
|
|
29
|
+ private final DevBundleDownloadListener bundleListenerMediator = new DevBundleDownloadListenerAdapter() {
|
|
30
|
+ @Override
|
|
31
|
+ public void onSuccess() {
|
|
32
|
+ if (bundleListener != null) {
|
|
33
|
+ bundleListener.onSuccess();
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ };
|
|
37
|
+
|
|
38
|
+ public NavigationReactNativeHost(NavigationApplication application) {
|
|
39
|
+ this(application, application.isDebug(), application.createAdditionalReactPackages());
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ @SuppressWarnings("WeakerAccess")
|
|
43
|
+ public NavigationReactNativeHost(Application application, boolean isDebug, final List<ReactPackage> additionalReactPackages) {
|
|
44
|
+ super(application);
|
|
45
|
+ this.isDebug = isDebug;
|
|
46
|
+ this.additionalReactPackages = additionalReactPackages;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @Override
|
|
50
|
+ public void setBundleLoaderListener(NavigationDevBundleDownloadListener listener) {
|
|
51
|
+ bundleListener = listener;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ @Override
|
|
55
|
+ public boolean getUseDeveloperSupport() {
|
|
56
|
+ return isDebug;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ @Override
|
|
60
|
+ protected List<ReactPackage> getPackages() {
|
|
61
|
+ List<ReactPackage> packages = new ArrayList<>();
|
|
62
|
+ boolean hasMainReactPackage = false;
|
|
63
|
+ packages.add(new NavigationPackage(this));
|
|
64
|
+ if (additionalReactPackages != null) {
|
|
65
|
+ for (ReactPackage p : additionalReactPackages) {
|
|
66
|
+ if (!(p instanceof NavigationPackage)) {
|
|
67
|
+ packages.add(p);
|
|
68
|
+ }
|
|
69
|
+ if (p instanceof MainReactPackage) hasMainReactPackage = true;
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ if (!hasMainReactPackage) {
|
|
73
|
+ packages.add(new MainReactPackage());
|
|
74
|
+ }
|
|
75
|
+ return packages;
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ protected ReactInstanceManager createReactInstanceManager() {
|
|
79
|
+ ReactInstanceManagerBuilder builder = ReactInstanceManager.builder()
|
|
80
|
+ .setApplication(getApplication())
|
|
81
|
+ .setJSMainModulePath(getJSMainModuleName())
|
|
82
|
+ .setUseDeveloperSupport(getUseDeveloperSupport())
|
|
83
|
+ .setRedBoxHandler(getRedBoxHandler())
|
|
84
|
+ .setJavaScriptExecutorFactory(getJavaScriptExecutorFactory())
|
|
85
|
+ .setUIImplementationProvider(getUIImplementationProvider())
|
|
86
|
+ .setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
|
|
87
|
+ .setDevBundleDownloadListener(getDevBundleDownloadListener());
|
|
88
|
+
|
|
89
|
+ for (ReactPackage reactPackage : getPackages()) {
|
|
90
|
+ builder.addPackage(reactPackage);
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ String jsBundleFile = getJSBundleFile();
|
|
94
|
+ if (jsBundleFile != null) {
|
|
95
|
+ builder.setJSBundleFile(jsBundleFile);
|
|
96
|
+ } else {
|
|
97
|
+ builder.setBundleAssetName(Assertions.assertNotNull(getBundleAssetName()));
|
|
98
|
+ }
|
|
99
|
+ return builder.build();
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ @SuppressWarnings("WeakerAccess")
|
|
103
|
+ @NonNull
|
|
104
|
+ protected DevBundleDownloadListener getDevBundleDownloadListener() {
|
|
105
|
+ return bundleListenerMediator;
|
|
106
|
+ }
|
|
107
|
+}
|