Skip to content

Commit 2b40742

Browse files
committed
refactor(android): cleanup code
1 parent c87a6ef commit 2b40742

File tree

5 files changed

+60
-45
lines changed

5 files changed

+60
-45
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

+20-9
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
2121
BottomSheetDialog(reactContext) {
2222

2323
private var keyboardManager = KeyboardManager(reactContext)
24-
private var sheetView: ViewGroup
2524
private var windowAnimation: Int = 0
2625

26+
// First child of the rootSheetView
27+
private val containerView: ViewGroup
28+
get() = rootSheetView.getChildAt(0) as ViewGroup
29+
30+
private val sheetContainerView: ViewGroup
31+
get() = rootSheetView.parent as ViewGroup
32+
2733
/**
2834
* Specify whether the sheet background is dimmed.
2935
* Set to `false` to allow interaction with the background components.
@@ -62,17 +68,22 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
6268

6369
var cornerRadius: Float = 0f
6470
var backgroundColor: Int = Color.WHITE
65-
var footerView: ViewGroup? = null
71+
72+
// 1st child is the content view
73+
val contentView: ViewGroup
74+
get() = containerView.getChildAt(0) as ViewGroup
75+
76+
// 2nd child is the footer view
77+
val footerView: ViewGroup
78+
get() = containerView.getChildAt(1) as ViewGroup
6679

6780
var sizes: Array<Any> = arrayOf("medium", "large")
6881

6982
init {
7083
setContentView(rootSheetView)
7184

72-
sheetView = rootSheetView.parent as ViewGroup
73-
74-
sheetView.setBackgroundColor(backgroundColor)
75-
sheetView.clipToOutline = true
85+
sheetContainerView.setBackgroundColor(backgroundColor)
86+
sheetContainerView.clipToOutline = true
7687

7788
// Setup window params to adjust layout based on Keyboard state
7889
window?.apply {
@@ -120,7 +131,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
120131

121132
// Use current background color
122133
background.paint.color = backgroundColor
123-
sheetView.background = background
134+
sheetContainerView.background = background
124135
}
125136

126137
/**
@@ -185,8 +196,8 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
185196
}
186197

187198
fun positionFooter() {
188-
footerView?.apply {
189-
y = (maxScreenHeight - sheetView.top - footerHeight).toFloat()
199+
footerView.apply {
200+
y = (maxScreenHeight - sheetContainerView.top - footerHeight).toFloat()
190201
}
191202
}
192203

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

+31-27
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TrueSheetView(context: Context) :
7575
val data = Arguments.createMap()
7676
data.putDouble("width", Utils.toDIP(w.toFloat()).toDouble())
7777
data.putDouble("height", Utils.toDIP(h.toFloat()).toDouble())
78+
7879
dispatchEvent(TrueSheetEvent.CONTAINER_SIZE_CHANGE, data)
7980
}
8081

@@ -97,7 +98,7 @@ class TrueSheetView(context: Context) :
9798
}
9899

99100
// Dispatch onPresent event
100-
dispatchEvent(TrueSheetEvent.PRESENT, sizeInfoData(sheetDialog.getSizeInfoForIndex(currentSizeIndex)))
101+
dispatchEvent(TrueSheetEvent.PRESENT, sizeInfoData(getSizeInfoForIndex(currentSizeIndex)))
101102
}
102103

103104
// Setup listener when the dialog has been dismissed.
@@ -118,7 +119,7 @@ class TrueSheetView(context: Context) :
118119
behavior.addBottomSheetCallback(
119120
object : BottomSheetBehavior.BottomSheetCallback() {
120121
override fun onSlide(sheetView: View, slideOffset: Float) {
121-
when (sheetDialog.behavior.state) {
122+
when (behavior.state) {
122123
// For consistency with IOS, we consider SETTLING as dragging change.
123124
BottomSheetBehavior.STATE_DRAGGING,
124125
BottomSheetBehavior.STATE_SETTLING -> handleDragChange(sheetView)
@@ -172,39 +173,35 @@ class TrueSheetView(context: Context) :
172173
// Do nothing as we are laid out by UIManager
173174
}
174175

176+
override fun onAttachedToWindow() {
177+
super.onAttachedToWindow()
178+
179+
// Initialize content
180+
UiThreadUtil.runOnUiThread {
181+
setContentHeight(sheetDialog.contentView.height)
182+
setFooterHeight(sheetDialog.footerView.height)
183+
184+
if (initialIndex >= 0) {
185+
currentSizeIndex = initialIndex
186+
sheetDialog.present(initialIndex, initialIndexAnimated)
187+
}
188+
189+
// Dispatch onMount event
190+
dispatchEvent(TrueSheetEvent.MOUNT)
191+
}
192+
}
193+
175194
override fun onDetachedFromWindow() {
176195
super.onDetachedFromWindow()
177196
sheetDialog.dismiss()
178197
}
179198

180199
override fun addView(child: View, index: Int) {
200+
UiThreadUtil.assertOnUiThread()
201+
rootSheetView.addView(child, index)
202+
181203
// Hide this host view
182204
visibility = GONE
183-
184-
(child as ViewGroup).let {
185-
// rootView's first child is the Container View
186-
rootSheetView.addView(it, index)
187-
188-
// Initialize content
189-
UiThreadUtil.runOnUiThread {
190-
// 1st child is the content view
191-
val contentView = it.getChildAt(0) as ViewGroup?
192-
setContentHeight(contentView?.height ?: 0)
193-
194-
// 2nd child is the footer view
195-
val footerView = it.getChildAt(1) as ViewGroup?
196-
sheetDialog.footerView = footerView
197-
setFooterHeight(footerView?.height ?: 0)
198-
199-
if (initialIndex >= 0) {
200-
currentSizeIndex = initialIndex
201-
sheetDialog.present(initialIndex, initialIndexAnimated)
202-
}
203-
204-
// Dispatch onMount event
205-
dispatchEvent(TrueSheetEvent.MOUNT)
206-
}
207-
}
208205
}
209206

210207
override fun getChildCount(): Int {
@@ -216,10 +213,13 @@ class TrueSheetView(context: Context) :
216213
override fun getChildAt(index: Int): View = rootSheetView.getChildAt(index)
217214

218215
override fun removeView(child: View) {
216+
UiThreadUtil.assertOnUiThread()
219217
rootSheetView.removeView(child)
220218
}
221219

222220
override fun removeViewAt(index: Int) {
221+
UiThreadUtil.assertOnUiThread()
222+
223223
val child = getChildAt(index)
224224
rootSheetView.removeView(child)
225225
}
@@ -394,6 +394,8 @@ class TrueSheetView(context: Context) :
394394
* Present the sheet at given size index.
395395
*/
396396
fun present(sizeIndex: Int, promiseCallback: () -> Unit) {
397+
UiThreadUtil.assertOnUiThread()
398+
397399
currentSizeIndex = sizeIndex
398400

399401
if (sheetDialog.isShowing) {
@@ -414,6 +416,8 @@ class TrueSheetView(context: Context) :
414416
* Dismisses the sheet.
415417
*/
416418
fun dismiss(promiseCallback: () -> Unit) {
419+
UiThreadUtil.assertOnUiThread()
420+
417421
dismissPromise = promiseCallback
418422
sheetDialog.dismiss()
419423
}

android/src/main/java/com/lodev09/truesheet/core/RootSheetView.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ class RootSheetView(private val context: Context?) :
3636

3737
var eventDispatcher: EventDispatcher? = null
3838

39+
private val reactContext: ThemedReactContext
40+
get() = context as ThemedReactContext
41+
3942
init {
4043
if (ReactFeatureFlags.dispatchPointerEvents) {
4144
jSPointerDispatcher = JSPointerDispatcher(this)
4245
}
4346
}
4447

45-
private val reactContext: ThemedReactContext
46-
get() = context as ThemedReactContext
47-
4848
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
4949
super.onSizeChanged(w, h, oldw, oldh)
5050

example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"react": "18.3.1",
1616
"react-native": "0.77.1",
1717
"react-native-edge-to-edge": "^1.4.3",
18-
"react-native-gesture-handler": "^2.23.1",
18+
"react-native-gesture-handler": "^2.24.0",
1919
"react-native-maps": "^1.20.1",
2020
"react-native-reanimated": "^3.16.7",
2121
"react-native-safe-area-context": "^5.2.0",

yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -17327,17 +17327,17 @@ __metadata:
1732717327
languageName: node
1732817328
linkType: hard
1732917329

17330-
"react-native-gesture-handler@npm:^2.23.1":
17331-
version: 2.23.1
17332-
resolution: "react-native-gesture-handler@npm:2.23.1"
17330+
"react-native-gesture-handler@npm:^2.24.0":
17331+
version: 2.24.0
17332+
resolution: "react-native-gesture-handler@npm:2.24.0"
1733317333
dependencies:
1733417334
"@egjs/hammerjs": ^2.0.17
1733517335
hoist-non-react-statics: ^3.3.0
1733617336
invariant: ^2.2.4
1733717337
peerDependencies:
1733817338
react: "*"
1733917339
react-native: "*"
17340-
checksum: f7afb2fdd370fdac52ef1ba37495ef598569df666a25f9a5f5825436861c90933face6b7ba8cb551fd90ba5438a1754495cf5690d9b663d4ab7a24c75d5abfc1
17340+
checksum: 65abaeef68180fee2811d01d88ff50c231a91faca05279222fcaaa55349e758b68a7d6a9ac3eddfb1887f6b4c4790ac195b99989f1ad8a2a1f3f3bdff3ba0a76
1734117341
languageName: node
1734217342
linkType: hard
1734317343

@@ -17422,7 +17422,7 @@ __metadata:
1742217422
react-native: 0.77.1
1742317423
react-native-builder-bob: ^0.37.0
1742417424
react-native-edge-to-edge: ^1.4.3
17425-
react-native-gesture-handler: ^2.23.1
17425+
react-native-gesture-handler: ^2.24.0
1742617426
react-native-maps: ^1.20.1
1742717427
react-native-reanimated: ^3.16.7
1742817428
react-native-safe-area-context: ^5.2.0

0 commit comments

Comments
 (0)