Skip to content

Commit

Permalink
Refine empty tip & all task item file
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Aug 14, 2024
1 parent 9fd6d41 commit 331d098
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 204 deletions.
3 changes: 3 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/constant/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ object Constants {
const val BUNDLE_TODO_SUBJECT = "todoSubject"
const val BUNDLE_TODO_STATE = "todoState"
const val BUNDLE_TODO_UUID = "todoUUID"

const val EMPTY_VIEW_TYPE = 0
const val DEFAULT_VIEW_TYPE = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class AllTasksActivity : BaseActivity<ActivityAllTasksBinding>() {
false -> window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

val todoListAll = viewModel.todoListAll
val layoutManager = LinearLayoutManager(ToDoApp.context)
binding.allTasksList.layoutManager = layoutManager
val adapter = AllTasksAdapter(viewModel.todoListAll, supportFragmentManager)
val adapter = AllTasksAdapter(todoListAll, supportFragmentManager)
binding.allTasksList.adapter = adapter

FastScrollerBuilder(binding.allTasksList).apply {
Expand All @@ -43,24 +44,8 @@ class AllTasksActivity : BaseActivity<ActivityAllTasksBinding>() {
finish()
}

viewModel.emptyTipVis.observe(this, Observer { visibility ->
if (visibility == View.VISIBLE) {
binding.allTasksList.alpha = 1f
binding.allTasksList.animate().alpha(0f).duration = 200
binding.allTasksList.visibility = View.GONE

binding.emptyTip.alpha = 0f
binding.emptyTip.visibility = View.VISIBLE
binding.emptyTip.animate().alpha(1f).duration = 200
} else {
binding.allTasksList.alpha = 0f
binding.allTasksList.visibility = View.VISIBLE
binding.allTasksList.animate().alpha(1f).duration = 200

binding.emptyTip.alpha = 1f
binding.emptyTip.animate().alpha(0f).duration = 200
binding.emptyTip.visibility = View.GONE
}
viewModel.refreshData.observe(this, Observer {
binding.allTasksList.adapter?.notifyItemRangeChanged(0, todoListAll.size + 1)
})
}

Expand Down
86 changes: 52 additions & 34 deletions app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,81 @@ package cn.super12138.todo.views.all
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.Constants.DEFAULT_VIEW_TYPE
import cn.super12138.todo.constant.Constants.EMPTY_VIEW_TYPE
import cn.super12138.todo.logic.model.ToDo
import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.views.todo.ToDoAdapter.DefaultViewHolder

class AllTasksAdapter(
private val todoList: MutableList<ToDo>,
private val fragmentManager: FragmentManager
) :
RecyclerView.Adapter<AllTasksAdapter.ViewHolder>() {

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val todoContext: TextView = view.findViewById(R.id.todo_content_all)
val todoSubject: TextView = view.findViewById(R.id.todo_subject_all)
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// 默认待办项
inner class DefaultViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val todoContext: TextView = view.findViewById(R.id.todo_content)
val todoSubject: TextView = view.findViewById(R.id.todo_subject)
val itemBackground: LinearLayout = view.findViewById(R.id.item_background)
val checkBtn: Button = view.findViewById(R.id.check_item_btn)
}

// 空项目提示
inner class EmptyViewHolder(view: View) : RecyclerView.ViewHolder(view)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_all_tasks, parent, false)
return ViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == EMPTY_VIEW_TYPE) { // 如果列表是空的
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_empty, parent, false)
EmptyViewHolder(view)
} else {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_todo, parent, false)
DefaultViewHolder(view)
}
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val todo = todoList[position]
holder.todoContext.text = todo.content
holder.todoSubject.text = todo.subject
override fun getItemViewType(position: Int): Int {
return if (todoList.isEmpty()) EMPTY_VIEW_TYPE else DEFAULT_VIEW_TYPE
}

if (todo.state == 1) {
holder.itemBackground.background =
ContextCompat.getDrawable(ToDoApp.context, R.drawable.bg_item_complete)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
// 判断当前的holder是不是待办项目的holder
if (holder is DefaultViewHolder) {
val todo = todoList[position]
holder.apply {
checkBtn.visibility = View.GONE
todoContext.text = todo.content
todoSubject.text = todo.subject
}

holder.itemView.setOnClickListener {
VibrationUtils.performHapticFeedback(it)
if (todo.state == 1) {
holder.itemBackground.background =
ContextCompat.getDrawable(ToDoApp.context, R.drawable.bg_item_complete)
}

val infoBottomSheet = InfoBottomSheet.newInstance(
todo.content,
todo.subject,
todo.state,
todo.uuid
)
infoBottomSheet.show(fragmentManager, InfoBottomSheet.TAG)
}
holder.itemView.setOnClickListener {
VibrationUtils.performHapticFeedback(it)

/*if (!todo.isAnimated) {
holder.itemView.alpha = 0f
holder.itemView.animate().alpha(1f).duration = 200
todo.isAnimated = true
}*/
val infoBottomSheet = InfoBottomSheet.newInstance(
todo.content,
todo.subject,
todo.state,
todo.uuid
)
infoBottomSheet.show(fragmentManager, InfoBottomSheet.TAG)
}
}
}

