Skip to content

Commit 1496e0f

Browse files
better props cast
1 parent 45716c6 commit 1496e0f

File tree

3 files changed

+60
-69
lines changed

3 files changed

+60
-69
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.callstack.reactnativebrownfield
2+
3+
import android.os.Bundle
4+
import com.facebook.react.bridge.ReadableArray
5+
import com.facebook.react.bridge.ReadableMap
6+
import java.util.ArrayList
7+
8+
object PropsBundle {
9+
fun fromHashMap(map: HashMap<String, *>): Bundle {
10+
val bundle = Bundle()
11+
map.forEach {
12+
when (it.value) {
13+
is ArrayList<*> -> {
14+
bundle.putSerializable(it.key, it.value as ArrayList<*>)
15+
}
16+
is ReadableArray -> {
17+
bundle.putSerializable(it.key, (it.value as ReadableArray).toArrayList())
18+
}
19+
is HashMap<*, *> -> {
20+
bundle.putBundle(it.key, fromHashMap(it.value as HashMap<String, *>))
21+
}
22+
is ReadableMap -> {
23+
bundle.putBundle(it.key, fromHashMap((it.value as ReadableMap).toHashMap()))
24+
}
25+
is Boolean -> {
26+
bundle.putBoolean(it.key, it.value as Boolean)
27+
}
28+
is Int -> {
29+
bundle.putInt(it.key, it.value as Int)
30+
}
31+
is String -> {
32+
bundle.putString(it.key, it.value as String)
33+
}
34+
is Double -> {
35+
bundle.putDouble(it.key, it.value as Double)
36+
}
37+
else -> {
38+
bundle.putSerializable(it.key, null)
39+
}
40+
}
41+
}
42+
43+
return bundle
44+
}
45+
}

android/src/main/java/com/callstack/reactnativebrownfield/ReactNativeActivity.kt

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import com.facebook.react.ReactRootView
1212
import com.facebook.react.devsupport.DoubleTapReloadRecognizer
1313
import com.facebook.react.modules.core.PermissionListener
1414
import com.facebook.react.bridge.Callback
15-
import com.facebook.react.bridge.ReadableArray
1615
import com.facebook.react.bridge.ReadableMap
17-
import com.facebook.react.bridge.WritableMap
1816
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
1917
import com.facebook.react.modules.core.PermissionAwareActivity
2018

@@ -23,15 +21,14 @@ private const val INITIAL_PROPS = "com.callstack.reactnativebrownfield.ACTIVITY_
2321

2422
class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, PermissionAwareActivity {
2523
private var reactRootView: ReactRootView? = null
26-
private lateinit var moduleName: String
2724
private lateinit var doubleTapReloadRecognizer: DoubleTapReloadRecognizer
2825
private lateinit var permissionsCallback: Callback
2926
private var permissionListener: PermissionListener? = null
3027

3128
override fun onCreate(savedInstanceState: Bundle?) {
3229
super.onCreate(savedInstanceState)
3330

34-
moduleName = intent.getStringExtra(MODULE_NAME)
31+
val moduleName = intent.getStringExtra(MODULE_NAME)
3532
val initialProps = intent.getBundleExtra(INITIAL_PROPS)
3633

3734
reactRootView = ReactRootView(this)
@@ -162,36 +159,13 @@ class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, Perm
162159
}
163160

164161
@JvmStatic
165-
fun createReactActivityIntent(context: Context, moduleName: String, initialProps: WritableMap): Intent {
166-
val bundle = Bundle()
167-
initialProps.toHashMap().forEach {
168-
when (it.value) {
169-
// Should we go recursively with arrays and maps?
170-
is ReadableArray -> {
171-
bundle.putSerializable(it.key, (it.value as ReadableArray).toArrayList())
172-
}
173-
is ReadableMap -> {
174-
bundle.putSerializable(it.key, (it.value as ReadableMap).toHashMap())
175-
}
176-
is Boolean -> {
177-
bundle.putBoolean(it.key, it.value as Boolean)
178-
}
179-
is Int -> {
180-
bundle.putInt(it.key, it.value as Int)
181-
}
182-
is String -> {
183-
bundle.putString(it.key, it.value as String)
184-
}
185-
is Double -> {
186-
bundle.putDouble(it.key, it.value as Double)
187-
}
188-
else -> {
189-
bundle.putSerializable(it.key, null)
190-
}
191-
}
192-
}
162+
fun createReactActivityIntent(context: Context, moduleName: String, initialProps: HashMap<String, *>): Intent {
163+
return createReactActivityIntent(context, moduleName, PropsBundle.fromHashMap(initialProps))
164+
}
193165

194-
return createReactActivityIntent(context, moduleName, bundle)
166+
@JvmStatic
167+
fun createReactActivityIntent(context: Context, moduleName: String, initialProps: ReadableMap): Intent {
168+
return createReactActivityIntent(context, moduleName, initialProps.toHashMap())
195169
}
196170
}
197171
}

