234 lines
8.4 KiB
Kotlin
234 lines
8.4 KiB
Kotlin
package com.focusbox.app.ui.stats
|
||
|
||
import android.content.Context
|
||
import android.graphics.Typeface
|
||
import android.util.AttributeSet
|
||
import android.util.TypedValue
|
||
import android.view.Gravity
|
||
import android.widget.ImageView
|
||
import android.widget.LinearLayout
|
||
import android.widget.TextView
|
||
import androidx.core.content.ContextCompat
|
||
import com.focusbox.app.R
|
||
import com.focusbox.app.data.Store
|
||
import com.focusbox.app.util.Dates
|
||
import com.focusbox.app.util.col
|
||
import com.focusbox.app.util.dpi
|
||
import com.focusbox.app.widget.LabelDonutView
|
||
|
||
/**
|
||
* 「项目分布 / 任务分布」卡片:自带 日 / 周 / 月 / 年 四档与左右翻页。
|
||
*
|
||
* 为什么做成独立控件而不是在 Fragment 里堆代码:两张卡片结构完全一样,
|
||
* 只有数据源不同(按项目 / 按任务)。做成控件后各自持有自己的区间状态,
|
||
* 互不影响——看"本周的项目分布"和"这一年的任务分布"可以同时成立。
|
||
*/
|
||
class PeriodDistributionCard @JvmOverloads constructor(
|
||
context: Context,
|
||
attrs: AttributeSet? = null,
|
||
) : LinearLayout(context, attrs) {
|
||
|
||
enum class Scale(val labelRes: Int) {
|
||
DAY(R.string.scale_day),
|
||
WEEK(R.string.scale_week),
|
||
MONTH(R.string.scale_month),
|
||
YEAR(R.string.scale_year),
|
||
}
|
||
|
||
private val tvTitle = TextView(context)
|
||
private val segItems = ArrayList<TextView>(4)
|
||
private val tvPeriod = TextView(context)
|
||
private val btnPrev = ImageView(context)
|
||
private val btnNext = ImageView(context)
|
||
private val donut = LabelDonutView(context)
|
||
|
||
/** 当前粒度,默认按天 */
|
||
var scale = Scale.DAY
|
||
private set
|
||
|
||
/** 锚点:落在目标周期内的任意时刻,实际区间由它推导 */
|
||
private var anchor = Dates.todayStart()
|
||
|
||
/** true = 按任务汇总,false = 按项目汇总 */
|
||
var byTask = false
|
||
set(value) {
|
||
field = value
|
||
refresh()
|
||
}
|
||
|
||
var cardTitle: String = ""
|
||
set(value) {
|
||
field = value
|
||
tvTitle.text = value
|
||
}
|
||
|
||
init {
|
||
orientation = VERTICAL
|
||
background = ContextCompat.getDrawable(context, R.drawable.bg_card)
|
||
setPadding(context.dpi(16f), context.dpi(15f), context.dpi(16f), context.dpi(10f))
|
||
build()
|
||
refresh()
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 结构
|
||
|
||
private fun build() {
|
||
tvTitle.apply {
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
|
||
setTextColor(context.col(R.color.title))
|
||
typeface = Typeface.DEFAULT_BOLD
|
||
includeFontPadding = false
|
||
}
|
||
addView(tvTitle)
|
||
|
||
val seg = LinearLayout(context).apply {
|
||
orientation = HORIZONTAL
|
||
background = ContextCompat.getDrawable(context, R.drawable.bg_segment_track)
|
||
setPadding(context.dpi(3f), context.dpi(3f), context.dpi(3f), context.dpi(3f))
|
||
}
|
||
addView(seg, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply {
|
||
topMargin = context.dpi(13f)
|
||
})
|
||
|
||
Scale.entries.forEach { s ->
|
||
val tv = TextView(context).apply {
|
||
text = context.getString(s.labelRes)
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
|
||
gravity = Gravity.CENTER
|
||
setPadding(0, context.dpi(8f), 0, context.dpi(8f))
|
||
isClickable = true
|
||
setOnClickListener { switchScale(s) }
|
||
}
|
||
segItems.add(tv)
|
||
seg.addView(tv, LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f))
|
||
}
|
||
|
||
// 翻页行:左右箭头 + 居中周期文字(参考图的排布)
|
||
val periodRow = LinearLayout(context).apply {
|
||
orientation = HORIZONTAL
|
||
gravity = Gravity.CENTER_VERTICAL
|
||
}
|
||
addView(periodRow, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply {
|
||
topMargin = context.dpi(4f)
|
||
})
|
||
|
||
btnPrev.apply {
|
||
setImageResource(R.drawable.ic_chevron_right)
|
||
rotation = 180f
|
||
setColorFilter(context.col(R.color.text_secondary))
|
||
scaleType = ImageView.ScaleType.FIT_CENTER
|
||
setPadding(context.dpi(8f), context.dpi(8f), context.dpi(8f), context.dpi(8f))
|
||
isClickable = true
|
||
setOnClickListener { shift(-1) }
|
||
}
|
||
periodRow.addView(btnPrev, LayoutParams(context.dpi(36f), context.dpi(40f)))
|
||
|
||
tvPeriod.apply {
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
|
||
setTextColor(context.col(R.color.text))
|
||
typeface = Typeface.DEFAULT_BOLD
|
||
gravity = Gravity.CENTER
|
||
includeFontPadding = false
|
||
}
|
||
periodRow.addView(tvPeriod, LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f))
|
||
|
||
btnNext.apply {
|
||
setImageResource(R.drawable.ic_chevron_right)
|
||
setColorFilter(context.col(R.color.text_secondary))
|
||
scaleType = ImageView.ScaleType.FIT_CENTER
|
||
setPadding(context.dpi(8f), context.dpi(8f), context.dpi(8f), context.dpi(8f))
|
||
isClickable = true
|
||
setOnClickListener { shift(1) }
|
||
}
|
||
periodRow.addView(btnNext, LayoutParams(context.dpi(36f), context.dpi(40f)))
|
||
|
||
addView(donut, LayoutParams(LayoutParams.MATCH_PARENT, context.dpi(222f)).apply {
|
||
topMargin = context.dpi(2f)
|
||
})
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 交互
|
||
|
||
private fun switchScale(s: Scale) {
|
||
if (scale == s) return
|
||
scale = s
|
||
// 换粒度回到"当前",否则从 2024 年的日视图切到年视图会落在 2024 年,容易困惑
|
||
anchor = Dates.todayStart()
|
||
refresh()
|
||
}
|
||
|
||
private fun shift(delta: Int) {
|
||
if (delta > 0 && !canNext()) return
|
||
val s = start()
|
||
anchor = when (scale) {
|
||
Scale.DAY -> Dates.addDays(s, delta)
|
||
Scale.WEEK -> Dates.addDays(s, 7 * delta)
|
||
Scale.MONTH -> Dates.addMonths(s, delta)
|
||
Scale.YEAR -> Dates.addYears(s, delta)
|
||
}
|
||
refresh()
|
||
}
|
||
|
||
private fun periodStart(ms: Long): Long = when (scale) {
|
||
Scale.DAY -> Dates.dayStart(ms)
|
||
Scale.WEEK -> Dates.weekStart(ms)
|
||
Scale.MONTH -> Dates.monthStart(ms)
|
||
Scale.YEAR -> Dates.yearStart(ms)
|
||
}
|
||
|
||
private fun start(): Long = periodStart(anchor)
|
||
|
||
private fun end(s: Long): Long = when (scale) {
|
||
Scale.DAY -> Dates.addDays(s, 1)
|
||
Scale.WEEK -> Dates.addDays(s, 7)
|
||
Scale.MONTH -> Dates.addMonths(s, 1)
|
||
Scale.YEAR -> Dates.addYears(s, 1)
|
||
}
|
||
|
||
/** 未来没有记录,往后翻到当前周期为止 */
|
||
private fun canNext(): Boolean = start() < periodStart(Dates.todayStart())
|
||
|
||
// ------------------------------------------------------------------ 渲染
|
||
|
||
fun refresh() {
|
||
val from = start()
|
||
val to = end(from)
|
||
|
||
val raw = if (byTask) Store.taskSlices(from, to) else Store.distribution(from, to)
|
||
donut.setSlices(
|
||
raw.map {
|
||
LabelDonutView.Slice(
|
||
name = it.name,
|
||
seconds = it.seconds,
|
||
color = if (it.color == -1) context.col(R.color.text_tertiary) else it.color,
|
||
)
|
||
}
|
||
)
|
||
donut.centerText = Dates.durationBrief(raw.sumOf { it.seconds })
|
||
|
||
tvPeriod.text = periodLabel(from, to)
|
||
applySegments()
|
||
btnNext.alpha = if (canNext()) 1f else 0.2f
|
||
}
|
||
|
||
private fun periodLabel(from: Long, to: Long): String = when (scale) {
|
||
Scale.DAY -> Dates.mdLong(from)
|
||
Scale.WEEK -> context.getString(R.string.period_week_range, Dates.md(from), Dates.md(to - 1))
|
||
Scale.MONTH -> context.getString(R.string.period_month, Dates.yearOf(from), Dates.monthOf(from))
|
||
Scale.YEAR -> context.getString(R.string.period_year, Dates.yearOf(from))
|
||
}
|
||
|
||
private fun applySegments() {
|
||
val onText = context.col(R.color.title)
|
||
val offText = context.col(R.color.text_secondary)
|
||
val onBg = ContextCompat.getDrawable(context, R.drawable.bg_segment_on)
|
||
Scale.entries.forEachIndexed { i, s ->
|
||
val tv = segItems[i]
|
||
val selected = s == scale
|
||
tv.background = if (selected) onBg else null
|
||
tv.setTextColor(if (selected) onText else offText)
|
||
tv.typeface = if (selected) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
|
||
}
|
||
}
|
||
}
|