mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-10-15 17:40:43 +00:00
ios: save draft when another chat is opened in place
ComposeView saves the draft only in onDisappear, which does not run when chatModel.chatId changes while ChatView stays presented - opening a group member's direct chat, a "forwarded from" chat, or a chat from a notification. The compose text was carried into the opened chat, and if the user could not send messages there (observer, channel subscriber, review by admins), the sendMsgEnabled change handler cleared it, losing the draft. The draft is now saved or cleared in ChatView's chatId change handler, mirroring onDisappear, before chat is replaced - ComposeView still renders the previous chat at that point, so its sendMsgEnabled handler fires only after the draft is safe, and clearCurrentDraft skips it because draftChatId no longer matches the opened chat. Skipped: secondary (member support) chat views, which share the group's chat id and would clobber the main chat's draft; forwarding compose state, which is prepared for the destination chat; drafts already prepared for the opened chat (shareChatLink); live messages and active voice recordings, which only ComposeView can complete.
This commit is contained in:
@@ -55,6 +55,7 @@ struct ChatView: View {
|
||||
@State private var showChatInfoSheet: Bool = false
|
||||
@State private var showAddMembersSheet: Bool = false
|
||||
@State private var composeState = ComposeState()
|
||||
@UserDefault(DEFAULT_PRIVACY_SAVE_LAST_DRAFT) private var saveLastDraft = true
|
||||
@State private var selectedRange = NSRange()
|
||||
@State private var keyboardVisible = false
|
||||
@State private var keyboardHiddenDate = Date.now
|
||||
@@ -359,6 +360,27 @@ struct ChatView: View {
|
||||
revealedItems = Set()
|
||||
stopAudioPlayer()
|
||||
if let cId {
|
||||
// ComposeView.onDisappear does not run when another chat is opened in place -
|
||||
// save or clear the previous chat's draft here, as in onDisappear.
|
||||
// Skipped in secondary (member support) chat views sharing the group's chat id,
|
||||
// and for forwarding, live messages and voice recordings.
|
||||
if im.secondaryIMFilter == nil,
|
||||
cId != chat.id,
|
||||
!composeState.forwarding,
|
||||
composeState.liveMessage == nil,
|
||||
composeState.voiceMessageRecordingState != .recording {
|
||||
if !composeState.empty && !composeState.inProgress {
|
||||
// keep draft prepared for the opened chat (shareChatLink)
|
||||
if saveLastDraft, chatModel.draftChatId != cId {
|
||||
chatModel.draft = composeState
|
||||
chatModel.draftChatId = chat.id
|
||||
}
|
||||
} else if chatModel.draftChatId == chat.id {
|
||||
chatModel.draft = nil
|
||||
chatModel.draftChatId = nil
|
||||
}
|
||||
composeState = ComposeState()
|
||||
}
|
||||
if let c = chatModel.getChat(cId) {
|
||||
chat = c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user