Add logging in to additional accounts and switching accounts via Preferences

See #16
This commit is contained in:
Shadowfacts 2020-01-18 22:43:10 -05:00
parent 08c84688cf
commit 863867c522
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 82 additions and 17 deletions

View File

@ -7,8 +7,9 @@
//
import Foundation
import Combine
class LocalData {
class LocalData: ObservableObject {
static let shared = LocalData()
@ -52,6 +53,7 @@ class LocalData {
}
}
set {
objectWillChange.send()
let array = newValue.map { (info) in
return [
"instanceURL": info.instanceURL.absoluteString,
@ -71,6 +73,7 @@ class LocalData {
return defaults.string(forKey: mostRecentAccountKey)
}
set {
objectWillChange.send()
defaults.set(newValue, forKey: mostRecentAccountKey)
}
}

View File

@ -13,8 +13,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
// let mastodonController = MastodonController.shared
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
@ -111,6 +109,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
DraftsManager.save()
}
func activateAccount(_ account: LocalData.UserAccountInfo) {
LocalData.shared.mostRecentAccount = account.accessToken
window!.windowScene!.session.mastodonController = MastodonController.getForAccount(account)
showAppUI()
}
func showAppUI() {
let mastodonController = window!.windowScene!.session.mastodonController!
mastodonController.getOwnAccount()
@ -138,8 +142,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
extension SceneDelegate: OnboardingViewControllerDelegate {
func didFinishOnboarding(account: LocalData.UserAccountInfo) {
LocalData.shared.mostRecentAccount = account.accessToken
window!.windowScene!.session.mastodonController = MastodonController.getForAccount(account)
showAppUI()
activateAccount(account)
}
}

View File

@ -12,7 +12,7 @@ import SwiftUI
class PreferencesNavigationController: UINavigationController {
init(mastodonController: MastodonController) {
let view = PreferencesView(currentAccount: mastodonController.accountInfo!)
let view = PreferencesView()
let hostingController = UIHostingController(rootView: view)
super.init(rootViewController: hostingController)
hostingController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
@ -22,6 +22,13 @@ class PreferencesNavigationController: UINavigationController {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(showAddAccount), name: .addAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(activateAccount(_:)), name: .activateAccount, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
@ -33,4 +40,34 @@ class PreferencesNavigationController: UINavigationController {
dismiss(animated: true)
}
@objc func showAddAccount() {
let onboardingController = OnboardingViewController()
onboardingController.onboardingDelegate = self
onboardingController.instanceSelector.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelAddAccount))
show(onboardingController, sender: self)
}
@objc func cancelAddAccount() {
dismiss(animated: true)
}
@objc func activateAccount(_ notification: Notification) {
let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo
let sceneDelegate = self.view.window!.windowScene!.delegate as! SceneDelegate
dismiss(animated: true) {
sceneDelegate.activateAccount(account)
}
}
}
extension Notification.Name {
static let addAccount = Notification.Name("Tusker.addAccount")
static let activateAccount = Notification.Name("Tusker.activateAccount")
}
extension PreferencesNavigationController: OnboardingViewControllerDelegate {
func didFinishOnboarding(account: LocalData.UserAccountInfo) {
LocalData.shared.mostRecentAccount = account.accessToken
}
}

View File

@ -7,8 +7,8 @@
import SwiftUI
struct PreferencesView : View {
var currentAccount: LocalData.UserAccountInfo
struct PreferencesView: View {
@ObservedObject var localData = LocalData.shared
@State private var showingLogoutConfirmation = false
var body: some View {
@ -16,14 +16,37 @@ struct PreferencesView : View {
// NavigationView {
List {
Section {
ForEach(localData.accounts, id: \.accessToken) { (account) in
Button(action: {
NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": account])
}) {
HStack {
Text(account.username)
.foregroundColor(.primary)
Spacer()
if account.accessToken == self.localData.mostRecentAccount {
Image(systemName: "checkmark")
.renderingMode(.template)
.foregroundColor(.secondary)
}
}
}
}
Button(action: {
NotificationCenter.default.post(name: .addAccount, object: nil)
}) {
Text("Add Account...")
}
if localData.mostRecentAccount != nil {
Button(action: {
self.showingLogoutConfirmation = true
}) {
Text("Logout")
Text("Logout from current")
}.alert(isPresented: $showingLogoutConfirmation) {
Alert(title: Text("Are you sure you want to logout?"), message: nil, primaryButton: .destructive(Text("Logout"), action: self.logoutPressed), secondaryButton: .cancel())
}
}
}
Section {
NavigationLink(destination: AppearancePrefsView()) {
@ -50,7 +73,8 @@ struct PreferencesView : View {
}
func logoutPressed() {
LocalData.shared.removeAccount(currentAccount)
// LocalData.shared.removeAccount(currentAccount)
localData.removeAccount(localData.getMostRecentAccount()!)
NotificationCenter.default.post(name: .userLoggedOut, object: nil)
}
}
@ -58,8 +82,7 @@ struct PreferencesView : View {
#if DEBUG
struct PreferencesView_Previews : PreviewProvider {
static var previews: some View {
let account = LocalData.UserAccountInfo(instanceURL: URL(string: "https://mastodon.social")!, clientID: "clientID", clientSecret: "clientSecret", username: "example", accessToken: "accessToken")
return PreferencesView(currentAccount: account)
return PreferencesView()
}
}
#endif

View File

@ -128,7 +128,7 @@ class ProfileTableViewController: EnhancedTableViewController {
}
@objc func updateUIForPreferences() {
guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
guard let accountID = accountID, let account = mastodonController.cache.account(for: accountID) else { return }
navigationItem.title = account.realDisplayName
}