office: let owner name a document before sharing it online (used as title everywhere)
- Commit
1584e317e827c1785a013664a3e689902ac02c12- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-06 16:28 UTC (5 months ago)
- Parents
81401fa
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 c2e91f2954..9bbd975ffd 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -270,14 +270,17 @@ private fun OnlineInit(viewModel: OfficeViewModel) { fun ShareOnlineDialog( deviceId: String, isOwner: Boolean,+ isOnline: Boolean,+ initialName: String, myRole: String, members: List<com.vayunmathur.office.util.OfficeMember>,- onShare: (String, String, (String?) -> Unit) -> Unit,+ onShare: (String, String, String, (String?) -> Unit) -> Unit, onSetRole: (String, String) -> Unit, onComputeCode: (String, (String?) -> Unit) -> Unit, onDismiss: () -> Unit ) { var recipient by remember { mutableStateOf("") }+ var docName by remember { mutableStateOf(initialName) } var addRole by remember { mutableStateOf(com.vayunmathur.office.util.OfficeRoles.EDITOR) } var roleMenu by remember { mutableStateOf(false) } var code by remember { mutableStateOf<String?>(null) }@@ -316,6 +319,17 @@ fun ShareOnlineDialog( TextButton(onClick = { if (deviceId.isNotEmpty()) clipboard.setText(AnnotatedString(deviceId)) }) { Text("Copy") } } } else {+ if (!isOnline) {+ // Owner names the document before it first goes online.+ OutlinedTextField(+ value = docName, onValueChange = { docName = it },+ label = { Text("Document name") }, singleLine = true, modifier = Modifier.fillMaxWidth()+ )+ Spacer(Modifier.height(8.dp))+ } else {+ Text("Document: ${docName.ifBlank { "Untitled" }}", style = MaterialTheme.typography.titleSmall)+ Spacer(Modifier.height(8.dp))+ } Text("Add someone by device id (copies this document into your online folder, end-to-end encrypted):") Spacer(Modifier.height(8.dp)) OutlinedTextField(@@ -355,10 +369,10 @@ fun ShareOnlineDialog( }, confirmButton = { if (isOwner) TextButton(- enabled = recipient.isNotBlank() && !sharing,+ enabled = recipient.isNotBlank() && (isOnline || docName.isNotBlank()) && !sharing, onClick = { sharing = true; status = null- onShare(recipient.trim(), addRole) { err ->+ onShare(recipient.trim(), addRole, docName.trim()) { err -> sharing = false status = err ?: "Added ✓" if (err == null) recipient = ""@@ -929,10 +943,12 @@ fun DocumentScreen(document: OdfDocument, viewModel: OfficeViewModel, activity: ShareOnlineDialog( deviceId = viewModel.syncDeviceId, isOwner = viewModel.currentDocRole() == com.vayunmathur.office.util.OfficeRoles.OWNER,+ isOnline = isOnline,+ initialName = document.title, myRole = viewModel.currentDocRole(), members = members,- onShare = { recipientId, role, cb ->- viewModel.shareCurrentDocument(recipientId, role) { err ->+ onShare = { recipientId, role, name, cb ->+ viewModel.shareCurrentDocument(recipientId, role, name) { err -> if (err == null) { viewModel.documentMembers { members = it } // refresh roster on success viewModel.currentOnlineDocId()?.let { onBecameOnline(it) } // now a cloud doc
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 88e0d86710..de512665aa 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -2255,21 +2255,27 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) * into the online folder (new doc id + content key + CRDT upload), then the invite is sent. */ /** 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 = {}) {+ fun shareCurrentDocument(recipientId: String, role: String = OfficeRoles.EDITOR, docName: String = "", onResult: (String?) -> Unit = {}) { viewModelScope.launch(Dispatchers.IO) { val error: String? = try { OfficeSync.init(getApplication()) val ds = DataStoreUtils.getInstance(getApplication()) val doc = (_state.value as? ViewState.Loaded)?.document- val title = doc?.title ?: "Document"- val charMode = doc != null && TextDocCodec.isEligible(doc) val firstShare = currentDocId == null+ // The owner names the document when it first goes online; later shares keep that name.+ val title = if (firstShare) docName.trim().ifBlank { doc?.title ?: "Document" }+ else (currentOnlineTitle() ?: doc?.title ?: "Document")+ val charMode = doc != null && TextDocCodec.isEligible(doc) val docId = currentDocId ?: OfficeSync.newDocumentId() val key = currentDocKey ?: OfficeSync.newDocumentKey() if (firstShare) { currentRole = OfficeRoles.OWNER currentOwnerKey = OfficeSync.publicBundle currentMembers[OfficeSync.deviceId] = OfficeRoles.OWNER+ // Reflect the chosen name in the open editor so its title matches the online doc.+ if (doc != null) withContext(Dispatchers.Main) {+ (_state.value as? ViewState.Loaded)?.document?.let { _state.value = ViewState.Loaded(withTitle(it, title)) }+ } } when { currentRole != OfficeRoles.OWNER -> "Only the owner can share this document."@@ -2383,6 +2389,18 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) /** The open document's online id, or null if it isn't a cloud document. */ fun currentOnlineDocId(): String? = currentDocId + /** The stored online title of the open document, if any. */+ private fun currentOnlineTitle(): String? =+ currentDocId?.let { id -> _onlineDocs.value.firstOrNull { it.docId == id }?.title }++ /** Returns a copy of [doc] with a new [title] (works across all document types). */+ private fun withTitle(doc: OdfDocument, title: String): OdfDocument = when (doc) {+ is OdfDocument.TextDocument -> doc.copy(title = title)+ is OdfDocument.Spreadsheet -> doc.copy(title = title)+ is OdfDocument.Presentation -> doc.copy(title = title)+ is OdfDocument.Drawing -> doc.copy(title = title)+ }+ /** True if the local user may edit the open document. */ fun canEditCurrent(): Boolean = currentDocId == null || OfficeRoles.canEdit(currentRole)