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..ffab9ac288 100644 --- a/Mail/Views/New Message/Select Mailbox/SelectMailboxViewModel.swift +++ b/Mail/Views/New Message/Select Mailbox/SelectComposeMailboxViewModel.swift @@ -22,7 +22,7 @@ import InfomaniakDI import MailCore import SwiftUI -final class SelectMailboxViewModel: ObservableObject { +final class SelectComposeMailboxViewModel: ObservableObject { @LazyInjectService private var accountManager: AccountManager @LazyInjectService private var mailboxInfosManager: MailboxInfosManager @@ -44,7 +44,7 @@ final class SelectMailboxViewModel: ObservableObject { } } - func mailboxHasBeenSelected() { + func validateMailboxChoice() { guard let selectedMailbox, let mailboxManager = accountManager.getMailboxManager(for: selectedMailbox) else { // TODO: display snackbar return @@ -75,7 +75,7 @@ final class SelectMailboxViewModel: ObservableObject { func initDefaultAccountAndMailbox() { selectedMailbox = accountManager.currentMailboxManager?.mailbox if accountManager.accounts.count == 1 && mailboxInfosManager.getMailboxes().count == 1 { - mailboxHasBeenSelected() + validateMailboxChoice() } } }