导入 v0.1.0 / v0.2.0 源码:五模块首版、交互重构、重复任务、任务分布

- 源码镜像 src/main(81 个文件):首页双模式、任务、项目、统计、设置
- 补齐 README / LICENSE / CHANGELOG / CONTRIBUTING / .gitignore
- 不含构建配置、签名材料、编译产物与备份数据
This commit is contained in:
XIGE
2026-11-28 00:25:22 +08:00
parent f87c29f232
commit d52deeff5e
85 changed files with 6215 additions and 1 deletions

View File

@@ -0,0 +1,242 @@
package com.focusbox.app.widget
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.focusbox.app.R
import com.focusbox.app.model.DayPoint
import com.focusbox.app.model.ProjectSlice
import com.focusbox.app.util.Dates
import com.focusbox.app.util.col
import com.focusbox.app.util.dpf
import com.focusbox.app.util.spf
import java.util.Calendar
/** 趋势柱状图:支持 7 / 30 天,柱脚标签按密度抽稀,柱子不会挤成一片色块。 */
class BarChartView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : View(context, attrs) {
private data class Bar(val label: String, val value: Int)
private var bars: List<Bar> = emptyList()
private val barPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val gridPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = context.dpf(1f)
color = context.col(R.color.divider)
}
private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.spf(10f)
color = context.col(R.color.text_tertiary)
textAlign = Paint.Align.CENTER
}
private val valuePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.spf(10f)
color = context.col(R.color.text_secondary)
textAlign = Paint.Align.CENTER
}
fun setData(points: List<DayPoint>) {
bars = points.map { Bar(it.label, it.minutes) }
invalidate()
}
override fun onDraw(canvas: Canvas) {
if (bars.isEmpty()) return
val labelH = context.dpf(20f)
val topPad = context.dpf(18f)
val baseY = height - labelH
val usableH = baseY - topPad
val brand = context.col(R.color.brand)
val soft = context.col(R.color.brand_soft)
val peak = (bars.maxOfOrNull { it.value } ?: 0).coerceAtLeast(30)
// 两条参考线:一半与峰顶,给出高度感又不喧宾夺主
gridPaint.color = context.col(R.color.divider)
canvas.drawLine(0f, baseY, width.toFloat(), baseY, gridPaint)
canvas.drawLine(0f, baseY - usableH / 2f, width.toFloat(), baseY - usableH / 2f, gridPaint)
val n = bars.size
val slot = width.toFloat() / n
val barW = (slot * if (n <= 7) 0.42f else 0.62f).coerceAtLeast(context.dpf(2f))
val radius = minOf(barW / 2f, context.dpf(4f))
val lastIndex = n - 1
bars.forEachIndexed { i, bar ->
val cx = slot * i + slot / 2f
val h = (bar.value.toFloat() / peak * usableH).coerceAtLeast(if (bar.value > 0) context.dpf(2f) else 0f)
if (h > 0f) {
barPaint.color = if (i == lastIndex) brand else soft
val r = RectF(cx - barW / 2f, baseY - h, cx + barW / 2f, baseY)
canvas.drawRoundRect(r, radius, radius, barPaint)
}
if (n <= 7) {
if (bar.value > 0) {
valuePaint.color = context.col(R.color.text_secondary)
canvas.drawText("${bar.value}", cx, baseY - h - context.dpf(5f), valuePaint)
}
textPaint.color = context.col(R.color.text_tertiary)
canvas.drawText(bar.label, cx, height - context.dpf(5f), textPaint)
} else if (i % 5 == 0 || i == lastIndex) {
textPaint.color = context.col(R.color.text_tertiary)
canvas.drawText(bar.label, cx, height - context.dpf(5f), textPaint)
}
}
}
}
/** 打卡热力图:7 行(周一…周日)× N 列(周),色阶五档。 */
class HeatmapView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : View(context, attrs) {
private var days: List<DayPoint> = emptyList()
private val cellPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.spf(9f)
color = context.col(R.color.text_tertiary)
textAlign = Paint.Align.RIGHT
}
private var levels = intArrayOf(
context.col(R.color.heat0),
context.col(R.color.heat1),
context.col(R.color.heat2),
context.col(R.color.heat3),
context.col(R.color.heat4),
)
fun setData(points: List<DayPoint>) {
days = points
invalidate()
}
private fun levelOf(minutes: Int): Int = when {
minutes <= 0 -> 0
minutes < 25 -> 1
minutes < 50 -> 2
minutes < 90 -> 3
else -> 4
}
private fun weekdayIndex(ms: Long): Int {
val c = Calendar.getInstance()
c.timeInMillis = ms
val dow = c.get(Calendar.DAY_OF_WEEK)
return if (dow == Calendar.SUNDAY) 6 else dow - 2
}
override fun onDraw(canvas: Canvas) {
if (days.isEmpty()) return
val labelW = context.dpf(16f)
val gap = context.dpf(3f)
val top = context.dpf(2f)
val firstMs = Dates.dayStartOfKey(days.first().key)
val startCol = weekdayIndex(firstMs) // 第一列从周一算起,前面留空
val totalCells = startCol + days.size
val cols = (totalCells + 6) / 7
val cellW = (width - labelW - gap * (cols - 1)) / cols
// 高度也要参与取小:宽屏时按宽度算出的格子会把第 7 行挤出可视区
val cellH = (height - top - gap * 6) / 7f
val cell = minOf(cellW.toFloat(), cellH, context.dpf(15f))
val radius = cell * 0.28f
val rect = RectF()
days.forEachIndexed { i, d ->
val pos = startCol + i
val col = pos / 7
val row = pos % 7
val x = labelW + col * (cell + gap)
val y = top + row * (cell + gap)
cellPaint.color = levels[levelOf(d.minutes)]
rect.set(x, y, x + cell, y + cell)
canvas.drawRoundRect(rect, radius, radius, cellPaint)
}
// 只在周一/三/五标字,避免七行小字糊在一起
val labels = mapOf(0 to "一", 2 to "三", 4 to "五")
labels.forEach { (row, s) ->
val y = top + row * (cell + gap) + cell * 0.78f
canvas.drawText(s, labelW - context.dpf(4f), y, textPaint)
}
}
}
/** 项目占比环:中间显示总时长,环上按项目分色。 */
class DonutView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : View(context, attrs) {
private var slices: List<ProjectSlice> = emptyList()
private val arcPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = context.dpf(16f)
strokeCap = Paint.Cap.BUTT
}
private val trackPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = context.dpf(16f)
color = context.col(R.color.ring_track)
}
private val valuePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.spf(19f)
color = context.col(R.color.title)
textAlign = Paint.Align.CENTER
isFakeBoldText = true
}
private val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.spf(10f)
color = context.col(R.color.text_tertiary)
textAlign = Paint.Align.CENTER
}
fun setSlices(list: List<ProjectSlice>) {
slices = list
invalidate()
}
/** 环中心的说明文字(项目分布 / 任务分布共用这个控件) */
var caption: String = ""
set(value) {
field = value
invalidate()
}
override fun onDraw(canvas: Canvas) {
val r = minOf(width, height) / 2f - arcPaint.strokeWidth / 2f - context.dpf(2f)
val cx = width / 2f
val cy = height / 2f
canvas.drawCircle(cx, cy, r, trackPaint)
val box = RectF(cx - r, cy - r, cx + r, cy + r)
val total = slices.sumOf { it.seconds }
if (total > 0) {
var start = -90f
val gapDeg = if (slices.size > 1) 2.4f else 0f
slices.forEach { s ->
val sweep = s.seconds.toFloat() / total * 360f
arcPaint.color = if (s.color == -1) context.col(R.color.text_tertiary) else s.color
canvas.drawArc(box, start + gapDeg / 2f, (sweep - gapDeg).coerceAtLeast(0.6f), false, arcPaint)
start += sweep
}
}
val centerText = if (total <= 0) "0m" else Dates.shortDuration(total)
canvas.drawText(centerText, cx, cy + context.dpf(2f), valuePaint)
val cap = if (caption.isEmpty()) "总专注" else caption
canvas.drawText(cap, cx, cy + context.dpf(18f), labelPaint)
}
}