override fun getItemCount() = todoList.size
override fun getItemCount(): Int {
return if (todoList.isEmpty()) 1 else todoList.size
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.super12138.todo.views.all

import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand All @@ -9,8 +8,8 @@ import cn.super12138.todo.logic.model.ToDo
import kotlinx.coroutines.launch

class AllTasksViewModel : ViewModel() {
val emptyTipVis = MutableLiveData<Int>(View.VISIBLE)
val todoListAll = ArrayList<ToDo>()
val refreshData = MutableLiveData<Int>(0)

init {
loadToDos()
Expand All @@ -19,15 +18,11 @@ class AllTasksViewModel : ViewModel() {
private fun loadToDos() {
viewModelScope.launch {
val todos = Repository.getAll()
var count = 0
for (todo in todos) {
todoListAll.add(ToDo(todo.uuid, todo.state, todo.content, todo.subject))
count++
}
if (count == 0) {
emptyTipVis.value = View.VISIBLE
} else {
emptyTipVis.value = View.GONE
if (todoListAll.size > 0) {
refreshData.value = 1
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ class ToDoBottomSheet : BottomSheetDialogFragment() {
todoViewModel.refreshData.value = 1
} else {
// 添加到 RecyclerView
if (todoList.size + 1 > 0) {
todoViewModel.emptyTipVis.value = View.GONE
}
todoList.add(
ToDo(randomUUID, 0, todoContent, todoSubject)
)
Expand Down Expand Up @@ -189,13 +186,6 @@ class ToDoBottomSheet : BottomSheetDialogFragment() {
progressViewModel.updateProgress()
todoViewModel.removeData.value = 1

// 空项目提示显示判断
if (todoList.isEmpty()) {
todoViewModel.emptyTipVis.value = View.VISIBLE
} else {
todoViewModel.emptyTipVis.value = View.GONE
}

dismiss()
}
}
Expand Down
116 changes: 62 additions & 54 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.RecyclerView
import cn.super12138.todo.R
import cn.super12138.todo.constant.Constants.DEFAULT_VIEW_TYPE
import cn.super12138.todo.constant.Constants.EMPTY_VIEW_TYPE
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.logic.model.ToDo
import cn.super12138.todo.utils.VibrationUtils
Expand All @@ -21,79 +23,85 @@ class ToDoAdapter(
private val todoList: MutableList<ToDo>,
private val viewModelStoreOwner: ViewModelStoreOwner,
private val fragmentManager: FragmentManager
) :
RecyclerView.Adapter<ToDoAdapter.ViewHolder>() {

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// 默认待办项
inner class DefaultViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val todoContext: TextView = view.findViewById(R.id.todo_content)
val todoSubject: TextView = view.findViewById(R.id.todo_subject)
val checkToDoBtn: Button = view.findViewById(R.id.check_item_btn)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_todo, parent, false)
// 空项目提示
inner class EmptyViewHolder(view: View) : RecyclerView.ViewHolder(view)

// 判断列表是否为空,为空显示空项目提示
override fun getItemViewType(position: Int): Int {
return if (todoList.isEmpty()) EMPTY_VIEW_TYPE else DEFAULT_VIEW_TYPE
}

return ViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == EMPTY_VIEW_TYPE) { // 如果列表是空的
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_empty, parent, false)
EmptyViewHolder(view)
} else {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_todo, parent, false)
DefaultViewHolder(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
}*/
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
// 判断当前的holder是不是待办项目的holder
if (holder is DefaultViewHolder) {
val todo = todoList[position]
holder.todoContext.text = todo.content
holder.todoSubject.text = todo.subject

val progressViewModel =
ViewModelProvider(viewModelStoreOwner)[ProgressViewModel::class.java]
val todoViewModel =
ViewModelProvider(viewModelStoreOwner)[ToDoViewModel::class.java]
val progressViewModel =
ViewModelProvider(viewModelStoreOwner)[ProgressViewModel::class.java]
val todoViewModel =
ViewModelProvider(viewModelStoreOwner)[ToDoViewModel::class.java]

holder.checkToDoBtn.setOnClickListener {
VibrationUtils.performHapticFeedback(it)
holder.checkToDoBtn.setOnClickListener {
VibrationUtils.performHapticFeedback(it)

if (position < 0 || position >= todoList.size) {
return@setOnClickListener
}
if (position >= todoList.size) {
return@setOnClickListener
}

todoList.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, todoList.size)
todoList.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, todoList.size)

todoViewModel.updateTaskState(todo.uuid)
todoViewModel.updateTaskState(todo.uuid)

// 设置空项目提示可见性
if (todoList.isEmpty()) {
todoViewModel.emptyTipVis.value = View.VISIBLE
} else {
todoViewModel.emptyTipVis.value = View.GONE
progressViewModel.updateProgress()
}
progressViewModel.updateProgress()
}

holder.itemView.setOnClickListener {
if (GlobalValues.devMode) {
VibrationUtils.performHapticFeedback(it)
"Current position: $position".showToast()
holder.itemView.setOnClickListener {
if (GlobalValues.devMode) {
VibrationUtils.performHapticFeedback(it)
"Current position: $position".showToast()
}
}
}

holder.itemView.setOnLongClickListener {
val toDoBottomSheet = ToDoBottomSheet.newInstance(
true,
position,
todo.uuid,
todo.state,
todo.subject,
todo.content
)
toDoBottomSheet.show(fragmentManager, ToDoBottomSheet.TAG)
true
holder.itemView.setOnLongClickListener {
val toDoBottomSheet = ToDoBottomSheet.newInstance(
true,
position,
todo.uuid,
todo.state,
todo.subject,
todo.content
)
toDoBottomSheet.show(fragmentManager, ToDoBottomSheet.TAG)
true
}
}
}

override fun getItemCount() = todoList.size
override fun getItemCount(): Int {
return if (todoList.isEmpty()) 1 else todoList.size
}
}
Loading

0 comments on commit 331d098

Please sign in to comment.