|
| 1 | +package com.microsoft.reacttestapp |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.app.Application |
| 5 | +import android.content.Context |
| 6 | +import com.facebook.react.PackageList |
| 7 | +import com.facebook.react.ReactApplication |
| 8 | +import com.facebook.soloader.SoLoader |
| 9 | +import com.microsoft.reacttestapp.manifest.ManifestProvider |
| 10 | +import com.microsoft.reacttestapp.react.ReactBundleNameProvider |
| 11 | +import com.microsoft.reacttestapp.react.TestAppReactNativeHost |
| 12 | +import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents |
| 13 | + |
| 14 | +class TestApp : Application(), ReactApplication { |
| 15 | + val bundleNameProvider: ReactBundleNameProvider |
| 16 | + get() = reactNativeBundleNameProvider |
| 17 | + |
| 18 | + val manifestProvider: ManifestProvider |
| 19 | + get() = manifestProviderInternal |
| 20 | + |
| 21 | + override val reactNativeHost: TestAppReactNativeHost |
| 22 | + get() = reactNativeHostInternal |
| 23 | + |
| 24 | + private lateinit var manifestProviderInternal: ManifestProvider |
| 25 | + private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider |
| 26 | + private lateinit var reactNativeHostInternal: TestAppReactNativeHost |
| 27 | + |
| 28 | + fun reloadJSFromServer(activity: Activity?, bundleURL: String) { |
| 29 | + reactNativeHostInternal.reloadJSFromServer(activity, bundleURL) |
| 30 | + } |
| 31 | + |
| 32 | + override fun onCreate() { |
| 33 | + super.onCreate() |
| 34 | + |
| 35 | + // We need to initialize SoLoader early because ManifestProvider may |
| 36 | + // need to use WritableNativeMap when parsing initial properties. |
| 37 | + SoLoader.init(this, false) |
| 38 | + |
| 39 | + manifestProviderInternal = ManifestProvider.create(this) |
| 40 | + val (manifest, _) = manifestProvider.fromResources() |
| 41 | + |
| 42 | + reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot) |
| 43 | + reactNativeHostInternal = |
| 44 | + TestAppReactNativeHost(this, reactNativeBundleNameProvider) |
| 45 | + |
| 46 | + val eventConsumers = PackageList(this).packages |
| 47 | + .filter { it is ReactTestAppLifecycleEvents } |
| 48 | + .map { it as ReactTestAppLifecycleEvents } |
| 49 | + |
| 50 | + eventConsumers.forEach { it.onTestAppInitialized() } |
| 51 | + |
| 52 | + reactNativeHostInternal.init( |
| 53 | + beforeReactNativeInit = { |
| 54 | + eventConsumers.forEach { it.onTestAppWillInitializeReactNative() } |
| 55 | + }, |
| 56 | + afterReactNativeInit = { |
| 57 | + eventConsumers.forEach { it.onTestAppDidInitializeReactNative() } |
| 58 | + } |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +val Context.testApp: TestApp |
| 64 | + get() = applicationContext as TestApp |
0 commit comments