android/src/main/java/com/callstack/reactnativebrownfield/ReactNativeFragment.kt

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.callstack.reactnativebrownfield;
22

33
import android.annotation.TargetApi
4-
import android.content.Context
5-
import android.content.Intent
64
import android.os.Build
75
import android.os.Bundle
86
import androidx.fragment.app.Fragment
@@ -13,8 +11,6 @@ import android.view.ViewGroup
1311
import com.facebook.infer.annotation.Assertions
1412
import com.facebook.react.ReactRootView
1513
import com.facebook.react.bridge.Callback
16-
import com.facebook.react.bridge.ReadableArray
17-
import com.facebook.react.bridge.ReadableMap
1814
import com.facebook.react.bridge.WritableMap
1915
import com.facebook.react.common.LifecycleState
2016
import com.facebook.react.devsupport.DoubleTapReloadRecognizer
@@ -28,14 +24,13 @@ private const val INITIAL_PROPS = "com.callstack.reactnativebrownfield.FRAGMENT_
2824
class ReactNativeFragment : Fragment(), PermissionAwareActivity {
2925

3026
private var reactRootView: ReactRootView? = null
31-
private lateinit var moduleName: String
3227
private lateinit var doubleTapReloadRecognizer: DoubleTapReloadRecognizer
3328
private lateinit var permissionsCallback: Callback
3429
private var permissionListener: PermissionListener? = null
3530

3631
override fun onCreate(savedInstanceState: Bundle?) {
3732
super.onCreate(savedInstanceState)
38-
moduleName = arguments?.getString(MODULE_NAME)!!
33+
val moduleName = arguments?.getString(MODULE_NAME)!!
3934
val initialProps = arguments?.getBundle(INITIAL_PROPS)
4035

4136
doubleTapReloadRecognizer = DoubleTapReloadRecognizer()
@@ -44,7 +39,7 @@ class ReactNativeFragment : Fragment(), PermissionAwareActivity {
4439
reactRootView?.startReactApplication(
4540
BridgeManager.shared.reactNativeHost.reactInstanceManager,
4641
moduleName,
47-
null
42+
initialProps
4843
)
4944
}
5045

@@ -167,36 +162,13 @@ class ReactNativeFragment : Fragment(), PermissionAwareActivity {
167162
}
168163

169164
@JvmStatic
170-
fun createReactNativeFragment(moduleName: String, initialProps: WritableMap): ReactNativeFragment {
171-
val bundle = Bundle()
172-
initialProps.toHashMap().forEach {
173-
when (it.value) {
174-
// Should we go recursively with arrays and maps?
175-
is ReadableArray -> {
176-
bundle.putSerializable(it.key, (it.value as ReadableArray).toArrayList())
177-
}
178-
is ReadableMap -> {
179-
bundle.putSerializable(it.key, (it.value as ReadableMap).toHashMap())
180-
}
181-
is Boolean -> {
182-
bundle.putBoolean(it.key, it.value as Boolean)
183-
}
184-
is Int -> {
185-
bundle.putInt(it.key, it.value as Int)
186-
}
187-
is String -> {
188-
bundle.putString(it.key, it.value as String)
189-
}
190-
is Double -> {
191-
bundle.putDouble(it.key, it.value as Double)
192-
}
193-
else -> {
194-
bundle.putSerializable(it.key, null)
195-
}
196-
}
197-
}
165+
fun createReactNativeFragment(moduleName: String, initialProps: HashMap<String, *>): ReactNativeFragment {
166+
return createReactNativeFragment(moduleName, PropsBundle.fromHashMap(initialProps))
167+
}
198168

199-
return createReactNativeFragment(moduleName, bundle)
169+
@JvmStatic
170+
fun createReactNativeFragment(moduleName: String, initialProps: WritableMap): ReactNativeFragment {
171+
return createReactNativeFragment(moduleName, initialProps.toHashMap())
200172
}
201173
}
202174

0 commit comments

Comments
 (0)