Skip to content

Commit

Permalink
refactor: Update SelectComposeMailboxView
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Feb 20, 2024
1 parent e53f273 commit 3d12fbc
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 69 deletions.
5 changes: 1 addition & 4 deletions Mail/Views/New Message/ComposeMessageIntentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions Mail/Views/New Message/Select Mailbox/AccountMailboxCell.swift
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = composeMessageIntent
_viewModel = StateObject(wrappedValue: SelectMailboxViewModel(composeMessageIntent: composeMessageIntent))
_viewModel = StateObject(wrappedValue: SelectComposeMailboxViewModel(composeMessageIntent: composeMessageIntent))
}

var body: some View {
Expand All @@ -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 {
Expand All @@ -85,7 +83,6 @@ struct SelectComposeMailboxView: View {
}
}

/// Something to dismiss the view regardless of presentation context
private func dismissMessageView() {
dismissModal()
dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComposeMessageIntent>

private(set) var accounts = [Account]()

init(composeMessageIntent: Binding<ComposeMessageIntent>) {
Expand All @@ -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
Expand All @@ -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()
}
}
}
2 changes: 1 addition & 1 deletion MailShareExtension/ComposeMessageWrapperView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit 3d12fbc

Please sign in to comment.