Added logout

This commit is contained in:
Raimund Wege
2017-08-23 09:17:18 +02:00
parent 26cf57d484
commit 3541a1c2db
2 changed files with 25 additions and 0 deletions
@@ -27,6 +27,8 @@ extension SignUpControllerError: LocalizedError {
func signUpControllerDidCancel(_ controller: SignUpController)
func signUpControllerDidLogout(_ controller: SignUpController)
}
class SignUpController: NSObject {
@@ -50,6 +52,17 @@ class SignUpController: NSObject {
}
}
func logoutWithViewController(_ controller: UIViewController) {
do {
try Keychain.userName.deleteItem()
try Keychain.userPassword.deleteItem()
try Keychain.userAccessToken.deleteItem()
} catch {
self.showAlert(forError: error, andMessage: nil, withViewController: controller)
}
delegate?.signUpControllerDidLogout(self)
}
fileprivate func showAlert(forError error: Error, andMessage message: String?, withViewController controller: UIViewController) {
let alertController = UIAlertController.init(title: error.localizedDescription, message: message, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: Translation.Common.OK.String, style: .cancel, handler: nil)
@@ -52,6 +52,9 @@ class TrainingTableViewController: UIViewController {
if let popoverController = alertController.popoverPresentationController {
popoverController.barButtonItem = sender as? UIBarButtonItem
}
let logoutAction = UIAlertAction(title: "LOGOUT ACTION", style: .default) { [weak self] action in
self?.logout()
}
let settingsAction = UIAlertAction(title: Translation.SettingsView.Title.String, style: .default) { [weak self] action in
self?.performSegue(withIdentifier: Segue.Settings, sender: alertController)
}
@@ -59,12 +62,17 @@ class TrainingTableViewController: UIViewController {
self?.performSegue(withIdentifier: Segue.About, sender: alertController)
}
let cancelAction = UIAlertAction(title: Translation.Common.Cancel.String, style: .cancel, handler: nil)
alertController.addAction(logoutAction)
alertController.addAction(settingsAction)
alertController.addAction(aboutAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
fileprivate func logout() {
signUpController.logoutWithViewController(self)
}
// MARK: - Properties
fileprivate lazy var signUpController: SignUpController = {
@@ -117,4 +125,8 @@ extension TrainingTableViewController: SignUpControllerDelegate {
navigationController?.popViewController(animated: true)
}
func signUpControllerDidLogout(_ controller: SignUpController) {
navigationController?.popToRootViewController(animated: true)
}
}