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, ) } // 完成横线只画任务名本身(StrikethroughSpan 限定区间), // 后面的重要度/紧急度/置顶图标不跟着划线。 // 注意:span 必须在 tvTitle.text = title 之前挂好 if (task.done) { title.setSpan( android.text.style.StrikethroughSpan(), 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 } } /** * 置顶图标:圆圈内向上箭头。 * 自绘而不是用字体字形——常用字体里没有"圆圈+箭头"的组合字符, * 画出来才能精确控制圆圈与箭头同色、且和重要度圆圈数字(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 val cx = x + s / 2f // 与 ①②③ 字形对齐:圆圈数字贴基线站立,圆心约在基线上方 0.5 个图标高 val cy = baseline - s * 0.5f // 不能用 apply { this.color = ... }:作用域里 color 会命中 Paint 自己的 // color 属性(默认黑色),把外面的品牌蓝遮掉——就是那版"图标变黑"的原因 val p = Paint(Paint.ANTI_ALIAS_FLAG) p.color = iconColor p.style = Paint.Style.STROKE p.strokeWidth = s / 8f p.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) } }