2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.ui.projects
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.res.ColorStateList
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import android.graphics.Paint
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
|
import android.view.View
|
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
|
import android.widget.ImageView
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import android.widget.LinearLayout
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.widget.ProgressBar
|
|
|
|
|
|
import android.widget.TextView
|
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
|
import com.focusbox.app.R
|
|
|
|
|
|
import com.focusbox.app.model.Project
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import com.focusbox.app.model.ProjectTaskStat
|
|
|
|
|
|
import com.focusbox.app.model.Task
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import com.focusbox.app.util.Dates
|
|
|
|
|
|
import com.focusbox.app.util.col
|
|
|
|
|
|
import com.focusbox.app.util.roundBg
|
|
|
|
|
|
import com.focusbox.app.util.tint
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import com.focusbox.app.util.visible
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
2026-12-01 11:24:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 项目列表:点击项目头就地展开该项目下的任务,不再跳转任务页。
|
2026-12-01 11:58:05 +08:00
|
|
|
|
* 展开行显示统计、右侧保留开始按钮;点击行复用任务页同款编辑/删除弹窗。
|
|
|
|
|
|
* 已完成任务默认折叠,用「展开/收起完成记录」开关控制,避免历史越积越长。
|
2026-12-01 11:24:03 +08:00
|
|
|
|
*/
|
2026-11-28 00:25:22 +08:00
|
|
|
|
class ProjectAdapter(
|
2026-12-01 11:24:03 +08:00
|
|
|
|
private val onToggleExpand: (Project) -> Unit,
|
2026-11-28 00:25:22 +08:00
|
|
|
|
private val onLongClick: (Project) -> Unit,
|
|
|
|
|
|
private val onMore: (Project) -> Unit,
|
2026-12-01 11:24:03 +08:00
|
|
|
|
private val onStart: (Task) -> Unit,
|
2026-12-01 11:58:05 +08:00
|
|
|
|
private val onEdit: (Task) -> Unit,
|
|
|
|
|
|
private val onToggleDone: () -> Unit,
|
2026-11-28 00:25:22 +08:00
|
|
|
|
) : RecyclerView.Adapter<ProjectAdapter.VH>() {
|
|
|
|
|
|
|
|
|
|
|
|
private var items: List<Project> = emptyList()
|
2026-12-01 11:24:03 +08:00
|
|
|
|
private var expandedId: Long = -1L
|
|
|
|
|
|
private var stats: Map<Long, List<ProjectTaskStat>> = emptyMap()
|
2026-12-01 11:58:05 +08:00
|
|
|
|
private var showDone: Boolean = false
|
|
|
|
|
|
|
|
|
|
|
|
fun submit(
|
|
|
|
|
|
list: List<Project>,
|
|
|
|
|
|
expanded: Long,
|
|
|
|
|
|
taskStats: Map<Long, List<ProjectTaskStat>>,
|
|
|
|
|
|
showDoneRows: Boolean,
|
|
|
|
|
|
) {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
items = list
|
2026-12-01 11:24:03 +08:00
|
|
|
|
expandedId = expanded
|
|
|
|
|
|
stats = taskStats
|
2026-12-01 11:58:05 +08:00
|
|
|
|
showDone = showDoneRows
|
2026-11-28 00:25:22 +08:00
|
|
|
|
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)
|
2026-12-01 11:24:03 +08:00
|
|
|
|
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)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
fun bind(p: Project) {
|
|
|
|
|
|
val ctx = itemView.context
|
|
|
|
|
|
name.text = p.name
|
|
|
|
|
|
colorBar.background = ctx.roundBg(p.color, 2f)
|
|
|
|
|
|
|
2026-12-01 11:24:03 +08:00
|
|
|
|
// 只看任务与时长,不再显示番茄数(与改动前一致)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
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) }
|
|
|
|
|
|
|
2026-12-01 11:24:03 +08:00
|
|
|
|
header.setOnClickListener { onToggleExpand(p) }
|
|
|
|
|
|
header.setOnLongClickListener {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
onLongClick(p)
|
|
|
|
|
|
true
|
|
|
|
|
|
}
|
2026-12-01 11:24:03 +08:00
|
|
|
|
|
|
|
|
|
|
val expanded = expandedId == p.id
|
|
|
|
|
|
val rows = if (expanded) stats[p.id].orEmpty() else emptyList()
|
2026-12-01 11:58:05 +08:00
|
|
|
|
val openRows = rows.filter { !it.task.done }
|
|
|
|
|
|
val doneRows = rows.filter { it.task.done }
|
2026-12-01 11:24:03 +08:00
|
|
|
|
taskList.visible(expanded && rows.isNotEmpty())
|
|
|
|
|
|
tvTaskEmpty.visible(expanded && rows.isEmpty())
|
|
|
|
|
|
if (!expanded) return
|
|
|
|
|
|
|
2026-12-01 11:58:05 +08:00
|
|
|
|
fun addTaskRow(row: ProjectTaskStat) {
|
2026-12-01 11:24:03 +08:00
|
|
|
|
val item = LayoutInflater.from(ctx)
|
|
|
|
|
|
.inflate(R.layout.item_project_task, taskList, false)
|
|
|
|
|
|
bindTaskRow(item, row, p)
|
|
|
|
|
|
taskList.addView(item)
|
|
|
|
|
|
}
|
2026-12-01 11:58:05 +08:00
|
|
|
|
|
|
|
|
|
|
taskList.removeAllViews()
|
|
|
|
|
|
openRows.forEach { addTaskRow(it) }
|
|
|
|
|
|
|
|
|
|
|
|
// 已完成任务默认折叠:开关行永远紧贴在已完成记录之前
|
|
|
|
|
|
if (doneRows.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,
|
|
|
|
|
|
doneRows.size,
|
|
|
|
|
|
)
|
|
|
|
|
|
tv.setTextColor(ctx.col(R.color.brand))
|
|
|
|
|
|
toggle.setOnClickListener { onToggleDone() }
|
|
|
|
|
|
taskList.addView(toggle)
|
|
|
|
|
|
if (showDone) doneRows.forEach { addTaskRow(it) }
|
|
|
|
|
|
}
|
2026-12-01 11:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun bindTaskRow(item: View, row: ProjectTaskStat, p: Project) {
|
|
|
|
|
|
val ctx = item.context
|
|
|
|
|
|
val task = row.task
|
|
|
|
|
|
val tvTitle: TextView = item.findViewById(R.id.tvTaskTitle)
|
|
|
|
|
|
val tvMeta: TextView = item.findViewById(R.id.tvTaskMeta)
|
|
|
|
|
|
val btnPlay: ImageView = item.findViewById(R.id.btnTaskPlay)
|
|
|
|
|
|
item.findViewById<View>(R.id.taskColorBar).background = ctx.roundBg(p.color, 2f)
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-01 11:58:05 +08:00
|
|
|
|
// 统计:单轮时长 · 最近完成时间 · 累计次数+累计专注分钟;不显示分类(项目名)
|
2026-12-01 11:24:03 +08:00
|
|
|
|
val parts = ArrayList<String>(3)
|
|
|
|
|
|
parts.add(ctx.getString(R.string.minutes_value, 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))
|
|
|
|
|
|
}
|
2026-12-01 11:58:05 +08:00
|
|
|
|
val totals = StringBuilder()
|
2026-12-01 11:24:03 +08:00
|
|
|
|
if (row.doneCount > 0) {
|
2026-12-01 11:58:05 +08:00
|
|
|
|
totals.append(ctx.getString(R.string.project_task_done_count, row.doneCount))
|
|
|
|
|
|
}
|
|
|
|
|
|
if (row.focusSeconds > 0) {
|
|
|
|
|
|
// 累计专注只写「数字 分钟」,不加任何说明文案;紧跟在累计次数后一个空格
|
|
|
|
|
|
if (totals.isNotEmpty()) totals.append(' ')
|
|
|
|
|
|
totals.append(row.focusSeconds / 60).append(" 分钟")
|
2026-12-01 11:24:03 +08:00
|
|
|
|
}
|
2026-12-01 11:58:05 +08:00
|
|
|
|
if (totals.isNotEmpty()) parts.add(totals.toString())
|
2026-12-01 11:24:03 +08:00
|
|
|
|
tvMeta.text = parts.joinToString(" · ")
|
|
|
|
|
|
|
|
|
|
|
|
btnPlay.visible(!task.done)
|
|
|
|
|
|
btnPlay.tint(ctx.col(R.color.brand))
|
|
|
|
|
|
btnPlay.setOnClickListener { onStart(task) }
|
2026-12-01 11:58:05 +08:00
|
|
|
|
// 与任务页一致:点行打开编辑/删除弹窗
|
|
|
|
|
|
item.setOnClickListener { onEdit(task) }
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|