统计页重构:去掉顶部区间筛选,分布卡新增日/周/月/年与翻页,环形图改为引线标注并显示时长
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
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 })
|
||||
donut.centerCaption =
|
||||
context.getString(if (byTask) R.string.task_share else R.string.total_focus)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,31 @@
|
||||
package com.focusbox.app.ui.stats
|
||||
|
||||
import android.graphics.Typeface
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.focusbox.app.R
|
||||
import com.focusbox.app.data.Store
|
||||
import com.focusbox.app.model.ProjectSlice
|
||||
import com.focusbox.app.util.Dates
|
||||
import com.focusbox.app.util.Prefs
|
||||
import com.focusbox.app.util.circleBg
|
||||
import com.focusbox.app.util.col
|
||||
import com.focusbox.app.util.dpi
|
||||
import com.focusbox.app.util.roundBg
|
||||
import com.focusbox.app.util.visible
|
||||
import com.focusbox.app.widget.BarChartView
|
||||
import com.focusbox.app.widget.DonutView
|
||||
import com.focusbox.app.widget.HeatmapView
|
||||
import com.focusbox.app.widget.RingView
|
||||
import java.util.Calendar
|
||||
|
||||
/**
|
||||
* 统计页。
|
||||
*
|
||||
* 三层信息:今天(我要不要现在开始)→ 区间趋势(这周/这月还行不行)→ 累计与分布
|
||||
* (时间到底花在哪)。四个区间(今日 / 本周 / 本月 / 全部)切换只影响趋势与两处分布,
|
||||
* 不改变最上方的"今日"卡片——它永远是今天。
|
||||
* 三层信息:今天(我要不要现在开始)→ 近一周趋势与打卡 → 时间到底花在哪。
|
||||
*
|
||||
* 顶部的区间筛选已经去掉:趋势固定看最近 7 天,两个分布卡各自带
|
||||
* 日 / 周 / 月 / 年 与左右翻页([PeriodDistributionCard]),
|
||||
* 想同时比较"本周的项目分布"和"今年的任务分布"也不会互相牵制。
|
||||
*/
|
||||
class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
|
||||
@@ -46,18 +41,12 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
private lateinit var heatmap: HeatmapView
|
||||
private lateinit var tvHeatHint: TextView
|
||||
private lateinit var heatLegend: LinearLayout
|
||||
private lateinit var donut: DonutView
|
||||
private lateinit var donutLegend: LinearLayout
|
||||
private lateinit var donutTask: DonutView
|
||||
private lateinit var taskLegend: LinearLayout
|
||||
private lateinit var cardProject: PeriodDistributionCard
|
||||
private lateinit var cardTask: PeriodDistributionCard
|
||||
private lateinit var summaryGrid: LinearLayout
|
||||
private lateinit var segToday: TextView
|
||||
private lateinit var segWeek: TextView
|
||||
private lateinit var segMonth: TextView
|
||||
private lateinit var segAll: TextView
|
||||
|
||||
/** 0 今日 / 1 本周 / 2 本月 / 3 全部 */
|
||||
private var range = 0
|
||||
/** 趋势固定显示最近 7 天(含今天) */
|
||||
private val trendDays = 7
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
@@ -73,20 +62,14 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
heatmap = view.findViewById(R.id.heatmap)
|
||||
tvHeatHint = view.findViewById(R.id.tvHeatHint)
|
||||
heatLegend = view.findViewById(R.id.heatLegend)
|
||||
donut = view.findViewById(R.id.donut)
|
||||
donutLegend = view.findViewById(R.id.donutLegend)
|
||||
donutTask = view.findViewById(R.id.donutTask)
|
||||
taskLegend = view.findViewById(R.id.taskLegend)
|
||||
cardProject = view.findViewById(R.id.cardProject)
|
||||
cardTask = view.findViewById(R.id.cardTask)
|
||||
summaryGrid = view.findViewById(R.id.summaryGrid)
|
||||
segToday = view.findViewById(R.id.segToday)
|
||||
segWeek = view.findViewById(R.id.segWeek)
|
||||
segMonth = view.findViewById(R.id.segMonth)
|
||||
segAll = view.findViewById(R.id.segAll)
|
||||
|
||||
segToday.setOnClickListener { switchRange(0) }
|
||||
segWeek.setOnClickListener { switchRange(1) }
|
||||
segMonth.setOnClickListener { switchRange(2) }
|
||||
segAll.setOnClickListener { switchRange(3) }
|
||||
cardProject.cardTitle = getString(R.string.distribution_title)
|
||||
cardProject.byTask = false
|
||||
cardTask.cardTitle = getString(R.string.task_distribution_title)
|
||||
cardTask.byTask = true
|
||||
|
||||
buildHeatLegend()
|
||||
reload()
|
||||
@@ -102,37 +85,17 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
if (!hidden && view != null) reload()
|
||||
}
|
||||
|
||||
private fun switchRange(r: Int) {
|
||||
if (range == r) return
|
||||
range = r
|
||||
reload()
|
||||
}
|
||||
|
||||
fun reload() {
|
||||
if (view == null || !isAdded) return
|
||||
applySegments()
|
||||
renderToday()
|
||||
renderTrend()
|
||||
renderHeatmap()
|
||||
renderDistribution()
|
||||
renderTaskDistribution()
|
||||
// 两张分布卡各自持有区间状态,回到页面只要重新取数
|
||||
cardProject.refresh()
|
||||
cardTask.refresh()
|
||||
renderSummary()
|
||||
}
|
||||
|
||||
private fun applySegments() {
|
||||
val ctx = requireContext()
|
||||
val onText = ctx.col(R.color.title)
|
||||
val offText = ctx.col(R.color.text_secondary)
|
||||
listOf(segToday, segWeek, segMonth, segAll).forEachIndexed { i, tv ->
|
||||
val selected = i == range
|
||||
tv.background = if (selected) {
|
||||
ContextCompat.getDrawable(ctx, R.drawable.bg_segment_on)
|
||||
} else null
|
||||
tv.setTextColor(if (selected) onText else offText)
|
||||
tv.typeface = if (selected) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
private fun renderToday() {
|
||||
val goal = Prefs.dailyGoalMinutes
|
||||
val stat = Store.todayStat(goal)
|
||||
@@ -147,46 +110,10 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
tvStreak.text = "连续打卡 ${streak} 天 · 目标 ${goal} 分钟"
|
||||
}
|
||||
|
||||
private fun rangeStart(): Long = when (range) {
|
||||
0 -> Dates.todayStart()
|
||||
1 -> Dates.weekStart()
|
||||
2 -> Store.monthStart()
|
||||
else -> 0L
|
||||
}
|
||||
|
||||
private fun renderTrend() {
|
||||
val from = Dates.addDays(Dates.todayStart(), -(trendDays - 1))
|
||||
val series = Store.dailySeries(from, trendDays)
|
||||
barChart.labelStep = 0
|
||||
if (range == 0) {
|
||||
// 今日:按小时看节奏,24 根柱子,标签每 6 小时一次
|
||||
val series = Store.hourlySeries(Dates.todayStart())
|
||||
barChart.labelStep = 6
|
||||
barChart.setData(series)
|
||||
val totalMinutes = series.sumOf { it.minutes }
|
||||
tvTrendTotal.text = if (totalMinutes <= 0) {
|
||||
getString(R.string.no_data)
|
||||
} else {
|
||||
"共 ${Dates.duration(totalMinutes * 60)}"
|
||||
}
|
||||
return
|
||||
}
|
||||
val from: Long
|
||||
val days: Int
|
||||
when (range) {
|
||||
1 -> {
|
||||
from = Dates.weekStart()
|
||||
days = (((Dates.todayStart() - from) / 86_400_000L).toInt() + 1).coerceIn(1, 7)
|
||||
}
|
||||
2 -> {
|
||||
from = Store.monthStart()
|
||||
val c = Calendar.getInstance()
|
||||
days = c.get(Calendar.DAY_OF_MONTH).coerceIn(1, 31)
|
||||
}
|
||||
else -> {
|
||||
from = Dates.addDays(Dates.todayStart(), -29)
|
||||
days = 30
|
||||
}
|
||||
}
|
||||
val series = Store.dailySeries(from, days)
|
||||
barChart.setData(series)
|
||||
val totalMinutes = series.sumOf { it.minutes }
|
||||
tvTrendTotal.text = if (totalMinutes <= 0) {
|
||||
@@ -234,78 +161,6 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
}
|
||||
|
||||
private fun renderDistribution() {
|
||||
val slices = Store.distribution(rangeStart())
|
||||
donut.caption = getString(R.string.total_focus)
|
||||
donut.setSlices(slices)
|
||||
fillLegend(donutLegend, slices, R.string.more_projects)
|
||||
}
|
||||
|
||||
/** 任务分布:同样一张占比环,数据换成按任务分组 */
|
||||
private fun renderTaskDistribution() {
|
||||
val slices = Store.taskSlices(rangeStart())
|
||||
donutTask.caption = getString(R.string.task_share)
|
||||
donutTask.setSlices(slices)
|
||||
fillLegend(taskLegend, slices, R.string.more_tasks)
|
||||
}
|
||||
|
||||
private fun fillLegend(container: LinearLayout, slices: List<ProjectSlice>, moreRes: Int) {
|
||||
container.removeAllViews()
|
||||
if (slices.isEmpty()) {
|
||||
container.addView(TextView(requireContext()).apply {
|
||||
text = getString(R.string.no_data)
|
||||
textSize = 13f
|
||||
setTextColor(col(R.color.text_tertiary))
|
||||
})
|
||||
return
|
||||
}
|
||||
val total = slices.sumOf { it.seconds }.coerceAtLeast(1)
|
||||
slices.take(5).forEach { s -> container.addView(legendRow(s, total)) }
|
||||
if (slices.size > 5) {
|
||||
container.addView(TextView(requireContext()).apply {
|
||||
text = getString(moreRes, slices.size - 5)
|
||||
textSize = 11f
|
||||
setTextColor(col(R.color.text_tertiary))
|
||||
setPadding(0, dpi(4f), 0, 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun legendRow(slice: ProjectSlice, total: Int): LinearLayout {
|
||||
val ctx = requireContext()
|
||||
val row = LinearLayout(ctx).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
setPadding(0, dpi(4f), 0, dpi(4f))
|
||||
}
|
||||
val dot = View(ctx)
|
||||
val dlp = LinearLayout.LayoutParams(dpi(8f), dpi(8f))
|
||||
dlp.marginEnd = dpi(7f)
|
||||
dot.layoutParams = dlp
|
||||
dot.background = circleBg(if (slice.color == -1) ctx.col(R.color.text_tertiary) else slice.color)
|
||||
|
||||
val name = TextView(ctx).apply {
|
||||
text = slice.name
|
||||
textSize = 12.5f
|
||||
setTextColor(col(R.color.text))
|
||||
maxLines = 1
|
||||
ellipsize = android.text.TextUtils.TruncateAt.END
|
||||
}
|
||||
val nameLp = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
|
||||
name.layoutParams = nameLp
|
||||
|
||||
val pct = (slice.seconds * 100 / total).coerceAtLeast(1)
|
||||
val value = TextView(ctx).apply {
|
||||
text = "$pct%"
|
||||
textSize = 12f
|
||||
setTextColor(col(R.color.text_secondary))
|
||||
}
|
||||
row.addView(dot)
|
||||
row.addView(name)
|
||||
row.addView(value)
|
||||
return row
|
||||
}
|
||||
|
||||
private fun renderSummary() {
|
||||
val totals = Store.totals()
|
||||
summaryGrid.removeAllViews()
|
||||
|
||||
Reference in New Issue
Block a user