feat: 删除单个待办
This commit is contained in:
parent
cfe6228e99
commit
1656063449
7 changed files with 38 additions and 9 deletions
|
|
@ -14,7 +14,11 @@ object Repository {
|
||||||
|
|
||||||
fun getAllTodos(): Flow<List<TodoEntity>> = toDoDao.getAll()
|
fun getAllTodos(): Flow<List<TodoEntity>> = toDoDao.getAll()
|
||||||
|
|
||||||
suspend fun updateTodo(toDo: TodoEntity){
|
suspend fun updateTodo(toDo: TodoEntity) {
|
||||||
toDoDao.update(toDo)
|
toDoDao.update(toDo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun deleteTodo(toDo: TodoEntity) {
|
||||||
|
toDoDao.delete(toDo.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -17,4 +17,7 @@ interface TodoDao {
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
suspend fun update(toDo: TodoEntity)
|
suspend fun update(toDo: TodoEntity)
|
||||||
|
|
||||||
|
@Query("DELETE FROM todo WHERE id = :toDoId")
|
||||||
|
suspend fun delete(toDoId: Int)
|
||||||
}
|
}
|
||||||
|
|
@ -158,15 +158,7 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
},
|
},
|
||||||
toDo = selectedEditTodoItem,
|
toDo = selectedEditTodoItem,
|
||||||
onSave = { toDo ->
|
onSave = { toDo ->
|
||||||
viewModel.setEditTodoItem(null)
|
|
||||||
viewModel.addTodo(toDo)
|
viewModel.addTodo(toDo)
|
||||||
scope
|
|
||||||
.launch { bottomSheetState.hide() }
|
|
||||||
.invokeOnCompletion {
|
|
||||||
if (!bottomSheetState.isVisible) {
|
|
||||||
openBottomSheet = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onClose = {
|
onClose = {
|
||||||
viewModel.setEditTodoItem(null)
|
viewModel.setEditTodoItem(null)
|
||||||
|
|
@ -177,6 +169,9 @@ fun MainPage(viewModel: MainViewModel, modifier: Modifier = Modifier) {
|
||||||
openBottomSheet = false
|
openBottomSheet = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onDelete = {
|
||||||
|
if (selectedEditTodoItem !== null) viewModel.deleteTodo(selectedEditTodoItem)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material3.ButtonColors
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.FilledTonalButton
|
import androidx.compose.material3.FilledTonalButton
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
|
@ -44,6 +45,7 @@ fun TodoBottomSheet(
|
||||||
toDo: TodoEntity? = null,
|
toDo: TodoEntity? = null,
|
||||||
onSave: (TodoEntity) -> Unit,
|
onSave: (TodoEntity) -> Unit,
|
||||||
onClose: () -> Unit,
|
onClose: () -> Unit,
|
||||||
|
onDelete: () -> Unit,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
@ -104,6 +106,22 @@ fun TodoBottomSheet(
|
||||||
OutlinedButton(onClick = onClose) {
|
OutlinedButton(onClick = onClose) {
|
||||||
Text(stringResource(R.string.action_cancel))
|
Text(stringResource(R.string.action_cancel))
|
||||||
}
|
}
|
||||||
|
if (toDo !== null) {
|
||||||
|
FilledTonalButton(
|
||||||
|
onClick = {
|
||||||
|
onDelete()
|
||||||
|
onClose()
|
||||||
|
},
|
||||||
|
colors = ButtonColors(
|
||||||
|
containerColor = MaterialTheme.colorScheme.errorContainer,
|
||||||
|
contentColor = MaterialTheme.colorScheme.onErrorContainer,
|
||||||
|
disabledContainerColor = MaterialTheme.colorScheme.onSurface,
|
||||||
|
disabledContentColor = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.action_delete))
|
||||||
|
}
|
||||||
|
}
|
||||||
FilledTonalButton(
|
FilledTonalButton(
|
||||||
onClick = {
|
onClick = {
|
||||||
// 文本为空
|
// 文本为空
|
||||||
|
|
@ -121,6 +139,7 @@ fun TodoBottomSheet(
|
||||||
id = toDo?.id ?: 0
|
id = toDo?.id ?: 0
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
onClose()
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
Text(stringResource(R.string.action_save))
|
Text(stringResource(R.string.action_save))
|
||||||
|
|
|
||||||
|
|
@ -35,4 +35,10 @@ class MainViewModel : ViewModel() {
|
||||||
fun setEditTodoItem(toDo: TodoEntity?) {
|
fun setEditTodoItem(toDo: TodoEntity?) {
|
||||||
selectedEditTodoItem = toDo
|
selectedEditTodoItem = toDo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun deleteTodo(toDo: TodoEntity) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
Repository.deleteTodo(toDo)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -25,4 +25,5 @@
|
||||||
<string name="page_crash">应用程序出现错误</string>
|
<string name="page_crash">应用程序出现错误</string>
|
||||||
<string name="tip_no_crash_logs">没有日志传入</string>
|
<string name="tip_no_crash_logs">没有日志传入</string>
|
||||||
<string name="action_exit_app">退出应用</string>
|
<string name="action_exit_app">退出应用</string>
|
||||||
|
<string name="action_delete">删除</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -24,4 +24,5 @@
|
||||||
<string name="page_crash">Oops! App went wrong</string>
|
<string name="page_crash">Oops! App went wrong</string>
|
||||||
<string name="tip_no_crash_logs">No crash logs</string>
|
<string name="tip_no_crash_logs">No crash logs</string>
|
||||||
<string name="action_exit_app">Exit app</string>
|
<string name="action_exit_app">Exit app</string>
|
||||||
|
<string name="action_delete">Delete</string>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in a new issue