mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-06-30 19:00:46 +02:00
Fixes bottom bar appearing in chats when the keyboard is open.
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.navigation
|
package com.vitorpamplona.amethyst.ui.navigation
|
||||||
|
|
||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
|
import android.view.View
|
||||||
import android.view.ViewTreeObserver
|
import android.view.ViewTreeObserver
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
@ -53,23 +54,30 @@ enum class Keyboard {
|
|||||||
Opened, Closed
|
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
|
@Composable
|
||||||
fun keyboardAsState(): State<Keyboard> {
|
fun keyboardAsState(): State<Keyboard> {
|
||||||
val keyboardState = remember { mutableStateOf(Keyboard.Closed) }
|
|
||||||
val view = LocalView.current
|
val view = LocalView.current
|
||||||
|
|
||||||
|
val keyboardState = remember(view) {
|
||||||
|
mutableStateOf(isKeyboardOpen(view))
|
||||||
|
}
|
||||||
|
|
||||||
DisposableEffect(view) {
|
DisposableEffect(view) {
|
||||||
val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener {
|
val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener {
|
||||||
val rect = Rect()
|
val newKeyboardValue = isKeyboardOpen(view)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newKeyboardValue != keyboardState.value) {
|
if (newKeyboardValue != keyboardState.value) {
|
||||||
keyboardState.value = newKeyboardValue
|
keyboardState.value = newKeyboardValue
|
||||||
@ -89,8 +97,8 @@ fun keyboardAsState(): State<Keyboard> {
|
|||||||
fun IfKeyboardClosed(
|
fun IfKeyboardClosed(
|
||||||
inner: @Composable () -> Unit
|
inner: @Composable () -> Unit
|
||||||
) {
|
) {
|
||||||
val isKeyboardOpen by keyboardAsState()
|
val isKeyboardState by keyboardAsState()
|
||||||
if (isKeyboardOpen == Keyboard.Closed) {
|
if (isKeyboardState == Keyboard.Closed) {
|
||||||
inner()
|
inner()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user