From ea380216cfd784ce6b600c1ebadaa932ac9b15f1 Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Mon, 17 Jul 2023 13:36:34 +0200 Subject: [PATCH] refcator: Use same components for action elements --- .../Bottom sheets/Actions/ActionsView.swift | 30 +++++----- .../Actions/ActionsViewModel.swift | 18 ++++++ .../ContactActionsHeaderView.swift | 2 +- .../{ => Actions}/ContactActionsView.swift | 59 ++++++------------- .../Actions/ReportJunkView.swift | 14 ++++- 5 files changed, 65 insertions(+), 58 deletions(-) rename Mail/Views/Bottom sheets/{ => Actions}/ContactActionsHeaderView.swift (99%) rename Mail/Views/Bottom sheets/{ => Actions}/ContactActionsView.swift (66%) diff --git a/Mail/Views/Bottom sheets/Actions/ActionsView.swift b/Mail/Views/Bottom sheets/Actions/ActionsView.swift index 35eb9445e8..d8d3bf060f 100644 --- a/Mail/Views/Bottom sheets/Actions/ActionsView.swift +++ b/Mail/Views/Bottom sheets/Actions/ActionsView.swift @@ -63,8 +63,15 @@ struct ActionsView: View { if action != viewModel.listActions.first { IKDivider() } - ActionView(viewModel: viewModel, action: action) - .padding(.horizontal, 24) + + ActionView(action: action) { + Task { + await tryOrDisplayError { + try await viewModel.didTap(action: action) + } + } + } + .padding(.horizontal, 24) } } .padding(.horizontal, 8) @@ -118,27 +125,22 @@ struct QuickActionView: View { } struct ActionView: View { - @Environment(\.dismiss) var dismiss - @ObservedObject var viewModel: ActionsViewModel - let action: Action + @Environment(\.dismiss) private var dismiss - @AppStorage(UserDefaults.shared.key(.accentColor)) private var accentColor = DefaultPreferences.accentColor + let action: Action + let handler: () -> Void var body: some View { Button { dismiss() - Task { - await tryOrDisplayError { - try await viewModel.didTap(action: action) - } - } + handler() } label: { - HStack(spacing: 20) { + HStack(spacing: 24) { action.icon .resizable() .scaledToFit() - .frame(width: 21, height: 21) - .foregroundColor(action == .report ? MailResourcesAsset.princeColor : accentColor.primary) + .frame(width: 24, height: 24) + .foregroundColor(action == .report ? MailResourcesAsset.princeColor.swiftUIColor : .accentColor) Text(action.title) .foregroundColor(action == .report ? MailResourcesAsset.princeColor : MailResourcesAsset.textPrimaryColor) .textStyle(.body) diff --git a/Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift b/Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift index ac1c1c3848..b4feb6f34e 100644 --- a/Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift +++ b/Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift @@ -157,6 +157,24 @@ struct Action: Identifiable, Equatable { icon: MailResourcesAsset.drawer, matomoName: "moveToInbox" ) + static let writeEmailAction = Action( + id: 18, + title: MailResourcesStrings.Localizable.contactActionWriteEmail, + icon: MailResourcesAsset.pencil, + matomoName: "writeEmail" + ) + static let addContactsAction = Action( + id: 19, + title: MailResourcesStrings.Localizable.contactActionAddToContacts, + icon: MailResourcesAsset.userAdd, + matomoName: "addToContacts" + ) + static let copyEmailAction = Action( + id: 20, + title: MailResourcesStrings.Localizable.contactActionCopyEmailAddress, + icon: MailResourcesAsset.duplicate, + matomoName: "copyEmailAddress" + ) static let quickActions: [Action] = [.reply, .replyAll, .forward, .delete] diff --git a/Mail/Views/Bottom sheets/ContactActionsHeaderView.swift b/Mail/Views/Bottom sheets/Actions/ContactActionsHeaderView.swift similarity index 99% rename from Mail/Views/Bottom sheets/ContactActionsHeaderView.swift rename to Mail/Views/Bottom sheets/Actions/ContactActionsHeaderView.swift index bb6832e6b9..e792a3f5db 100644 --- a/Mail/Views/Bottom sheets/ContactActionsHeaderView.swift +++ b/Mail/Views/Bottom sheets/Actions/ContactActionsHeaderView.swift @@ -23,7 +23,7 @@ struct ContactActionsHeaderView: View { let displayablePerson: CommonContact var body: some View { HStack { - AvatarView(displayablePerson: displayablePerson, size: 32) + AvatarView(displayablePerson: displayablePerson, size: 40) .accessibilityHidden(true) VStack(alignment: .leading) { Text(displayablePerson, format: .displayablePerson()) diff --git a/Mail/Views/Bottom sheets/ContactActionsView.swift b/Mail/Views/Bottom sheets/Actions/ContactActionsView.swift similarity index 66% rename from Mail/Views/Bottom sheets/ContactActionsView.swift rename to Mail/Views/Bottom sheets/Actions/ContactActionsView.swift index 761371e604..6a6a17be7d 100644 --- a/Mail/Views/Bottom sheets/ContactActionsView.swift +++ b/Mail/Views/Bottom sheets/Actions/ContactActionsView.swift @@ -24,14 +24,16 @@ import MailResources import SwiftUI struct ContactActionsView: View { - @EnvironmentObject var mailboxManager: MailboxManager - @Environment(\.dismiss) var dismiss + @Environment(\.dismiss) private var dismiss + + @EnvironmentObject private var mailboxManager: MailboxManager + @LazyInjectService private var matomo: MatomoUtils @State private var writtenToRecipient: Recipient? let recipient: Recipient - private var actions: [ContactAction] { + private var actions: [Action] { let isRemoteContact = mailboxManager.contactManager.getContact(for: recipient)?.remote != nil if isRemoteContact { @@ -41,53 +43,30 @@ struct ContactActionsView: View { } } - private struct ContactAction: Hashable { - let name: String - let image: UIImage - let matomoName: String - - static let writeEmailAction = ContactAction( - name: MailResourcesStrings.Localizable.contactActionWriteEmail, - image: MailResourcesAsset.pencil.image, - matomoName: "writeEmail" - ) - static let addContactsAction = ContactAction( - name: MailResourcesStrings.Localizable.contactActionAddToContacts, - image: MailResourcesAsset.userAdd.image, - matomoName: "addToContacts" - ) - static let copyEmailAction = ContactAction( - name: MailResourcesStrings.Localizable.contactActionCopyEmailAddress, - image: MailResourcesAsset.duplicate.image, - matomoName: "copyEmailAddress" - ) - } - var body: some View { - VStack(alignment: .leading, spacing: 16) { + VStack(alignment: .leading, spacing: 12) { ContactActionsHeaderView(displayablePerson: CommonContact( recipient: recipient, contextMailboxManager: mailboxManager )) + .padding(.horizontal, 16) - ForEach(actions, id: \.self) { action in - Button { - matomo.track(eventWithCategory: .contactActions, name: action.matomoName) - handleAction(action) - } label: { - HStack(spacing: 20) { - Image(uiImage: action.image) - Text(action.name) - .textStyle(.body) - } - } - if action != actions.last { + ForEach(actions) { action in + if action != actions.first { IKDivider() } + + ActionView(action: action) { + if let matomoName = action.matomoName { + matomo.track(eventWithCategory: .contactActions, name: matomoName) + } + handleAction(action) + } + .padding(.horizontal, 24) } } .frame(maxWidth: .infinity, alignment: .leading) - .padding(.horizontal, 24) + .padding(.horizontal, 8) .sheet(item: $writtenToRecipient) { writtenToRecipient in ComposeMessageView.writingTo(recipient: writtenToRecipient, mailboxManager: mailboxManager) } @@ -96,7 +75,7 @@ struct ContactActionsView: View { // MARK: - Actions - private func handleAction(_ action: ContactAction) { + private func handleAction(_ action: Action) { switch action { case .writeEmailAction: writeEmail() diff --git a/Mail/Views/Bottom sheets/Actions/ReportJunkView.swift b/Mail/Views/Bottom sheets/Actions/ReportJunkView.swift index 7c98ac597f..11b9bdbce0 100644 --- a/Mail/Views/Bottom sheets/Actions/ReportJunkView.swift +++ b/Mail/Views/Bottom sheets/Actions/ReportJunkView.swift @@ -43,15 +43,23 @@ struct ReportJunkView: View { } var body: some View { - Group { + VStack(alignment: .leading, spacing: 12) { ForEach(actions) { action in if action != actions.first { IKDivider() } - ActionView(viewModel: viewModel, action: action) - .padding(.horizontal, 24) + + ActionView(action: action) { + Task { + await tryOrDisplayError { + try await viewModel.didTap(action: action) + } + } + } + .padding(.horizontal, 24) } } + .padding(.horizontal, 8) .matomoView(view: [MatomoUtils.View.bottomSheet.displayName, "ReportJunkView"]) } }