mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-20 22:29:10 +00:00
add TextInputDialog
This commit is contained in:
parent
4de92f9aa1
commit
33e03cedd0
2 changed files with 106 additions and 1 deletions
|
@ -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") }
|
||||
)
|
||||
}
|
|
@ -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) }
|
||||
|
|
Loading…
Add table
Reference in a new issue