From 65ceb83d2daaf7d0e1ac30d9b96ed61cec701286 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 16 Sep 2019 13:12:23 -0400 Subject: [PATCH] Add logout button to preferences --- Tusker/AppDelegate.swift | 6 +++ Tusker/LocalData.swift | 4 ++ .../Screens/Preferences/PreferencesView.swift | 43 ++++++++++++++----- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index b381219d7c..4b5272c292 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -22,6 +22,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { showOnboardingUI() } + NotificationCenter.default.addObserver(self, selector: #selector(onUserLoggedOut), name: .userLoggedOut, object: nil) + window!.makeKeyAndVisible() return true @@ -93,6 +95,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { onboarding.onboardingDelegate = self window!.rootViewController = onboarding } + + @objc func onUserLoggedOut() { + showOnboardingUI() + } } diff --git a/Tusker/LocalData.swift b/Tusker/LocalData.swift index 7d7fef2868..26a7ccbbf5 100644 --- a/Tusker/LocalData.swift +++ b/Tusker/LocalData.swift @@ -68,3 +68,7 @@ class LocalData { } } + +extension Notification.Name { + static let userLoggedOut = Notification.Name("userLoggedOut") +} diff --git a/Tusker/Screens/Preferences/PreferencesView.swift b/Tusker/Screens/Preferences/PreferencesView.swift index 033402ced0..9df96be2fc 100644 --- a/Tusker/Screens/Preferences/PreferencesView.swift +++ b/Tusker/Screens/Preferences/PreferencesView.swift @@ -8,21 +8,35 @@ import SwiftUI struct PreferencesView : View { + @State private var showingLogoutConfirmation = false + var body: some View { // workaround: the navigation view is provided by MyProfileTableViewController so that it can inject the Done button // NavigationView { List { - NavigationLink(destination: AppearancePrefsView()) { - Text("Appearance") + Section { + Button(action: { + self.showingLogoutConfirmation = true + }) { + Text("Logout") + }.alert(isPresented: $showingLogoutConfirmation) { + Alert(title: Text("Are you sure you want to logout?"), message: nil, primaryButton: .destructive(Text("Logout"), action: self.logoutPressed), secondaryButton: .cancel()) + } } - NavigationLink(destination: BehaviorPrefsView()) { - Text("Behavior") - } - NavigationLink(destination: WellnessPrefsView()) { - Text("Digital Wellness") - } - NavigationLink(destination: AdvancedPrefsView()) { - Text("Advanced") + + Section { + NavigationLink(destination: AppearancePrefsView()) { + Text("Appearance") + } + NavigationLink(destination: BehaviorPrefsView()) { + Text("Behavior") + } + NavigationLink(destination: WellnessPrefsView()) { + Text("Digital Wellness") + } + NavigationLink(destination: AdvancedPrefsView()) { + Text("Advanced") + } } } .listStyle(GroupedListStyle()) @@ -33,6 +47,15 @@ struct PreferencesView : View { } // } } + + func logoutPressed() { + LocalData.shared.onboardingComplete = false + LocalData.shared.instanceURL = nil + LocalData.shared.clientID = nil + LocalData.shared.clientSecret = nil + LocalData.shared.accessToken = nil + NotificationCenter.default.post(name: .userLoggedOut, object: nil) + } } #if DEBUG