diff --git a/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheet.kt b/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheet.kt index 053679f769a..bfd2a1f1575 100644 --- a/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheet.kt +++ b/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheet.kt @@ -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() + } } } diff --git a/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheetKeyboardHandler.kt b/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheetKeyboardHandler.kt index cfaabbcada7..08ad3fd5070 100644 --- a/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheetKeyboardHandler.kt +++ b/paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheetKeyboardHandler.kt @@ -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) @@ -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) }