From 6772360ae7a51fd7cf0d02f0af765f93e6f67d4c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 4 Dec 2023 10:48:29 -0500 Subject: [PATCH] Fixes bottom bar appearing in chats when the keyboard is open. --- .../amethyst/ui/navigation/AppBottomBar.kt | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt index 16d2bc61d..39e3c0e84 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt @@ -1,6 +1,7 @@ package com.vitorpamplona.amethyst.ui.navigation import android.graphics.Rect +import android.view.View import android.view.ViewTreeObserver import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box @@ -53,23 +54,30 @@ enum class Keyboard { Opened, Closed } +fun isKeyboardOpen(view: View): Keyboard { + val rect = Rect() + view.getWindowVisibleDisplayFrame(rect) + val screenHeight = view.rootView.height + val keypadHeight = screenHeight - rect.bottom + + return if (keypadHeight > screenHeight * 0.15) { + Keyboard.Opened + } else { + Keyboard.Closed + } +} + @Composable fun keyboardAsState(): State { - val keyboardState = remember { mutableStateOf(Keyboard.Closed) } val view = LocalView.current + val keyboardState = remember(view) { + mutableStateOf(isKeyboardOpen(view)) + } + DisposableEffect(view) { val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener { - val rect = Rect() - view.getWindowVisibleDisplayFrame(rect) - val screenHeight = view.rootView.height - val keypadHeight = screenHeight - rect.bottom - - val newKeyboardValue = if (keypadHeight > screenHeight * 0.15) { - Keyboard.Opened - } else { - Keyboard.Closed - } + val newKeyboardValue = isKeyboardOpen(view) if (newKeyboardValue != keyboardState.value) { keyboardState.value = newKeyboardValue @@ -89,8 +97,8 @@ fun keyboardAsState(): State { fun IfKeyboardClosed( inner: @Composable () -> Unit ) { - val isKeyboardOpen by keyboardAsState() - if (isKeyboardOpen == Keyboard.Closed) { + val isKeyboardState by keyboardAsState() + if (isKeyboardState == Keyboard.Closed) { inner() } }