Daniel Zlotin 7 年 前
コミット
5976b7bf9a

+ 8
- 1
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java ファイルの表示

@@ -1,11 +1,15 @@
1 1
 package com.reactnativenavigation;
2 2
 
3 3
 import android.app.Application;
4
+import android.support.annotation.Nullable;
4 5
 
5 6
 import com.facebook.react.ReactApplication;
6 7
 import com.facebook.react.ReactNativeHost;
8
+import com.facebook.react.ReactPackage;
7 9
 import com.reactnativenavigation.react.ReactGateway;
8 10
 
11
+import java.util.List;
12
+
9 13
 public abstract class NavigationApplication extends Application implements ReactApplication {
10 14
 
11 15
 	private ReactGateway reactGateway;
@@ -13,7 +17,7 @@ public abstract class NavigationApplication extends Application implements React
13 17
 	@Override
14 18
 	public void onCreate() {
15 19
 		super.onCreate();
16
-		reactGateway = new ReactGateway(this, isDebug());
20
+		reactGateway = new ReactGateway(this, isDebug(), createAdditionalReactPackages());
17 21
 	}
18 22
 
19 23
 	public ReactGateway getReactGateway() {
@@ -26,4 +30,7 @@ public abstract class NavigationApplication extends Application implements React
26 30
 	}
27 31
 
28 32
 	public abstract boolean isDebug();
33
+
34
+	@Nullable
35
+	public abstract List<ReactPackage> createAdditionalReactPackages();
29 36
 }

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java ファイルの表示

@@ -10,6 +10,7 @@ import android.view.KeyEvent;
10 10
 import com.facebook.react.ReactInstanceManager;
11 11
 
12 12
 public class JsDevReloadHandler {
13
+	private static final String RELOAD_BROADCAST = "com.reactnativenavigation.broadcast.RELOAD";
13 14
 	private final BroadcastReceiver reloadReceiver = new BroadcastReceiver() {
14 15
 		@Override
15 16
 		public void onReceive(final Context context, final Intent intent) {
@@ -24,7 +25,7 @@ public class JsDevReloadHandler {
24 25
 	}
25 26
 
26 27
 	public void onActivityResumed(Activity activity) {
27
-		activity.registerReceiver(reloadReceiver, new IntentFilter("com.reactnativenavigation.broadcast.RELOAD"));
28
+		activity.registerReceiver(reloadReceiver, new IntentFilter(RELOAD_BROADCAST));
28 29
 	}
29 30
 
30 31
 	public void onActivityPaused(Activity activity) {

+ 15
- 6
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java ファイルの表示

@@ -6,16 +6,18 @@ import com.facebook.react.ReactNativeHost;
6 6
 import com.facebook.react.ReactPackage;
7 7
 import com.facebook.react.shell.MainReactPackage;
8 8
 
9
-import java.util.Arrays;
9
+import java.util.ArrayList;
10 10
 import java.util.List;
11 11
 
12 12
 public class NavigationReactNativeHost extends ReactNativeHost {
13 13
 
14 14
 	private final boolean isDebug;
15
+	private final List<ReactPackage> additionalReactPackages;
15 16
 
16
-	public NavigationReactNativeHost(Application application, boolean isDebug) {
17
+	public NavigationReactNativeHost(Application application, boolean isDebug, final List<ReactPackage> additionalReactPackages) {
17 18
 		super(application);
18 19
 		this.isDebug = isDebug;
20
+		this.additionalReactPackages = additionalReactPackages;
19 21
 	}
20 22
 
21 23
 	@Override
@@ -25,9 +27,16 @@ public class NavigationReactNativeHost extends ReactNativeHost {
25 27
 
26 28
 	@Override
27 29
 	protected List<ReactPackage> getPackages() {
28
-		return Arrays.<ReactPackage>asList(
29
-				new MainReactPackage(),
30
-				new NavigationPackage(this)
31
-		);
30
+		List<ReactPackage> packages = new ArrayList<>();
31
+		packages.add(new MainReactPackage());
32
+		packages.add(new NavigationPackage(this));
33
+		if (additionalReactPackages != null) {
34
+			for (ReactPackage p : additionalReactPackages) {
35
+				if (!(p instanceof MainReactPackage || p instanceof NavigationPackage)) {
36
+					packages.add(p);
37
+				}
38
+			}
39
+		}
40
+		return packages;
32 41
 	}
33 42
 }

+ 9
- 3
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java ファイルの表示

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

+ 42
- 0
lib/android/app/src/test/java/com/reactnativenavigation/react/NavigationReactNativeHostTest.java ファイルの表示

@@ -0,0 +1,42 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import com.facebook.react.ReactPackage;
4
+import com.facebook.react.shell.MainReactPackage;
5
+import com.reactnativenavigation.BaseTest;
6
+
7
+import org.junit.Test;
8
+import org.robolectric.RuntimeEnvironment;
9
+
10
+import java.util.Arrays;
11
+import java.util.Collections;
12
+
13
+import static org.assertj.core.api.Java6Assertions.assertThat;
14
+import static org.mockito.Mockito.mock;
15
+
16
+public class NavigationReactNativeHostTest extends BaseTest {
17
+
18
+	@Test
19
+	public void getPackagesDefaults() throws Exception {
20
+		NavigationReactNativeHost uut = new NavigationReactNativeHost(RuntimeEnvironment.application, false, null);
21
+		assertThat(uut.getPackages()).hasSize(2).extracting("class").containsOnly(MainReactPackage.class, NavigationPackage.class);
22
+	}
23
+
24
+	@Test
25
+	public void getPackagesAddsAdditionalPackages() throws Exception {
26
+		ReactPackage myPackage = mock(ReactPackage.class);
27
+		NavigationReactNativeHost uut = new NavigationReactNativeHost(RuntimeEnvironment.application, false, Collections.singletonList(myPackage));
28
+		assertThat(uut.getPackages()).hasSize(3).containsOnlyOnce(myPackage);
29
+	}
30
+
31
+	@Test
32
+	public void getPackages_DoesNotAddDefaultTwice() throws Exception {
33
+		NavigationReactNativeHost uut = new NavigationReactNativeHost(
34
+				RuntimeEnvironment.application,
35
+				false,
36
+				Arrays.<ReactPackage>asList(
37
+						new MainReactPackage(),
38
+						new MainReactPackage(),
39
+						new MainReactPackage()));
40
+		assertThat(uut.getPackages()).hasSize(2).extracting("class").containsOnly(MainReactPackage.class, NavigationPackage.class);
41
+	}
42
+}

+ 11
- 0
playground/android/app/src/main/java/com/reactnativenavigation/playground/MainApplication.java ファイルの表示

@@ -1,11 +1,22 @@
1 1
 package com.reactnativenavigation.playground;
2 2
 
3
+import android.support.annotation.Nullable;
4
+
5
+import com.facebook.react.ReactPackage;
3 6
 import com.reactnativenavigation.NavigationApplication;
4 7
 
8
+import java.util.List;
9
+
5 10
 public class MainApplication extends NavigationApplication {
6 11
 
7 12
 	@Override
8 13
 	public boolean isDebug() {
9 14
 		return BuildConfig.DEBUG;
10 15
 	}
16
+
17
+	@Nullable
18
+	@Override
19
+	public List<ReactPackage> createAdditionalReactPackages() {
20
+		return null;
21
+	}
11 22
 }