-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMendixReactApplication.kt
More file actions
174 lines (153 loc) · 7.34 KB
/
Copy pathMendixReactApplication.kt
File metadata and controls
174 lines (153 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.mendix.mendixnative
import android.app.Application
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.JSBundleLoaderDelegate
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.defaults.DefaultComponentsRegistry
import com.facebook.react.defaults.DefaultReactHostDelegate
import com.facebook.react.defaults.DefaultTurboModuleManagerDelegate
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.fabric.ComponentFactory
import com.facebook.react.runtime.ReactHostImpl
import com.facebook.react.runtime.hermes.HermesInstance
import com.facebook.react.soloader.OpenSourceMergedSoMapping
import com.facebook.soloader.SoLoader
import com.mendix.mendixnative.error.ErrorHandler
import com.mendix.mendixnative.error.ErrorHandlerFactory
import com.mendix.mendixnative.error.mapErrorHandlerToRedBox
import com.mendix.mendixnative.handler.DummyErrorHandler
import com.mendix.mendixnative.devsupport.MendixDevSupportManagerFactory
import com.mendix.mendixnative.react.ota.OtaJSBundleUrlProvider
import com.mendix.mendixnative.react.splash.MendixSplashScreenPresenter
import com.mendixnative.MendixNativePackage
import java.util.*
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactNativeHost
abstract class MendixReactApplication : Application(), MendixApplication, ErrorHandlerFactory {
private val appSessionId = "" + Math.random() * 1000 + Date().time
override fun getAppSessionId(): String = appSessionId
// Lazily create RedBox handler only in dev. In release builds this stays null.
private val redBoxHandler: RedBoxHandler? by lazy {
if (useDeveloperSupport) mapErrorHandlerToRedBox(createErrorHandler()) else null
}
// Lazily obtain splash presenter so subclass overrides are respected.
private val splashScreenPresenter by lazy { createSplashScreenPresenter() }
private var jsBundleFileProvider: JSBundleFileProvider? = jsBundleProvider
override var reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
override fun getUseDeveloperSupport(): Boolean = this@MendixReactApplication.useDeveloperSupport
override fun getPackages(): List<ReactPackage> {
val pkgs: MutableList<ReactPackage> = ArrayList()
// Use the packages provided by the concrete Application subclass.
pkgs.addAll(this@MendixReactApplication.packages)
// Inject splashScreenPresenter into any MendixNativePackage instances without creating duplicates.
applyInternalPackageAugmentations(pkgs)
return pkgs
}
override fun getJSBundleFile(): String? = this@MendixReactApplication.jsBundleFile
override fun getJSMainModuleName(): String = "index"
override fun getBundleAssetName(): String? = super.getBundleAssetName()
override fun getRedBoxHandler(): RedBoxHandler? = null
// Hermes & New Arch flags; Hermes executor will be picked automatically when isHermesEnabled is true.
override val isNewArchEnabled: Boolean = true
override val isHermesEnabled: Boolean = true
}
/**
* Build the [ReactHost] ourselves instead of using [DefaultReactHost.getDefaultReactHost],
* because that factory evaluates [ReactNativeHost.getJSBundleFile] once at creation time and
* bakes the result into a fixed [JSBundleLoader]. After an OTA update deploys a new bundle,
* a subsequent [ReactHost.reload] would still load the stale bundle.
*
* By providing a **dynamic** [JSBundleLoader] whose [JSBundleLoader.loadScript] calls
* [getJSBundleFile] on every invocation, each reload picks up the latest bundle path —
* whether it comes from OTA, a custom [JSBundleFileProvider], or the default asset bundle.
*/
@OptIn(UnstableReactNativeAPI::class)
override val reactHost: ReactHost by lazy {
val dynamicBundleLoader = object : JSBundleLoader() {
override fun loadScript(delegate: JSBundleLoaderDelegate): String {
val bundle = jsBundleFile
if (bundle != null) {
if (bundle.startsWith("assets://")) {
delegate.loadScriptFromAssets(assets, bundle, true)
} else {
delegate.loadScriptFromFile(bundle, bundle, false)
}
return bundle
}
val defaultBundle = "assets://index.android.bundle"
delegate.loadScriptFromAssets(assets, defaultBundle, true)
return defaultBundle
}
}
val hostPackages: MutableList<ReactPackage> = ArrayList(this@MendixReactApplication.packages)
applyInternalPackageAugmentations(hostPackages)
val delegate = DefaultReactHostDelegate(
jsMainModulePath = "index",
jsBundleLoader = dynamicBundleLoader,
reactPackages = hostPackages,
jsRuntimeFactory = HermesInstance(),
turboModuleManagerDelegateBuilder = DefaultTurboModuleManagerDelegate.Builder(),
)
val componentFactory = ComponentFactory()
DefaultComponentsRegistry.register(componentFactory)
ReactHostImpl(
context = applicationContext,
reactHostDelegate = delegate,
componentFactory = componentFactory,
allowPackagerServerAccess = true,
useDevSupport = useDeveloperSupport,
devSupportManagerFactory = MendixDevSupportManagerFactory(devBundleDownloadListener),
)
}
/**
* Apply internal augmentations to packages (e.g., attach presenters) without instantiating
* duplicate packages that would cause "duplicate module name" initialization errors.
*/
private fun applyInternalPackageAugmentations(packages: MutableList<ReactPackage>) {
packages.filterIsInstance<MendixNativePackage>().forEach { pkg ->
// Only set if not already set to avoid overwriting from user code.
if (pkg.splashScreenPresenter == null) {
pkg.splashScreenPresenter = splashScreenPresenter
}
}
}
override fun onCreate() {
super.onCreate()
SoLoader.init(this, OpenSourceMergedSoMapping)
// Only load the New Architecture entry point when enabled (always true here, but guarded for safety).
if (reactNativeHost is DefaultReactNativeHost) {
load()
}
}
override fun getJSBundleFile(): String? {
// Check for Native OTA
OtaJSBundleUrlProvider().getJSBundleFile(this)?.let {
return it
}
// Fallback to bundled bundle
return if (jsBundleFileProvider != null) jsBundleFileProvider!!.getJSBundleFile(this) else null
}
abstract override fun getUseDeveloperSupport(): Boolean
abstract override fun getPackages(): List<ReactPackage>
override fun createSplashScreenPresenter(): MendixSplashScreenPresenter? {
return null
}
override fun createErrorHandler(): ErrorHandler {
return DummyErrorHandler()
}
open val jsBundleProvider: JSBundleFileProvider?
get() = null
/**
* Override this to provide a [DevBundleDownloadListener] that receives bundle download
* progress, success, and failure callbacks. This is used to drive custom loading UIs.
*
* The listener is injected at [ReactHost] creation time via [MendixDevSupportManagerFactory].
* Use a delegating holder pattern if the actual listener is created after app initialization.
*/
open val devBundleDownloadListener: DevBundleDownloadListener?
get() = null
}