Skip to content

Commit

Permalink
Fix bottom sheet for APIs below 30
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Aug 31, 2023
1 parent 5a6ff9c commit bc28c35
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ internal class BottomSheetState(
// a CancellationException.
keyboardHandler.dismiss()
if (modalBottomSheetState.isVisible) {
modalBottomSheetState.hide()
repeatUntilSucceededOrLimit(10) {
// Hiding the bottom sheet can be interrupted.
// We keep trying until it's fully hidden.
modalBottomSheetState.hide()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.stripe.android.common.ui

import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.isImeVisible
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.SoftwareKeyboardController
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlinx.coroutines.flow.first

@OptIn(ExperimentalComposeUiApi::class)
Expand All @@ -30,11 +32,23 @@ internal class BottomSheetKeyboardHandler(
}
}

@OptIn(ExperimentalLayoutApi::class, ExperimentalComposeUiApi::class)
@OptIn(ExperimentalComposeUiApi::class)
@Composable
internal fun rememberBottomSheetKeyboardHandler(): BottomSheetKeyboardHandler {
val isImeVisible = WindowInsets.isImeVisible
val isImeVisibleState = rememberUpdatedState(isImeVisible)
val view = LocalView.current
val isImeVisible = remember { mutableStateOf(false) }

DisposableEffect(view) {
ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
isImeVisible.value = insets.isVisible(WindowInsetsCompat.Type.ime())
insets
}

onDispose {
ViewCompat.setOnApplyWindowInsetsListener(view, null)
}
}

val keyboardController = LocalSoftwareKeyboardController.current
return BottomSheetKeyboardHandler(keyboardController, isImeVisibleState)
return BottomSheetKeyboardHandler(keyboardController, isImeVisible)
}

0 comments on commit bc28c35

Please sign in to comment.