Skip to content

Commit fe0f4c8

Browse files
chore: improved null check on Android ViewGroup
1 parent 0675588 commit fe0f4c8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
2424
private var windowAnimation: Int = 0
2525

2626
// First child of the rootSheetView
27-
private val containerView: ViewGroup
28-
get() = rootSheetView.getChildAt(0) as ViewGroup
27+
private val containerView: ViewGroup?
28+
get() = if (rootSheetView.childCount > 0) {
29+
rootSheetView.getChildAt(0) as? ViewGroup
30+
} else {
31+
null
32+
}
2933

30-
private val sheetContainerView: ViewGroup
31-
get() = rootSheetView.parent as ViewGroup
34+
private val sheetContainerView: ViewGroup?
35+
get() = rootSheetView.parent?.let { it as? ViewGroup }
3236

3337
/**
3438
* Specify whether the sheet background is dimmed.
@@ -71,19 +75,19 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
7175

7276
// 1st child is the content view
7377
val contentView: ViewGroup?
74-
get() = containerView.getChildAt(0) as? ViewGroup
78+
get() = containerView?.getChildAt(0) as? ViewGroup
7579

7680
// 2nd child is the footer view
7781
val footerView: ViewGroup?
78-
get() = containerView.getChildAt(1) as? ViewGroup
82+
get() = containerView?.getChildAt(1) as? ViewGroup
7983

8084
var sizes: Array<Any> = arrayOf("medium", "large")
8185

8286
init {
8387
setContentView(rootSheetView)
8488

85-
sheetContainerView.setBackgroundColor(backgroundColor)
86-
sheetContainerView.clipToOutline = true
89+
sheetContainerView?.setBackgroundColor(backgroundColor)
90+
sheetContainerView?.clipToOutline = true
8791

8892
// Setup window params to adjust layout based on Keyboard state
8993
window?.apply {
@@ -131,7 +135,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
131135

132136
// Use current background color
133137
background.paint.color = backgroundColor
134-
sheetContainerView.background = background
138+
sheetContainerView?.background = background
135139
}
136140

137141
/**
@@ -196,11 +200,14 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
196200
}
197201

198202
fun positionFooter() {
199-
footerView?.apply {
200-
y = (maxScreenHeight - sheetContainerView.top - footerHeight).toFloat()
203+
footerView?.let { footer ->
204+
sheetContainerView?.let { container ->
205+
footer.y = (maxScreenHeight - container.top - footerHeight).toFloat()
206+
}
201207
}
202208
}
203209

210+
204211
/**
205212
* Set the state based for the given size index.
206213
*/

0 commit comments

Comments
 (0)