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-12-01 16:52:28 +08:00
|
|
|
|
import android.text.Spannable
|
|
|
|
|
|
import android.text.SpannableString
|
|
|
|
|
|
import android.text.style.StrikethroughSpan
|
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 14:51:04 +08:00
|
|
|
|
import com.focusbox.app.model.ProjectCompletion
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import com.focusbox.app.model.ProjectTaskStat
|
2026-12-01 13:42:12 +08:00
|
|
|
|
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 14:51:04 +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 13:42:12 +08:00
|
|
|
|
private val onStart: (Task) -> Unit,
|
|
|
|
|
|
private val onEdit: (Task) -> Unit,
|
2026-12-01 11:58:05 +08:00
|
|
|
|
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 14:51:04 +08:00
|
|
|
|
private var completions: Map<Long, List<ProjectCompletion>> = 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>>,
|
2026-12-01 14:51:04 +08:00
|
|
|
|
completionRows: Map<Long, List<ProjectCompletion>>,
|
2026-12-01 11:58:05 +08:00
|
|
|
|
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 14:51:04 +08:00
|
|
|
|
completions = completionRows
|
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 14:51:04 +08:00
|
|
|
|
// 项目页只统计项目下创建的任务:总数、已完成数、累计专注时长
|
2026-11-28 00:25:22 +08:00
|
|
|
|
val parts = ArrayList<String>(3)
|
2026-12-01 14:51:04 +08:00
|
|
|
|
parts.add(ctx.getString(R.string.project_tasks_total, p.totalTasks))
|
|
|
|
|
|
if (p.doneTasks > 0) parts.add(ctx.getString(R.string.project_tasks_done, p.doneTasks))
|
2026-11-28 00:25:22 +08:00
|
|
|
|
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 14:51:04 +08:00
|
|
|
|
val records = if (expanded) completions[p.id].orEmpty() else emptyList()
|
|
|
|
|
|
taskList.visible(expanded && (rows.isNotEmpty() || records.isNotEmpty()))
|
|
|
|
|
|
tvTaskEmpty.visible(expanded && rows.isEmpty() && records.isEmpty())
|
2026-12-01 11:24:03 +08:00
|
|
|
|
if (!expanded) return
|
|
|
|
|
|
|
2026-12-01 13:42:12 +08:00
|
|
|
|
fun addTaskRow(row: ProjectTaskStat) {
|
|
|
|
|
|
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()
|
2026-12-01 14:51:04 +08:00
|
|
|
|
// 项目下创建的任务全部列出:不按完成/逾期过滤,也不重复生成
|
|
|
|
|
|
rows.forEach { addTaskRow(it) }
|
2026-12-01 11:58:05 +08:00
|
|
|
|
|
2026-12-01 14:51:04 +08:00
|
|
|
|
// 任务下面单独一条开关:展开/隐藏该项目下的完成记录
|
|
|
|
|
|
if (records.isNotEmpty()) {
|
2026-12-01 11:58:05 +08:00
|
|
|
|
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,
|
2026-12-01 14:51:04 +08:00
|
|
|
|
records.size,
|
2026-12-01 11:58:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
tv.setTextColor(ctx.col(R.color.brand))
|
|
|
|
|
|
toggle.setOnClickListener { onToggleDone() }
|
|
|
|
|
|
taskList.addView(toggle)
|
2026-12-01 14:51:04 +08:00
|
|
|
|
if (showDone) records.forEach { addCompletionRow(it, p) }
|
2026-12-01 11:58:05 +08:00
|
|
|
|
}
|
2026-12-01 11:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-01 13:42:12 +08:00
|
|
|
|
private fun bindTaskRow(item: View, row: ProjectTaskStat, p: Project) {
|
2026-12-01 11:24:03 +08:00
|
|
|
|
val ctx = item.context
|
|
|
|
|
|
val task = row.task
|
2026-12-01 13:18:59 +08:00
|
|
|
|
val tvTitle: TextView = item.findViewById(R.id.tvTaskTitle)
|
|
|
|
|
|
val tvMeta: TextView = item.findViewById(R.id.tvTaskMeta)
|
2026-12-01 13:42:12 +08:00
|
|
|
|
val btnPlay: ImageView = item.findViewById(R.id.btnTaskPlay)
|
|
|
|
|
|
item.findViewById<View>(R.id.taskColorBar).background = ctx.roundBg(p.color, 2f)
|
|
|
|
|
|
|
2026-12-01 16:52:28 +08:00
|
|
|
|
// 顶部是「创建了就一直在」的任务清单:不划线、不变灰;
|
|
|
|
|
|
// 完成与否只体现在下面的完成记录里
|
2026-12-01 11:24:03 +08:00
|
|
|
|
tvTitle.text = task.title
|
2026-12-01 16:52:28 +08:00
|
|
|
|
tvTitle.setTextColor(ctx.col(R.color.text))
|
|
|
|
|
|
tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
|
2026-12-01 11:24:03 +08:00
|
|
|
|
|
2026-12-01 16:52:28 +08:00
|
|
|
|
// 任务时间(专注时长)· 最近完成 · 累计次数与累计专注
|
|
|
|
|
|
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))
|
2026-12-01 13:42:12 +08:00
|
|
|
|
}
|
2026-12-01 16:52:28 +08:00
|
|
|
|
if (row.focusSeconds > 0) {
|
|
|
|
|
|
// 累计专注只写「数字分钟」,不加说明文案,也不留空格
|
|
|
|
|
|
totals.append(row.focusSeconds / 60).append("分钟")
|
|
|
|
|
|
}
|
|
|
|
|
|
if (totals.isNotEmpty()) parts.add(totals.toString())
|
|
|
|
|
|
tvMeta.text = parts.joinToString(" · ")
|
2026-12-01 11:24:03 +08:00
|
|
|
|
|
2026-12-01 13:42:12 +08:00
|
|
|
|
btnPlay.visible(!task.done)
|
|
|
|
|
|
btnPlay.tint(ctx.col(R.color.brand))
|
|
|
|
|
|
btnPlay.setOnClickListener { onStart(task) }
|
|
|
|
|
|
// 与任务页一致:点行打开编辑/删除弹窗
|
|
|
|
|
|
item.setOnClickListener { onEdit(task) }
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-12-01 14:51:04 +08:00
|
|
|
|
|
|
|
|
|
|
private fun addCompletionRow(record: ProjectCompletion, p: Project) {
|
|
|
|
|
|
val ctx = itemView.context
|
|
|
|
|
|
val item = LayoutInflater.from(ctx)
|
|
|
|
|
|
.inflate(R.layout.item_project_task, taskList, false)
|
|
|
|
|
|
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)
|
|
|
|
|
|
val btnPlay: ImageView = item.findViewById(R.id.btnTaskPlay)
|
|
|
|
|
|
btnPlay.visible(false)
|
|
|
|
|
|
|
2026-12-01 16:52:28 +08:00
|
|
|
|
// 完成记录与任务页底部一致:任务名划线,完成原因跟在后面但不划线
|
|
|
|
|
|
val noteText = record.note.replace('\n', ' ')
|
|
|
|
|
|
val titleText = if (noteText.isBlank()) {
|
2026-12-01 14:51:04 +08:00
|
|
|
|
record.taskTitle
|
|
|
|
|
|
} else {
|
2026-12-01 16:52:28 +08:00
|
|
|
|
"${record.taskTitle} · $noteText"
|
|
|
|
|
|
}
|
|
|
|
|
|
val line = SpannableString(titleText)
|
|
|
|
|
|
if (record.taskTitle.isNotEmpty()) {
|
|
|
|
|
|
line.setSpan(
|
|
|
|
|
|
StrikethroughSpan(), 0, record.taskTitle.length,
|
|
|
|
|
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
|
|
|
|
|
)
|
2026-12-01 14:51:04 +08:00
|
|
|
|
}
|
2026-12-01 16:52:28 +08:00
|
|
|
|
tvTitle.text = line
|
2026-12-01 14:51:04 +08:00
|
|
|
|
tvTitle.setTextColor(ctx.col(R.color.text_secondary))
|
|
|
|
|
|
tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
|
|
|
|
|
|
|
2026-12-01 16:52:28 +08:00
|
|
|
|
// 与任务页完成记录同格式:日期 周几 起-止 · 专注X;没有真实时长就只写完成时刻
|
|
|
|
|
|
tvMeta.text = if (record.seconds > 0) {
|
|
|
|
|
|
"${Dates.dateWithWeek(record.startAt)} ${Dates.hms(record.startAt)}" +
|
|
|
|
|
|
"-${Dates.hms(record.endAt)} · 专注${Dates.duration(record.seconds)}"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"${Dates.dateWithWeek(record.endAt)} ${Dates.hms(record.endAt)}完成"
|
|
|
|
|
|
}
|
2026-12-01 14:51:04 +08:00
|
|
|
|
item.isClickable = false
|
|
|
|
|
|
item.setOnClickListener(null)
|
|
|
|
|
|
taskList.addView(item)
|
|
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|