休息纯计时解耦滑动;重复任务按周期自动生成;统计数字去空格

This commit is contained in:
XIGE
2026-11-28 13:25:28 +08:00
parent ef2fb3a955
commit c3e0c9d67a
11 changed files with 147 additions and 102 deletions

View File

@@ -199,11 +199,12 @@ object Store {
sort = c.getInt(11),
repeatType = c.getInt(12),
repeatEvery = c.getInt(13),
originId = c.getLong(14),
)
private val taskColumns =
"_id, title, note, project_id, priority, focus_min, done_pomodoros, due, done, done_at, " +
"created_at, sort, repeat_type, repeat_every"
"created_at, sort, repeat_type, repeat_every, origin_id"
fun tasks(projectId: Long = 0L, includeDone: Boolean = true): List<Task> {
val where = StringBuilder("1 = 1")
@@ -275,67 +276,44 @@ object Store {
}
/**
* 勾选完成时,重复任务会立刻生成下一周期的一条新任务。
* 这里只做"生成",不改动当前任务的统计归属。
* 勾选完成只标记当前这一次,不再立刻生成下一条。
* 新实例由 [rollRepeatingTasks] 按周期(跨零点)自动创建,
* 因此原因备注只留在已完成的那一条上,不会污染后续周期。
*/
fun setTaskDone(id: Long, done: Boolean) {
if (!done) {
val t = task(id)
db.update(
"tasks",
ContentValues().apply {
put("done", 0)
put("done_at", 0)
},
"_id = ?", arrayOf(id.toString())
)
// 撤销重复任务时,上次勾选自动生成的新一条要一起撤掉,否则清单会多出一条
if (t != null && t.repeating) {
db.delete(
"tasks",
"origin_id = ? AND done = 0 AND _id != ?",
arrayOf(id.toString(), id.toString())
)
}
return
}
val t = task(id) ?: return
if (t.done) return
if (done == t.done) return
val now = System.currentTimeMillis()
db.update(
"tasks",
ContentValues().apply {
put("done", 1)
put("done_at", now)
put("done", if (done) 1 else 0)
put("done_at", if (done) now else 0L)
},
"_id = ?", arrayOf(id.toString())
)
if (t.repeating) createNextOccurrence(t)
}
private fun createNextOccurrence(t: Task) {
val base = if (t.due > 0) t.due else Dates.todayStart()
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
val childId = addTask(
title = t.title,
note = t.note,
projectId = t.projectId,
priority = t.priority,
focusMinutes = t.focusMinutes,
/** 为重复系列生成一条全新实例:备注永远从空开始 */
private fun createSeriesInstance(rootId: Long, source: Task, due: Long): Long {
val id = addTask(
title = source.title,
note = "",
projectId = source.projectId,
priority = source.priority,
focusMinutes = source.focusMinutes,
due = due,
repeatType = t.repeatType,
repeatEvery = t.repeatEvery,
repeatType = source.repeatType,
repeatEvery = source.repeatEvery,
)
if (childId > 0) {
if (id > 0) {
db.update(
"tasks",
ContentValues().apply { put("origin_id", t.id) },
"_id = ?", arrayOf(childId.toString())
ContentValues().apply { put("origin_id", rootId) },
"_id = ?", arrayOf(id.toString())
)
}
return id
}
/** 把日期按重复规则往后推一个周期 */
@@ -350,47 +328,44 @@ object Store {
}
/**
* 重复任务按间隔自动滚到当前周期。
* 重复任务按周期自动生成新实例。
*
* 之前只有"勾选完成"才会生成下一条,用户连续几天没打开任务页时,
* 这条重复任务就一直停在过期日期上,清单里看不到新的待办。
* 这里在启动与回前台时把过期日期推进到不早于今天的位置,
* 保持"每个周期有一条待办"的语义,也避免同一系列堆积多条。
* 每个系列(根任务 origin_id=0,后续实例 origin_id=根 id)只保留"最新一条"作为节奏来源:
* 当它的截止日已经过去,就创建一条全新的当日/本周期任务,旧任务原样留在列表里,
* 这样完成原因只写在已完成的那一条上,循环任务本身不会越记越脏。
*
* 幂等:日期已不早于今天的任务不会被改动,重复调用无副作用。
* 启动、回前台、以及前台每 60 秒都会调用;跨零点后第一次调用即可生成当日任务。
* 幂等:最新一条的截止日不早于今天时不做任何事,重复调用无副作用。
*/
fun rollRepeatingTasks(): Int {
val today = Dates.todayStart()
val all = ArrayList<Task>()
db.rawQuery(
"SELECT $taskColumns FROM tasks WHERE repeat_type > 0 AND repeat_every > 0",
null,
).use { c ->
while (c.moveToNext()) all.add(cursorToTask(c))
}
var changed = 0
tasks(includeDone = false).forEach { t ->
if (!t.repeating) return@forEach
// 重复任务没设截止日时没有节奏可循,给它一个今天作为起点,
// 否则它既不在「今日」里,也永远不会滚动
if (t.due <= 0) {
db.update(
"tasks",
ContentValues().apply { put("due", today) },
"_id = ?",
arrayOf(t.id.toString()),
)
changed++
return@forEach
val roots = all.map { if (it.originId > 0) it.originId else it.id }.distinct()
for (root in roots) {
val series = all.filter { if (it.originId > 0) it.originId == root else it.id == root }
val latest = series.maxWith(compareBy<Task> { it.due }.thenBy { it.id })
if (latest.due >= today) continue
val due = if (latest.due <= 0) {
// 没设截止日的重复任务没有节奏可循,直接用今天作为新实例起点
today
} else {
var d = latest.due
var guard = 0
while (d < today && guard < 500) {
d = shiftDue(d, latest.repeatType, latest.repeatEvery)
guard++
}
d
}
if (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++
if (due <= latest.due) continue
if (createSeriesInstance(root, latest, due) > 0) changed++
}
return changed
}