Modern Apps git

← Modern-Apps.git

office: fold pending local edits into CRDT before applying echoed/remote ops (fixes self-resurrecting deletes)

Commit
a1eedfd7addc0bc6dcf2c1127a2a011ba9dca2a2
Author
Vayun Mathur <[email protected]>
When
2026-07-06 16:07 UTC (5 months ago)
Parents
79fec96

Diff

office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.ktindex 6c0514ab86..e664fc9aa1 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -2037,7 +2037,17 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)                 syncMutex.withLock {                     val crdt = currentCrdt ?: return@withLock                     val startVersion = editVersion-                    var changed = false+                    val localCells = currentDocCells()+                    // Fold pending local edits into the CRDT FIRST (and push them), so an incoming or+                    // echoed op can't render a stale state and resurrect a char we just deleted.+                    val localOps = crdt.update(localCells)+                    if (localOps.isNotEmpty() && OfficeRoles.canEdit(currentRole)) {+                        val opsJson = syncJson.encodeToString(localOps)+                        val sig = Base64.encode(OfficeSync.sign(opsJson.encodeToByteArray()))+                        val signed = syncJson.encodeToString(SignedOp(OfficeSync.deviceId, sig, opsJson))+                        if (!OfficeSync.liveAppend(docId, key, listOf(signed))) OfficeSync.appendDocActions(docId, key, listOf(signed))+                    }+                    var changed = localOps.isNotEmpty()                     for (blob in msg.actions) {                         val plain = OfficeSync.decrypt(key, blob) ?: continue                         if (applySignedOp(crdt, plain)) changed = true@@ -2047,7 +2057,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)                         ds.setLong("crdtCursor:$docId", msg.seq.toLong())                         saveCrdt(ds, docId, crdt)                         val merged = crdt.render()-                        if (merged != currentDocCells()) {+                        if (merged != localCells) {                             val doc = rebuildDoc(merged)                             if (doc != null) withContext(Dispatchers.Main) {                                 // Don't clobber a keystroke the user made while we were merging.