|
@@ -0,0 +1,106 @@
|
|
1
|
+package com.reactnativenavigation.react;
|
|
2
|
+
|
|
3
|
+import android.app.Application;
|
|
4
|
+import android.support.annotation.NonNull;
|
|
5
|
+import android.support.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
|
+ .setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
|
|
86
|
+ .setDevBundleDownloadListener(getDevBundleDownloadListener());
|
|
87
|
+
|
|
88
|
+ for (ReactPackage reactPackage : getPackages()) {
|
|
89
|
+ builder.addPackage(reactPackage);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ String jsBundleFile = getJSBundleFile();
|
|
93
|
+ if (jsBundleFile != null) {
|
|
94
|
+ builder.setJSBundleFile(jsBundleFile);
|
|
95
|
+ } else {
|
|
96
|
+ builder.setBundleAssetName(Assertions.assertNotNull(getBundleAssetName()));
|
|
97
|
+ }
|
|
98
|
+ return builder.build();
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ @SuppressWarnings("WeakerAccess")
|
|
102
|
+ @NonNull
|
|
103
|
+ protected DevBundleDownloadListener getDevBundleDownloadListener() {
|
|
104
|
+ return bundleListenerMediator;
|
|
105
|
+ }
|
|
106
|
+}
|