refactor: 编辑器文本框手动检查是否错误

This commit is contained in:
Super12138 2025-07-04 13:15:32 +08:00
parent 74840d137f
commit 4bcf1ac959
2 changed files with 19 additions and 12 deletions

View file

@ -205,15 +205,7 @@ fun TodoEditorPage(
value = uiState.categoryContent,
onValueChange = { uiState.categoryContent = it },
isError = uiState.isErrorCategory,
supportingText = when {
uiState.categoryContent.trim().isEmpty() ->
stringResource(R.string.error_no_content_entered)
uiState.categoryContent.length > 5 ->
stringResource(R.string.error_exceeds_5_chars)
else -> stringResource(R.string.tip_max_length_5)
},
supportingText = stringResource(uiState.categorySupportingText),
modifier = Modifier
.fillMaxWidth()
/*.sharedBounds(

View file

@ -8,6 +8,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import cn.super12138.todo.R
import cn.super12138.todo.logic.database.TodoEntity
class EditorState(val initialTodo: TodoEntity? = null) {
@ -19,6 +20,9 @@ class EditorState(val initialTodo: TodoEntity? = null) {
var priorityState by mutableFloatStateOf(initialTodo?.priority ?: 0f)
var isCompleted by mutableStateOf(initialTodo?.isCompleted == true)
var categorySupportingText by mutableIntStateOf(R.string.tip_max_length_5)
private set
var showExitConfirmDialog by mutableStateOf(false)
var showDeleteConfirmDialog by mutableStateOf(false)
@ -29,9 +33,20 @@ class EditorState(val initialTodo: TodoEntity? = null) {
*/
fun setErrorIfNotValid(): Boolean {
isErrorContent = toDoContent.trim().isEmpty()
isErrorCategory = if (selectedCategoryIndex == -1) {
categoryContent.trim().isEmpty() || categoryContent.trim().length > 5
} else false
if (selectedCategoryIndex == -1) {
if (categoryContent.trim().isEmpty()) {
isErrorCategory = true
categorySupportingText = R.string.error_no_content_entered
} else if (categoryContent.length > 5) {
isErrorCategory = true
categorySupportingText = R.string.error_exceeds_5_chars
} else {
isErrorCategory = false
categorySupportingText = R.string.tip_max_length_5
}
} else {
isErrorCategory = false
}
return isErrorContent || isErrorCategory
}