Browse Source

Allow subclasses of NavigationApplication to provide a custom ReactNativeHost. (#2552)

Currently the default NavigationReactNativeHost only adds the NavigationPackage. Users of React Native Navigation may need to override additional methods in ReactNativeHost. This change allows advanced users to provide their own implemenation of ReactNativeHost, with the requirement that they add the NavigationPackage themselves.
Alec B. Plumb 6 years ago
parent
commit
f8fa7341a1

+ 18
- 1
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

19
 	public void onCreate() {
19
 	public void onCreate() {
20
 		super.onCreate();
20
 		super.onCreate();
21
         instance = this;
21
         instance = this;
22
-        reactGateway = new ReactGateway(this, isDebug(), createAdditionalReactPackages());
22
+        reactGateway = createReactGateway();
23
 	}
23
 	}
24
 
24
 
25
+    /**
26
+     * Subclasses of NavigationApplication may override this method to create the singleton instance
27
+     * of {@link ReactGateway}. For example, subclasses may wish to provide a custom {@link ReactNativeHost}
28
+     * with the ReactGateway. This method will be called exactly once, in the application's {@link #onCreate()} method.
29
+     *
30
+     * Custom {@link ReactNativeHost}s must be sure to include {@link com.reactnativenavigation.react.NavigationPackage}
31
+     *
32
+     * @return a singleton {@link ReactGateway}
33
+     */
34
+	protected ReactGateway createReactGateway() {
35
+	    return new ReactGateway(this, isDebug(), createAdditionalReactPackages());
36
+    }
37
+
25
 	public ReactGateway getReactGateway() {
38
 	public ReactGateway getReactGateway() {
26
 		return reactGateway;
39
 		return reactGateway;
27
 	}
40
 	}
33
 
46
 
34
 	public abstract boolean isDebug();
47
 	public abstract boolean isDebug();
35
 
48
 
49
+    /**
50
+     * Create a list of additional {@link ReactPackage}s to include. This method will only be called by
51
+     * the default implementation of {@link #createReactGateway()}
52
+     */
36
 	@Nullable
53
 	@Nullable
37
 	public abstract List<ReactPackage> createAdditionalReactPackages();
54
 	public abstract List<ReactPackage> createAdditionalReactPackages();
38
 }
55
 }

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java View File

9
 import java.util.ArrayList;
9
 import java.util.ArrayList;
10
 import java.util.List;
10
 import java.util.List;
11
 
11
 
12
+/**
13
+ * Default implementation of {@link ReactNativeHost} that includes {@link NavigationPackage}
14
+ * and user-defined additional packages.
15
+ */
12
 public class NavigationReactNativeHost extends ReactNativeHost {
16
 public class NavigationReactNativeHost extends ReactNativeHost {
13
 
17
 
14
 	private final boolean isDebug;
18
 	private final boolean isDebug;
27
 
31
 
28
 	@Override
32
 	@Override
29
 	protected List<ReactPackage> getPackages() {
33
 	protected List<ReactPackage> getPackages() {
34
+		// Please note that users may provide their own implementations of ReactNativeHost. Any additional
35
+		// package requirements should be documented in the changelog.
30
 		List<ReactPackage> packages = new ArrayList<>();
36
 		List<ReactPackage> packages = new ArrayList<>();
31
 		packages.add(new MainReactPackage());
37
 		packages.add(new MainReactPackage());
32
 		packages.add(new NavigationPackage(this));
38
 		packages.add(new NavigationPackage(this));

+ 9
- 3
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java View File

1
 package com.reactnativenavigation.react;
1
 package com.reactnativenavigation.react;
2
 
2
 
3
+import android.app.Application;
4
+
3
 import com.facebook.react.ReactNativeHost;
5
 import com.facebook.react.ReactNativeHost;
4
 import com.facebook.react.ReactPackage;
6
 import com.facebook.react.ReactPackage;
5
 import com.facebook.soloader.SoLoader;
7
 import com.facebook.soloader.SoLoader;
10
 
12
 
11
 public class ReactGateway {
13
 public class ReactGateway {
12
 
14
 
13
-	private final NavigationReactNativeHost reactNativeHost;
15
+	private final ReactNativeHost reactNativeHost;
14
 	private final NavigationReactInitializer initializer;
16
 	private final NavigationReactInitializer initializer;
15
 	private final JsDevReloadHandler jsDevReloadHandler;
17
 	private final JsDevReloadHandler jsDevReloadHandler;
16
 
18
 
17
-	public ReactGateway(final NavigationApplication application, final boolean isDebug, final List<ReactPackage> additionalReactPackages) {
19
+	public ReactGateway(final Application application, final boolean isDebug, final List<ReactPackage> additionalReactPackages) {
20
+		this(application, isDebug, new NavigationReactNativeHost(application, isDebug, additionalReactPackages));
21
+	}
22
+
23
+	public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost reactNativeHost) {
18
 		SoLoader.init(application, false);
24
 		SoLoader.init(application, false);
19
-		reactNativeHost = new NavigationReactNativeHost(application, isDebug, additionalReactPackages);
25
+		this.reactNativeHost = reactNativeHost;
20
 		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug);
26
 		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug);
21
 		jsDevReloadHandler = new JsDevReloadHandler(reactNativeHost.getReactInstanceManager());
27
 		jsDevReloadHandler = new JsDevReloadHandler(reactNativeHost.getReactInstanceManager());
22
 	}
28
 	}