Skip to content

Commit

Permalink
fix: Handle remote Draft not loaded + Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jan 31, 2024
1 parent 4631aeb commit c141657
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
15 changes: 7 additions & 8 deletions Mail/Utils/DraftUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum DraftUtils {
guard let message = thread.messages.first else { return }
// If we already have the draft locally, present it directly
if let draft = mailboxManager.draft(messageUid: message.uid, using: nil)?.detached() {
matomoOpenDraft(draft: draft)
matomoOpenDraft(isLoadedRemotely: false)
composeMessageIntent.wrappedValue = ComposeMessageIntent.existing(draft: draft, originMailboxManager: mailboxManager)
} else {
DraftUtils.editDraft(from: message, mailboxManager: mailboxManager, composeMessageIntent: composeMessageIntent)
Expand All @@ -46,27 +46,26 @@ enum DraftUtils {
) {
// If we already have the draft locally, present it directly
if let draft = mailboxManager.draft(messageUid: message.uid, using: nil)?.detached() {
matomoOpenDraft(draft: draft)
matomoOpenDraft(isLoadedRemotely: false)
composeMessageIntent.wrappedValue = ComposeMessageIntent.existing(draft: draft, originMailboxManager: mailboxManager)
// Draft comes from API, we will update it after showing the ComposeMessageView
} else {
let draft = Draft(messageUid: message.uid)
matomoOpenDraft(draft: draft)
composeMessageIntent.wrappedValue = ComposeMessageIntent.existing(
draft: draft,
matomoOpenDraft(isLoadedRemotely: true)
composeMessageIntent.wrappedValue = ComposeMessageIntent.existingRemote(
messageUid: message.uid,
originMailboxManager: mailboxManager
)
}
}

private static func matomoOpenDraft(draft: Draft) {
private static func matomoOpenDraft(isLoadedRemotely: Bool) {
@InjectService var matomo: MatomoUtils
matomo.track(eventWithCategory: .newMessage, name: "openFromDraft")
matomo.track(
eventWithCategory: .newMessage,
action: .data,
name: "openLocalDraft",
value: !draft.isLoadedRemotely
value: !isLoadedRemotely
)
}
}
39 changes: 23 additions & 16 deletions Mail/Views/New Message/ComposeMessageIntentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import SwiftUI

struct ComposeMessageIntentView: View {
@LazyInjectService private var accountManager: AccountManager
@LazyInjectService private var snackbarPresenter: SnackBarPresentable

@Environment(\.dismiss) private var dismiss

@State private var draft: Draft?
@State private var mailboxManager: MailboxManager?
Expand Down Expand Up @@ -52,41 +55,45 @@ struct ComposeMessageIntentView: View {
for: composeMessageIntent.mailboxId,
userId: composeMessageIntent.userId
) else {
dismiss()
snackbarPresenter.show(message: MailError.unknownError.errorDescription ?? "")
return
}

var draftLocalUUID: String?
var newDraft: Draft?
var draftToWrite: Draft?
switch composeMessageIntent.type {
case .new:
newDraft = Draft(localUUID: UUID().uuidString)
draftToWrite = Draft(localUUID: UUID().uuidString)
case .existing(let existingDraftLocalUUID):
draftLocalUUID = existingDraftLocalUUID
draftToWrite = mailboxManager.draft(localUuid: existingDraftLocalUUID)
case .existingRemote(let messageUid):
draftToWrite = Draft(messageUid: messageUid)
case .mailTo(let mailToURLComponents):
newDraft = Draft.mailTo(urlComponents: mailToURLComponents)
draftToWrite = Draft.mailTo(urlComponents: mailToURLComponents)
case .writeTo(let recipient):
newDraft = Draft.writing(to: recipient)
draftToWrite = Draft.writing(to: recipient)
case .reply(let messageUid, let replyMode):
if let frozenMessage = mailboxManager.getRealm().object(ofType: Message.self, forPrimaryKey: messageUid)?.freeze() {
let messageReply = MessageReply(frozenMessage: frozenMessage, replyMode: replyMode)
self.messageReply = messageReply
newDraft = Draft.replying(
draftToWrite = Draft.replying(
reply: messageReply,
currentMailboxEmail: mailboxManager.mailbox.email
)
}
}

if let newDraft {
draftLocalUUID = newDraft.localUUID
writeDraftToRealm(mailboxManager.getRealm(), draft: newDraft)
}

guard let draftLocalUUID else { return }
if let draftToWrite {
let draftLocalUUID = draftToWrite.localUUID
writeDraftToRealm(mailboxManager.getRealm(), draft: draftToWrite)

Task { @MainActor in
draft = mailboxManager.draft(localUuid: draftLocalUUID)
self.mailboxManager = mailboxManager
Task { @MainActor in
draft = mailboxManager.draft(localUuid: draftLocalUUID)
self.mailboxManager = mailboxManager
}
} else {
dismiss()
snackbarPresenter.show(message: MailError.localMessageNotFound.errorDescription ?? "")
}
}

Expand Down
9 changes: 9 additions & 0 deletions MailCore/Models/ComposeMessageIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct ComposeMessageIntent: Codable, Identifiable, Hashable {
public enum IntentType: Codable, Hashable {
case new
case existing(draftLocalUUID: String)
case existingRemote(messageUid: String)
case mailTo(mailToURLComponents: URLComponents)
case writeTo(recipient: Recipient)
case reply(messageUid: String, replyMode: ReplyMode)
Expand Down Expand Up @@ -55,6 +56,14 @@ public struct ComposeMessageIntent: Codable, Identifiable, Hashable {
)
}

public static func existingRemote(messageUid: String, originMailboxManager: MailboxManageable) -> ComposeMessageIntent {
return ComposeMessageIntent(
userId: originMailboxManager.mailbox.userId,
mailboxId: originMailboxManager.mailbox.mailboxId,
type: .existingRemote(messageUid: messageUid)
)
}

public static func mailTo(mailToURLComponents: URLComponents, originMailboxManager: MailboxManager) -> ComposeMessageIntent {
return ComposeMessageIntent(
userId: originMailboxManager.mailbox.userId,
Expand Down

0 comments on commit c141657

Please sign in to comment.