android, desktop: fix draft appearing in another chat when switching chats while sending (#7308)

* ios, android, desktop: fix message being sent leaking into another chat

Compose state is shared between the chats opened in the same view, and
the send is launched in a scope that outlives the chat, so a send that
was still in flight when the chat was switched put its message (with the
reply context) into the compose state and then the draft of another chat,
and a late success cleared whatever was typed in the meantime.

The message being sent is no longer kept in the compose state when
leaving the chat, and the compose state is only cleared or restored after
sending if it still holds the message that was sent - the same check is
used by the other senders that show progress in the compose. A message
that failed to send is restored in the chat it was composed in, or kept
as its draft when another chat is open (iOS has no failed message
restore, there the message is dropped as before).

* android, desktop: keep only the chat switch fix

Revert the iOS changes and the same check in the three senders that
connect a prepared chat, leaving the fix for the compose state shared
between the chats opened in one view.

* plan: document what the narrowed change leaves to the connect senders

* android, desktop: use the same check where the sending flag is shared

The senders that connect a prepared chat set the same inProgress flag,
so a connect completing after the chat was switched cleared it for a
send started in the chat opened next, and that sent message was then
left in the compose.

* android, desktop: keep the live message clauses inside the open chat check

live and cs.liveMessage != null were alternatives to chatIsOpen, which
holds only while a live message is always sent to the chat that is open.
#7323 removes that: the live message committed by a chat switch is sent
to the chat it was composed in, while this view already shows another
one, so an unguarded clause here clears that chat's compose state - the
leak this fix exists to prevent.

liveSend stays an alternative to inProgress inside the guard, so a live
send behaves exactly as before while its chat is open, and it is excluded
from the restore/draft branch, which would otherwise write a draft on
every failing keystroke send once that chat is no longer open. On this
branch the only behaviour change is a live send completing after its chat
was left, which now leaves the opened chat alone.

Also load the draft of the chat opened next when the compose state is
cleared on switching away from a send in flight: clearState() returns
before the branch that loads a draft, so that draft was never shown, and
being in the slot but in no compose state it was then dropped by
clearPrevDraft on the next chat switch.

* plan: explain why live sends are guarded by the open chat check

Records that the exemption in "Deliberately unchanged" is from a guard
based on inProgress, not from chatIsOpen, and why the earlier form broke
once #7323 sends a live message to a chat other than the one open.
This commit is contained in:
Narasimha-sc
2026-08-04 21:01:35 +01:00
committed by GitHub
parent 0324517bfe
commit 7e99e68950
2 changed files with 298 additions and 20 deletions
@@ -597,6 +597,10 @@ fun ComposeView(
composeState.value = composeState.value.copy(inProgress = true)
}
// composeState and its inProgress flag are shared between the chats opened in this view, and sending is not cancelled
// when the chat is switched - a send may only clear or reset the state while it still holds the message that was sent
fun composeHasSentMessage(): Boolean = chatModel.chatId.value == chat.id && composeState.value.inProgress
suspend fun sendMemberContactInvitation() {
val mc = checkLinkPreview()
sending()
@@ -604,10 +608,10 @@ fun ComposeView(
if (contact != null) {
withContext(Dispatchers.Main) {
chatsCtx.updateContact(chat.remoteHostId, contact)
clearState()
if (composeHasSentMessage()) clearState()
}
} else {
composeState.value = composeState.value.copy(inProgress = false)
} else withContext(Dispatchers.Main) {
if (composeHasSentMessage()) composeState.value = composeState.value.copy(inProgress = false)
}
}
@@ -624,10 +628,10 @@ fun ComposeView(
if (contact != null) {
withContext(Dispatchers.Main) {
chatsCtx.updateContact(chat.remoteHostId, contact)
clearState()
if (composeHasSentMessage()) clearState()
}
} else {
composeState.value = composeState.value.copy(inProgress = false)
} else withContext(Dispatchers.Main) {
if (composeHasSentMessage()) composeState.value = composeState.value.copy(inProgress = false)
}
}
@@ -669,10 +673,10 @@ fun ComposeView(
chatModel.channelRelayHostnames.remove(groupInfo.groupId)
chatModel.groupMembers.value = relayResults.map { it.relayMember }
chatModel.populateGroupMembersIndexes()
clearState()
if (composeHasSentMessage()) clearState()
}
} else {
composeState.value = composeState.value.copy(inProgress = false)
} else withContext(Dispatchers.Main) {
if (composeHasSentMessage()) composeState.value = composeState.value.copy(inProgress = false)
}
}
@@ -932,16 +936,38 @@ fun ComposeView(
val wasForwarding = cs.forwarding
val forwardingFromChatId = (cs.contextItem as? ComposeContextItem.ForwardingItems)?.fromChatInfo?.id
val lastFailed = lastMessageFailedToSend
if (lastFailed == null) {
clearState(live)
} else {
composeState.value = lastFailed
}
val draft = chatModel.draft.value
if (wasForwarding && chatModel.draftChatId.value == draftChatId(chat.chatInfo.id, chatScope) && forwardingFromChatId != chat.chatInfo.id && draft != null) {
composeState.value = draft
} else {
clearCurrentDraft()
// composeState is shared between the chats opened in this view, and this runs after the send API call, so the user
// could have switched chats or typed another message in the meantime - only the message that was sent may be
// cleared or restored. On Main, so that these checks and changes are not interleaved with the user switching
// chats or typing.
withContext(Dispatchers.Main) {
val chatIsOpen = chatModel.chatId.value == chat.id
// a live message is held in the compose state of the chat it is sent to, but only while that chat is the one open
val liveSend = live || cs.liveMessage != null
val sentMessageInCompose = chatIsOpen && (liveSend || composeState.value.inProgress)
if (sentMessageInCompose) {
if (lastFailed == null) {
clearState(live)
} else {
composeState.value = lastFailed
}
}
val draft = chatModel.draft.value
if (wasForwarding && chatModel.draftChatId.value == draftChatId(chat.chatInfo.id, chatScope) && forwardingFromChatId != chat.chatInfo.id && draft != null) {
if (sentMessageInCompose) composeState.value = draft
} else {
clearCurrentDraft()
// liveSend excluded: a failing keystroke send would otherwise write a draft on every attempt
if (!sentMessageInCompose && !liveSend && lastFailed != null) {
// the message was not sent, so it is restored in the chat it was composed in, or kept as its draft if another chat is open
if (chatIsOpen && composeState.value.empty) {
composeState.value = lastFailed
} else if (saveLastDraft) {
chatModel.draft.value = lastFailed
chatModel.draftChatId.value = draftChatId(chat.id, chatScope)
}
}
}
}
return sent
}
@@ -1319,7 +1345,15 @@ fun ComposeView(
deleteUnusedFiles()
} else if (cs.inProgress) {
clearPrevDraft(prevChatId)
composeState.value = cs.copy(inProgress = false, progressByTimeout = false)
// the message being sent must not be kept in the compose state, it is shared with the chat opened next;
// if it fails to send it is restored in this chat or saved as its draft
clearState()
// clearState() does not load the draft of the chat opened next, and without this it is never shown and is
// dropped when that chat is left
val draft = chatModel.draft.value
if (draft != null && chatModel.draftChatId.value == draftChatId(chatModel.chatId.value, chatScope)) {
composeState.value = draft
}
} else if (!cs.empty) {
if (cs.preview is ComposePreview.VoicePreview && !cs.preview.finished) {
recState.value = RecordingState.NotStarted