2019-11-17 19:31:07 +00:00
|
|
|
//
|
|
|
|
// PreferencesHostingController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/17/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
class PreferencesNavigationController: UINavigationController {
|
2020-01-20 04:16:36 +00:00
|
|
|
|
|
|
|
private var isSwitchingAccounts = false
|
2019-11-17 19:31:07 +00:00
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
init(mastodonController: MastodonController) {
|
2020-01-19 03:43:10 +00:00
|
|
|
let view = PreferencesView()
|
2020-01-08 02:29:15 +00:00
|
|
|
let hostingController = UIHostingController(rootView: view)
|
2019-11-17 19:31:07 +00:00
|
|
|
super.init(rootViewController: hostingController)
|
|
|
|
hostingController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2020-01-19 03:43:10 +00:00
|
|
|
|
|
|
|
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)
|
2020-01-19 16:52:06 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userLoggedOut), name: .userLoggedOut, object: nil)
|
2020-01-19 03:43:10 +00:00
|
|
|
}
|
2019-11-17 19:31:07 +00:00
|
|
|
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
super.viewWillDisappear(animated)
|
|
|
|
|
2020-01-20 04:16:36 +00:00
|
|
|
if !isSwitchingAccounts {
|
|
|
|
// workaround for onDisappear not being called when a modally presented UIHostingController is dismissed
|
|
|
|
NotificationCenter.default.post(name: .preferencesChanged, object: nil)
|
|
|
|
}
|
2019-11-17 19:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func donePressed() {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
2020-01-19 03:43:10 +00:00
|
|
|
|
|
|
|
@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() {
|
2020-01-19 16:52:06 +00:00
|
|
|
dismiss(animated: true) // dismisses instance selector
|
2020-01-19 03:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func activateAccount(_ notification: Notification) {
|
2020-02-29 22:36:54 +00:00
|
|
|
// TODO: this is a temporary measure
|
|
|
|
// when switching accounts shortly after adding a new one, there is an old instance of PreferncesNavigationController still around
|
|
|
|
// which tries to handle the notification but is unable to because it no longer is in a window (and therefore doesn't have a scene delegate)
|
|
|
|
// the propper fix would be to figure out what's leaking instances of this class
|
|
|
|
guard let window = self.view.window,
|
|
|
|
let windowScene = window.windowScene,
|
|
|
|
let sceneDelegate = windowScene.delegate as? SceneDelegate else {
|
|
|
|
return
|
|
|
|
}
|
2020-01-19 03:43:10 +00:00
|
|
|
let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo
|
2020-01-20 04:16:36 +00:00
|
|
|
isSwitchingAccounts = true
|
2020-01-19 16:52:06 +00:00
|
|
|
dismiss(animated: true) { // dismiss preferences
|
2020-11-11 20:26:25 +00:00
|
|
|
sceneDelegate.activateAccount(account, animated: true)
|
2020-01-19 03:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 16:52:06 +00:00
|
|
|
|
|
|
|
@objc func userLoggedOut() {
|
2020-02-29 22:36:54 +00:00
|
|
|
guard let window = self.view.window,
|
|
|
|
let windowScene = window.windowScene,
|
|
|
|
let sceneDelegate = windowScene.delegate as? SceneDelegate else {
|
|
|
|
return
|
|
|
|
}
|
2020-01-20 04:16:36 +00:00
|
|
|
isSwitchingAccounts = true
|
2020-01-19 16:52:06 +00:00
|
|
|
dismiss(animated: true) { // dismiss preferences
|
|
|
|
sceneDelegate.logoutCurrent()
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 03:43:10 +00:00
|
|
|
|
|
|
|
}
|
2019-11-17 19:31:07 +00:00
|
|
|
|
2020-01-19 03:43:10 +00:00
|
|
|
extension PreferencesNavigationController: OnboardingViewControllerDelegate {
|
|
|
|
func didFinishOnboarding(account: LocalData.UserAccountInfo) {
|
2020-01-19 16:52:06 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
let sceneDelegate = self.view.window!.windowScene!.delegate as! SceneDelegate
|
|
|
|
self.dismiss(animated: true) { // dismiss instance selector
|
|
|
|
self.dismiss(animated: true) { // dismiss preferences
|
2020-11-11 20:26:25 +00:00
|
|
|
sceneDelegate.activateAccount(account, animated: false)
|
2020-01-19 16:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 03:43:10 +00:00
|
|
|
}
|
2019-11-17 19:31:07 +00:00
|
|
|
}
|