From 33e03cedd041bcbb32bbe461251c45aacb15663c Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sun, 2 Feb 2025 12:56:37 +0100 Subject: [PATCH] add TextInputDialog --- .../settings/dialogs/TextInputDialog.kt | 96 +++++++++++++++++++ .../settings/screens/AppearanceScreen.kt | 11 ++- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/helium314/keyboard/settings/dialogs/TextInputDialog.kt diff --git a/app/src/main/java/helium314/keyboard/settings/dialogs/TextInputDialog.kt b/app/src/main/java/helium314/keyboard/settings/dialogs/TextInputDialog.kt new file mode 100644 index 00000000..cf00e2c1 --- /dev/null +++ b/app/src/main/java/helium314/keyboard/settings/dialogs/TextInputDialog.kt @@ -0,0 +1,96 @@ +package helium314.keyboard.settings.dialogs + +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.material3.contentColorFor +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextRange +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.input.TextFieldValue +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.window.DialogProperties + +// taken from StreetComplete / SCEE +/** Dialog with which to input text. OK button is only clickable if [checkTextValid] returns true. */ +@Composable +fun TextInputDialog( + onDismissRequest: () -> Unit, + onConfirmed: (text: String) -> Unit, + modifier: Modifier = Modifier, + title: @Composable (() -> Unit)? = null, + initialText: String = "", + textInputLabel: @Composable (() -> Unit)? = null, + shape: Shape = MaterialTheme.shapes.medium, + backgroundColor: Color = MaterialTheme.colorScheme.surface, + contentColor: Color = contentColorFor(backgroundColor), + properties: DialogProperties = DialogProperties(), + keyboardType: KeyboardType = KeyboardType.Unspecified, + checkTextValid: (text: String) -> Boolean = { it.isNotBlank() } +) { + val focusRequester = remember { FocusRequester() } + + var value by remember { + mutableStateOf(TextFieldValue(initialText, selection = TextRange(0, initialText.length))) + } + + LaunchedEffect(initialText) { focusRequester.requestFocus() } + + AlertDialog( + onDismissRequest = onDismissRequest, + confirmButton = { + TextButton( + enabled = value.text.isNotBlank() && checkTextValid(value.text), + onClick = { onDismissRequest(); onConfirmed(value.text) } + ) { + Text(stringResource(android.R.string.ok)) + } + }, + dismissButton = { + TextButton(onClick = onDismissRequest) { Text(stringResource(android.R.string.cancel)) } + }, + modifier = modifier, + title = title, + text = { + OutlinedTextField( + value = value, + onValueChange = { value = it }, + modifier = Modifier.fillMaxWidth().focusRequester(focusRequester), + label = textInputLabel, + keyboardOptions = KeyboardOptions(keyboardType = keyboardType), + singleLine = true + ) + }, + shape = shape, + containerColor = backgroundColor, + textContentColor = contentColor, + properties = properties, + ) +} + +@Preview +@Composable +private fun Preview() { + TextInputDialog( + onDismissRequest = {}, + onConfirmed = {}, + title = { Text("Title") }, + initialText = "some text", + textInputLabel = { Text("fill it") } + ) +} \ No newline at end of file diff --git a/app/src/main/java/helium314/keyboard/settings/screens/AppearanceScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/AppearanceScreen.kt index b2dc1982..228234f5 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/AppearanceScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/AppearanceScreen.kt @@ -49,6 +49,7 @@ import helium314.keyboard.settings.SliderPreference import helium314.keyboard.settings.SwitchPreference import helium314.keyboard.settings.Theme import helium314.keyboard.settings.dialogs.CustomizeIconsDialog +import helium314.keyboard.settings.dialogs.TextInputDialog import helium314.keyboard.settings.keyboardNeedsReload import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -305,7 +306,15 @@ fun createAppearancePrefs(context: Context) = listOf( name = def.title, onClick = { showDialog = true }, description = prefs.getString(def.key, "") - ) // todo: create and show the dialog + ) + if (showDialog) { + TextInputDialog( + onDismissRequest = { showDialog = false }, + onConfirmed = { prefs.edit().putString(def.key, it).apply() }, + initialText = prefs.getString(def.key, "") ?: "", + title = { Text(def.title) } + ) + } }, PrefDef(context, NonSettingsPrefs.CUSTOM_FONT, R.string.custom_font) { def -> var showDialog by remember { mutableStateOf(false) }