155 lines
5.5 KiB
Kotlin
155 lines
5.5 KiB
Kotlin
|
|
package com.focusbox.app.ui.tasks
|
||
|
|
|
||
|
|
import android.graphics.Paint
|
||
|
|
import android.graphics.Typeface
|
||
|
|
import android.view.LayoutInflater
|
||
|
|
import android.view.View
|
||
|
|
import android.view.ViewGroup
|
||
|
|
import android.widget.ImageView
|
||
|
|
import android.widget.TextView
|
||
|
|
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,
|
||
|
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||
|
|
|
||
|
|
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()
|
||
|
|
}
|
||
|
|
|
||
|
|
private var rows: List<Row> = emptyList()
|
||
|
|
|
||
|
|
fun submit(list: List<Row>) {
|
||
|
|
rows = list
|
||
|
|
notifyDataSetChanged()
|
||
|
|
}
|
||
|
|
|
||
|
|
override fun getItemViewType(position: Int): Int =
|
||
|
|
if (rows[position] is Row.Footer) TYPE_FOOTER else TYPE_TASK
|
||
|
|
|
||
|
|
override fun getItemCount(): Int = rows.size
|
||
|
|
|
||
|
|
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 = rows[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
|
||
|
|
|
||
|
|
tvTitle.text = task.title
|
||
|
|
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)
|
||
|
|
btnPlay.tint(ctx.col(R.color.text_tertiary))
|
||
|
|
|
||
|
|
checkWrap.setOnClickListener { onToggle(task) }
|
||
|
|
itemView.setOnClickListener { onEdit(task) }
|
||
|
|
btnPlay.setOnClickListener { onStart(task) }
|
||
|
|
}
|
||
|
|
|
||
|
|
private fun buildMeta(row: Row.Item): String {
|
||
|
|
val task = row.task
|
||
|
|
val parts = ArrayList<String>(4)
|
||
|
|
parts.add(row.projectName)
|
||
|
|
if (task.done) {
|
||
|
|
row.doneRange?.let { parts.add(it) }
|
||
|
|
return parts.joinToString(" · ")
|
||
|
|
}
|
||
|
|
parts.add("${task.focusMinutes} 分钟")
|
||
|
|
if (task.due > 0) {
|
||
|
|
val label = Dates.dueLabel(task.due)
|
||
|
|
parts.add(if (row.overdue) "已逾期 · $label" else label)
|
||
|
|
}
|
||
|
|
if (task.priority >= 2) parts.add("高优先")
|
||
|
|
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
|
||
|
|
}
|
||
|
|
}
|