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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272package com.hyperswitch
import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.modules.core.PermissionAwareActivity
import com.facebook.react.modules.core.PermissionListener
import com.facebook.react.runtime.hermes.HermesInstance
import com.facebook.react.shell.MainReactPackage
import com.facebook.react.ReactDelegate
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.react.defaults.DefaultReactHost
import com.facebook.react.PackageList
/**
* Fragment for creating a React View. This allows the developer to "embed" a React Application
* inside native components such as a Drawer, ViewPager, etc.
*/
internal open class HyperFragment : Fragment(), PermissionAwareActivity {
protected lateinit var reactDelegate: ReactDelegate
private var disableHostLifecycleEvents = false
private var permissionListener: PermissionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var mainComponentName: String? = null
var launchOptions: Bundle? = null
var fabricEnabled = false
arguments?.let { args ->
mainComponentName = args.getString(ARG_COMPONENT_NAME)
launchOptions = args.getBundle(ARG_LAUNCH_OPTIONS)
fabricEnabled = args.getBoolean(ARG_FABRIC_ENABLED)
@Suppress("DEPRECATION")
disableHostLifecycleEvents = args.getBoolean(ARG_DISABLE_HOST_LIFECYCLE_EVENTS)
}
checkNotNull(mainComponentName) { "Cannot loadApp if component name is null" }
reactDelegate =
if (ReactNativeFeatureFlags.enableBridgelessArchitecture()) {
ReactDelegate(requireActivity(), reactHost, mainComponentName, launchOptions)
} else {
@Suppress("DEPRECATION")
(ReactDelegate(
requireActivity(),
reactNativeHost,
mainComponentName,
launchOptions,
fabricEnabled,
))
}
}
/**
* Get the [ReactNativeHost] used by this app. By default, assumes [Activity.getApplication] is an
* instance of [ReactApplication] and calls [ReactApplication.reactNativeHost]. Override this
* method if your application class does not implement `ReactApplication` or you simply have a
* different mechanism for storing a `ReactNativeHost`, e.g. as a static field somewhere.
*/
@Suppress("DEPRECATION")
@Deprecated(
"You should not use ReactNativeHost directly in the New Architecture. Use ReactHost instead.",
ReplaceWith("reactHost"),
)
protected open val reactNativeHost: ReactNativeHost?
get() = // (activity?.application as ReactApplication?)?.reactNativeHost
object : DefaultReactNativeHost(requireActivity().application) {
override fun getPackages(): List<ReactPackage> = PackageList(requireActivity().application).packages.apply{}
override fun getJSMainModuleName(): String = "index"
override fun getBundleAssetName(): String = "hyperswitch.bundle"
override fun getJSBundleFile(): String = "assets://hyperswitch.bundle"
override fun getUseDeveloperSupport(): Boolean = true
override val isNewArchEnabled: Boolean = true
override val isHermesEnabled: Boolean = true
}
/**
* Get the [ReactHost] used by this app. By default, assumes [Activity.getApplication] is an
* instance of [ReactApplication] and calls [ReactApplication.reactHost]. Override this method if
* your application class does not implement `ReactApplication` or you simply have a different
* mechanism for storing a `ReactHost`, e.g. as a static field somewhere.
*
* If you're using Old Architecture/Bridge Mode, this method should return null as [ReactHost] is
* a Bridgeless-only concept.
*/
protected open val reactHost: ReactHost?
get() = DefaultReactHost.getDefaultReactHost(
requireContext(),
PackageList(requireActivity().application).packages.apply{},
"index",
"hyperswitch",
"assets://hyperswitch.bundle",
HermesInstance(),
true
)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
reactDelegate.loadApp()
return reactDelegate.reactRootView
}
override fun onResume() {
super.onResume()
if (!disableHostLifecycleEvents) {
reactDelegate.onHostResume()
}
}
override fun onPause() {
super.onPause()
if (!disableHostLifecycleEvents) {
reactDelegate.onHostPause()
}
}
override fun onDestroy() {
super.onDestroy()
if (!disableHostLifecycleEvents) {
reactDelegate.onHostDestroy()
} else {
reactDelegate.unloadApp()
}
}
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@Suppress("DEPRECATION") super.onActivityResult(requestCode, resultCode, data)
reactDelegate.onActivityResult(requestCode, resultCode, data, false)
}
/**
* Helper to forward hardware back presses to our React Native Host.
*
* This must be called via a forward from your host Activity.
*/
open fun onBackPressed(): Boolean = reactDelegate.onBackPressed()
/**
* Helper to forward onKeyUp commands from our host Activity. This allows [ReactFragment] to
* handle double tap reloads and dev menus.
*
* This must be called via a forward from your host Activity.
*
* @param keyCode keyCode
* @param event event
* @return true if we handled onKeyUp
*/
open fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean =
reactDelegate.shouldShowDevMenuOrReload(keyCode, event)
@Deprecated("Deprecated in Java")
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray,
) {
@Suppress("DEPRECATION")
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionListener?.let {
if (it.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
permissionListener = null
}
}
}
override fun checkPermission(permission: String, pid: Int, uid: Int): Int =
activity?.checkPermission(permission, pid, uid) ?: 0
override fun checkSelfPermission(permission: String): Int =
activity?.checkSelfPermission(permission) ?: 0
@Suppress("DEPRECATION")
override fun requestPermissions(
permissions: Array<String>,
requestCode: Int,
listener: PermissionListener?,
) {
permissionListener = listener
requestPermissions(permissions, requestCode)
}
/** Builder class to help instantiate a ReactFragment. */
class Builder {
var componentName: String? = null
var launchOptions: Bundle? = null
var fabricEnabled: Boolean = false
/**
* Set the Component name for our React Native instance.
*
* @param componentName The name of the component
* @return Builder
*/
fun setComponentName(componentName: String): Builder {
this.componentName = componentName
return this
}
/**
* Set the Launch Options for our React Native instance.
*
* @param launchOptions launchOptions
* @return Builder
*/
fun setLaunchOptions(launchOptions: Bundle): Builder {
this.launchOptions = launchOptions
return this
}
fun build(): HyperFragment = newInstance(componentName, launchOptions, fabricEnabled)
@Deprecated(
"You should not change call ReactFragment.setFabricEnabled. Instead enable the NewArchitecture for the whole application with newArchEnabled=true in your gradle.properties file"
)
fun setFabricEnabled(fabricEnabled: Boolean): Builder {
this.fabricEnabled = fabricEnabled
return this
}
}
companion object {
protected const val ARG_COMPONENT_NAME: String = "arg_component_name"
protected const val ARG_LAUNCH_OPTIONS: String = "arg_launch_options"
protected const val ARG_FABRIC_ENABLED: String = "arg_fabric_enabled"
@Deprecated(
"We will remove this and use a different solution for handling Fragment lifecycle events."
)
protected const val ARG_DISABLE_HOST_LIFECYCLE_EVENTS: String =
"arg_disable_host_lifecycle_events"
/**
* @param componentName The name of the react native component
* @param launchOptions The launch options for the react native component
* @param fabricEnabled Flag to enable Fabric for ReactFragment
* @return A new instance of fragment ReactFragment.
*/
private fun newInstance(
componentName: String?,
launchOptions: Bundle?,
fabricEnabled: Boolean,
): HyperFragment {
val args =
Bundle().apply {
putString(ARG_COMPONENT_NAME, componentName)
putBundle(ARG_LAUNCH_OPTIONS, launchOptions)
putBoolean(ARG_FABRIC_ENABLED, fabricEnabled)
}
return HyperFragment().apply { setArguments(args) }
}
}
}