Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: IntentViewable #1268

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Mail/Utils/IntentViewable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import SwiftUI

protocol IntentViewable<Intent> {
associatedtype Intent

var resolvedIntent: State<Intent?> { get }
func initFromIntent() async
}
43 changes: 31 additions & 12 deletions Mail/Views/New Message/ComposeMessageIntentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,33 @@ import NavigationBackport
import RealmSwift
import SwiftUI

struct ComposeMessageIntentView: View {
struct ComposeMessageIntentView: View, IntentViewable {
typealias Intent = ResolvedIntent

@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?
@State private var messageReply: MessageReply?
let resolvedIntent = State<ResolvedIntent?>()

struct ResolvedIntent {
let mailboxManager: MailboxManager
let draft: Draft
let messageReply: MessageReply?
}

let composeMessageIntent: ComposeMessageIntent

var body: some View {
NBNavigationStack {
if let draft,
let mailboxManager {
ComposeMessageView(draft: draft, mailboxManager: mailboxManager, messageReply: messageReply)
.environmentObject(mailboxManager)
if let resolvedIntent = resolvedIntent.wrappedValue {
ComposeMessageView(
draft: resolvedIntent.draft,
mailboxManager: resolvedIntent.mailboxManager,
messageReply: resolvedIntent.messageReply
)
.environmentObject(resolvedIntent.mailboxManager)
} else {
ProgressView()
.progressViewStyle(.circular)
Expand All @@ -62,6 +71,7 @@ struct ComposeMessageIntentView: View {
}

var draftToWrite: Draft?
var maybeMessageReply: MessageReply?
switch composeMessageIntent.type {
case .new:
draftToWrite = Draft(localUUID: UUID().uuidString)
Expand All @@ -76,7 +86,7 @@ struct ComposeMessageIntentView: View {
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
maybeMessageReply = messageReply
draftToWrite = Draft.replying(
reply: messageReply,
currentMailboxEmail: mailboxManager.mailbox.email
Expand All @@ -88,9 +98,18 @@ struct ComposeMessageIntentView: View {
let draftLocalUUID = draftToWrite.localUUID
writeDraftToRealm(mailboxManager.getRealm(), draft: draftToWrite)

Task { @MainActor in
draft = mailboxManager.draft(localUuid: draftLocalUUID)
self.mailboxManager = mailboxManager
Task { @MainActor [maybeMessageReply] in
guard let liveDraft = mailboxManager.draft(localUuid: draftLocalUUID) else {
dismiss()
snackbarPresenter.show(message: MailError.localMessageNotFound.errorDescription ?? "")
return
}

resolvedIntent.wrappedValue = ResolvedIntent(
mailboxManager: mailboxManager,
draft: liveDraft,
messageReply: maybeMessageReply
)
}
} else {
dismiss()
Expand Down
6 changes: 0 additions & 6 deletions Mail/Views/Thread/OpenThreadIntentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import MailCore
import RealmSwift
import SwiftUI

protocol IntentViewable<Intent> {
associatedtype Intent
var resolvedIntent: State<Intent?> { get }
func initFromIntent() async
}

struct OpenThreadIntentView: View, IntentViewable {
typealias Intent = ResolvedIntent

Expand Down
Loading