2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.ui.tasks
|
|
|
|
|
|
|
|
|
|
|
|
import android.graphics.Paint
|
|
|
|
|
|
import android.graphics.Typeface
|
2026-11-28 17:40:00 +08:00
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
|
|
import android.text.style.RelativeSizeSpan
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
|
import android.view.View
|
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
|
import android.widget.ImageView
|
|
|
|
|
|
import android.widget.TextView
|
2026-11-28 21:21:36 +08:00
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
|
import com.focusbox.app.R
|
|
|
|
|
|
import com.focusbox.app.model.Task
|
|
|
|
|
|
import com.focusbox.app.util.Dates
|
|
|
|
|
|
import com.focusbox.app.util.circleBg
|
|
|
|
|
|
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 TaskAdapter(
|
|
|
|
|
|
private val onEdit: (Task) -> Unit,
|
|
|
|
|
|
private val onToggle: (Task) -> Unit,
|
|
|
|
|
|
private val onStart: (Task) -> Unit,
|
|
|
|
|
|
private val onFooterClick: () -> Unit,
|
2026-11-28 21:21:36 +08:00
|
|
|
|
) : ListAdapter<TaskAdapter.Row, RecyclerView.ViewHolder>(Diff) {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
sealed class Row {
|
|
|
|
|
|
data class Item(
|
|
|
|
|
|
val task: Task,
|
|
|
|
|
|
val projectName: String,
|
|
|
|
|
|
val projectColor: Int,
|
|
|
|
|
|
val overdue: Boolean,
|
|
|
|
|
|
/** 已完成任务的"日期 星期 起-止",无记录时为 null */
|
|
|
|
|
|
val doneRange: String? = null,
|
|
|
|
|
|
) : Row()
|
|
|
|
|
|
|
|
|
|
|
|
data class Footer(val text: String) : Row()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 21:21:36 +08:00
|
|
|
|
/** 勾选/筛选只重排差异行,不再整列表 notifyDataSetChanged 闪烁 */
|
|
|
|
|
|
private object Diff : DiffUtil.ItemCallback<Row>() {
|
|
|
|
|
|
override fun areItemsTheSame(oldItem: Row, newItem: Row): Boolean = when {
|
|
|
|
|
|
oldItem is Row.Item && newItem is Row.Item -> oldItem.task.id == newItem.task.id
|
|
|
|
|
|
oldItem is Row.Footer && newItem is Row.Footer -> true
|
|
|
|
|
|
else -> false
|
|
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
2026-11-28 21:21:36 +08:00
|
|
|
|
override fun areContentsTheSame(oldItem: Row, newItem: Row): Boolean = oldItem == newItem
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int =
|
2026-11-28 21:21:36 +08:00
|
|
|
|
if (getItem(position) is Row.Footer) TYPE_FOOTER else TYPE_TASK
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
|
|
|
|
val inflater = LayoutInflater.from(parent.context)
|
|
|
|
|
|
return if (viewType == TYPE_FOOTER) {
|
|
|
|
|
|
FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
TaskViewHolder(inflater.inflate(R.layout.item_task, parent, false))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
2026-11-28 21:21:36 +08:00
|
|
|
|
when (val row = getItem(position)) {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
is Row.Item -> (holder as TaskViewHolder).bind(row)
|
|
|
|
|
|
is Row.Footer -> (holder as FooterViewHolder).bind(row)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private inner class TaskViewHolder(v: View) : RecyclerView.ViewHolder(v) {
|
|
|
|
|
|
private val tvTitle: TextView = v.findViewById(R.id.tvTitle)
|
|
|
|
|
|
private val tvMeta: TextView = v.findViewById(R.id.tvMeta)
|
|
|
|
|
|
private val tvNote: TextView = v.findViewById(R.id.tvNote)
|
|
|
|
|
|
private val checkCircle: View = v.findViewById(R.id.checkCircle)
|
|
|
|
|
|
private val checkMark: ImageView = v.findViewById(R.id.checkMark)
|
|
|
|
|
|
private val checkWrap: View = v.findViewById(R.id.checkWrap)
|
|
|
|
|
|
private val btnPlay: ImageView = v.findViewById(R.id.btnPlay)
|
|
|
|
|
|
|
|
|
|
|
|
fun bind(row: Row.Item) {
|
|
|
|
|
|
val task = row.task
|
|
|
|
|
|
val ctx = itemView.context
|
|
|
|
|
|
|
2026-11-29 09:49:18 +08:00
|
|
|
|
// 标题右侧的属性图标,未完成与已完成都显示:
|
2026-11-29 10:05:14 +08:00
|
|
|
|
// 重要度圆圈数字(③ 高 / ② 中 / ① 普通,颜色与紧急度同色系)→
|
|
|
|
|
|
// 紧急度五角星 → 随时上箭头。
|
2026-11-29 10:54:00 +08:00
|
|
|
|
// 各字形在字体里的字面尺寸不同:以圆圈数字 0.85 为基准,
|
|
|
|
|
|
// 上箭头与圆圈同高;五角星 0.95 不作参照
|
2026-11-28 17:40:00 +08:00
|
|
|
|
val title = SpannableStringBuilder(task.title)
|
2026-11-29 09:49:18 +08:00
|
|
|
|
|
2026-11-29 10:05:14 +08:00
|
|
|
|
val (badge, badgeColor) = when (task.priority) {
|
|
|
|
|
|
2 -> " ③" to R.color.urgent_high
|
|
|
|
|
|
1 -> " ②" to R.color.urgent_mid
|
|
|
|
|
|
else -> " ①" to R.color.urgent_low
|
2026-11-29 09:49:18 +08:00
|
|
|
|
}
|
2026-11-29 10:05:14 +08:00
|
|
|
|
title.append(badge)
|
2026-11-29 09:49:18 +08:00
|
|
|
|
title.setSpan(
|
2026-11-29 10:05:14 +08:00
|
|
|
|
android.text.style.ForegroundColorSpan(ctx.col(badgeColor)),
|
|
|
|
|
|
title.length - badge.length, title.length, 0
|
2026-11-29 09:49:18 +08:00
|
|
|
|
)
|
2026-11-29 10:34:46 +08:00
|
|
|
|
title.setSpan(RelativeSizeSpan(0.85f), title.length - badge.length, title.length, 0)
|
2026-11-29 09:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 紧急度五角星:紧急三颗红 / 中两颗绿 / 不急一颗灰
|
|
|
|
|
|
val (stars, starColor) = when (task.urgency) {
|
|
|
|
|
|
2 -> " ★★★" to R.color.urgent_high
|
|
|
|
|
|
1 -> " ★★" to R.color.urgent_mid
|
|
|
|
|
|
else -> " ★" to R.color.urgent_low
|
2026-11-28 17:40:00 +08:00
|
|
|
|
}
|
2026-11-29 09:49:18 +08:00
|
|
|
|
title.append(stars)
|
|
|
|
|
|
title.setSpan(
|
|
|
|
|
|
android.text.style.ForegroundColorSpan(ctx.col(starColor)),
|
|
|
|
|
|
title.length - stars.length, title.length, 0
|
|
|
|
|
|
)
|
|
|
|
|
|
title.setSpan(RelativeSizeSpan(0.95f), title.length - stars.length, title.length, 0)
|
|
|
|
|
|
|
2026-11-29 23:09:39 +08:00
|
|
|
|
// 「随时」任务:标题后带品牌蓝圆圈上箭头表示置顶,列表里排在未完成最前面。
|
|
|
|
|
|
// 图标与重要度圆圈数字同规格(0.85 倍字号,圆圈里画向上箭头),
|
|
|
|
|
|
// 圆圈与箭头同色(品牌蓝);前面的细空格(U+2009)保持与星标的间距一致。
|
2026-11-29 09:49:18 +08:00
|
|
|
|
if (task.repeatType == com.focusbox.app.model.Repeat.ANYTIME && !task.done) {
|
2026-11-29 23:09:39 +08:00
|
|
|
|
title.append("\u2009 ")
|
2026-11-29 10:05:14 +08:00
|
|
|
|
title.setSpan(
|
2026-11-29 23:09:39 +08:00
|
|
|
|
PinIconSpan(ctx.col(R.color.brand), tvTitle.textSize * 0.85f),
|
|
|
|
|
|
title.length - 1, title.length, 0,
|
2026-11-29 10:05:14 +08:00
|
|
|
|
)
|
2026-11-29 09:49:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 17:40:00 +08:00
|
|
|
|
tvTitle.text = title
|
2026-11-29 23:09:39 +08:00
|
|
|
|
// 完成横线只画任务名本身(StrikethroughSpan 限定区间),
|
|
|
|
|
|
// 后面的重要度/紧急度/置顶图标不跟着划线
|
|
|
|
|
|
tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
|
|
|
|
|
|
if (task.done) {
|
|
|
|
|
|
title.setSpan(
|
|
|
|
|
|
android.text.style.StrikethroughSpan(),
|
|
|
|
|
|
0, task.title.length, 0,
|
|
|
|
|
|
)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
tvTitle.setTextColor(ctx.col(if (task.done) R.color.text_tertiary else R.color.text))
|
|
|
|
|
|
tvTitle.typeface = if (task.priority >= 2 && !task.done) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
|
|
|
|
|
|
|
|
|
|
|
|
if (task.done) {
|
|
|
|
|
|
checkCircle.background = ctx.circleBg(ctx.col(R.color.brand))
|
|
|
|
|
|
checkMark.visible(true)
|
|
|
|
|
|
checkMark.tint(ctx.col(R.color.on_brand))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
checkCircle.background = ctx.roundBg(
|
|
|
|
|
|
android.graphics.Color.TRANSPARENT, 100f, ctx.col(R.color.field_stroke), 1.5f
|
|
|
|
|
|
)
|
|
|
|
|
|
checkMark.visible(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tvMeta.text = buildMeta(row)
|
|
|
|
|
|
tvMeta.setTextColor(
|
|
|
|
|
|
ctx.col(
|
|
|
|
|
|
when {
|
|
|
|
|
|
row.overdue && !task.done -> R.color.brand
|
|
|
|
|
|
else -> R.color.text_tertiary
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 备注单独一行,跟在"项目 · 日期"下面,避免挤在元信息里被截断
|
|
|
|
|
|
tvNote.text = task.note
|
|
|
|
|
|
tvNote.visible(task.note.isNotBlank())
|
|
|
|
|
|
|
|
|
|
|
|
btnPlay.visible(!task.done)
|
2026-11-28 17:40:00 +08:00
|
|
|
|
btnPlay.tint(ctx.col(R.color.brand))
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
checkWrap.setOnClickListener { onToggle(task) }
|
|
|
|
|
|
itemView.setOnClickListener { onEdit(task) }
|
|
|
|
|
|
btnPlay.setOnClickListener { onStart(task) }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun buildMeta(row: Row.Item): String {
|
|
|
|
|
|
val task = row.task
|
2026-11-28 21:21:36 +08:00
|
|
|
|
val ctx = itemView.context
|
2026-11-28 00:25:22 +08:00
|
|
|
|
val parts = ArrayList<String>(4)
|
|
|
|
|
|
parts.add(row.projectName)
|
|
|
|
|
|
if (task.done) {
|
|
|
|
|
|
row.doneRange?.let { parts.add(it) }
|
|
|
|
|
|
return parts.joinToString(" · ")
|
|
|
|
|
|
}
|
2026-11-28 21:21:36 +08:00
|
|
|
|
parts.add(ctx.getString(R.string.minutes_value, task.focusMinutes))
|
2026-11-28 00:25:22 +08:00
|
|
|
|
if (task.due > 0) {
|
|
|
|
|
|
val label = Dates.dueLabel(task.due)
|
2026-11-28 21:21:36 +08:00
|
|
|
|
parts.add(if (row.overdue) ctx.getString(R.string.task_meta_overdue, label) else label)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-29 09:49:18 +08:00
|
|
|
|
// 重要度已改为标题后的竖线图标,meta 里不再重复「高优先」文字
|
2026-11-28 00:25:22 +08:00
|
|
|
|
return parts.joinToString(" · ")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private inner class FooterViewHolder(v: View) : RecyclerView.ViewHolder(v) {
|
|
|
|
|
|
private val tv: TextView = v.findViewById(R.id.tvFooter)
|
|
|
|
|
|
|
|
|
|
|
|
fun bind(row: Row.Footer) {
|
|
|
|
|
|
tv.text = row.text
|
|
|
|
|
|
itemView.setOnClickListener { onFooterClick() }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
private const val TYPE_TASK = 0
|
|
|
|
|
|
private const val TYPE_FOOTER = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-29 23:09:39 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 置顶图标:圆圈内向上箭头。
|
|
|
|
|
|
* 自绘而不是用字体字形——常用字体里没有"圆圈+箭头"的组合字符,
|
|
|
|
|
|
* 画出来才能精确控制圆圈与箭头同色、且和重要度圆圈数字(0.85 倍字号)等高。
|
|
|
|
|
|
*/
|
|
|
|
|
|
private class PinIconSpan(
|
|
|
|
|
|
private val color: Int,
|
|
|
|
|
|
private val size: Float,
|
|
|
|
|
|
) : android.text.style.ReplacementSpan() {
|
|
|
|
|
|
|
|
|
|
|
|
override fun getSize(
|
|
|
|
|
|
paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?,
|
|
|
|
|
|
): Int {
|
|
|
|
|
|
val s = size.toInt()
|
|
|
|
|
|
if (fm != null) {
|
|
|
|
|
|
// 占位与字形等高(顶部到基线),避免行高被图标撑开
|
|
|
|
|
|
fm.ascent = -s
|
|
|
|
|
|
fm.descent = 0
|
|
|
|
|
|
fm.top = fm.ascent
|
|
|
|
|
|
fm.bottom = fm.descent
|
|
|
|
|
|
}
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun draw(
|
|
|
|
|
|
canvas: android.graphics.Canvas, text: CharSequence, start: Int, end: Int,
|
|
|
|
|
|
x: Float, top: Int, baseline: Int, bottom: Int, paint: Paint,
|
|
|
|
|
|
) {
|
|
|
|
|
|
val s = size
|
|
|
|
|
|
val cx = x + s / 2f
|
|
|
|
|
|
// 与 ①②③ 字形对齐:圆圈数字贴基线站立,圆心约在基线上方 0.5 个图标高
|
|
|
|
|
|
val cy = baseline - s * 0.5f
|
|
|
|
|
|
val p = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
|
|
|
this.color = color
|
|
|
|
|
|
style = Paint.Style.STROKE
|
|
|
|
|
|
strokeWidth = s / 8f
|
|
|
|
|
|
strokeCap = Paint.Cap.ROUND
|
|
|
|
|
|
}
|
|
|
|
|
|
canvas.drawCircle(cx, cy, s / 2f - p.strokeWidth / 2f, p)
|
|
|
|
|
|
|
|
|
|
|
|
// 向上箭头:竖杆 + 两条斜线,整体落在圆内
|
|
|
|
|
|
val head = s * 0.17f
|
|
|
|
|
|
val yTop = cy - s * 0.15f
|
|
|
|
|
|
val yBot = cy + s * 0.19f
|
|
|
|
|
|
canvas.drawLine(cx, yBot, cx, yTop, p)
|
|
|
|
|
|
canvas.drawLine(cx - head, yTop + head, cx, yTop, p)
|
|
|
|
|
|
canvas.drawLine(cx + head, yTop + head, cx, yTop, p)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|