Modern Apps git

← Modern-Apps.git

office: push edits over WebSocket (live like presence) + poll fallback; hide toolbar Save for online docs

Commit
f3b77d216e93f899bdfd1b1c16aa2d9a055bc346
Author
Vayun Mathur <[email protected]>
When
2026-07-06 15:49 UTC (5 months ago)
Parents
ae759f4

Diff

office/src/main/java/com/vayunmathur/office/MainActivity.kt
diff --git a/office/src/main/java/com/vayunmathur/office/MainActivity.kt b/office/src/main/java/com/vayunmathur/office/MainActivity.ktindex b5cf47a4c9..c2e91f2954 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -645,8 +645,10 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity:                             if (canEdit) IconButton(onClick = { showSearch = !showSearch }) { Icon(painterResource(com.vayunmathur.library.R.drawable.outline_search_24), contentDescription = "Search") }                             IconButton(onClick = { viewModel.undo() }, enabled = canUndo) { Icon(painterResource(com.vayunmathur.library.R.drawable.undo_24px), contentDescription = "Undo") }                             IconButton(onClick = { viewModel.redo() }, enabled = canRedo) { Icon(painterResource(R.drawable.redo_24px), contentDescription = "Redo") }-                            IconButton(onClick = { if (viewModel.needsSaveAs()) saveAsLauncher.launch(saveAsName) else viewModel.save() }, enabled = hasUnsavedChanges && !isSaving) {-                                if (isSaving) CircularProgressIndicator(Modifier.size(20.dp)) else Icon(painterResource(com.vayunmathur.library.R.drawable.save_24px), contentDescription = "Save")+                            if (!isOnline) {+                                IconButton(onClick = { if (viewModel.needsSaveAs()) saveAsLauncher.launch(saveAsName) else viewModel.save() }, enabled = hasUnsavedChanges && !isSaving) {+                                    if (isSaving) CircularProgressIndicator(Modifier.size(20.dp)) else Icon(painterResource(com.vayunmathur.library.R.drawable.save_24px), contentDescription = "Save")+                                }                             }                         }                     )
office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.ktindex 1736b00362..8156133631 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt@@ -195,6 +195,20 @@ object OfficeSync {         runCatching { wsSession?.send(Frame.Text(json.encodeToString(PresenceMsg("presence", channel, data)))) }     } +    /**+     * Pushes encrypted action blobs over the WebSocket (same live path as presence) so peers get them+     * instantly. The server both stores and fans them out. Returns false if the socket isn't connected+     * (caller should fall back to [appendDocActions] over HTTP).+     */+    suspend fun liveAppend(channel: String, key: ByteArray, items: List<String>): Boolean {+        val session = wsSession ?: return false+        val blobs = items.map { Base64.encode(E2ee.aesEncrypt(key, it.encodeToByteArray())) }+        return runCatching {+            session.send(Frame.Text(json.encodeToString(AppendMsg("append", channel, blobs))))+            true+        }.getOrDefault(false)+    }+     /** Parses a raw live message. */     fun parseLive(raw: String): LiveMsg? = runCatching { json.decodeFromString<LiveMsg>(raw) }.getOrNull() @@ -260,6 +274,7 @@ object OfficeSync {      @Serializable private data class SubMsg(val t: String, val channel: String)     @Serializable private data class PresenceMsg(val t: String, val channel: String, val data: String)+    @Serializable private data class AppendMsg(val t: String, val channel: String, val actions: List<String>)      /** A live server message: `t` = "actions" (with [actions]+[seq]) or "presence" (with [data]). */     @Serializable data class LiveMsg(
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 b3c9b82c97..0b08a99fb7 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -14,6 +14,7 @@ import com.vayunmathur.office.odf.* import com.vayunmathur.library.ui.odf.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job+import kotlinx.coroutines.isActive import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -297,6 +298,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)         currentCrdt = null         currentCharMode = false         OfficeSync.stopLive()+        livePollJob?.cancel()         _remotePresence.value = emptyList()         _isOnline.value = false         autoSaveJob?.cancel()@@ -1949,6 +1951,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)     val isOnline: StateFlow<Boolean> = _isOnline.asStateFlow()     private var applyingRemote = false     private var livePushJob: Job? = null+    private var livePollJob: Job? = null     private var localCaret = 0     private var caretPresenceJob: Job? = null @@ -1998,6 +2001,15 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)             onConnected = { runCatching { syncDoc(docId, key) } }, // catch up on (re)connect             onMessage = { raw -> handleLive(raw, docId, key) },         )+        // Fallback poll: guarantees remote edits arrive even if the server doesn't push them over+        // the WebSocket. syncDoc is a no-op push when there are no local changes, then pulls+merges.+        livePollJob?.cancel()+        livePollJob = viewModelScope.launch(Dispatchers.IO) {+            while (isActive) {+                kotlinx.coroutines.delay(1200)+                runCatching { syncDoc(docId, key) }+            }+        }     }      /** Verifies an incoming signed op (author signature + editor/owner role) and applies it. */@@ -2147,7 +2159,10 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)             val opsJson = syncJson.encodeToString(localOps)             val sig = Base64.encode(OfficeSync.sign(opsJson.encodeToByteArray()))             val signed = syncJson.encodeToString(SignedOp(OfficeSync.deviceId, sig, opsJson))-            OfficeSync.appendDocActions(docId, key, listOf(signed))+            // Prefer the live WebSocket (peers get it instantly, like presence); HTTP is the fallback.+            if (!OfficeSync.liveAppend(docId, key, listOf(signed))) {+                OfficeSync.appendDocActions(docId, key, listOf(signed))+            }         }         // Pull from the OLD cursor so remote ops (and our just-pushed, idempotent ops) are merged.         val pulled = OfficeSync.pullDocActions(docId, key, cursor)