fix(editor): 检测内容是否修改逻辑错误
This commit is contained in:
parent
40961c70a8
commit
18f0642a0f
1 changed files with 25 additions and 37 deletions
|
|
@ -70,41 +70,29 @@ fun TodoEditorPage(
|
||||||
) {
|
) {
|
||||||
var showConfirmDialog by rememberSaveable { mutableStateOf(false) }
|
var showConfirmDialog by rememberSaveable { mutableStateOf(false) }
|
||||||
|
|
||||||
|
val context = LocalContext.current
|
||||||
|
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
|
||||||
|
|
||||||
|
var toDoContent by rememberSaveable { mutableStateOf(toDo?.content ?: "") }
|
||||||
|
var isError by rememberSaveable { mutableStateOf(false) }
|
||||||
|
var selectedSubjectIndex by rememberSaveable { mutableIntStateOf(toDo?.subject ?: 0) }
|
||||||
|
var priorityState by rememberSaveable { mutableFloatStateOf(toDo?.priority ?: 0f) }
|
||||||
|
|
||||||
fun checkModifiedBeforeBack() {
|
fun checkModifiedBeforeBack() {
|
||||||
if (toDo !== null) {
|
var isModified = false
|
||||||
val newTodo = TodoEntity(
|
if ((toDo?.content ?: "") != toDoContent) isModified = true
|
||||||
content = toDo.content,
|
if ((toDo?.subject ?: 0) != selectedSubjectIndex) isModified = true
|
||||||
subject = toDo.subject,
|
if ((toDo?.priority ?: 0f) != priorityState) isModified = true
|
||||||
isCompleted = toDo.isCompleted,
|
if (isModified) {
|
||||||
priority = toDo.priority,
|
showConfirmDialog = true
|
||||||
id = toDo.id
|
|
||||||
)
|
|
||||||
/*val newTodo = TodoEntity( // TODO: 只要用户有修改就阻止返回(当前是在编辑状态下检测)
|
|
||||||
content = toDo?.content ?: "",
|
|
||||||
subject = toDo?.subject ?: 0,
|
|
||||||
isCompleted = toDo?.isCompleted ?: false,
|
|
||||||
priority = toDo?.priority ?: 0f,
|
|
||||||
id = toDo?.id ?: 0
|
|
||||||
)*/
|
|
||||||
if (newTodo !== toDo) {
|
|
||||||
showConfirmDialog = true
|
|
||||||
} else {
|
|
||||||
onNavigateUp()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
onNavigateUp()
|
onNavigateUp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BackHandler {
|
BackHandler {
|
||||||
checkModifiedBeforeBack()
|
checkModifiedBeforeBack()
|
||||||
}
|
}
|
||||||
val context = LocalContext.current
|
|
||||||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
|
|
||||||
|
|
||||||
var text by rememberSaveable { mutableStateOf(toDo?.content ?: "") }
|
|
||||||
var isError by rememberSaveable { mutableStateOf(false) }
|
|
||||||
var selectedItemIndex by rememberSaveable { mutableIntStateOf(toDo?.subject ?: 0) }
|
|
||||||
var sliderPosition by rememberSaveable { mutableFloatStateOf(toDo?.priority ?: 0f) }
|
|
||||||
|
|
||||||
LargeTopAppBarScaffold(
|
LargeTopAppBarScaffold(
|
||||||
title = stringResource(if (toDo != null) R.string.title_edit_task else R.string.action_add_task),
|
title = stringResource(if (toDo != null) R.string.title_edit_task else R.string.action_add_task),
|
||||||
|
|
@ -136,7 +124,7 @@ fun TodoEditorPage(
|
||||||
text = { Text(stringResource(R.string.action_save)) },
|
text = { Text(stringResource(R.string.action_save)) },
|
||||||
expanded = true,
|
expanded = true,
|
||||||
onClick = {
|
onClick = {
|
||||||
if (text.trim().isEmpty()) {
|
if (toDoContent.trim().isEmpty()) {
|
||||||
isError = true
|
isError = true
|
||||||
return@AnimatedExtendedFloatingActionButton
|
return@AnimatedExtendedFloatingActionButton
|
||||||
}
|
}
|
||||||
|
|
@ -144,10 +132,10 @@ fun TodoEditorPage(
|
||||||
isError = false
|
isError = false
|
||||||
onSave(
|
onSave(
|
||||||
TodoEntity(
|
TodoEntity(
|
||||||
content = text,
|
content = toDoContent,
|
||||||
subject = selectedItemIndex,
|
subject = selectedSubjectIndex,
|
||||||
isCompleted = toDo?.isCompleted ?: false,
|
isCompleted = toDo?.isCompleted ?: false,
|
||||||
priority = sliderPosition,
|
priority = priorityState,
|
||||||
id = toDo?.id ?: 0
|
id = toDo?.id ?: 0
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
@ -174,8 +162,8 @@ fun TodoEditorPage(
|
||||||
) {
|
) {
|
||||||
with(sharedTransitionScope) {
|
with(sharedTransitionScope) {
|
||||||
TextField(
|
TextField(
|
||||||
value = text,
|
value = toDoContent,
|
||||||
onValueChange = { text = it },
|
onValueChange = { toDoContent = it },
|
||||||
label = { Text(stringResource(R.string.placeholder_add_todo)) },
|
label = { Text(stringResource(R.string.placeholder_add_todo)) },
|
||||||
isError = isError,
|
isError = isError,
|
||||||
supportingText = {
|
supportingText = {
|
||||||
|
|
@ -210,7 +198,7 @@ fun TodoEditorPage(
|
||||||
items = subjects,
|
items = subjects,
|
||||||
defaultSelectedItemIndex = toDo?.subject ?: 0,
|
defaultSelectedItemIndex = toDo?.subject ?: 0,
|
||||||
onSelectedChanged = {
|
onSelectedChanged = {
|
||||||
selectedItemIndex = it
|
selectedSubjectIndex = it
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
)
|
)
|
||||||
|
|
@ -230,8 +218,8 @@ fun TodoEditorPage(
|
||||||
modifier = Modifier.semantics {
|
modifier = Modifier.semantics {
|
||||||
contentDescription = context.getString(R.string.label_priority)
|
contentDescription = context.getString(R.string.label_priority)
|
||||||
},
|
},
|
||||||
value = sliderPosition,
|
value = priorityState,
|
||||||
onValueChange = { sliderPosition = it },
|
onValueChange = { priorityState = it },
|
||||||
valueRange = -10f..10f,
|
valueRange = -10f..10f,
|
||||||
steps = 3,
|
steps = 3,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
|
|
@ -243,7 +231,7 @@ fun TodoEditorPage(
|
||||||
.sizeIn(45.dp, 25.dp)
|
.sizeIn(45.dp, 25.dp)
|
||||||
.wrapContentWidth()
|
.wrapContentWidth()
|
||||||
) {
|
) {
|
||||||
Text(Priority.fromFloat(sliderPosition).getDisplayName(context))
|
Text(Priority.fromFloat(priorityState).getDisplayName(context))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
interactionSource = interactionSource
|
interactionSource = interactionSource
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue