Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Button loader #1007

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Mail/Components/Buttons/ModalButtonsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import SwiftUI
struct ModalButtonsView: View {
@Environment(\.dismiss) private var dismiss

@State var isButtonLoading = false

let primaryButtonTitle: String
var secondaryButtonTitle: String? = MailResourcesStrings.Localizable.buttonCancel
var primaryButtonEnabled = true
let primaryButtonAction: () -> Void
let primaryButtonAction: () async -> Void
var secondaryButtonAction: (() -> Void)?

var body: some View {
Expand All @@ -40,10 +42,15 @@ struct ModalButtonsView: View {
}

MailButton(label: primaryButtonTitle) {
primaryButtonAction()
dismiss()
Task {
isButtonLoading = true
await primaryButtonAction()
isButtonLoading = false
dismiss()
}
}
.disabled(!primaryButtonEnabled)
.mailButtonLoading(isButtonLoading)
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
Expand Down
12 changes: 5 additions & 7 deletions Mail/Views/Alerts/CreateFolderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ struct CreateFolderView: View {
ModalButtonsView(primaryButtonTitle: mode.buttonTitle, primaryButtonEnabled: buttonIsEnabled) {
@InjectService var matomo: MatomoUtils
matomo.track(eventWithCategory: .createFolder, name: "confirm")
Task {
await tryOrDisplayError {
let folder = try await mailboxManager.createFolder(name: folderName)
if case .move(let moveHandler) = mode {
moveHandler(folder)
NotificationCenter.default.post(Notification(name: .dismissMoveSheetNotificationName))
}
await tryOrDisplayError {
let folder = try await mailboxManager.createFolder(name: folderName)
if case .move(let moveHandler) = mode {
moveHandler(folder)
NotificationCenter.default.post(Notification(name: .dismissMoveSheetNotificationName))
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions Mail/Views/Alerts/DetachMailboxConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ struct DetachMailboxConfirmationView: View {
}
}

private func detach() {
private func detach() async {
@InjectService var matomo: MatomoUtils
matomo.track(eventWithCategory: .invalidPasswordMailbox, name: "detachMailboxConfirm")

Task {
await tryOrDisplayError {
try await accountManager.detachMailbox(mailbox: mailbox)
navigationState.transitionToRootViewDestination(.mainView)
}
await tryOrDisplayError {
try await accountManager.detachMailbox(mailbox: mailbox)
navigationState.transitionToRootViewDestination(.mainView)
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions Mail/Views/Alerts/LogoutConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ struct LogoutConfirmationView: View {
}
}

private func logout() {
private func logout() async {
@InjectService var matomo: MatomoUtils
matomo.track(eventWithCategory: .account, name: "logOutConfirm")
Task {
@InjectService var notificationService: InfomaniakNotifications
await notificationService.removeStoredTokenFor(userId: account.userId)
@InjectService var notificationService: InfomaniakNotifications
await notificationService.removeStoredTokenFor(userId: account.userId)

accountManager.removeTokenAndAccount(account: account)
if let nextAccount = accountManager.accounts.first {
accountManager.switchAccount(newAccount: nextAccount)
}
accountManager.saveAccounts()
accountManager.removeTokenAndAccount(account: account)
if let nextAccount = accountManager.accounts.first {
accountManager.switchAccount(newAccount: nextAccount)
}
accountManager.saveAccounts()
}
}

Expand Down
18 changes: 8 additions & 10 deletions Mail/Views/Alerts/ReportPhishingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ struct ReportPhishingView: View {
}
}

private func report() {
Task {
await tryOrDisplayError {
let response = try await mailboxManager.apiFetcher.reportPhishing(message: message)
if response {
var messages = [message.freezeIfNeeded()]
messages.append(contentsOf: message.duplicates)
_ = try await mailboxManager.move(messages: messages, to: .spam)
await snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarReportPhishingConfirmation)
}
private func report() async {
await tryOrDisplayError {
let response = try await mailboxManager.apiFetcher.reportPhishing(message: message)
if response {
var messages = [message.freezeIfNeeded()]
messages.append(contentsOf: message.duplicates)
_ = try await mailboxManager.move(messages: messages, to: .spam)
snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarReportPhishingConfirmation)
}
}
}
Expand Down
24 changes: 11 additions & 13 deletions Mail/Views/Bottom sheets/ReportDisplayProblemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,18 @@ struct ReportDisplayProblemView: View {
.matomoView(view: [MatomoUtils.View.bottomSheet.displayName, "ReportDisplayProblemView"])
}

private func report() {
Task {
await tryOrDisplayError {
// Download message
let fileURL = try await mailboxManager.apiFetcher.download(message: message)
// Send it via Sentry
let fileAttachment = Attachment(path: fileURL.path,
filename: fileURL.lastPathComponent,
contentType: "message/rfc822")
_ = SentrySDK.capture(message: "Message display problem reported") { scope in
scope.add(fileAttachment)
}
snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarDisplayProblemReported)
private func report() async {
await tryOrDisplayError {
// Download message
let fileURL = try await mailboxManager.apiFetcher.download(message: message)
// Send it via Sentry
let fileAttachment = Attachment(path: fileURL.path,
filename: fileURL.lastPathComponent,
contentType: "message/rfc822")
_ = SentrySDK.capture(message: "Message display problem reported") { scope in
scope.add(fileAttachment)
}
snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarDisplayProblemReported)
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions Mail/Views/Bottom sheets/RestoreEmailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ struct RestoreEmailsView: View {
.matomoView(view: [MatomoUtils.View.bottomSheet.displayName, "RestoreEmailsView"])
}

private func restoreEmails() {
private func restoreEmails() async {
matomo.track(eventWithCategory: .restoreEmailsBottomSheet, name: "restore")
Task {
await tryOrDisplayError {
try await mailboxManager.apiFetcher.restoreBackup(mailbox: mailboxManager.mailbox, date: selectedDate)
snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarRestorationLaunched)
}
await tryOrDisplayError {
try await mailboxManager.apiFetcher.restoreBackup(mailbox: mailboxManager.mailbox, date: selectedDate)
snackbarPresenter.show(message: MailResourcesStrings.Localizable.snackbarRestorationLaunched)
}
}

Expand Down
4 changes: 1 addition & 3 deletions Mail/Views/Thread List/FlushFolderAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ struct FlushFolderAlertView: View {
@InjectService var matomo: MatomoUtils
matomo.track(eventWithCategory: .threadList, name: "empty\(folder.matomoName.capitalized)Confirm")
}
Task {
await flushAlert.completion()
}
await flushAlert.completion()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct UpdateMailboxPasswordView: View {
}
.mailButtonFullWidth(true)
.disabled(disableButton)
.mailButtonLoading(isLoading)

MailButton(label: MailResourcesStrings.Localizable.buttonRequestPassword) {
matomo.track(eventWithCategory: .invalidPasswordMailbox, name: "requestPassword")
Expand Down
Loading