From 3d12fbcdf32e81654d2474f2868c28ca8fd068f4 Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Tue, 20 Feb 2024 13:13:47 +0100 Subject: [PATCH] refactor: Update SelectComposeMailboxView --- .../ComposeMessageIntentView.swift | 5 +-- .../Select Mailbox/AccountMailboxCell.swift | 41 +++++++++++++++++++ .../AccountMailboxesListView.swift | 40 ++++++------------ .../SelectComposeMailboxView.swift | 33 +++++++-------- ...ft => SelectComposeMailboxViewModel.swift} | 36 ++++++++-------- .../ComposeMessageWrapperView.swift | 2 +- 6 files changed, 88 insertions(+), 69 deletions(-) create mode 100644 Mail/Views/New Message/Select Mailbox/AccountMailboxCell.swift rename Mail/Views/New Message/Select Mailbox/{SelectMailboxViewModel.swift => SelectComposeMailboxViewModel.swift} (95%) diff --git a/Mail/Views/New Message/ComposeMessageIntentView.swift b/Mail/Views/New Message/ComposeMessageIntentView.swift index da14b680eb..2ddebd27c5 100644 --- a/Mail/Views/New Message/ComposeMessageIntentView.swift +++ b/Mail/Views/New Message/ComposeMessageIntentView.swift @@ -73,10 +73,7 @@ struct ComposeMessageIntentView: View, IntentViewable { func initFromIntent() async { guard let mailboxId = composeMessageIntent.mailboxId, let userId = composeMessageIntent.userId, - let mailboxManager = accountManager.getMailboxManager( - for: mailboxId, - userId: userId - ) else { + let mailboxManager = accountManager.getMailboxManager(for: mailboxId, userId: userId) else { dismiss() snackbarPresenter.show(message: MailError.unknownError.errorDescription ?? "") return diff --git a/Mail/Views/New Message/Select Mailbox/AccountMailboxCell.swift b/Mail/Views/New Message/Select Mailbox/AccountMailboxCell.swift new file mode 100644 index 0000000000..69348e1086 --- /dev/null +++ b/Mail/Views/New Message/Select Mailbox/AccountMailboxCell.swift @@ -0,0 +1,41 @@ +/* + 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 . + */ + +import MailCore +import MailResources +import SwiftUI + +struct AccountMailboxCell: View { + let mailbox: Mailbox + var selectedMailbox: Mailbox? + let selectMailbox: (Mailbox) -> Void + + var body: some View { + MailboxesManagementButtonView( + icon: MailResourcesAsset.envelope, + mailbox: mailbox, + isSelected: selectedMailbox?.mailboxId == mailbox.mailboxId + ) { + selectMailbox(mailbox) + } + } +} + +#Preview { + AccountMailboxCell(mailbox: PreviewHelper.sampleMailbox) { _ in } +} diff --git a/Mail/Views/New Message/Select Mailbox/AccountMailboxesListView.swift b/Mail/Views/New Message/Select Mailbox/AccountMailboxesListView.swift index e88ceeac16..62eb620a56 100644 --- a/Mail/Views/New Message/Select Mailbox/AccountMailboxesListView.swift +++ b/Mail/Views/New Message/Select Mailbox/AccountMailboxesListView.swift @@ -25,41 +25,27 @@ import SwiftUI struct AccountMailboxesListView: View { @LazyInjectService private var accountManager: AccountManager - var accounts: [Account] + let account: Account var selectedMailbox: Mailbox? - var selectMailbox: (Mailbox) -> Void + let selectMailbox: (Mailbox) -> Void + + private var currentMailbox: Mailbox? { + return accountManager.currentMailboxManager?.mailbox + } var body: some View { - ForEach(accounts) { account in - Text(account.user.displayName) + Text(account.user.displayName) - if account.userId == accountManager.currentUserId, - let currentMailbox = accountManager.currentMailboxManager?.mailbox { - MailboxesManagementButtonView( - icon: MailResourcesAsset.envelope, - mailbox: currentMailbox, - isSelected: selectedMailbox?.id == currentMailbox.id - ) { - selectMailbox(currentMailbox) - } - } + if account.userId == accountManager.currentUserId, let currentMailbox { + AccountMailboxCell(mailbox: currentMailbox, selectedMailbox: selectedMailbox, selectMailbox: selectMailbox) + } - ForEachMailboxView( - userId: account.userId, - excludedMailboxIds: [accountManager.currentMailboxManager?.mailbox.mailboxId].compactMap { $0 } - ) { mailbox in - MailboxesManagementButtonView( - icon: MailResourcesAsset.envelope, - mailbox: mailbox, - isSelected: selectedMailbox?.id == mailbox.id - ) { - selectMailbox(mailbox) - } - } + ForEachMailboxView(userId: account.userId, excludedMailboxIds: [currentMailbox?.mailboxId].compactMap { $0 }) { mailbox in + AccountMailboxCell(mailbox: mailbox, selectedMailbox: selectedMailbox, selectMailbox: selectMailbox) } } } #Preview { - AccountMailboxesListView(accounts: [], selectedMailbox: PreviewHelper.sampleMailbox) { _ in } + AccountMailboxesListView(account: PreviewHelper.sampleAccount, selectedMailbox: PreviewHelper.sampleMailbox) { _ in } } diff --git a/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxView.swift b/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxView.swift index 549371da36..5cac3cd40e 100644 --- a/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxView.swift +++ b/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxView.swift @@ -33,13 +33,13 @@ struct SelectComposeMailboxView: View { @AppStorage(UserDefaults.shared.key(.accentColor)) private var accentColor = DefaultPreferences.accentColor - @Binding var composeMessageIntent: ComposeMessageIntent + @StateObject private var viewModel: SelectComposeMailboxViewModel - @StateObject private var viewModel: SelectMailboxViewModel + @Binding var composeMessageIntent: ComposeMessageIntent init(composeMessageIntent: Binding) { _composeMessageIntent = composeMessageIntent - _viewModel = StateObject(wrappedValue: SelectMailboxViewModel(composeMessageIntent: composeMessageIntent)) + _viewModel = StateObject(wrappedValue: SelectComposeMailboxViewModel(composeMessageIntent: composeMessageIntent)) } var body: some View { @@ -57,25 +57,23 @@ struct SelectComposeMailboxView: View { .frame(maxWidth: .infinity, alignment: .leading) ScrollView { - AccountMailboxesListView( - accounts: viewModel.accounts, - selectedMailbox: viewModel.selectedMailbox, - selectMailbox: viewModel.selectMailbox - ) + ForEach(viewModel.accounts) { account in + AccountMailboxesListView( + account: account, + selectedMailbox: viewModel.selectedMailbox, + selectMailbox: viewModel.selectMailbox + ) + } } - Button(MailResourcesStrings.Localizable.buttonContinue) { - viewModel.mailboxHasBeenSelected() - } - .buttonStyle(.ikPlain) - .controlSize(.large) - .ikButtonFullWidth(true) + Button(MailResourcesStrings.Localizable.buttonContinue, action: viewModel.validateMailboxChoice) + .buttonStyle(.ikPlain) + .controlSize(.large) + .ikButtonFullWidth(true) } .padding(.horizontal, value: .medium) .mailboxCellStyle(.account) - .onAppear { - viewModel.initDefaultAccountAndMailbox() - } + .onAppear(perform: viewModel.initDefaultAccountAndMailbox) .toolbar { ToolbarItem(placement: .navigationBarLeading) { if !platformDetector.isMac { @@ -85,7 +83,6 @@ struct SelectComposeMailboxView: View { } } - /// Something to dismiss the view regardless of presentation context private func dismissMessageView() { dismissModal() dismiss() diff --git a/Mail/Views/New Message/Select Mailbox/SelectMailboxViewModel.swift b/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxViewModel.swift similarity index 95% rename from Mail/Views/New Message/Select Mailbox/SelectMailboxViewModel.swift rename to Mail/Views/New Message/Select Mailbox/SelectComposeMailboxViewModel.swift index 5fd90286aa..e73c98db94 100644 --- a/Mail/Views/New Message/Select Mailbox/SelectMailboxViewModel.swift +++ b/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxViewModel.swift @@ -22,14 +22,13 @@ import InfomaniakDI import MailCore import SwiftUI -final class SelectMailboxViewModel: ObservableObject { +final class SelectComposeMailboxViewModel: ObservableObject { @LazyInjectService private var accountManager: AccountManager @LazyInjectService private var mailboxInfosManager: MailboxInfosManager @Published private(set) var selectedMailbox: Mailbox? private(set) var composeMessageIntent: Binding - private(set) var accounts = [Account]() init(composeMessageIntent: Binding) { @@ -44,7 +43,22 @@ final class SelectMailboxViewModel: ObservableObject { } } - func mailboxHasBeenSelected() { + func initDefaultAccountAndMailbox() { + selectedMailbox = accountManager.currentMailboxManager?.mailbox + if accountManager.accounts.count == 1 && mailboxInfosManager.getMailboxes().count == 1 { + validateMailboxChoice() + } + } + + func selectMailbox(_ mailbox: Mailbox) { + guard mailbox.isAvailable else { + // TODO: Display snackbar + return + } + selectedMailbox = mailbox + } + + func validateMailboxChoice() { guard let selectedMailbox, let mailboxManager = accountManager.getMailboxManager(for: selectedMailbox) else { // TODO: display snackbar return @@ -62,20 +76,4 @@ final class SelectMailboxViewModel: ObservableObject { break } } - - func selectMailbox(_ mailbox: Mailbox) { - guard mailbox.isAvailable else { - // TODO: Display snackbar - return - } - - selectedMailbox = mailbox - } - - func initDefaultAccountAndMailbox() { - selectedMailbox = accountManager.currentMailboxManager?.mailbox - if accountManager.accounts.count == 1 && mailboxInfosManager.getMailboxes().count == 1 { - mailboxHasBeenSelected() - } - } } diff --git a/MailShareExtension/ComposeMessageWrapperView.swift b/MailShareExtension/ComposeMessageWrapperView.swift index 6bce7f22fe..2454c48f6e 100644 --- a/MailShareExtension/ComposeMessageWrapperView.swift +++ b/MailShareExtension/ComposeMessageWrapperView.swift @@ -34,7 +34,7 @@ struct ComposeMessageWrapperView: View { var body: some View { if let mailboxManager = accountManager.currentMailboxManager { - ComposeMessageIntentView(composeMessageIntent: .new(), attachments: itemProviders) + ComposeMessageIntentView(composeMessageIntent: .new(fromExtension: true), attachments: itemProviders) .environmentObject(mailboxManager) .environment(\.dismissModal) { dismissHandler(())