Skip to content

Commit

Permalink
Merge pull request #465 from Infomaniak/logout-alert
Browse files Browse the repository at this point in the history
Logout alert
  • Loading branch information
PhilippeWeidmann authored Dec 23, 2022
2 parents dbdbf0b + 1b7d90b commit f58b7db
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Mail/Helpers/PreviewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import InfomaniakLogin
import MailCore
import RealmSwift
import SwiftUI
import InfomaniakCore

struct PreviewHelper {
static let sampleMailboxManager = MailboxManager(mailbox: sampleMailbox, apiFetcher: MailApiFetcher())
Expand Down Expand Up @@ -122,4 +123,6 @@ struct PreviewHelper {
)

static let sampleMergedContact = MergedContact(email: "[email protected]", remote: nil, local: nil)

static let sampleAccount = Account(apiToken: ApiToken(accessToken: "", expiresIn: 0, refreshToken: "", scope: "", tokenType: "", userId: 0, expirationDate: Date()))
}
65 changes: 65 additions & 0 deletions Mail/Views/Bottom sheets/LogoutConfirmationView.swift
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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())
}
}
33 changes: 20 additions & 13 deletions Mail/Views/Switch User/AccountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ class AccountSheet: SheetState<AccountSheet.State> {
}
}

class AccountAlert: SheetState<AccountAlert.State> {
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]
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
}
}

Expand Down
Binary file modified MailResources/en.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/fr.lproj/Localizable.strings
Binary file not shown.

0 comments on commit f58b7db

Please sign in to comment.