Modern Apps git

← Modern-Apps.git

office: auto-save survives closing a document

Commit
624529a9b546b50ab1d250aff3ec41bcb6680335
Author
Vayun Mathur <[email protected]>
When
2026-11-30 21:58 UTC (5 days ago)
Parents
1e399d3
The auto-save loop was started once at app launch (loadSettings)
but cancelled by clearDocument() — and nothing ever restarted
it. Open document A, edit, go back, open document B in the same
process: B's edits set hasUnsavedChanges, yet no job existed to
ever save them, so killing the app silently lost every later
document's edits while auto-save still showed as enabled.

clearDocument no longer cancels the loop (it is already gated on
hasUnsavedChanges, a harmless no-op with nothing open — exactly
how it behaves before the first document). Disabling via Settings
still cancels it through setAutoSave(false). Regression tests:
loop alive after clearDocument + a fresh document; disabled stays
disabled. Formula conversion, atomic writes, recents and sync
were audited and cleared. Verified: :office:testDevUnitTest 1099
tests, 0 failures; lint errors are pre-existing on untouched
files.

Diff

office/src/main/java/com/vayunmathur/office/platform/OfficeViewModelDocuments.kt
diff --git a/office/src/main/java/com/vayunmathur/office/platform/OfficeViewModelDocuments.kt b/office/src/main/java/com/vayunmathur/office/platform/OfficeViewModelDocuments.ktindex fee1908285..9586dd1156 100644--- a/office/src/main/java/com/vayunmathur/office/platform/OfficeViewModelDocuments.kt+++ b/office/src/main/java/com/vayunmathur/office/platform/OfficeViewModelDocuments.kt@@ -129,7 +129,12 @@ fun OfficeViewModel.clearDocument() {     presenceTickJob?.cancel()     remotePresenceMutable.value = emptyList()     isOnlineMutable.value = false-    autoSaveJob?.cancel()+    // Do NOT cancel autoSaveJob here: the auto-save loop is owned by the+    // settings lifecycle (setAutoSave, started from loadSettings in+    // MainActivity.onCreate), not by the open document. Cancelling it on+    // editor exit left every subsequently opened document without+    // auto-save for the rest of the process. The loop is already gated+    // on hasUnsavedChanges, so it is a no-op while no document is open. }  fun OfficeViewModel.createNewTextDocument() {
office/src/test/java/com/vayunmathur/office/platform/OfficeViewModelCoreSlice3Test.kt
diff --git a/office/src/test/java/com/vayunmathur/office/platform/OfficeViewModelCoreSlice3Test.kt b/office/src/test/java/com/vayunmathur/office/platform/OfficeViewModelCoreSlice3Test.ktindex f4eb826006..6e7978a5cf 100644--- a/office/src/test/java/com/vayunmathur/office/platform/OfficeViewModelCoreSlice3Test.kt+++ b/office/src/test/java/com/vayunmathur/office/platform/OfficeViewModelCoreSlice3Test.kt@@ -114,6 +114,37 @@ class OfficeViewModelCoreSlice3Test : OfficeViewModelSlice3Harness() {         dispatcher.scheduler.runCurrent()     } +    @Test+    fun `clearDocument does not cancel the auto-save loop`() = runTest {+        // Regression: closing a document must not kill auto-save for the+        // next one. loadSettings (MainActivity.onCreate) is the only place+        // that starts the loop, and it is not re-run when a second document+        // is opened in the same process.+        val vm = idleVm()+        vm.setAutoSave(true, 30)+        dispatcher.scheduler.runCurrent()+        assertTrue(vm.autoSaveJob?.isActive == true)+        vm.stateMutable.value = OfficeViewModel.ViewState.Loaded(textDoc("one"))+        vm.clearDocument()+        assertTrue(vm.autoSaveJob?.isActive == true)+        assertTrue(vm.autoSaveEnabled.value)+        // A subsequently opened/created document is covered by the same loop.+        vm.createNewTextDocument()+        assertTrue(vm.autoSaveJob?.isActive == true)+        vm.setAutoSave(false)+        dispatcher.scheduler.runCurrent()+    }++    @Test+    fun `clearDocument keeps auto-save disabled when it was off`() = runTest {+        val vm = idleVm()+        vm.setAutoSave(false)+        vm.clearDocument()+        assertFalse(vm.autoSaveEnabled.value)+        assertTrue(vm.autoSaveJob?.isActive != true)+        dispatcher.scheduler.runCurrent()+    }+     @Test     fun `saveSettings persists and restores`() = runTest {         val vm = idleVm()