From f78da1e48e72de585f071a312f23f575d557a1c0 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Sun, 18 Sep 2022 00:07:27 +0200 Subject: [PATCH] Add: StringPref DialogUI --- .../fdroid/ui/dialog/InputDialogUI.kt | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/main/kotlin/com/machiav3lli/fdroid/ui/dialog/InputDialogUI.kt b/src/main/kotlin/com/machiav3lli/fdroid/ui/dialog/InputDialogUI.kt index 4d26d92f..acca3260 100644 --- a/src/main/kotlin/com/machiav3lli/fdroid/ui/dialog/InputDialogUI.kt +++ b/src/main/kotlin/com/machiav3lli/fdroid/ui/dialog/InputDialogUI.kt @@ -131,4 +131,86 @@ fun IntInputPrefDialogUIPrefview() { AppTheme { IntInputPrefDialogUI(Preferences.Key.NewApps, state) } +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun StringInputPrefDialogUI( + prefKey: Preferences.Key, + openDialogCustom: MutableState +) { + 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) + } } \ No newline at end of file