From 6983063838f556b4d775c59637468a811ef8d1fd Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Thu, 13 Jul 2023 18:28:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B8=A6=E8=BE=93=E5=85=A5=E6=A1=86?= =?UTF-8?q?=E7=9A=84=E5=AF=B9=E8=AF=9D=E6=A1=86=E8=AF=B7=E6=B1=82=E7=84=A6?= =?UTF-8?q?=E7=82=B9=E6=97=B6=E9=97=AA=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/common/prefs/widgets/EditTextPref.kt | 26 ++++++++++++------- .../tieba/post/ui/widgets/compose/Dialogs.kt | 23 +++++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/common/prefs/widgets/EditTextPref.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/common/prefs/widgets/EditTextPref.kt index f9a8cea7..bbd4a5d6 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/common/prefs/widgets/EditTextPref.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/common/prefs/widgets/EditTextPref.kt @@ -6,7 +6,14 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.MaterialTheme import androidx.compose.material.Text -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Size @@ -119,19 +126,20 @@ fun EditTextPref( } PromptDialog( - dialogState = dialogState, - modifier = Modifier.onGloballyPositioned { - dialogSize = it.size.toSize() - }, - value = textVal, onConfirm = { textVal = it onValueChange(it) edit() }, - onValueChange = { - textVal = it - onValueChange(it) + modifier = Modifier.onGloballyPositioned { + dialogSize = it.size.toSize() + }, + dialogState = dialogState, + initialValue = textVal, + onValueChange = { newVal, _ -> + textVal = newVal + onValueChange(newVal) + true }, title = { if (dialogTitle != null) { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Dialogs.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Dialogs.kt index 97f07247..2c97229f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Dialogs.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Dialogs.kt @@ -215,24 +215,29 @@ fun ConfirmDialog( } } +/** + * 带输入框的对话框 + * + * @param onValueChange 输入框内容变化时的回调,返回true表示允许变化,false表示不允许变化 + */ @OptIn(ExperimentalComposeUiApi::class) @Composable fun PromptDialog( - dialogState: DialogState = rememberDialogState(), - modifier: Modifier = Modifier, - value: String = "", onConfirm: (String) -> Unit, - onValueChange: ((String) -> Unit)? = null, + modifier: Modifier = Modifier, + dialogState: DialogState = rememberDialogState(), + initialValue: String = "", + onValueChange: (newVal: String, oldVal: String) -> Boolean = { _, _ -> true }, onCancel: (() -> Unit)? = null, confirmText: String = stringResource(id = R.string.button_sure_default), cancelText: String = stringResource(id = R.string.button_cancel), title: @Composable (DialogScope.() -> Unit) = {}, content: @Composable (DialogScope.() -> Unit) = {}, ) { - var textVal by remember { mutableStateOf(value) } + var textVal by remember { mutableStateOf(initialValue) } // 每次显示时重置输入框内容 LaunchedEffect(dialogState.show) { - textVal = value + textVal = initialValue } Dialog( modifier = modifier, @@ -244,7 +249,7 @@ fun PromptDialog( DialogNegativeButton(text = cancelText, onClick = onCancel) }, ) { - val focusRequester = FocusRequester.Default + val focusRequester = remember { FocusRequester() } val softwareKeyboardController = LocalSoftwareKeyboardController.current Column( modifier = Modifier @@ -260,8 +265,7 @@ fun PromptDialog( OutlinedTextField( value = textVal, onValueChange = { - textVal = it - onValueChange?.invoke(it) + if (onValueChange.invoke(it, textVal)) textVal = it }, maxLines = 1, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), @@ -288,7 +292,6 @@ fun PromptDialog( } } -@OptIn(ExperimentalComposeUiApi::class) @Composable fun Dialog( modifier: Modifier = Modifier,