Modern Apps git

← Modern-Apps.git

office: report specific share failure reason (unregistered recipient, not owner, network)

Commit
abb8efe51c371003cce7d588de6a3ff8d5ac0f97
Author
Vayun Mathur <[email protected]>
When
2026-07-05 18:25 UTC (5 months ago)
Parents
9d8059b

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 91dd7d32fc..136e9bb6a3 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -232,7 +232,7 @@ fun ShareOnlineDialog(     isOwner: Boolean,     myRole: String,     members: List<com.vayunmathur.office.util.OfficeMember>,-    onShare: (String, String, (Boolean) -> Unit) -> Unit,+    onShare: (String, String, (String?) -> Unit) -> Unit,     onSetRole: (String, String) -> Unit,     onComputeCode: (String, (String?) -> Unit) -> Unit,     onDismiss: () -> Unit@@ -318,10 +318,10 @@ fun ShareOnlineDialog(                 enabled = recipient.isNotBlank() && !sharing,                 onClick = {                     sharing = true; status = null-                    onShare(recipient.trim(), addRole) { ok ->+                    onShare(recipient.trim(), addRole) { err ->                         sharing = false-                        status = if (ok) "Added ✓" else "Couldn't share — check your connection and the server."-                        if (ok) recipient = ""+                        status = err ?: "Added ✓"+                        if (err == null) recipient = ""                     }                 }             ) { Text(if (sharing) "Adding…" else "Add") }@@ -880,9 +880,9 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity:             myRole = viewModel.currentDocRole(),             members = members,             onShare = { recipientId, role, cb ->-                viewModel.shareCurrentDocument(recipientId, role) { ok ->-                    if (ok) viewModel.documentMembers { members = it } // refresh roster after sharing-                    cb(ok)+                viewModel.shareCurrentDocument(recipientId, role) { err ->+                    if (err == null) viewModel.documentMembers { members = it } // refresh roster on success+                    cb(err)                 }             },             onSetRole = { memberId, role ->
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 c55b5c654b..c794993d9e 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -2149,9 +2149,10 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)      * Shares the current document with [recipientId]. If it isn't online yet, it is first copied      * into the online folder (new doc id + content key + CRDT upload), then the invite is sent.      */-    fun shareCurrentDocument(recipientId: String, role: String = OfficeRoles.EDITOR, onResult: (Boolean) -> Unit = {}) {+    /** Shares with a recipient. onResult receives null on success, or a human-readable error reason. */+    fun shareCurrentDocument(recipientId: String, role: String = OfficeRoles.EDITOR, onResult: (String?) -> Unit = {}) {         viewModelScope.launch(Dispatchers.IO) {-            val ok = runCatching {+            val error: String? = try {                 OfficeSync.init(getApplication())                 val ds = DataStoreUtils.getInstance(getApplication())                 val doc = (_state.value as? ViewState.Loaded)?.document@@ -2160,34 +2161,41 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)                 val firstShare = currentDocId == null                 val docId = currentDocId ?: OfficeSync.newDocumentId()                 val key = currentDocKey ?: OfficeSync.newDocumentKey()-                currentDocId = docId-                currentDocKey = key-                currentCharMode = charMode-                // The sharer is the owner. Only the owner may share (client-enforced everywhere).                 if (firstShare) {                     currentRole = OfficeRoles.OWNER                     currentOwnerKey = OfficeSync.publicKeyPem                     currentMembers[OfficeSync.deviceId] = OfficeRoles.OWNER                 }-                if (currentRole != OfficeRoles.OWNER) return@runCatching false-                val ownerKeyB64 = Base64.encode(OfficeSync.publicKeyPem)-                syncDoc(docId, key)-                indexMutex.withLock {-                    val index = loadIndex(ds).associateBy { it.docId }.toMutableMap()-                    if (!index.containsKey(docId)) {-                        index[docId] = OfficeDocMeta(docId, title, Base64.encode(key), owner = true, charMode = charMode, role = OfficeRoles.OWNER, ownerKeyB64 = ownerKeyB64)-                        saveIndex(ds, index.values.toList())+                when {+                    currentRole != OfficeRoles.OWNER -> "Only the owner can share this document."+                    OfficeSync.getKey(recipientId) == null ->+                        "That device id isn't registered yet. Ask them to open Office once (Online tab), then try again."+                    else -> {+                        currentDocId = docId+                        currentDocKey = key+                        currentCharMode = charMode+                        val ownerKeyB64 = Base64.encode(OfficeSync.publicKeyPem)+                        syncDoc(docId, key)+                        indexMutex.withLock {+                            val index = loadIndex(ds).associateBy { it.docId }.toMutableMap()+                            if (!index.containsKey(docId)) {+                                index[docId] = OfficeDocMeta(docId, title, Base64.encode(key), owner = true, charMode = charMode, role = OfficeRoles.OWNER, ownerKeyB64 = ownerKeyB64)+                                saveIndex(ds, index.values.toList())+                            }+                        }+                        startLive(docId, key)+                        recordMembers(docId, key, listOf(+                            OfficeMember(OfficeSync.deviceId, myName(), OfficeRoles.OWNER),+                            OfficeMember(recipientId, "", role),+                        ))+                        if (OfficeSync.sendInvite(recipientId, docId, key, title, charMode, role, ownerKeyB64)) null+                        else "Couldn't deliver the invite — check your connection."                     }                 }-                startLive(docId, key)-                // Owner-signed roster: record self (owner) and the new member with their role.-                recordMembers(docId, key, listOf(-                    OfficeMember(OfficeSync.deviceId, myName(), OfficeRoles.OWNER),-                    OfficeMember(recipientId, "", role),-                ))-                OfficeSync.sendInvite(recipientId, docId, key, title, charMode, role, ownerKeyB64)-            }.getOrDefault(false)-            withContext(Dispatchers.Main) { onResult(ok) }+            } catch (e: Exception) {+                "Error: ${e.message ?: e.javaClass.simpleName}"+            }+            withContext(Dispatchers.Main) { onResult(error) }         }     }