todo: restore subtasks when undoing a delete
- Commit
6fc8b24d4613548a90ecec1946b965d955dcf112- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:40 UTC (28 days ago)
- Parents
40088b7
Bug: deleting a task with subtasks soft-deleted the parent and, via the tombstone cascade, every subtask โ but the undo path only resurrected the parent. TodoViewModel.restore upserted just the listed rows with deleted=false, and TodoRepository.restore touched only its single row, while the DAO subtask queries all filter deleted=0, so the tombstoned subtasks were unreachable and permanently lost from the UI on undo. Fix: TodoRepository.restore now also restores the tombstoned subtasks via a new getSubtasksIncludingDeleted DAO query, touching only rows still marked deleted so live subtasks keep their timestamps; TodoViewModel.restore routes through repository.restore so every undo call site (swipe, bulk, editor) gets the cascade. No schema change โ a query addition only. Known edge, accepted: a subtask deleted on its own before its parent is resurrected too, since rows carry no deletion timestamps to distinguish the cases. Verified with :todo:testDevUnitTest (new tombstonedSubtasksVisibleForRestore regression test in TodoDaoTest green; full module suite green).
Diff
todo/src/main/java/com/vayunmathur/todo/data/TodoDatabase.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/data/TodoDatabase.kt b/todo/src/main/java/com/vayunmathur/todo/data/TodoDatabase.ktindex b0ab998e09..b8f494cf17 100644--- a/todo/src/main/java/com/vayunmathur/todo/data/TodoDatabase.kt+++ b/todo/src/main/java/com/vayunmathur/todo/data/TodoDatabase.kt@@ -41,6 +41,10 @@ abstract class TodoDao { @Query("SELECT * FROM Todo WHERE parentSyncId = :parentSyncId AND deleted = 0 ORDER BY position") abstract fun getSubtasksFlow(parentSyncId: String): Flow<List<Todo>> + /** Subtasks tombstones included: what [TodoRepository.restore] needs to undo a delete. */+ @Query("SELECT * FROM Todo WHERE parentSyncId = :parentSyncId ORDER BY position")+ abstract suspend fun getSubtasksIncludingDeleted(parentSyncId: String): List<Todo>+ @Query("SELECT * FROM Todo WHERE updatedAt > :since") abstract suspend fun getUpdatedSince(since: Long): List<Todo>
todo/src/main/java/com/vayunmathur/todo/data/TodoRepository.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/data/TodoRepository.kt b/todo/src/main/java/com/vayunmathur/todo/data/TodoRepository.ktindex 83b03c4825..f73a358126 100644--- a/todo/src/main/java/com/vayunmathur/todo/data/TodoRepository.kt+++ b/todo/src/main/java/com/vayunmathur/todo/data/TodoRepository.kt@@ -58,8 +58,19 @@ class TodoRepository private constructor( dao.upsertAll(dao.getSubtasks(value.syncId).map { it.copy(deleted = true) }) } + /**+ * Undoes [tombstone]: restores the task and the subtasks the delete cascaded to.+ * Only rows still marked deleted are touched, so live subtasks keep their timestamps.+ * (A subtask deleted on its own before its parent was deleted comes back too โ the+ * rows carry no deletion timestamps to tell the two cases apart.)+ */ suspend fun restore(value: Todo) = inTransaction { dao.upsert(value.copy(deleted = false))+ dao.upsertAll(+ dao.getSubtasksIncludingDeleted(value.syncId)+ .filter { it.deleted }+ .map { it.copy(deleted = false) },+ ) } /**
todo/src/main/java/com/vayunmathur/todo/platform/TodoViewModel.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/platform/TodoViewModel.kt b/todo/src/main/java/com/vayunmathur/todo/platform/TodoViewModel.ktindex b630865659..15a62a7b1d 100644--- a/todo/src/main/java/com/vayunmathur/todo/platform/TodoViewModel.kt+++ b/todo/src/main/java/com/vayunmathur/todo/platform/TodoViewModel.kt@@ -221,7 +221,7 @@ class TodoViewModel( } fun restore(todos: List<Todo>) {- viewModelScope.launch(io) { repository.upsertAll(todos.map { it.copy(deleted = false) }) }+ viewModelScope.launch(io) { todos.forEach { repository.restore(it) } } } fun upsertAll(todos: List<Todo>) {
todo/src/test/java/com/vayunmathur/todo/data/TodoDaoTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/data/TodoDaoTest.kt b/todo/src/test/java/com/vayunmathur/todo/data/TodoDaoTest.ktindex 9b4c3a483d..392d8af384 100644--- a/todo/src/test/java/com/vayunmathur/todo/data/TodoDaoTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/data/TodoDaoTest.kt@@ -49,6 +49,24 @@ class TodoDaoTest { assertEquals(listOf("in"), dao.getInList("L").map { it.syncId }) assertEquals(2.0, dao.maxPosition()) }+ @Test fun tombstonedSubtasksVisibleForRestore() = runTest {+ dao.upsert(Todo(syncId = "p", title = "P"))+ dao.upsert(Todo(syncId = "s", title = "S", parentSyncId = "p"))+ // Mirror TodoRepository.tombstone: parent plus subtasks are soft-deleted together.+ dao.upsert(dao.getBySyncId("p")!!.copy(deleted = true))+ dao.upsertAll(dao.getSubtasks("p").map { it.copy(deleted = true) })+ assertTrue(dao.getSubtasks("p").isEmpty())+ // The restore path needs the tombstoned subtasks back; the live query hides them.+ assertEquals(listOf("s"), dao.getSubtasksIncludingDeleted("p").map { it.syncId })+ // Mirror TodoRepository.restore: only still-deleted rows are touched.+ dao.upsert(dao.getBySyncId("p")!!.copy(deleted = false))+ dao.upsertAll(+ dao.getSubtasksIncludingDeleted("p")+ .filter { it.deleted }+ .map { it.copy(deleted = false) },+ )+ assertEquals(listOf("s"), dao.getSubtasks("p").map { it.syncId })+ } @Test fun updatedSinceAndCounts() = runTest { dao.upsertRaw(Todo(syncId = "x", title = "X", updatedAt = 5000L)) dao.upsertRaw(Todo(syncId = "y", title = "Y", updatedAt = 1000L))