feat: 支持修改待办

This commit is contained in:
Super12138 2025-01-12 13:35:33 +08:00
parent d19311b6bf
commit 3cc10cd216
5 changed files with 20 additions and 7 deletions

View file

@ -2,13 +2,14 @@ package cn.super12138.todo.logic.database
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import androidx.room.Update import androidx.room.Update
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface TodoDao { interface TodoDao {
@Insert @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(toDo: TodoEntity) suspend fun insert(toDo: TodoEntity)
@Query("SELECT * FROM todo") @Query("SELECT * FROM todo")

View file

@ -46,6 +46,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
} }
} }
var selectedTodoItem by remember { mutableStateOf<TodoEntity?>(null) }
var openBottomSheet by rememberSaveable { mutableStateOf(false) } var openBottomSheet by rememberSaveable { mutableStateOf(false) }
val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@ -105,8 +106,9 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
ManagerFragment( ManagerFragment(
state = listState, state = listState,
list = toDoList.value.filter { item -> !item.isCompleted }, list = toDoList.value.filter { item -> !item.isCompleted },
onItemClick = { onItemClick = { item ->
openBottomSheet = true
selectedTodoItem = item
}, },
onItemChecked = { item -> onItemChecked = { item ->
item.apply { item.apply {
@ -130,8 +132,11 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
sheetState = bottomSheetState, sheetState = bottomSheetState,
onDismissRequest = { onDismissRequest = {
openBottomSheet = false openBottomSheet = false
selectedTodoItem = null
}, },
toDo = selectedTodoItem,
onSave = { toDo -> onSave = { toDo ->
selectedTodoItem = null
viewModel.addTodo(toDo) viewModel.addTodo(toDo)
scope scope
.launch { bottomSheetState.hide() } .launch { bottomSheetState.hide() }
@ -142,6 +147,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
} }
}, },
onClose = { onClose = {
selectedTodoItem = null
scope scope
.launch { bottomSheetState.hide() } .launch { bottomSheetState.hide() }
.invokeOnCompletion { .invokeOnCompletion {

View file

@ -38,6 +38,7 @@ import cn.super12138.todo.ui.pages.main.components.FilterChipGroup
fun TodoBottomSheet( fun TodoBottomSheet(
sheetState: SheetState, sheetState: SheetState,
onDismissRequest: () -> Unit, onDismissRequest: () -> Unit,
toDo: TodoEntity? = null,
onSave: (TodoEntity) -> Unit, onSave: (TodoEntity) -> Unit,
onClose: () -> Unit, onClose: () -> Unit,
modifier: Modifier = Modifier modifier: Modifier = Modifier
@ -54,13 +55,13 @@ fun TodoBottomSheet(
modifier = Modifier.padding(horizontal = 16.dp) modifier = Modifier.padding(horizontal = 16.dp)
) { ) {
Text( Text(
text = stringResource(R.string.action_add_task), text = stringResource(if (toDo != null) R.string.title_edit_task else R.string.action_add_task),
style = MaterialTheme.typography.headlineMedium style = MaterialTheme.typography.headlineMedium
) )
Spacer(Modifier.size(25.dp)) Spacer(Modifier.size(25.dp))
var text by rememberSaveable { mutableStateOf("") } var text by rememberSaveable { mutableStateOf(toDo?.content ?: "") }
var isError by rememberSaveable { mutableStateOf(false) } var isError by rememberSaveable { mutableStateOf(false) }
TextField( TextField(
value = text, value = text,
@ -82,9 +83,10 @@ fun TodoBottomSheet(
it.getDisplayName(context) it.getDisplayName(context)
} }
} }
var selectedItemIndex by rememberSaveable { mutableIntStateOf(0) } var selectedItemIndex by rememberSaveable { mutableIntStateOf(toDo?.subject ?: 0) }
FilterChipGroup( FilterChipGroup(
items = subjects, items = subjects,
defaultSelectedItemIndex = toDo?.subject ?: 0,
onSelectedChanged = { onSelectedChanged = {
selectedItemIndex = it selectedItemIndex = it
}, },
@ -109,7 +111,9 @@ fun TodoBottomSheet(
onSave( onSave(
TodoEntity( TodoEntity(
content = text, content = text,
subject = selectedItemIndex subject = selectedItemIndex,
isCompleted = toDo?.isCompleted ?: false,
id = toDo?.id ?: 0
) )
) )
} }

View file

@ -20,4 +20,5 @@
<string name="subject_chemistry">化学</string> <string name="subject_chemistry">化学</string>
<string name="subject_history">历史</string> <string name="subject_history">历史</string>
<string name="subject_others">其它</string> <string name="subject_others">其它</string>
<string name="title_edit_task">修改待办</string>
</resources> </resources>

View file

@ -19,4 +19,5 @@
<string name="subject_chemistry">Chemistry</string> <string name="subject_chemistry">Chemistry</string>
<string name="subject_history">History</string> <string name="subject_history">History</string>
<string name="subject_others">Others</string> <string name="subject_others">Others</string>
<string name="title_edit_task">Edit Task</string>
</resources> </resources>