View File

@@ -0,0 +1,73 @@
package com.focusbox.app.widget
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.focusbox.app.R
import com.focusbox.app.util.col
import com.focusbox.app.util.dpf
/**
* 计时圆环。
*
* 只用一条弧表达进度:从 -90° 顺时针扫过 progress * 360°。
* 进度为 0 时不画弧——圆头端点会在起点留下一个圆点,看起来像"已经走了 1%"。
*/
class RingView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : View(context, attrs) {
var progress: Float = 0f
set(value) {
field = value.coerceIn(0f, 1f)
invalidate()
}
var ringColor: Int = context.col(R.color.brand)
set(value) {
field = value
invalidate()
}
var trackColor: Int = context.col(R.color.ring_track)
set(value) {
field = value
invalidate()
}
var strokeDp: Float = 12f
set(value) {
field = value
invalidate()
}
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeCap = Paint.Cap.ROUND
}
private val rect = RectF()
override fun onDraw(canvas: Canvas) {
val stroke = context.dpf(strokeDp)
paint.strokeWidth = stroke
val pad = stroke / 2f + context.dpf(2f)
rect.set(pad, pad, width - pad, height - pad)
val radius = minOf(rect.width(), rect.height()) / 2f
paint.color = trackColor
canvas.drawCircle(rect.centerX(), rect.centerY(), radius, paint)
if (progress > 0.002f) {
paint.color = ringColor
val box = RectF(
rect.centerX() - radius, rect.centerY() - radius,
rect.centerX() + radius, rect.centerY() + radius,
)
canvas.drawArc(box, -90f, 360f * progress, false, paint)
}
}
}