Modern Apps git

← Modern-Apps.git

office: faster live sync push, hide Save for online docs, back returns to home, mark shared docs online

Commit
e2f6026f50dc29548b6205ebff7137ec45cf6ba4
Author
Vayun Mathur <[email protected]>
When
2026-07-06 15:23 UTC (5 months ago)
Parents
84aacf9

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 136e9bb6a3..e21ce61e37 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -431,6 +431,7 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity:      val isEditMode by viewModel.isEditMode.collectAsState()     val hasUnsavedChanges by viewModel.hasUnsavedChanges.collectAsState()+    val isOnline by viewModel.isOnline.collectAsState()     val isSaving by viewModel.isSaving.collectAsState()     val canUndo by viewModel.canUndo.collectAsState()     val canRedo by viewModel.canRedo.collectAsState()@@ -549,7 +550,11 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity:         }     } -    BackHandler(enabled = hasUnsavedChanges) { showUnsavedDialog = true }+    // Always intercept back inside a document so it returns to the home screen instead of exiting+    // the app. Online docs sync live (nothing to save); offline docs with edits prompt to save.+    BackHandler(enabled = true) {+        if (!isOnline && hasUnsavedChanges) showUnsavedDialog = true else onBack()+    }      ModalNavigationDrawer(         drawerState = drawerState,@@ -611,8 +616,12 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity:                             Box {                                 TextButton(onClick = { fileMenu = true }) { Text("File") }                                 DropdownMenu(expanded = fileMenu, onDismissRequest = { fileMenu = false }) {-                                    DropdownMenuItem(text = { Text("Save") }, enabled = hasUnsavedChanges, leadingIcon = { Icon(painterResource(com.vayunmathur.library.R.drawable.save_24px), null) }, onClick = { fileMenu = false; if (viewModel.needsSaveAs()) saveAsLauncher.launch(saveAsName) else viewModel.save() })-                                    DropdownMenuItem(text = { Text("Save As…") }, onClick = { fileMenu = false; saveAsLauncher.launch(saveAsName) })+                                    if (!isOnline) {+                                        DropdownMenuItem(text = { Text("Save") }, enabled = hasUnsavedChanges, leadingIcon = { Icon(painterResource(com.vayunmathur.library.R.drawable.save_24px), null) }, onClick = { fileMenu = false; if (viewModel.needsSaveAs()) saveAsLauncher.launch(saveAsName) else viewModel.save() })+                                        DropdownMenuItem(text = { Text("Save As…") }, onClick = { fileMenu = false; saveAsLauncher.launch(saveAsName) })+                                    } else {+                                        DropdownMenuItem(text = { Text("Synced to cloud") }, enabled = false, leadingIcon = { Icon(painterResource(com.vayunmathur.library.R.drawable.save_24px), null) }, onClick = {})+                                    }                                     DropdownMenuItem(text = { Text("Share online…") }, leadingIcon = { Icon(painterResource(com.vayunmathur.library.R.drawable.share_24px), null) }, onClick = { fileMenu = false; showShareDialog = true })                                     DropdownMenuItem(text = { Text(stringResource(R.string.print_doc)) }, onClick = { fileMenu = false; printDocument(activity, document) })                                     DropdownMenuItem(text = { Text("Export to PDF…") }, leadingIcon = { Icon(painterResource(com.vayunmathur.library.R.drawable.outline_file_download_24), null) }, onClick = { fileMenu = false; printDocument(activity, document) })
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 58b395b725..b9fba92dc8 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -245,6 +245,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)         currentDocKey = onlineDocKey         currentCrdt = null         currentCharMode = false+        _isOnline.value = onlineDocId != null         if (onlineDocId == null) {             // Offline document: you own your own local file and may edit it freely.             currentRole = OfficeRoles.OWNER@@ -297,6 +298,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)         currentCharMode = false         OfficeSync.stopLive()         _remotePresence.value = emptyList()+        _isOnline.value = false         autoSaveJob?.cancel()     } @@ -306,7 +308,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)      fun createNewTextDocument() {         currentDocId = null; currentDocKey = null; currentCrdt = null; currentCharMode = false-        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear()+        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear(); _isOnline.value = false         undoStack.clear(); redoStack.clear()         _canUndo.value = false; _canRedo.value = false         val doc = OdfDocument.TextDocument(@@ -321,7 +323,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)      fun createNewSpreadsheet() {         currentDocId = null; currentDocKey = null; currentCrdt = null; currentCharMode = false-        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear()+        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear(); _isOnline.value = false         undoStack.clear(); redoStack.clear()         _canUndo.value = false; _canRedo.value = false         val rows = (0 until 10).map { OdfRow(List(5) { OdfCell(text = "") }) }@@ -337,7 +339,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)      fun createNewPresentation() {         currentDocId = null; currentDocKey = null; currentCrdt = null; currentCharMode = false-        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear()+        currentRole = OfficeRoles.OWNER; currentOwnerKey = null; currentMembers.clear(); _isOnline.value = false         undoStack.clear(); redoStack.clear()         _canUndo.value = false; _canRedo.value = false         val doc = OdfDocument.Presentation(@@ -1941,6 +1943,10 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)     private val _remotePresence = MutableStateFlow<List<OfficePresence>>(emptyList())     /** Other people currently in the open document (name + typing), for the presence indicator. */     val remotePresence: StateFlow<List<OfficePresence>> = _remotePresence.asStateFlow()++    private val _isOnline = MutableStateFlow(false)+    /** True when the open document is an online (cloud-synced) document — hides Save, changes back nav. */+    val isOnline: StateFlow<Boolean> = _isOnline.asStateFlow()     private var applyingRemote = false     private var livePushJob: Job? = null     private var localCaret = 0@@ -1979,7 +1985,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)         }         livePushJob?.cancel()         livePushJob = viewModelScope.launch(Dispatchers.IO) {-            delay(400)+            delay(150)             runCatching { OfficeSync.init(getApplication()); syncDoc(docId, key) }         }     }@@ -2186,6 +2192,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)                         currentDocId = docId                         currentDocKey = key                         currentCharMode = charMode+                        _isOnline.value = true                         val ownerKeyB64 = Base64.encode(OfficeSync.publicBundle)                         syncDoc(docId, key)                         indexMutex.withLock {