Implemented confirming a dialog by pressing enter in one of its TextFields

This commit is contained in:
dankito 2024-09-01 18:23:22 +02:00
parent e292b24eca
commit 362675cb12
4 changed files with 45 additions and 24 deletions

View File

@ -10,6 +10,7 @@ import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.codinux.banking.ui.IOorDefault
import net.codinux.banking.ui.composables.BankIcon
import net.codinux.banking.ui.config.Colors
import net.codinux.banking.ui.config.DI
@ -35,6 +36,24 @@ fun AddAccountDialog(
val coroutineScope = rememberCoroutineScope()
fun confirmCalled() {
selectedBank?.let {
isAddingAccount = true
coroutineScope.launch(Dispatchers.IOorDefault) {
val successful = DI.bankingService.addAccount(selectedBank!!, loginName, password)
withContext(Dispatchers.Main) {
isAddingAccount = false
if (successful) {
onDismiss()
}
}
}
}
}
BaseDialog(
title = "Bank Konto hinzufügen",
@ -44,21 +63,7 @@ fun AddAccountDialog(
useMoreThanPlatformDefaultWidthOnMobile = true,
onDismiss = onDismiss,
onConfirm = {
selectedBank?.let {
isAddingAccount = true
coroutineScope.launch { // TODO: launch on Dispatchers.IO where it is available
val successful = DI.bankingService.addAccount(selectedBank!!, loginName, password)
withContext(Dispatchers.Main) {
isAddingAccount = false
if (successful) {
onDismiss()
}
}
}
}
confirmCalled()
}
) {
Column {
@ -102,7 +107,7 @@ fun AddAccountDialog(
Spacer(modifier = Modifier.height(12.dp))
PasswordTextField(password) { password = it }
PasswordTextField(password, onChange = { password = it }, onEnterPressed = { confirmCalled() })
Spacer(modifier = Modifier.height(16.dp))
}

View File

@ -48,14 +48,19 @@ fun EnterTanDialog(tanChallengeReceived: TanChallengeReceived, onDismiss: () ->
var enteredTan by remember { mutableStateOf("") }
fun confirmCalled() {
if (enteredTan.length > 2) {
tanChallengeReceived.callback(EnterTanResult(enteredTan))
onDismiss()
}
}
BaseDialog(
title = "TAN Eingabe",
useMoreThanPlatformDefaultWidthOnMobile = true,
confirmButtonEnabled = enteredTan.length > 2,
onConfirm = {
tanChallengeReceived.callback(EnterTanResult(enteredTan))
onDismiss()
},
onConfirm = { confirmCalled() },
onDismiss = {
tanChallengeReceived.callback(EnterTanResult(null))
onDismiss()
@ -177,7 +182,8 @@ fun EnterTanDialog(tanChallengeReceived: TanChallengeReceived, onDismiss: () ->
keyboardOptions = KeyboardOptions(
autoCorrect = false,
keyboardType = if (challenge.selectedTanMethod.allowedTanFormat == AllowedTanFormat.Numeric) KeyboardType.Number else KeyboardType.Text
)
),
onEnterPressed = { confirmCalled() }
)
}
}

View File

@ -10,6 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.key.*
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.VisualTransformation
import net.codinux.banking.ui.config.Colors
@ -35,6 +36,7 @@ fun OutlinedTextField(
minLines: Int = 1,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small,
onEnterPressed: (() -> Unit)? = null
// colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors() // TODO: merge
) {
val textFieldColors = TextFieldDefaults.outlinedTextFieldColors(
@ -45,7 +47,14 @@ fun OutlinedTextField(
androidx.compose.material.OutlinedTextField(
value = value,
onValueChange = onValueChange,
modifier = modifier,
modifier = modifier.onKeyEvent { event ->
if (onEnterPressed != null && event.type == KeyEventType.KeyUp && (event.key == Key.Enter || event.key == Key.NumPadEnter)) {
onEnterPressed()
true
} else {
false
}
},
enabled = enabled,
readOnly = readOnly,
textStyle = textStyle,

View File

@ -18,7 +18,7 @@ import bankmeister.composeapp.generated.resources.visibility_off
import org.jetbrains.compose.resources.imageResource
@Composable
fun PasswordTextField(password: String = "", label: String = "Passwort", onChange: (String) -> Unit) {
fun PasswordTextField(password: String = "", label: String = "Passwort", onEnterPressed: (() -> Unit)? = null, onChange: (String) -> Unit) {
var passwordVisible by remember { mutableStateOf(false) }
@ -40,6 +40,7 @@ fun PasswordTextField(password: String = "", label: String = "Passwort", onChang
modifier = Modifier.size(24.dp).clickable { passwordVisible = !passwordVisible }
)
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
onEnterPressed = onEnterPressed
)
}