From b4164ff1dee49bb843a70b91538b405f5e96e1fe Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 29 Oct 2026 08:57:27 +0100 Subject: [PATCH] Keep PM drafts until the core confirms persistence --- .../java/com/utun/chat/data/ChatRepository.kt | 5 ++--- .../ui/components/ConversationComposer.kt | 4 ++-- .../com/utun/chat/viewmodel/ChatViewModel.kt | 21 +++++++++---------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt index bf54d4a6f..5cd40ca6d 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt @@ -209,8 +209,6 @@ fun cleanSocketName(raw: String): String = raw.removePrefix("as_").replace(Regex("_v[46]_(tcp|udp)$"), " $1") class ChatRepository { - private val myNodeId: Long = NativeLib.getMyNodeId() - fun getChannels(): List { val list = mutableListOf() try { @@ -536,7 +534,8 @@ class ChatRepository { fun dmStart(nodeId: Long, sourceChId: String, peerName: String) = NativeLib.dmStart(nodeId, sourceChId, peerName) - fun dmSend(convId: String, text: String, generation: Long) = NativeLib.dmSend(convId, text, generation) + fun dmSend(convId: String, text: String, generation: Long, requestId: Long): Boolean = + NativeLib.dmSend(convId, text, generation, requestId) fun parseDmConversations(json: String): List { val list = mutableListOf() diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/components/ConversationComposer.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/components/ConversationComposer.kt index bd591ec4f..0dd41b931 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/components/ConversationComposer.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/components/ConversationComposer.kt @@ -67,7 +67,7 @@ fun ConversationComposer(target: ConversationTarget, viewModel: ChatViewModel, val sendFailed by viewModel.messageSendFailed.collectAsState() val submitting by viewModel.submittingChannels.collectAsState() val deletingDm by viewModel.dmDeleting.collectAsState() - val pending = if (target is ConversationTarget.Channel) target.id in submitting else target.id in deletingDm + val pending = target.nativeId in submitting || (target is ConversationTarget.Pm && target.id in deletingDm) if (sendFailed) AlertDialog(onDismissRequest = viewModel::dismissMessageSendError, text = { Text(stringResource(R.string.message_send_failed)) }, confirmButton = { TextButton(onClick = viewModel::dismissMessageSendError) { Text(stringResource(R.string.close)) } }) @@ -184,7 +184,7 @@ fun ConversationComposer(target: ConversationTarget, viewModel: ChatViewModel, if (sentText.isNotBlank()) { textSaved = if (target is ConversationTarget.Channel) viewModel.sendChannelText(target, sentText, capturedReply).await() - else viewModel.sendDmText(target as ConversationTarget.Pm, sentText) + else viewModel.sendDmText(target as ConversationTarget.Pm, sentText).await() if (textSaved && textRevision == revision) { text = TextFieldValue(); textRevision++ } } if (attachmentSaved && textSaved) viewModel.finishReply(capturedReply) diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt index 7b46ae664..2cf64fff5 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt @@ -48,7 +48,7 @@ class ChatViewModel : ViewModel() { fun cancelEdit(edit: ChannelEdit) { if (_editing.value == edit) _editing.value = null } fun saveEdit(edit: ChannelEdit, text: String): Deferred = viewModelScope.async { - submitChannel(edit.channelId, null, reportError = false) { + submitMessage(edit.channelId, null, reportError = false) { NativeLib.editMessage(edit.channelId, edit.message.messageId, edit.message.version, text, it) }.also { if (it) cancelEdit(edit) } } @@ -112,7 +112,7 @@ class ChatViewModel : ViewModel() { result.complete(success) } - private suspend fun submitChannel(channel: String, reply: ChannelReply?, reportError: Boolean = true, + private suspend fun submitMessage(channel: String, reply: ChannelReply?, reportError: Boolean = true, queue: (Long) -> Boolean): Boolean { if (reply != null && reply.channelId != channel) { LogManager.addLog("ERROR", "Reply", "target mismatch ch=$channel reply=${reply.channelId}") @@ -143,7 +143,7 @@ class ChatViewModel : ViewModel() { } fun sendChannelText(target: ConversationTarget.Channel, text: String, reply: ChannelReply?): Deferred = viewModelScope.async { - submitChannel(target.id, reply) { NativeLib.sendMessage(target.id, text, reply?.message?.messageId.orEmpty(), it) } + submitMessage(target.id, reply) { NativeLib.sendMessage(target.id, text, reply?.message?.messageId.orEmpty(), it) } } data class PmInviteState(val peer: Long, val name: String, val source: String, val groups: List, @@ -1324,11 +1324,10 @@ class ChatViewModel : ViewModel() { return true } - fun sendDmText(target: ConversationTarget.Pm, text: String): Boolean { - val r = repo ?: return false - if (text.isBlank() || !dmTargetCurrent(target)) return false - r.dmSend(target.id, text, target.generation) - return true + fun sendDmText(target: ConversationTarget.Pm, text: String): Deferred = viewModelScope.async { + val r = repo ?: return@async false + if (text.isBlank() || !dmTargetCurrent(target)) return@async false + submitMessage(target.nativeId, null) { r.dmSend(target.id, text, target.generation, it) } } fun deleteDmConversation(dm: DmConversation) { @@ -1634,7 +1633,7 @@ class ChatViewModel : ViewModel() { } return@async accepted } - submitChannel(target.id, reply) { request -> + submitMessage(target.id, reply) { request -> NativeLib.attachmentSend(target.id, filePath, reply?.message?.messageId.orEmpty(), request) } } @@ -1666,7 +1665,7 @@ class ChatViewModel : ViewModel() { } else NativeLib.videoSend(target.nativeId, filePath, meta.first, meta.second, meta.third, reply?.message?.messageId.orEmpty(), request, (target as? ConversationTarget.Pm)?.generation ?: 0).also { accepted = it } } - saved = if (target is ConversationTarget.Channel) submitChannel(target.id, reply, queue = queue) else queue(0) + saved = if (target is ConversationTarget.Channel) submitMessage(target.id, reply, queue = queue) else queue(0) if (!saved && job !in discardedPreparationJobs) _messageSendFailed.value = true if (saved && clearReply) finishReply(reply) } catch (e: kotlinx.coroutines.CancellationException) { throw e } @@ -1699,7 +1698,7 @@ class ChatViewModel : ViewModel() { } else NativeLib.photoSend(target.nativeId, filePath, meta.width, meta.height, meta.mime, reply?.message?.messageId.orEmpty(), request, (target as? ConversationTarget.Pm)?.generation ?: 0).also { accepted = it } } - saved = if (target is ConversationTarget.Channel) submitChannel(target.id, reply, queue = queue) else queue(0) + saved = if (target is ConversationTarget.Channel) submitMessage(target.id, reply, queue = queue) else queue(0) completed = true if (!saved) _messageSendFailed.value = true if (saved && clearReply) finishReply(reply)