Files
FocusBox/src/main/java/com/focusbox/app/ui/projects/ProjectAdapter.kt

177 lines
7.7 KiB
Kotlin
Raw Normal View History

package com.focusbox.app.ui.projects
import android.content.res.ColorStateList
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.focusbox.app.R
import com.focusbox.app.model.Project
import com.focusbox.app.model.ProjectCompletion
import com.focusbox.app.model.ProjectTaskStat
import com.focusbox.app.util.Dates
import com.focusbox.app.util.col
import com.focusbox.app.util.roundBg
import com.focusbox.app.util.tint
import com.focusbox.app.util.visible
/**
* 项目是任务的二级分类:展开区只列「用户创建、未删除、未逾期」的任务,
* 以及该项目的完成记录。任务的开始与编辑都留在任务页,这里不提供入口。
*/
class ProjectAdapter(
private val onToggleExpand: (Project) -> Unit,
private val onLongClick: (Project) -> Unit,
private val onMore: (Project) -> Unit,
private val onToggleDone: () -> Unit,
) : RecyclerView.Adapter<ProjectAdapter.VH>() {
private var items: List<Project> = emptyList()
private var expandedId: Long = -1L
private var stats: Map<Long, List<ProjectTaskStat>> = emptyMap()
private var completions: Map<Long, List<ProjectCompletion>> = emptyMap()
private var showDone: Boolean = false
fun submit(
list: List<Project>,
expanded: Long,
taskStats: Map<Long, List<ProjectTaskStat>>,
completionRows: Map<Long, List<ProjectCompletion>>,
showDoneRows: Boolean,
) {
items = list
expandedId = expanded
stats = taskStats
completions = completionRows
showDone = showDoneRows
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH =
VH(LayoutInflater.from(parent.context).inflate(R.layout.item_project, parent, false))
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: VH, position: Int) = holder.bind(items[position])
inner class VH(v: View) : RecyclerView.ViewHolder(v) {
private val name: TextView = v.findViewById(R.id.tvName)
private val meta: TextView = v.findViewById(R.id.tvMeta)
private val progress: ProgressBar = v.findViewById(R.id.progress)
private val colorBar: View = v.findViewById(R.id.colorBar)
private val more: ImageView = v.findViewById(R.id.btnMore)
private val header: View = v.findViewById(R.id.projectHeader)
private val taskList: LinearLayout = v.findViewById(R.id.taskList)
private val tvTaskEmpty: TextView = v.findViewById(R.id.tvTaskEmpty)
fun bind(p: Project) {
val ctx = itemView.context
name.text = p.name
colorBar.background = ctx.roundBg(p.color, 2f)
// 统计取 Store.projects 的同一口径:待办/已完成只数用户创建的未逾期任务,
// 专注时长由 sessions 汇总,与展开区可见行一一对应。
val parts = ArrayList<String>(3)
parts.add("${p.openTasks}待办")
if (p.doneTasks > 0) parts.add("${p.doneTasks}已完成")
if (p.focusSeconds >= 60) parts.add(Dates.duration(p.focusSeconds))
meta.text = parts.joinToString(" · ")
progress.progress = (p.progress * 100).toInt()
progress.progressTintList = ColorStateList.valueOf(p.color)
progress.progressBackgroundTintList = ColorStateList.valueOf(ctx.col(R.color.ring_track))
more.tint(ctx.col(R.color.text_tertiary))
more.setOnClickListener { onMore(p) }
header.setOnClickListener { onToggleExpand(p) }
header.setOnLongClickListener {
onLongClick(p)
true
}
val expanded = expandedId == p.id
val rows = if (expanded) stats[p.id].orEmpty() else emptyList()
val records = if (expanded) completions[p.id].orEmpty() else emptyList()
taskList.visible(expanded && (rows.isNotEmpty() || records.isNotEmpty()))
tvTaskEmpty.visible(expanded && rows.isEmpty() && records.isEmpty())
if (!expanded) return
taskList.removeAllViews()
rows.forEach { addTaskRow(it, p) }
// 完成记录与任务行分开:一条专注一条,默认折叠,不混进任务列表
if (records.isNotEmpty()) {
val toggle = LayoutInflater.from(ctx)
.inflate(R.layout.item_footer, taskList, false)
val tv = toggle.findViewById<TextView>(R.id.tvFooter)
tv.text = ctx.getString(
if (showDone) R.string.project_collapse_done else R.string.project_expand_done,
records.size,
)
tv.setTextColor(ctx.col(R.color.brand))
toggle.setOnClickListener { onToggleDone() }
taskList.addView(toggle)
if (showDone) records.forEach { addCompletionRow(it, p) }
}
}
private fun addTaskRow(row: ProjectTaskStat, p: Project) {
val item = LayoutInflater.from(itemView.context)
.inflate(R.layout.item_project_task, taskList, false)
val ctx = item.context
val task = row.task
item.findViewById<View>(R.id.taskColorBar).background = ctx.roundBg(p.color, 2f)
val tvTitle: TextView = item.findViewById(R.id.tvTaskTitle)
val tvMeta: TextView = item.findViewById(R.id.tvTaskMeta)
tvTitle.text = task.title
tvTitle.setTextColor(ctx.col(if (task.done) R.color.text_tertiary else R.color.text))
tvTitle.paintFlags = if (task.done) {
tvTitle.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
} else {
tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
val parts = ArrayList<String>(3)
parts.add("${task.focusMinutes}分钟")
if (row.lastDoneAt > 0) {
val whenText = "${Dates.md(row.lastDoneAt)}·${Dates.hm(row.lastDoneAt)}"
parts.add(ctx.getString(R.string.project_task_last_done, whenText))
}
val totals = StringBuilder()
if (row.doneCount > 0) totals.append(ctx.getString(R.string.project_task_done_count, row.doneCount))
if (row.focusSeconds > 0) totals.append(row.focusSeconds / 60).append("分钟")
if (totals.isNotEmpty()) parts.add(totals.toString())
tvMeta.text = parts.joinToString(" · ")
taskList.addView(item)
}
private fun addCompletionRow(record: ProjectCompletion, p: Project) {
val item = LayoutInflater.from(itemView.context)
.inflate(R.layout.item_project_task, taskList, false)
val ctx = item.context
item.findViewById<View>(R.id.taskColorBar).background = ctx.roundBg(p.color, 2f)
val tvTitle: TextView = item.findViewById(R.id.tvTaskTitle)
val tvMeta: TextView = item.findViewById(R.id.tvTaskMeta)
tvTitle.text = if (record.note.isBlank()) {
record.taskTitle
} else {
"${record.taskTitle} · ${record.note.replace('\n', ' ')}"
}
tvTitle.setTextColor(ctx.col(R.color.text_secondary))
tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
val whenText = "${Dates.md(record.startAt)}·${Dates.hm(record.startAt)}-${Dates.hm(record.endAt)}"
tvMeta.text = "$whenText · 专注${Dates.duration(record.seconds)}"
taskList.addView(item)
}
}
}