Refine code
This commit is contained in:
parent
d8cd2cd822
commit
7f826e1bd3
3 changed files with 29 additions and 24 deletions
|
|
@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData
|
|||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import cn.super12138.todo.logic.Repository
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ProgressFragmentViewModel : ViewModel() {
|
||||
|
|
@ -13,6 +14,7 @@ class ProgressFragmentViewModel : ViewModel() {
|
|||
|
||||
fun updateProgress() {
|
||||
viewModelScope.launch {
|
||||
delay(30)
|
||||
val total = Repository.getAll().size
|
||||
val complete = Repository.getAllComplete().size
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,10 @@ import androidx.lifecycle.ViewModelProvider
|
|||
import androidx.lifecycle.ViewModelStoreOwner
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import cn.super12138.todo.R
|
||||
import cn.super12138.todo.logic.Repository
|
||||
import cn.super12138.todo.logic.dao.ToDoRoom
|
||||
import cn.super12138.todo.logic.model.ToDo
|
||||
import cn.super12138.todo.views.progress.ProgressFragmentViewModel
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ToDoAdapter(private val todoList: MutableList<ToDo>, private val viewModelStoreOwner: ViewModelStoreOwner) :
|
||||
RecyclerView.Adapter<ToDoAdapter.ViewHolder>() {
|
||||
|
|
@ -28,21 +25,31 @@ class ToDoAdapter(private val todoList: MutableList<ToDo>, private val viewModel
|
|||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.item_todo, parent, false)
|
||||
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val todo = todoList[position]
|
||||
holder.todoContext.text = todo.content
|
||||
holder.todoSubject.text = todo.subject
|
||||
/*if (!todo.isAnimated) {
|
||||
holder.itemView.alpha = 0f
|
||||
holder.itemView.animate().alpha(1f).duration = 200
|
||||
todo.isAnimated = true
|
||||
}*/
|
||||
|
||||
val progressViewModel =
|
||||
ViewModelProvider(viewModelStoreOwner)[ProgressFragmentViewModel::class.java]
|
||||
val todoViewModel =
|
||||
ViewModelProvider(viewModelStoreOwner)[ToDoFragmentViewModel::class.java]
|
||||
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.item_todo, parent, false)
|
||||
val holder = ViewHolder(view)
|
||||
|
||||
holder.checkToDoBtn.setOnClickListener {
|
||||
val position = holder.absoluteAdapterPosition
|
||||
if (position < 0 || position >= todoList.size) {
|
||||
return@setOnClickListener
|
||||
}
|
||||
val todo = todoList[position]
|
||||
|
||||
todoList.removeAt(position)
|
||||
notifyItemRemoved(position)
|
||||
|
|
@ -60,18 +67,15 @@ class ToDoAdapter(private val todoList: MutableList<ToDo>, private val viewModel
|
|||
}
|
||||
|
||||
holder.delToDoBtn.setOnClickListener {
|
||||
val position = holder.absoluteAdapterPosition
|
||||
if (position < 0 || position >= todoList.size) {
|
||||
return@setOnClickListener
|
||||
}
|
||||
val todo = todoList[position]
|
||||
|
||||
todoList.removeAt(position)
|
||||
notifyItemRemoved(position)
|
||||
notifyItemRangeChanged(position, todoList.size)
|
||||
|
||||
todoViewModel.deleteTask(todo.uuid)
|
||||
progressViewModel.updateProgress()
|
||||
|
||||
// 设置空项目提示可见性
|
||||
if (todoList.isEmpty()) {
|
||||
|
|
@ -80,6 +84,7 @@ class ToDoAdapter(private val todoList: MutableList<ToDo>, private val viewModel
|
|||
todoViewModel.emptyTipVis.value = View.GONE
|
||||
}
|
||||
|
||||
progressViewModel.updateProgress()
|
||||
Snackbar.make(it, R.string.task_deleted, Snackbar.LENGTH_LONG)
|
||||
.setAction(R.string.delete_undo) {
|
||||
if (todoList.size + 1 > 0) {
|
||||
|
|
@ -102,18 +107,6 @@ class ToDoAdapter(private val todoList: MutableList<ToDo>, private val viewModel
|
|||
}
|
||||
.show()
|
||||
}
|
||||
return holder
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val todo = todoList[position]
|
||||
holder.todoContext.text = todo.content
|
||||
holder.todoSubject.text = todo.subject
|
||||
/*if (!todo.isAnimated) {
|
||||
holder.itemView.alpha = 0f
|
||||
holder.itemView.animate().alpha(1f).duration = 200
|
||||
todo.isAnimated = true
|
||||
}*/
|
||||
}
|
||||
|
||||
override fun getItemCount() = todoList.size
|
||||
|
|
|
|||
|
|
@ -85,6 +85,7 @@ class ToDoFragment : Fragment() {
|
|||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.show()
|
||||
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||
val todoContent = toDoDialogBinding.todoContent.editText?.text.toString()
|
||||
if (todoContent.isEmpty()) {
|
||||
|
|
@ -100,7 +101,9 @@ class ToDoFragment : Fragment() {
|
|||
}
|
||||
dialog.dismiss()
|
||||
} else {
|
||||
// 随机UUID
|
||||
val randomUUID = UUID.randomUUID().toString()
|
||||
// 待办学科
|
||||
val todoSubject = when (toDoDialogBinding.todoSubject.checkedChipId) {
|
||||
R.id.subject_chinese -> getString(R.string.subject_chinese)
|
||||
R.id.subject_math -> getString(R.string.subject_math)
|
||||
|
|
@ -124,6 +127,7 @@ class ToDoFragment : Fragment() {
|
|||
ToDo(randomUUID, 0, todoContent, todoSubject)
|
||||
)
|
||||
|
||||
// 插入数据库
|
||||
lifecycleScope.launch {
|
||||
Repository.insert(
|
||||
ToDoRoom(
|
||||
|
|
@ -149,13 +153,17 @@ class ToDoFragment : Fragment() {
|
|||
.setTitle(R.string.warning)
|
||||
.setMessage(R.string.delete_confirm)
|
||||
.setPositiveButton(R.string.ok) { _, _ ->
|
||||
// 清除 Recycler View
|
||||
todoList.clear()
|
||||
// 清除数据库
|
||||
lifecycleScope.launch {
|
||||
Repository.deleteAll()
|
||||
progressViewModel.updateProgress()
|
||||
}
|
||||
// 通知数据更改
|
||||
binding.todoList.adapter?.notifyItemRangeRemoved(0, todoList.size + 1)
|
||||
|
||||
// 显示空项目提示
|
||||
todoViewModel.emptyTipVis.value = View.VISIBLE
|
||||
}
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
|
|
@ -167,9 +175,11 @@ class ToDoFragment : Fragment() {
|
|||
binding.todoList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
||||
val fab = binding.addItem
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
// 列表下滑,隐藏FAB
|
||||
if (dy > 0 && fab.isShown) {
|
||||
fab.hide()
|
||||
}
|
||||
// 列表上滑,显示FAB
|
||||
if (dy < 0 && !fab.isShown) {
|
||||
fab.show()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue