From 7f826e1bd3e6cf5a7dfee9933cc4e8c9de58c01c Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sun, 10 Mar 2024 21:07:20 +0800 Subject: [PATCH] Refine code --- .../progress/ProgressFragmentViewModel.kt | 2 + .../super12138/todo/views/todo/ToDoAdapter.kt | 41 ++++++++----------- .../todo/views/todo/ToDoFragment.kt | 10 +++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragmentViewModel.kt b/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragmentViewModel.kt index 2eef5d7..a5509f4 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragmentViewModel.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragmentViewModel.kt @@ -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 diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt index 2f8b9ec..805aead 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt @@ -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, private val viewModelStoreOwner: ViewModelStoreOwner) : RecyclerView.Adapter() { @@ -28,21 +25,31 @@ class ToDoAdapter(private val todoList: MutableList, 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, 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, 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, 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 diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt index 3f14250..8110159 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt @@ -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() }