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

Asks user for confirmation before deleting token from device #217

Merged
merged 11 commits into from
Apr 24, 2018
45 changes: 39 additions & 6 deletions Authenticator/Source/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ class AppController {
}

case let .deletePersistentToken(persistentToken, failure):
do {
try store.deletePersistentToken(persistentToken)
updateView()
} catch {
handleEvent(failure(error))
}
confirmDeletion(of: persistentToken, failure: failure)

case let .showErrorMessage(message):
SVProgressHUD.showError(withStatus: message)
Expand Down Expand Up @@ -205,6 +200,44 @@ class AppController {
func addTokenFromURL(_ token: Token) {
handleAction(.addTokenFromURL(token))
}

private func confirmDeletion(of persistentToken: PersistentToken, failure: @escaping (Error) -> Root.Event) {
let messagePrefix = persistentToken.token.displayName.map({ "The token “\($0)”" }) ?? "The unnamed token"
let message = messagePrefix + " will be permanently deleted from this device."

let alert = UIAlertController(title: "Delete Token?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { [weak self] _ in
self?.permanentlyDelete(persistentToken, failure: failure)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

let presenter = topViewController(presentedFrom: rootViewController)
presenter.present(alert, animated: true)
}

private func permanentlyDelete(_ persistentToken: PersistentToken, failure: @escaping (Error) -> Root.Event) {
do {
try store.deletePersistentToken(persistentToken)
updateView()
} catch {
handleEvent(failure(error))
}
}
}

private extension Token {
var displayName: String? {
switch (!name.isEmpty, !issuer.isEmpty) {
case (true, true):
return "\(issuer): \(name)"
case (true, false):
return name
case (false, true):
return issuer
case (false, false):
return nil
}
}
}

private extension DisplayTime {
Expand Down