diff --git a/Mail/Helpers/PreviewHelper.swift b/Mail/Helpers/PreviewHelper.swift index 7aefb8a6c..d5e921a7a 100644 --- a/Mail/Helpers/PreviewHelper.swift +++ b/Mail/Helpers/PreviewHelper.swift @@ -21,6 +21,7 @@ import InfomaniakLogin import MailCore import RealmSwift import SwiftUI +import InfomaniakCore struct PreviewHelper { static let sampleMailboxManager = MailboxManager(mailbox: sampleMailbox, apiFetcher: MailApiFetcher()) @@ -122,4 +123,6 @@ struct PreviewHelper { ) static let sampleMergedContact = MergedContact(email: "mergedContact@example.com", remote: nil, local: nil) + + static let sampleAccount = Account(apiToken: ApiToken(accessToken: "", expiresIn: 0, refreshToken: "", scope: "", tokenType: "", userId: 0, expirationDate: Date())) } diff --git a/Mail/Views/Bottom sheets/LogoutConfirmationView.swift b/Mail/Views/Bottom sheets/LogoutConfirmationView.swift new file mode 100644 index 000000000..e59c01b0f --- /dev/null +++ b/Mail/Views/Bottom sheets/LogoutConfirmationView.swift @@ -0,0 +1,65 @@ +/* + Infomaniak Mail - iOS App + Copyright (C) 2022 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 SwiftUI +import MailResources +import MailCore +import InfomaniakCore + +struct LogoutConfirmationView: View { + + @Environment(\.window) private var window + + let account: Account + + let state: AccountAlert + + var body: some View { + VStack(alignment: .leading, spacing: 24) { + Text( MailResourcesStrings.Localizable.confirmLogoutTitle(account.user.email)) + .textStyle(.header5) + Text(MailResourcesStrings.Localizable.confirmLogoutDescription) + .textStyle(.bodySecondary) + BottomSheetButtonsView(primaryButtonTitle: MailResourcesStrings.Localizable.buttonLogout, + secondaryButtonTitle: MailResourcesStrings.Localizable.buttonCancel, + primaryButtonAction: logout, + secondaryButtonAction: cancel) + } + } + + private func cancel() { + state.state = nil + } + + private func logout() { + AccountManager.instance.removeTokenAndAccount(token: account.token) + if let nextAccount = AccountManager.instance.accounts.first { + (window?.windowScene?.delegate as? SceneDelegate)?.switchAccount(nextAccount) + } else { + (window?.windowScene?.delegate as? SceneDelegate)?.showLoginView() + } + AccountManager.instance.saveAccounts() + state.state = nil + } +} + +struct LogoutConfirmationView_Previews: PreviewProvider { + static var previews: some View { + LogoutConfirmationView(account: PreviewHelper.sampleAccount, state: AccountAlert()) + } +} diff --git a/Mail/Views/Switch User/AccountView.swift b/Mail/Views/Switch User/AccountView.swift index cfc65d452..60e8c43d3 100644 --- a/Mail/Views/Switch User/AccountView.swift +++ b/Mail/Views/Switch User/AccountView.swift @@ -49,13 +49,20 @@ class AccountSheet: SheetState { } } +class AccountAlert: SheetState { + enum State { + case logout + } +} + struct AccountView: View { @Environment(\.dismiss) private var dismiss @Environment(\.window) private var window @State private var avatarImage = Image(resource: MailResourcesAsset.placeholderAvatar) - @StateObject private var account = AccountManager.instance.currentAccount! + @StateObject private var account = AccountManager.instance.currentAccount @StateObject private var sheet = AccountSheet() + @StateObject private var alert = AccountAlert() @State private var delegate = AccountViewDelegate() let mailboxes: [Mailbox] @@ -108,8 +115,10 @@ struct AccountView: View { } // Buttons - LargeButton(title: MailResourcesStrings.Localizable.buttonAccountDisconnect, action: logout) - .padding(.bottom, 24) + LargeButton(title: MailResourcesStrings.Localizable.buttonAccountDisconnect) { + alert.state = .logout + } + .padding(.bottom, 24) Button { sheet.state = .deleteAccount } label: { @@ -137,17 +146,15 @@ struct AccountView: View { EmptyView() } } - .defaultAppStorage(.shared) - } - - private func logout() { - AccountManager.instance.removeTokenAndAccount(token: account.token) - if let nextAccount = AccountManager.instance.accounts.first { - (window?.windowScene?.delegate as? SceneDelegate)?.switchAccount(nextAccount) - } else { - (window?.windowScene?.delegate as? SceneDelegate)?.showLoginView() + .customAlert(isPresented: $alert.isShowing) { + switch alert.state { + case .logout: + LogoutConfirmationView(account: account, state: alert) + case .none: + EmptyView() + } } - AccountManager.instance.saveAccounts() + .defaultAppStorage(.shared) } } diff --git a/MailResources/en.lproj/Localizable.strings b/MailResources/en.lproj/Localizable.strings index ab996a330..ffbfa5fc9 100644 Binary files a/MailResources/en.lproj/Localizable.strings and b/MailResources/en.lproj/Localizable.strings differ diff --git a/MailResources/fr.lproj/Localizable.strings b/MailResources/fr.lproj/Localizable.strings index 272be046d..5c25aa609 100644 Binary files a/MailResources/fr.lproj/Localizable.strings and b/MailResources/fr.lproj/Localizable.strings differ