Add: StringPref DialogUI

This commit is contained in:
machiav3lli 2022-09-18 00:07:27 +02:00
parent 47382e993a
commit f78da1e48e

View File

@ -131,4 +131,86 @@ fun IntInputPrefDialogUIPrefview() {
AppTheme {
IntInputPrefDialogUI(Preferences.Key.NewApps, state)
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StringInputPrefDialogUI(
prefKey: Preferences.Key<String>,
openDialogCustom: MutableState<Boolean>
) {
val context = LocalContext.current
val focusManager = LocalFocusManager.current
val textFieldFocusRequester = remember { FocusRequester() }
var savedValue by remember {
mutableStateOf(Preferences[prefKey])
}
LaunchedEffect(textFieldFocusRequester) {
delay(100)
textFieldFocusRequester.requestFocus()
}
Card(
shape = MaterialTheme.shapes.large,
modifier = Modifier.padding(8.dp),
elevation = CardDefaults.elevatedCardElevation(8.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background)
) {
Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = stringResource(NonBooleanPrefsMeta[prefKey] ?: -1),
style = MaterialTheme.typography.titleLarge
)
TextField(
modifier = Modifier
.fillMaxWidth()
.focusRequester(textFieldFocusRequester),
value = savedValue,
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
),
shape = MaterialTheme.shapes.medium,
singleLine = true,
onValueChange = { savedValue = it },
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = KeyboardType.Text
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
)
Row(
Modifier.fillMaxWidth()
) {
DialogNegativeButton(
onClick = { openDialogCustom.value = false }
)
Spacer(Modifier.weight(1f))
DialogPositiveButton(
modifier = Modifier.padding(start = 16.dp),
onClick = {
if (savedValue.isNotEmpty()) Preferences[prefKey] = savedValue
openDialogCustom.value = false
}
)
}
}
}
}
@Preview
@Composable
fun StringInputPrefDialogUIPrefview() {
val state = remember {
mutableStateOf(true)
}
AppTheme {
StringInputPrefDialogUI(Preferences.Key.ProxyHost, state)
}
}