|
| 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.react.ReactHost |
| 9 | +import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost |
| 10 | +import com.facebook.react.soloader.OpenSourceMergedSoMapping |
| 11 | +import com.facebook.soloader.SoLoader |
| 12 | +import com.microsoft.reacttestapp.manifest.Manifest |
| 13 | +import com.microsoft.reacttestapp.manifest.ManifestProvider |
| 14 | +import com.microsoft.reacttestapp.react.ReactBundleNameProvider |
| 15 | +import com.microsoft.reacttestapp.react.TestAppReactNativeHost |
| 16 | +import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents |
| 17 | + |
| 18 | +class TestApp : |
| 19 | + Application(), |
| 20 | + ReactApplication { |
| 21 | + |
| 22 | + val bundleNameProvider: ReactBundleNameProvider |
| 23 | + get() = reactNativeBundleNameProvider |
| 24 | + |
| 25 | + val manifest: Manifest by lazy { |
| 26 | + ManifestProvider.manifest() |
| 27 | + } |
| 28 | + |
| 29 | + override val reactHost: ReactHost |
| 30 | + get() = getDefaultReactHost(this.applicationContext, reactNativeHost) |
| 31 | + |
| 32 | + override val reactNativeHost: TestAppReactNativeHost |
| 33 | + get() = reactNativeHostInternal |
| 34 | + |
| 35 | + private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider |
| 36 | + private lateinit var reactNativeHostInternal: TestAppReactNativeHost |
| 37 | + |
| 38 | + fun reloadJSFromServer(activity: Activity, bundleURL: String) { |
| 39 | + reactNativeHostInternal.reloadJSFromServer(activity, bundleURL) |
| 40 | + } |
| 41 | + |
| 42 | + override fun onCreate() { |
| 43 | + super.onCreate() |
| 44 | + |
| 45 | + SoLoader.init(this, OpenSourceMergedSoMapping) |
| 46 | + |
| 47 | + reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot) |
| 48 | + reactNativeHostInternal = |
| 49 | + TestAppReactNativeHost(this, reactNativeBundleNameProvider) |
| 50 | + |
| 51 | + val eventConsumers = PackageList(this).packages |
| 52 | + .filter { it is ReactTestAppLifecycleEvents } |
| 53 | + .map { it as ReactTestAppLifecycleEvents } |
| 54 | + |
| 55 | + eventConsumers.forEach { it.onTestAppInitialized() } |
| 56 | + |
| 57 | + reactNativeHostInternal.init( |
| 58 | + beforeReactNativeInit = { |
| 59 | + eventConsumers.forEach { it.onTestAppWillInitializeReactNative() } |
| 60 | + }, |
| 61 | + afterReactNativeInit = { |
| 62 | + eventConsumers.forEach { it.onTestAppDidInitializeReactNative() } |
| 63 | + } |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +val Context.testApp: TestApp |
| 69 | + get() = applicationContext as TestApp |
0 commit comments