v0.3.0:今日筛选含逾期与今日完成、统计今日区间、重复任务自动滚动、通知自定义布局
This commit is contained in:
@@ -13,6 +13,8 @@ class App : Application() {
|
||||
super.onCreate()
|
||||
Prefs.init(this)
|
||||
Store.init(this)
|
||||
// 重复任务按间隔滚到当前周期:冷启动就补齐,避免过期后不再出现在清单里
|
||||
runCatching { Store.rollRepeatingTasks() }
|
||||
Notif.createChannels(this)
|
||||
applyTheme()
|
||||
TimerCore.init(this)
|
||||
|
||||
@@ -294,13 +294,8 @@ object Store {
|
||||
|
||||
private fun createNextOccurrence(t: Task) {
|
||||
val base = if (t.due > 0) t.due else Dates.todayStart()
|
||||
val n = t.repeatEvery.coerceAtLeast(1)
|
||||
val next = when (t.repeatType) {
|
||||
Repeat.DAY -> Dates.addDays(base, n)
|
||||
Repeat.WEEK -> Dates.addDays(base, n * 7)
|
||||
Repeat.MONTH -> Dates.addMonths(base, n)
|
||||
else -> return
|
||||
}
|
||||
val next = shiftDue(base, t.repeatType, t.repeatEvery)
|
||||
if (next == base) return
|
||||
// 下一次的截止日至少比今天晚一天,否则刚生成就会显示"已逾期"
|
||||
val due = if (next <= Dates.todayStart()) Dates.addDays(Dates.todayStart(), 1) else next
|
||||
addTask(
|
||||
@@ -315,6 +310,50 @@ object Store {
|
||||
)
|
||||
}
|
||||
|
||||
/** 把日期按重复规则往后推一个周期 */
|
||||
private fun shiftDue(from: Long, type: Int, every: Int): Long {
|
||||
val n = every.coerceAtLeast(1)
|
||||
return when (type) {
|
||||
Repeat.DAY -> Dates.addDays(from, n)
|
||||
Repeat.WEEK -> Dates.addDays(from, n * 7)
|
||||
Repeat.MONTH -> Dates.addMonths(from, n)
|
||||
else -> from
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重复任务按间隔自动滚到当前周期。
|
||||
*
|
||||
* 之前只有"勾选完成"才会生成下一条,用户连续几天没打开任务页时,
|
||||
* 这条重复任务就一直停在过期日期上,清单里看不到新的待办。
|
||||
* 这里在启动与回前台时把过期日期推进到不早于今天的位置,
|
||||
* 保持"每个周期有一条待办"的语义,也避免同一系列堆积多条。
|
||||
*
|
||||
* 幂等:日期已不早于今天的任务不会被改动,重复调用无副作用。
|
||||
*/
|
||||
fun rollRepeatingTasks(): Int {
|
||||
val today = Dates.todayStart()
|
||||
var changed = 0
|
||||
tasks(includeDone = false).forEach { t ->
|
||||
if (!t.repeating || t.due <= 0 || t.due >= today) return@forEach
|
||||
var due = t.due
|
||||
var guard = 0
|
||||
while (due < today && guard < 500) {
|
||||
due = shiftDue(due, t.repeatType, t.repeatEvery)
|
||||
guard++
|
||||
}
|
||||
if (due <= t.due) return@forEach
|
||||
db.update(
|
||||
"tasks",
|
||||
ContentValues().apply { put("due", due) },
|
||||
"_id = ?",
|
||||
arrayOf(t.id.toString()),
|
||||
)
|
||||
changed++
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
fun deleteTask(id: Long) {
|
||||
db.delete("tasks", "_id = ?", arrayOf(id.toString()))
|
||||
db.update("sessions", ContentValues().apply { put("task_id", 0) }, "task_id = ?", arrayOf(id.toString()))
|
||||
@@ -385,8 +424,37 @@ object Store {
|
||||
return out
|
||||
}
|
||||
|
||||
fun distribution(fromMs: Long): List<ProjectSlice> {
|
||||
val list = ArrayList<ProjectSlice>()
|
||||
/**
|
||||
* 某一天的 24 小时分布。
|
||||
* 统计页选"今日"时,按天的柱子只有一根、看不出节奏,
|
||||
* 换成小时粒度才能回答"今天是早上干的还是晚上干的"。
|
||||
*/
|
||||
fun hourlySeries(dayStart: Long): List<DayPoint> {
|
||||
val minutes = IntArray(24)
|
||||
val pomos = IntArray(24)
|
||||
db.rawQuery(
|
||||
"""
|
||||
SELECT CAST(strftime('%H', start_at / 1000, 'unixepoch', 'localtime') AS INTEGER) AS h,
|
||||
IFNULL(SUM(seconds),0), IFNULL(SUM(completed),0)
|
||||
FROM sessions WHERE start_at >= ? AND start_at < ?
|
||||
GROUP BY h
|
||||
""".trimIndent(),
|
||||
arrayOf(dayStart.toString(), Dates.addDays(dayStart, 1).toString())
|
||||
).use { c ->
|
||||
while (c.moveToNext()) {
|
||||
val h = c.getInt(0).coerceIn(0, 23)
|
||||
minutes[h] = c.getInt(1) / 60
|
||||
pomos[h] = c.getInt(2)
|
||||
}
|
||||
}
|
||||
val out = ArrayList<DayPoint>(24)
|
||||
for (h in 0 until 24) {
|
||||
out.add(DayPoint(key = "%02d".format(h), label = "$h", minutes = minutes[h], pomodoros = pomos[h]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
fun distribution(fromMs: Long): List<ProjectSlice> { val list = ArrayList<ProjectSlice>()
|
||||
db.rawQuery(
|
||||
"""
|
||||
SELECT COALESCE(p._id, 0), COALESCE(p.name, '未归类'), COALESCE(p.color, -1),
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.focusbox.app.R
|
||||
@@ -82,6 +83,9 @@ object Notif {
|
||||
val taskName = if (TimerCore.taskId > 0) {
|
||||
com.focusbox.app.data.Store.task(TimerCore.taskId)?.title
|
||||
} else null
|
||||
// 没有选任务时给一个中性占位,避免第一行留空白
|
||||
val headline = taskName?.takeIf { it.isNotBlank() }
|
||||
?: context.getString(R.string.notif_no_task)
|
||||
|
||||
val b = NotificationCompat.Builder(context, CHANNEL_TIMER)
|
||||
// 透明小图标:状态栏与通知左侧不再出现应用图标
|
||||
@@ -92,46 +96,85 @@ object Notif {
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setSilent(true)
|
||||
// 系统模板的"应用名 + 时间"行不需要,时间由自定义布局里的 Chronometer 承担
|
||||
.setShowWhen(false)
|
||||
// 模板兜底:ROM 若不支持自定义布局,至少还有任务名与读数
|
||||
.setContentTitle(headline)
|
||||
.setContentText(Dates.clock(TimerCore.displayNow()))
|
||||
|
||||
if (running) {
|
||||
b.setUsesChronometer(true)
|
||||
if (countUp) {
|
||||
// 正计时:系统计时器从"已用时"往前推起点,界面上就是一直往上走
|
||||
b.setWhen(System.currentTimeMillis() - TimerCore.displayNow())
|
||||
b.setChronometerCountDown(false)
|
||||
} else {
|
||||
// 倒计时:渲染到结束时刻,系统自己一秒一秒减
|
||||
b.setWhen(TimerCore.endAtMs())
|
||||
b.setChronometerCountDown(true)
|
||||
}
|
||||
// 不再出现"专注中/休息中"字样,标题只留任务名(无任务则空白)
|
||||
b.setContentTitle(taskName ?: " ")
|
||||
} else {
|
||||
b.setShowWhen(false)
|
||||
b.setContentTitle(Dates.clock(TimerCore.displayNow()))
|
||||
b.setContentText(taskName ?: "")
|
||||
}
|
||||
val rv = RemoteViews(context.packageName, R.layout.notif_timer)
|
||||
rv.setTextViewText(R.id.tvNotifTask, headline)
|
||||
bindChronometer(rv, running, countUp)
|
||||
rv.setOnClickPendingIntent(
|
||||
R.id.notifPause,
|
||||
serviceAction(
|
||||
context,
|
||||
if (running) TimerService.ACTION_PAUSE else TimerService.ACTION_RESUME,
|
||||
11,
|
||||
),
|
||||
)
|
||||
rv.setImageViewResource(
|
||||
R.id.notifPause,
|
||||
if (running) R.drawable.ic_pause else R.drawable.ic_play,
|
||||
)
|
||||
rv.setOnClickPendingIntent(
|
||||
R.id.notifStop,
|
||||
serviceAction(context, TimerService.ACTION_STOP, 13),
|
||||
)
|
||||
b.setCustomContentView(rv)
|
||||
b.setCustomBigContentView(rv)
|
||||
|
||||
val toggleIcon = if (running) R.drawable.ic_pause else R.drawable.ic_play
|
||||
// 模板动作保留作兜底(自定义布局生效时系统不会重复绘制这两颗按钮)
|
||||
b.addAction(
|
||||
NotificationCompat.Action(
|
||||
toggleIcon, "",
|
||||
if (running) R.drawable.ic_pause else R.drawable.ic_play, "",
|
||||
serviceAction(
|
||||
context,
|
||||
if (running) TimerService.ACTION_PAUSE else TimerService.ACTION_RESUME,
|
||||
11,
|
||||
21,
|
||||
),
|
||||
)
|
||||
)
|
||||
b.addAction(
|
||||
NotificationCompat.Action(
|
||||
R.drawable.ic_stop, "",
|
||||
serviceAction(context, TimerService.ACTION_STOP, 13),
|
||||
serviceAction(context, TimerService.ACTION_STOP, 23),
|
||||
)
|
||||
)
|
||||
return b.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 把计时读数交给系统 Chronometer。
|
||||
*
|
||||
* 倒计时:base = 结束时刻,配合 countDown 显示"还剩多少";
|
||||
* 正计时:base 往前推已用时长,显示"已经过了多久";
|
||||
* 暂停时 started=false,Chronometer 停在按下暂停那一刻的值。
|
||||
*/
|
||||
private fun bindChronometer(rv: RemoteViews, running: Boolean, countUp: Boolean) {
|
||||
val now = System.currentTimeMillis()
|
||||
val base: Long
|
||||
val countDown: Boolean
|
||||
when {
|
||||
countUp -> {
|
||||
base = now - TimerCore.displayNow()
|
||||
countDown = false
|
||||
}
|
||||
running -> {
|
||||
base = TimerCore.endAtMs()
|
||||
countDown = true
|
||||
}
|
||||
else -> {
|
||||
base = now + TimerCore.remainingNow()
|
||||
countDown = true
|
||||
}
|
||||
}
|
||||
rv.setChronometer(R.id.notifChrono, base, null, running)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
rv.setChronometerCountDown(R.id.notifChrono, countDown)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateOngoing(context: Context) {
|
||||
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
|
||||
runCatching {
|
||||
|
||||
@@ -85,6 +85,13 @@ class MainActivity : AppCompatActivity() {
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
// 进程常驻时跨天不会重走 App.onCreate,回到前台再滚一次重复任务。
|
||||
// 放在 super 之前:片段 onResume 里的 reload 读到的就是推进后的日期。
|
||||
runCatching { com.focusbox.app.data.Store.rollRepeatingTasks() }
|
||||
super.onResume()
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
val tab = intent?.getIntExtra(EXTRA_TAB, -1) ?: -1
|
||||
|
||||
@@ -28,8 +28,9 @@ import java.util.Calendar
|
||||
/**
|
||||
* 统计页。
|
||||
*
|
||||
* 三层信息:今天(我要不要现在开始)→ 本周趋势(这周还行不行)→ 累计与项目分布
|
||||
* (时间到底花在哪)。三个区间的切换只影响趋势与分布,不改变"今日"卡片。
|
||||
* 三层信息:今天(我要不要现在开始)→ 区间趋势(这周/这月还行不行)→ 累计与分布
|
||||
* (时间到底花在哪)。四个区间(今日 / 本周 / 本月 / 全部)切换只影响趋势与两处分布,
|
||||
* 不改变最上方的"今日"卡片——它永远是今天。
|
||||
*/
|
||||
class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
|
||||
@@ -50,10 +51,12 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
private lateinit var donutTask: DonutView
|
||||
private lateinit var taskLegend: LinearLayout
|
||||
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
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@@ -75,13 +78,15 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
donutTask = view.findViewById(R.id.donutTask)
|
||||
taskLegend = view.findViewById(R.id.taskLegend)
|
||||
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)
|
||||
|
||||
segWeek.setOnClickListener { switchRange(0) }
|
||||
segMonth.setOnClickListener { switchRange(1) }
|
||||
segAll.setOnClickListener { switchRange(2) }
|
||||
segToday.setOnClickListener { switchRange(0) }
|
||||
segWeek.setOnClickListener { switchRange(1) }
|
||||
segMonth.setOnClickListener { switchRange(2) }
|
||||
segAll.setOnClickListener { switchRange(3) }
|
||||
|
||||
buildHeatLegend()
|
||||
reload()
|
||||
@@ -118,7 +123,7 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
val ctx = requireContext()
|
||||
val onText = ctx.col(R.color.title)
|
||||
val offText = ctx.col(R.color.text_secondary)
|
||||
listOf(segWeek, segMonth, segAll).forEachIndexed { i, tv ->
|
||||
listOf(segToday, segWeek, segMonth, segAll).forEachIndexed { i, tv ->
|
||||
val selected = i == range
|
||||
tv.background = if (selected) {
|
||||
ContextCompat.getDrawable(ctx, R.drawable.bg_segment_on)
|
||||
@@ -143,20 +148,35 @@ class StatsFragment : Fragment(R.layout.fragment_stats) {
|
||||
}
|
||||
|
||||
private fun rangeStart(): Long = when (range) {
|
||||
0 -> Dates.weekStart()
|
||||
1 -> Store.monthStart()
|
||||
0 -> Dates.todayStart()
|
||||
1 -> Dates.weekStart()
|
||||
2 -> Store.monthStart()
|
||||
else -> 0L
|
||||
}
|
||||
|
||||
private fun renderTrend() {
|
||||
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) {
|
||||
0 -> {
|
||||
1 -> {
|
||||
from = Dates.weekStart()
|
||||
days = (((Dates.todayStart() - from) / 86_400_000L).toInt() + 1).coerceIn(1, 7)
|
||||
}
|
||||
1 -> {
|
||||
2 -> {
|
||||
from = Store.monthStart()
|
||||
val c = Calendar.getInstance()
|
||||
days = c.get(Calendar.DAY_OF_MONTH).coerceIn(1, 31)
|
||||
|
||||
@@ -117,9 +117,17 @@ class TasksFragment : Fragment(R.layout.fragment_tasks) {
|
||||
ViewFilter.TODAY -> {
|
||||
val from = Dates.todayStart()
|
||||
val to = Dates.addDays(from, 1)
|
||||
val today = pending.filter { it.due >= from && it.due < to }
|
||||
today.forEach { rows.add(toRow(it, spans)) }
|
||||
tvCount.text = getString(R.string.task_count_today, today.size)
|
||||
// 今日 = 截止日不晚于今天且未完成(含逾期,逾期本就该今天处理)
|
||||
// + 今天勾选完成的任务
|
||||
val dueToday = pending.filter { it.due > 0 && it.due < to }
|
||||
val doneToday = done.filter { it.doneAt >= from && it.doneAt < to }
|
||||
dueToday.forEach { rows.add(toRow(it, spans)) }
|
||||
doneToday.forEach { rows.add(toRow(it, spans)) }
|
||||
tvCount.text = if (doneToday.isEmpty()) {
|
||||
getString(R.string.task_count_today, dueToday.size)
|
||||
} else {
|
||||
getString(R.string.task_count_today_mixed, dueToday.size, doneToday.size)
|
||||
}
|
||||
tvEmpty.setText(R.string.empty_today)
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,12 @@ class BarChartView @JvmOverloads constructor(
|
||||
|
||||
private var bars: List<Bar> = emptyList()
|
||||
|
||||
/**
|
||||
* 横轴标签密度:0 = 自动(7 根以内全标,超过则每 5 根标一次);
|
||||
* 大于 0 表示每 N 根标一次。24 小时的今日视图用 3,避免标签糊成一片。
|
||||
*/
|
||||
var labelStep: Int = 0
|
||||
|
||||
private val barPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
private val gridPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.STROKE
|
||||
@@ -68,6 +74,9 @@ class BarChartView @JvmOverloads constructor(
|
||||
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
|
||||
// 标签密度:显式设置优先,否则 <=7 根全标、多根每 5 根标一次
|
||||
val step = if (labelStep > 0) labelStep else if (n <= 7) 1 else 5
|
||||
val denseValue = labelStep == 0 && n <= 7
|
||||
|
||||
bars.forEachIndexed { i, bar ->
|
||||
val cx = slot * i + slot / 2f
|
||||
@@ -78,16 +87,13 @@ class BarChartView @JvmOverloads constructor(
|
||||
canvas.drawRoundRect(r, radius, radius, barPaint)
|
||||
}
|
||||
|
||||
if (n <= 7) {
|
||||
if (bar.value > 0) {
|
||||
if (i % step == 0 || i == lastIndex) {
|
||||
if (denseValue && 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user