v3.20 重复任务不再复活:周期设置写穿系列 + 启动自检修复僵局

This commit is contained in:
XIGE
2026-12-02 18:13:08 +08:00
parent e0beddaaf5
commit 787b5db81e
3 changed files with 156 additions and 1 deletions

View File

@@ -604,6 +604,61 @@ object Store {
arrayOf(task.projectId, old.projectId, root, root, root),
)
}
// 重复设置是「系列级」属性,不是单行属性。
// 列表里看到的那一行通常只是本周期的实例,只改这一行的话根任务还是「每天」,
// 下次启动 [rollRepeatingTasks] 照旧按根再滚一条出来 ——
// 表现就是「改成不重复、手点完成,重启后修改前的重复任务又回来」的死循环。
// 因此这里把新的周期设置写穿整条系列;已完成的历史行不动,避免污染旧记录。
if (old != null &&
(old.repeatType != task.repeatType || old.repeatEvery != task.repeatEvery) &&
!(old.done && old.repeatType == Repeat.NONE && old.originId > 0)
) {
val rootId = seriesRootId(task.id)
val ids = seriesIds(rootId)
if (ids.isNotEmpty()) {
val args = ArrayList<String>(ids.size + 3)
args.add(task.repeatType.toString())
args.add(task.repeatEvery.coerceAtLeast(1).toString())
ids.forEach { args.add(it.toString()) }
args.add(rootId.toString())
db.execSQL(
"UPDATE tasks SET repeat_type = ?, repeat_every = ? " +
"WHERE _id IN (" + ids.joinToString(",") { "?" } + ") " +
"AND (done = 0 OR _id = ?)",
args.toTypedArray(),
)
}
}
}
/** 沿 origin 链向上找系列根;上游行已不存在(旧版本物理删过)时以当前行为根 */
private fun seriesRootId(id: Long): Long {
var cur = id
var guard = 0
while (guard++ < 16) {
val t = task(cur) ?: return id
if (t.originId <= 0L) return cur
if (task(t.originId) == null) return cur
cur = t.originId
}
return cur
}
/** 系列内全部行 id(根 + 各级实例 + 完成记录副本) */
private fun seriesIds(rootId: Long): List<Long> {
val ids = ArrayList<Long>()
db.rawQuery(
"""
WITH RECURSIVE series(_id) AS (
SELECT _id FROM tasks WHERE _id = ?
UNION
SELECT t._id FROM tasks t JOIN series s ON t.origin_id = s._id
)
SELECT _id FROM series
""".trimIndent(),
arrayOf(rootId.toString()),
).use { c -> while (c.moveToNext()) ids.add(c.getLong(0)) }
return ids
}
/**
@@ -795,6 +850,69 @@ object Store {
db.delete("tasks", "_id IN ($marks)", args)
}
/**
* 修复「重复设置只改了某一行」留下的僵局(v3.20)。
*
* 老版本的编辑只写当前这一行:用户把某一轮实例改成「不重复」,根任务还是「每天」,
* 于是每次启动都再滚出一条新实例,怎么都结束不了。
* 这里以系列里最新的那条普通行为准(完成记录副本不表达节奏),把根的周期类型对齐;
* 根不再是周期类型后,[rollRepeatingTasks] 自然不再滚动。
*
* 同时把系列里其余的待办行收尾:否则根或旧实例会以「逾期未完成」的身份留在列表里,
* 看起来还是没结束。用户自己改的那一条(latest)保持原样。
*/
private fun repairSeriesRepeat() {
val roots = ArrayList<Long>()
db.rawQuery(
"SELECT _id FROM tasks WHERE origin_id = 0 AND deleted = 0 " +
"AND repeat_type IN (1, 2, 3)",
null,
).use { c -> while (c.moveToNext()) roots.add(c.getLong(0)) }
if (roots.isEmpty()) return
val now = System.currentTimeMillis()
for (root in roots) {
val plain = ArrayList<Task>(4)
db.rawQuery(
"SELECT $taskColumns FROM tasks WHERE deleted = 0 " +
"AND (_id = ? OR origin_id = ?)",
arrayOf(root.toString(), root.toString()),
).use { c ->
while (c.moveToNext()) {
val t = cursorToTask(c)
// 完成记录副本(一次专注一条)不表达周期节奏,跳过
if (t.done && t.originId > 0L && t.repeatType == Repeat.NONE) continue
plain.add(t)
}
}
val latest = plain.maxWithOrNull(compareBy<Task>({ it.due }, { it.id })) ?: continue
if (latest.repeatType in Repeat.DAY..Repeat.MONTH) continue
db.update(
"tasks",
ContentValues().apply {
put("repeat_type", latest.repeatType)
put("repeat_every", latest.repeatEvery.coerceAtLeast(1))
},
"_id = ?", arrayOf(root.toString()),
)
for (t in plain) {
if (t.done || t.id == latest.id) continue
db.update(
"tasks",
ContentValues().apply {
put("done", 1)
put("done_at", now)
put(
"note",
if (t.note.isBlank()) AUTO_DONE_NOTE
else t.note.trimEnd() + "\n" + AUTO_DONE_NOTE,
)
},
"_id = ?", arrayOf(t.id.toString()),
)
}
}
}
/**
* 重复任务按周期自动生成新实例。
*
@@ -811,6 +929,8 @@ object Store {
// 先清掉"系列根已被删除、只剩实例"的残渣,再谈滚动。
// 不清的话这些实例会一直挂在任务列表里,表现为"删掉的重复任务还在"。
pruneDanglingInstances()
// 再收掉"用户已把某一轮改成不重复、根还在按周期滚"的僵局
repairSeriesRepeat()
val all = ArrayList<Task>()
db.rawQuery(
// 只滚真正的周期重复(1-3);「随时」(4) 与不重复 (0) 不参与

View File

@@ -737,6 +737,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
companion object {
const val VERSION = "3.19"
const val VERSION = "3.20"
}
}