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