A safer method of manage tasks
This commit is contained in:
parent
d43e599f74
commit
8742836ec9
2 changed files with 35 additions and 23 deletions
|
|
@ -48,10 +48,8 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
|
||||||
notifyItemRemoved(position)
|
notifyItemRemoved(position)
|
||||||
notifyItemRangeChanged(position, todoList.size)
|
notifyItemRangeChanged(position, todoList.size)
|
||||||
|
|
||||||
GlobalScope.launch {
|
todoViewModel.updateTask(todo.uuid)
|
||||||
Repository.updateStateByUUID(todo.uuid)
|
progressViewModel.updateProgress()
|
||||||
progressViewModel.updateProgress()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置空项目提示可见性
|
// 设置空项目提示可见性
|
||||||
if (todoList.isEmpty()) {
|
if (todoList.isEmpty()) {
|
||||||
|
|
@ -72,12 +70,9 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
|
||||||
notifyItemRemoved(position)
|
notifyItemRemoved(position)
|
||||||
notifyItemRangeChanged(position, todoList.size)
|
notifyItemRangeChanged(position, todoList.size)
|
||||||
|
|
||||||
GlobalScope.launch {
|
todoViewModel.deleteTask(todo.uuid)
|
||||||
Repository.deleteByUUID(todo.uuid)
|
progressViewModel.updateProgress()
|
||||||
progressViewModel.updateProgress()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 设置空项目提示可见性
|
// 设置空项目提示可见性
|
||||||
if (todoList.isEmpty()) {
|
if (todoList.isEmpty()) {
|
||||||
todoViewModel.emptyTipVis.value = View.VISIBLE
|
todoViewModel.emptyTipVis.value = View.VISIBLE
|
||||||
|
|
@ -90,21 +85,19 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
|
||||||
if (todoList.size + 1 > 0) {
|
if (todoList.size + 1 > 0) {
|
||||||
todoViewModel.emptyTipVis.value = View.GONE
|
todoViewModel.emptyTipVis.value = View.GONE
|
||||||
}
|
}
|
||||||
todoList.add(ToDo(todo.uuid,0 , todo.content, todo.subject))
|
todoList.add(ToDo(todo.uuid, 0, todo.content, todo.subject))
|
||||||
|
|
||||||
todoViewModel.refreshData.value = 1
|
todoViewModel.refreshData.value = 1
|
||||||
|
|
||||||
GlobalScope.launch {
|
todoViewModel.insertTask(
|
||||||
Repository.insert(
|
ToDoRoom(
|
||||||
ToDoRoom(
|
todo.uuid,
|
||||||
todo.uuid,
|
0,
|
||||||
0,
|
todo.subject,
|
||||||
todo.subject,
|
todo.content
|
||||||
todo.content
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
progressViewModel.updateProgress()
|
)
|
||||||
}
|
progressViewModel.updateProgress()
|
||||||
}
|
}
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import cn.super12138.todo.logic.Repository
|
import cn.super12138.todo.logic.Repository
|
||||||
|
import cn.super12138.todo.logic.dao.ToDoRoom
|
||||||
import cn.super12138.todo.logic.model.ToDo
|
import cn.super12138.todo.logic.model.ToDo
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
|
@ -14,10 +15,10 @@ class ToDoFragmentViewModel : ViewModel() {
|
||||||
val todoList = ArrayList<ToDo>()
|
val todoList = ArrayList<ToDo>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
loadToDos()
|
loadTasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadToDos() {
|
private fun loadTasks() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val todos = Repository.getAllIncomplete()
|
val todos = Repository.getAllIncomplete()
|
||||||
var count = 0
|
var count = 0
|
||||||
|
|
@ -32,4 +33,22 @@ class ToDoFragmentViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun deleteTask(uuid: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
Repository.deleteByUUID(uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun insertTask(todo: ToDoRoom) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
Repository.insert(todo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateTask(uuid: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
Repository.updateStateByUUID(uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue