Closing FilterBar on Android back button press or Escape press

This commit is contained in:
dankito 2024-09-23 21:48:08 +02:00
parent 97282adf12
commit 4d7cca7a7e
7 changed files with 43 additions and 3 deletions

View File

@ -6,8 +6,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import net.codinux.banking.persistence.AndroidContext import net.codinux.banking.persistence.AndroidContext
import net.codinux.banking.persistence.SqliteBankingRepository
import net.codinux.banking.ui.config.DI
import net.codinux.banking.ui.service.AuthenticationService import net.codinux.banking.ui.service.AuthenticationService
import net.codinux.banking.ui.service.BiometricAuthenticationService import net.codinux.banking.ui.service.BiometricAuthenticationService
import net.codinux.banking.ui.service.ImageService import net.codinux.banking.ui.service.ImageService

View File

@ -3,6 +3,7 @@ package net.codinux.banking.ui
import android.os.Build import android.os.Build
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -11,6 +12,10 @@ import kotlinx.coroutines.Dispatchers
actual val Dispatchers.IOorDefault: CoroutineDispatcher actual val Dispatchers.IOorDefault: CoroutineDispatcher
get() = Dispatchers.IO get() = Dispatchers.IO
actual fun KeyEvent.isBackButtonPressedEvent(): Boolean =
this.nativeKeyEvent.keyCode == android.view.KeyEvent.KEYCODE_BACK
@Composable @Composable
actual fun rememberScreenSizeInfo(): ScreenSizeInfo { actual fun rememberScreenSizeInfo(): ScreenSizeInfo {
val config = LocalConfiguration.current val config = LocalConfiguration.current

View File

@ -1,6 +1,7 @@
package net.codinux.banking.ui package net.codinux.banking.ui
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -9,6 +10,9 @@ import kotlinx.coroutines.Dispatchers
expect val Dispatchers.IOorDefault: CoroutineDispatcher expect val Dispatchers.IOorDefault: CoroutineDispatcher
expect fun KeyEvent.isBackButtonPressedEvent(): Boolean
@Composable @Composable
expect fun rememberScreenSizeInfo(): ScreenSizeInfo expect fun rememberScreenSizeInfo(): ScreenSizeInfo

View File

@ -1,18 +1,24 @@
package net.codinux.banking.ui.appskeleton package net.codinux.banking.ui.appskeleton
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.*
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import net.codinux.banking.ui.config.DI import net.codinux.banking.ui.config.DI
import net.codinux.banking.ui.config.Internationalization import net.codinux.banking.ui.config.Internationalization
import net.codinux.banking.ui.forms.RoundedCornersCard import net.codinux.banking.ui.forms.RoundedCornersCard
import net.codinux.banking.ui.forms.Select import net.codinux.banking.ui.forms.Select
import net.codinux.banking.ui.isBackButtonPressedEvent
import net.codinux.banking.ui.model.settings.TransactionsGrouping import net.codinux.banking.ui.model.settings.TransactionsGrouping
private val uiState = DI.uiState private val uiState = DI.uiState
@ -37,10 +43,20 @@ fun FilterBar() {
val months = listOf("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" /*, "1. Quartal", "2. Quartal", "3. Quartal", "4. Quartal" */, null) val months = listOf("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" /*, "1. Quartal", "2. Quartal", "3. Quartal", "4. Quartal" */, null)
val filterBarFocus = remember { FocusRequester() }
Box( Box(
contentAlignment = Alignment.BottomEnd, contentAlignment = Alignment.BottomEnd,
modifier = Modifier.fillMaxSize().zIndex(100f) modifier = Modifier.fillMaxSize().zIndex(100f)
.padding(bottom = 64.dp, end = 74.dp) .padding(bottom = 64.dp, end = 74.dp).focusable(true).focusRequester(filterBarFocus).focusTarget().onKeyEvent { event ->
if (event.isBackButtonPressedEvent() || event.key == Key.Escape) {
DI.uiState.showFilterBar.value = false
true
} else {
false
}
}
) { ) {
Column(Modifier.height(230.dp).width(390.dp)) { Column(Modifier.height(230.dp).width(390.dp)) {
RoundedCornersCard(cornerSize = 4.dp, shadowElevation = 24.dp) { RoundedCornersCard(cornerSize = 4.dp, shadowElevation = 24.dp) {
@ -90,4 +106,9 @@ fun FilterBar() {
} }
} }
LaunchedEffect(filterBarFocus) {
filterBarFocus.requestFocus() // focus filter bar so that it receives key events to handle e.g. Escape button press
}
} }

View File

@ -3,6 +3,7 @@ package net.codinux.banking.ui
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalWindowInfo import androidx.compose.ui.platform.LocalWindowInfo
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -11,6 +12,9 @@ import kotlinx.coroutines.Dispatchers
actual val Dispatchers.IOorDefault: CoroutineDispatcher actual val Dispatchers.IOorDefault: CoroutineDispatcher
get() = Dispatchers.IO get() = Dispatchers.IO
actual fun KeyEvent.isBackButtonPressedEvent(): Boolean = false
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
actual fun rememberScreenSizeInfo(): ScreenSizeInfo { actual fun rememberScreenSizeInfo(): ScreenSizeInfo {

View File

@ -2,6 +2,7 @@ package net.codinux.banking.ui
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ExperimentalForeignApi
@ -16,6 +17,9 @@ import platform.UIKit.UIScreen
actual val Dispatchers.IOorDefault: CoroutineDispatcher actual val Dispatchers.IOorDefault: CoroutineDispatcher
get() = Dispatchers.IO get() = Dispatchers.IO
actual fun KeyEvent.isBackButtonPressedEvent(): Boolean = false // TODO
@OptIn(ExperimentalForeignApi::class) @OptIn(ExperimentalForeignApi::class)
@Composable @Composable
actual fun rememberScreenSizeInfo(): ScreenSizeInfo { actual fun rememberScreenSizeInfo(): ScreenSizeInfo {

View File

@ -3,6 +3,7 @@ package net.codinux.banking.ui
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalWindowInfo import androidx.compose.ui.platform.LocalWindowInfo
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -11,6 +12,9 @@ import kotlinx.coroutines.Dispatchers
actual val Dispatchers.IOorDefault: CoroutineDispatcher actual val Dispatchers.IOorDefault: CoroutineDispatcher
get() = Dispatchers.Default get() = Dispatchers.Default
actual fun KeyEvent.isBackButtonPressedEvent(): Boolean = false
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
actual fun rememberScreenSizeInfo(): ScreenSizeInfo { actual fun rememberScreenSizeInfo(): ScreenSizeInfo {