From 9f2d5486b6fb3c25a6d0a0805c90491ec374948e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 31 Dec 2022 10:25:32 +0000 Subject: [PATCH] =?UTF-8?q?mobile:=20fix=20race=20condition=20with=20live?= =?UTF-8?q?=20messages=20(when=20message=20is=20"revived"=20when=20it=20is?= =?UTF-8?q?=20being=20sent=20as=20no=20longer=20live=20=E2=80=93=20possibl?= =?UTF-8?q?y=20core=20should=20reject=20"live"=20updates=20to=20non-live?= =?UTF-8?q?=20messages=20too)=20(#1668)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mobile: fix race condition with live messages (when message is "revived" when it is being sent as no longer live – possibly core should reject "live" updates to non-live messages too) * android: move delay for live messages --- .../main/java/chat/simplex/app/views/chat/ComposeView.kt | 6 ++++-- .../main/java/chat/simplex/app/views/chat/SendMsgView.kt | 3 ++- .../Shared/Views/Chat/ComposeMessage/ComposeView.swift | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt index 7fa723e0f1..e4581931f7 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt @@ -421,15 +421,17 @@ fun ComposeView( return null } + val liveMessage = cs.liveMessage if (!live) { + if (liveMessage != null) composeState.value = cs.copy(liveMessage = null) sending() } if (cs.contextItem is ComposeContextItem.EditingItem) { val ei = cs.contextItem.chatItem sent = updateMessage(ei, cInfo, live) - } else if (cs.liveMessage != null) { - sent = updateMessage(cs.liveMessage.chatItem, cInfo, live) + } else if (liveMessage != null) { + sent = updateMessage(liveMessage.chatItem, cInfo, live) } else { val msgs: ArrayList = ArrayList() val files: ArrayList = ArrayList() diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt index 04a9b296f6..439601ef79 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt @@ -454,9 +454,10 @@ private fun startLiveMessage( sendButtonAlpha.snapTo(1f) } scope.launch { + delay(3000) while (composeState.value.liveMessage != null) { - delay(3000) update() + delay(3000) } } } diff --git a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift index e44147d448..fff6f5a178 100644 --- a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift +++ b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift @@ -505,10 +505,14 @@ struct ComposeView: View { private func sendMessageAsync(_ text: String?, live: Bool) async -> ChatItem? { var sent: ChatItem? let msgText = text ?? composeState.message - if !live { await sending() } + let liveMessage = composeState.liveMessage + if !live { + if liveMessage != nil { composeState = composeState.copy(liveMessage: nil) } + await sending() + } if case let .editingItem(ci) = composeState.contextItem { sent = await updateMessage(ci, live: live) - } else if let liveMessage = composeState.liveMessage { + } else if let liveMessage = liveMessage { sent = await updateMessage(liveMessage.chatItem, live: live) } else { var quoted: Int64? = nil