Skip to content

Commit

Permalink
Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Mar 10, 2024
1 parent d8cd2cd commit 7f826e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -13,6 +14,7 @@ class ProgressFragmentViewModel : ViewModel() {

fun updateProgress() {
viewModelScope.launch {
delay(30)
val total = Repository.getAll().size
val complete = Repository.getAllComplete().size

Expand Down
41 changes: 17 additions & 24 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>() {
Expand All @@ -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)
Expand All @@ -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()) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)
Expand All @@ -124,6 +127,7 @@ class ToDoFragment : Fragment() {
ToDo(randomUUID, 0, todoContent, todoSubject)
)

// 插入数据库
lifecycleScope.launch {
Repository.insert(
ToDoRoom(
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down

0 comments on commit 7f826e1

Please sign in to comment.