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 11:09:43 +08:00
|
|
|
|
// 「随时」任务:标题后带品牌蓝上箭头表示置顶,列表里排在未完成最前面。
|
|
|
|
|
|
// ↑ 字形窄、两侧留白大,全角空格会显得左边距比五角星宽,
|
|
|
|
|
|
// 改用细空格(U+2009)让两侧间隙与星标一致。
|
2026-11-29 09:49:18 +08:00
|
|
|
|
if (task.repeatType == com.focusbox.app.model.Repeat.ANYTIME && !task.done) {
|
2026-11-29 11:09:43 +08:00
|
|
|
|
title.append("\u2009↑")
|
2026-11-29 10:05:14 +08:00
|
|
|
|
title.setSpan(
|
|
|
|
|
|
android.text.style.ForegroundColorSpan(ctx.col(R.color.brand)),
|
|
|
|
|
|
title.length - 2, title.length, 0
|
|
|
|
|
|
)
|
2026-11-29 10:54:00 +08:00
|
|
|
|
title.setSpan(RelativeSizeSpan(0.85f), title.length - 2, title.length, 0)
|
2026-11-29 09:49:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 17:40:00 +08:00
|
|
|
|
tvTitle.text = title
|
2026-11-28 00:25:22 +08:00
|
|
|
|
tvTitle.paintFlags = if (task.done) {
|
|
|
|
|
|
tvTitle.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
|
|
|
|
|
|
} else {
|
|
|
|
|
|
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)
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|