package com.focusbox.app.ui.tasks import android.graphics.Paint import android.graphics.Typeface import android.text.SpannableStringBuilder import android.text.style.RelativeSizeSpan import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter 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, ) : ListAdapter(Diff) { 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() } /** 勾选/筛选只重排差异行,不再整列表 notifyDataSetChanged 闪烁 */ private object Diff : DiffUtil.ItemCallback() { 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 } override fun areContentsTheSame(oldItem: Row, newItem: Row): Boolean = oldItem == newItem } override fun getItemViewType(position: Int): Int = if (getItem(position) is Row.Footer) TYPE_FOOTER else TYPE_TASK 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) { when (val row = getItem(position)) { 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 // 标题右侧的属性图标,未完成与已完成都显示: // 重要度圆圈数字(③ 高 / ② 中 / ① 普通,颜色与紧急度同色系)→ // 紧急度五角星 → 随时上箭头。 // 各字形在字体里的字面尺寸不同:以圆圈数字 0.85 为基准, // 上箭头与圆圈同高;五角星 0.95 不作参照 val title = SpannableStringBuilder(task.title) val (badge, badgeColor) = when (task.priority) { 2 -> " ③" to R.color.urgent_high 1 -> " ②" to R.color.urgent_mid else -> " ①" to R.color.urgent_low } title.append(badge) title.setSpan( android.text.style.ForegroundColorSpan(ctx.col(badgeColor)), title.length - badge.length, title.length, 0 ) title.setSpan(RelativeSizeSpan(0.85f), title.length - badge.length, title.length, 0) // 紧急度五角星:紧急三颗红 / 中两颗绿 / 不急一颗灰 val (stars, starColor) = when (task.urgency) { 2 -> " ★★★" to R.color.urgent_high 1 -> " ★★" to R.color.urgent_mid else -> " ★" to R.color.urgent_low } 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) // 「随时」任务:标题后带品牌蓝圆圈上箭头表示置顶,列表里排在未完成最前面。 // 图标与重要度圆圈数字同规格(0.85 倍字号,圆圈里画向上箭头), // 圆圈与箭头同色(品牌蓝);前面的细空格(U+2009)保持与星标的间距一致。 if (task.repeatType == com.focusbox.app.model.Repeat.ANYTIME && !task.done) { title.append("\u2009 ") title.setSpan( PinIconSpan(ctx.col(R.color.brand), tvTitle.textSize * 0.85f), title.length - 1, title.length, 0, ) } // 完成横线只画任务名本身(MidStrikeSpan 限定区间,且划线取文字墨迹 // 的垂直中点——系统 StrikethroughSpan 用整字面半高定位,数字/汉字 // 会让线显得偏下)。重要度/紧急度/置顶图标不跟着划线。 // 注意:span 必须在 tvTitle.text = title 之前挂好 if (task.done) { title.setSpan(MidStrikeSpan(), 0, task.title.length, 0) } tvTitle.text = title tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv() 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) btnPlay.tint(ctx.col(R.color.brand)) checkWrap.setOnClickListener { onToggle(task) } itemView.setOnClickListener { onEdit(task) } btnPlay.setOnClickListener { onStart(task) } } private fun buildMeta(row: Row.Item): String { val task = row.task val ctx = itemView.context val parts = ArrayList(4) parts.add(row.projectName) if (task.done) { row.doneRange?.let { parts.add(it) } return parts.joinToString(" · ") } parts.add(ctx.getString(R.string.minutes_value, task.focusMinutes)) if (task.due > 0) { val label = Dates.dueLabel(task.due) parts.add(if (row.overdue) ctx.getString(R.string.task_meta_overdue, label) else label) } // 重要度已改为标题后的竖线图标,meta 里不再重复「高优先」文字 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 } } /** * 任务名删除线:系统 StrikethroughSpan 的划线位置按"字体半高"(baseline + ascent/2) * 计算,但数字/汉字的实际墨迹顶比字体 ascent 低不少,划线视觉上偏下。 * 这里自绘:按实际墨迹边界(getTextBounds)取垂直中点,线严格穿过文字中间。 */ private class MidStrikeSpan : android.text.style.ReplacementSpan() { override fun getSize( paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?, ): Int = paint.measureText(text, start, end).toInt() override fun draw( canvas: android.graphics.Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, baseline: Int, bottom: Int, paint: Paint, ) { // 先按原样式画字(无删除线标志) canvas.drawText(text, start, end, x, baseline.toFloat(), paint) val s = text.toString() val bounds = android.graphics.Rect() paint.getTextBounds(s, start, end, bounds) // 墨迹垂直中点:bounds.top/bottom 相对基线的偏移(top 为负) val lineY = baseline + (bounds.top + bounds.bottom) / 2f val w = paint.measureText(text, start, end) val line = Paint(Paint.ANTI_ALIAS_FLAG) line.color = paint.color line.strokeWidth = (paint.textSize / 28f).coerceAtLeast(1f) canvas.drawLine(x, lineY, x + w, lineY, line) } } /** * 置顶图标:圆圈内向上箭头。 * 自绘而不是用字体字形——常用字体里没有"圆圈+箭头"的组合字符, * 画出来才能精确控制圆圈与箭头同色、且和重要度圆圈数字(0.85 倍字号)等高。 * 颜色用品牌蓝(R.color.brand,深浅色主题各有一版),与之前的 ↑ 字符颜色一致。 */ private class PinIconSpan( private val iconColor: 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 // 与 ①②③ 底部对齐:字体里的圆圈数字底缘略微越过基线(约 -0.05 个图标高), // 圆心取基线上方 0.45 个图标高,比之前 0.50 略下移,底部不再"悬空"显偏上 val cx = x + s / 2f val cy = baseline - s * 0.45f // 圈线与重要度圆圈数字的描边粗细一致(约 1/10 图标高) val ring = Paint(Paint.ANTI_ALIAS_FLAG) ring.color = iconColor ring.style = Paint.Style.STROKE ring.strokeWidth = s / 10f ring.strokeCap = Paint.Cap.ROUND canvas.drawCircle(cx, cy, s / 2f - ring.strokeWidth / 2f, ring) // 箭头比圈线更细(约 1/15 图标高),整体落在圆内 val arrow = Paint(Paint.ANTI_ALIAS_FLAG) arrow.color = iconColor arrow.style = Paint.Style.STROKE arrow.strokeWidth = s / 15f arrow.strokeCap = Paint.Cap.ROUND val head = s * 0.18f val yTop = cy - s * 0.16f val yBot = cy + s * 0.18f canvas.drawLine(cx, yBot, cx, yTop, arrow) canvas.drawLine(cx - head, yTop + head, cx, yTop, arrow) canvas.drawLine(cx + head, yTop + head, cx, yTop, arrow) } }