Persist state when switching accounts

This commit is contained in:
Shadowfacts 2023-02-25 17:59:35 -05:00
parent b6f32ca6be
commit 8e9e0fa346
3 changed files with 21 additions and 13 deletions

View File

@ -218,9 +218,9 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDelegate
} else { } else {
direction = .none direction = .none
} }
container.setRoot(newRoot, animating: direction) container.setRoot(newRoot, for: account, animating: direction)
} else { } else {
window!.rootViewController = AccountSwitchingContainerViewController(root: newRoot) window!.rootViewController = AccountSwitchingContainerViewController(root: newRoot, for: account)
} }
} }

View File

@ -11,9 +11,13 @@ import ScreenCorners
class AccountSwitchingContainerViewController: UIViewController { class AccountSwitchingContainerViewController: UIViewController {
private var currentAccountID: String
private(set) var root: TuskerRootViewController private(set) var root: TuskerRootViewController
init(root: TuskerRootViewController) { private var userActivities: [String: NSUserActivity] = [:]
init(root: TuskerRootViewController, for account: LocalData.UserAccountInfo) {
self.currentAccountID = account.id
self.root = root self.root = root
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
@ -29,14 +33,27 @@ class AccountSwitchingContainerViewController: UIViewController {
embedChild(root) embedChild(root)
} }
func setRoot(_ newRoot: TuskerRootViewController, animating direction: AnimationDirection) { func setRoot(_ newRoot: TuskerRootViewController, for account: LocalData.UserAccountInfo, animating direction: AnimationDirection) {
let oldRoot = self.root let oldRoot = self.root
if direction == .none { if direction == .none {
oldRoot.removeViewAndController() oldRoot.removeViewAndController()
} }
if let activity = oldRoot.stateRestorationActivity() {
stateRestorationLogger.debug("AccountSwitchingContainer: saving \(activity.activityType, privacy: .public) for \(self.currentAccountID, privacy: .public)")
userActivities[currentAccountID] = activity
}
self.currentAccountID = account.id
self.root = newRoot self.root = newRoot
embedChild(newRoot) embedChild(newRoot)
if let activity = userActivities.removeValue(forKey: account.id) {
stateRestorationLogger.debug("AccountSwitchingContainer: restoring \(activity.activityType, privacy: .public) for \(account.id, privacy: .public)")
let context = StateRestorationUserActivityHandlingContext(root: newRoot)
_ = activity.handleResume(manager: UserActivityManager(scene: view.window!.windowScene!, context: context))
context.finalize(activity: activity)
}
if direction != .none { if direction != .none {
if UIAccessibility.prefersCrossFadeTransitions { if UIAccessibility.prefersCrossFadeTransitions {
newRoot.view.alpha = 0 newRoot.view.alpha = 0

View File

@ -32,15 +32,6 @@ class UserActivityManager {
scene.session.mastodonController! scene.session.mastodonController!
} }
private func getMainViewController() -> TuskerRootViewController {
let window = scene.windows.first { $0.isKeyWindow } ?? scene.windows.first!
return window.rootViewController as! TuskerRootViewController
}
private func present(_ vc: UIViewController, animated: Bool = true) {
getMainViewController().present(vc, animated: animated)
}
static func getAccount(from activity: NSUserActivity) -> LocalData.UserAccountInfo? { static func getAccount(from activity: NSUserActivity) -> LocalData.UserAccountInfo? {
guard let id = activity.userInfo?["accountID"] as? String else { guard let id = activity.userInfo?["accountID"] as? String else {
return nil return nil