feat: 支持修改待办
This commit is contained in:
parent
d19311b6bf
commit
3cc10cd216
5 changed files with 20 additions and 7 deletions
|
|
@ -2,13 +2,14 @@ package cn.super12138.todo.logic.database
|
|||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface TodoDao {
|
||||
@Insert
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(toDo: TodoEntity)
|
||||
|
||||
@Query("SELECT * FROM todo")
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
}
|
||||
}
|
||||
|
||||
var selectedTodoItem by remember { mutableStateOf<TodoEntity?>(null) }
|
||||
var openBottomSheet by rememberSaveable { mutableStateOf(false) }
|
||||
val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
|
||||
val scope = rememberCoroutineScope()
|
||||
|
|
@ -105,8 +106,9 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
ManagerFragment(
|
||||
state = listState,
|
||||
list = toDoList.value.filter { item -> !item.isCompleted },
|
||||
onItemClick = {
|
||||
|
||||
onItemClick = { item ->
|
||||
openBottomSheet = true
|
||||
selectedTodoItem = item
|
||||
},
|
||||
onItemChecked = { item ->
|
||||
item.apply {
|
||||
|
|
@ -130,8 +132,11 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
sheetState = bottomSheetState,
|
||||
onDismissRequest = {
|
||||
openBottomSheet = false
|
||||
selectedTodoItem = null
|
||||
},
|
||||
toDo = selectedTodoItem,
|
||||
onSave = { toDo ->
|
||||
selectedTodoItem = null
|
||||
viewModel.addTodo(toDo)
|
||||
scope
|
||||
.launch { bottomSheetState.hide() }
|
||||
|
|
@ -142,6 +147,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
|||
}
|
||||
},
|
||||
onClose = {
|
||||
selectedTodoItem = null
|
||||
scope
|
||||
.launch { bottomSheetState.hide() }
|
||||
.invokeOnCompletion {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ import cn.super12138.todo.ui.pages.main.components.FilterChipGroup
|
|||
fun TodoBottomSheet(
|
||||
sheetState: SheetState,
|
||||
onDismissRequest: () -> Unit,
|
||||
toDo: TodoEntity? = null,
|
||||
onSave: (TodoEntity) -> Unit,
|
||||
onClose: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
|
|
@ -54,13 +55,13 @@ fun TodoBottomSheet(
|
|||
modifier = Modifier.padding(horizontal = 16.dp)
|
||||
) {
|
||||
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
|
||||
)
|
||||
|
||||
Spacer(Modifier.size(25.dp))
|
||||
|
||||
var text by rememberSaveable { mutableStateOf("") }
|
||||
var text by rememberSaveable { mutableStateOf(toDo?.content ?: "") }
|
||||
var isError by rememberSaveable { mutableStateOf(false) }
|
||||
TextField(
|
||||
value = text,
|
||||
|
|
@ -82,9 +83,10 @@ fun TodoBottomSheet(
|
|||
it.getDisplayName(context)
|
||||
}
|
||||
}
|
||||
var selectedItemIndex by rememberSaveable { mutableIntStateOf(0) }
|
||||
var selectedItemIndex by rememberSaveable { mutableIntStateOf(toDo?.subject ?: 0) }
|
||||
FilterChipGroup(
|
||||
items = subjects,
|
||||
defaultSelectedItemIndex = toDo?.subject ?: 0,
|
||||
onSelectedChanged = {
|
||||
selectedItemIndex = it
|
||||
},
|
||||
|
|
@ -109,7 +111,9 @@ fun TodoBottomSheet(
|
|||
onSave(
|
||||
TodoEntity(
|
||||
content = text,
|
||||
subject = selectedItemIndex
|
||||
subject = selectedItemIndex,
|
||||
isCompleted = toDo?.isCompleted ?: false,
|
||||
id = toDo?.id ?: 0
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,4 +20,5 @@
|
|||
<string name="subject_chemistry">化学</string>
|
||||
<string name="subject_history">历史</string>
|
||||
<string name="subject_others">其它</string>
|
||||
<string name="title_edit_task">修改待办</string>
|
||||
</resources>
|
||||
|
|
@ -19,4 +19,5 @@
|
|||
<string name="subject_chemistry">Chemistry</string>
|
||||
<string name="subject_history">History</string>
|
||||
<string name="subject_others">Others</string>
|
||||
<string name="title_edit_task">Edit Task</string>
|
||||
</resources>
|
||||
Loading…
Reference in a new issue