Skip to content

Commit 592a9f1

Browse files
lovegaoshilodev09
andauthored
fix: android auto responsive sheet size (#193)
* fix: android auto responsive sheet size * feat: fix auto responsive sheet size * feat: solution 1 this sets layout.height right after STATE.COLLAPSED is set to give an animation effect. sometimes janky. * feat: solution 2 * feat: disable dragging on auto size animation * refactor: simplify code, forget about animated size change * chore: remove unused code --------- Co-authored-by: lodev09 <lodev09@gmail.com>
1 parent 3b6c84d commit 592a9f1

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
210210
/**
211211
* Set the state based for the given size index.
212212
*/
213-
private fun setStateForSizeIndex(index: Int) {
213+
fun setStateForSizeIndex(index: Int) {
214214
behavior.state = getStateForSizeIndex(index)
215215
}
216216

@@ -263,29 +263,32 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
263263
/**
264264
* Determines the state based from the given size index.
265265
*/
266-
private fun getStateForSizeIndex(index: Int) =
267-
when (sizes.size) {
268-
1 -> BottomSheetBehavior.STATE_EXPANDED
266+
private fun getStateForSizeIndex(index: Int): Int {
267+
return when (sizes.size) {
268+
1 -> {
269+
return BottomSheetBehavior.STATE_EXPANDED
270+
}
269271

270-
2 -> {
271-
when (index) {
272-
0 -> BottomSheetBehavior.STATE_COLLAPSED
273-
1 -> BottomSheetBehavior.STATE_EXPANDED
274-
else -> BottomSheetBehavior.STATE_HIDDEN
275-
}
276-
}
272+
2 -> {
273+
when (index) {
274+
0 -> BottomSheetBehavior.STATE_COLLAPSED
275+
1 -> BottomSheetBehavior.STATE_EXPANDED
276+
else -> BottomSheetBehavior.STATE_HIDDEN
277+
}
278+
}
279+
280+
3 -> {
281+
when (index) {
282+
0 -> BottomSheetBehavior.STATE_COLLAPSED
283+
1 -> BottomSheetBehavior.STATE_HALF_EXPANDED
284+
2 -> BottomSheetBehavior.STATE_EXPANDED
285+
else -> BottomSheetBehavior.STATE_HIDDEN
286+
}
287+
}
277288

278-
3 -> {
279-
when (index) {
280-
0 -> BottomSheetBehavior.STATE_COLLAPSED
281-
1 -> BottomSheetBehavior.STATE_HALF_EXPANDED
282-
2 -> BottomSheetBehavior.STATE_EXPANDED
283289
else -> BottomSheetBehavior.STATE_HIDDEN
284-
}
285290
}
286-
287-
else -> BottomSheetBehavior.STATE_HIDDEN
288-
}
291+
}
289292

290293
/**
291294
* Handle keyboard state changes and adjust maxScreenHeight (sheet max height) accordingly.
@@ -331,6 +334,15 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
331334
1 -> {
332335
maxHeight = getSizeHeight(sizes[0])
333336
skipCollapsed = true
337+
338+
if (sizes[0] == "auto") {
339+
// Force a layout update
340+
sheetContainerView?.let {
341+
val params = it.layoutParams
342+
params.height = maxHeight
343+
it.layoutParams = params
344+
}
345+
}
334346
}
335347

336348
2 -> {
@@ -358,6 +370,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
358370
when (sizes.size) {
359371
1 -> {
360372
when (state) {
373+
BottomSheetBehavior.STATE_COLLAPSED -> SizeInfo(0, Utils.toDIP(behavior.maxHeight.toFloat()))
361374
BottomSheetBehavior.STATE_EXPANDED -> SizeInfo(0, Utils.toDIP(behavior.maxHeight.toFloat()))
362375
else -> null
363376
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ class TrueSheetView(context: Context) :
324324
fun configureIfShowing() {
325325
if (sheetDialog.isShowing) {
326326
sheetDialog.configure()
327-
sheetDialog.positionFooter()
327+
sheetDialog.setStateForSizeIndex(currentSizeIndex)
328+
329+
UiThreadUtil.runOnUiThread {
330+
sheetDialog.positionFooter()
331+
}
328332
}
329333
}
330334

example/src/components/sheets/PromptSheet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const PromptSheet = forwardRef((props: PromptSheetProps, ref: Ref<TrueShe
3838
grabber={false}
3939
edgeToEdge
4040
name="prompt-sheet"
41-
sizes={['auto', 'large']}
41+
sizes={['auto']}
4242
contentContainerStyle={styles.content}
4343
blurTint="dark"
4444
backgroundColor={DARK}

0 commit comments

Comments
 (0)