v0.3.0:今日筛选含逾期与今日完成、统计今日区间、重复任务自动滚动、通知自定义布局

This commit is contained in:
XIGE
2026-11-28 01:07:24 +08:00
parent 7c8dc3d0de
commit 3e237fafdb
11 changed files with 301 additions and 56 deletions

View File

@@ -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),