Skip to content

Commit

Permalink
refcator: Use same components for action elements
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jul 17, 2023
1 parent 934b582 commit f719aff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
30 changes: 16 additions & 14 deletions Mail/Views/Bottom sheets/Actions/ActionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -96,7 +75,7 @@ struct ContactActionsView: View {

// MARK: - Actions

private func handleAction(_ action: ContactAction) {
private func handleAction(_ action: Action) {
switch action {
case .writeEmailAction:
writeEmail()
Expand Down
14 changes: 11 additions & 3 deletions Mail/Views/Bottom sheets/Actions/ReportJunkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
}
Expand Down

0 comments on commit f719aff

Please sign in to comment.