diff --git a/Tusker/Scenes/MainSceneDelegate.swift b/Tusker/Scenes/MainSceneDelegate.swift index 395e107c..7ca55ae0 100644 --- a/Tusker/Scenes/MainSceneDelegate.swift +++ b/Tusker/Scenes/MainSceneDelegate.swift @@ -218,9 +218,9 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDelegate } else { direction = .none } - container.setRoot(newRoot, animating: direction) + container.setRoot(newRoot, for: account, animating: direction) } else { - window!.rootViewController = AccountSwitchingContainerViewController(root: newRoot) + window!.rootViewController = AccountSwitchingContainerViewController(root: newRoot, for: account) } } diff --git a/Tusker/Screens/Main/AccountSwitchingContainerViewController.swift b/Tusker/Screens/Main/AccountSwitchingContainerViewController.swift index d1fd7692..eb35c7ea 100644 --- a/Tusker/Screens/Main/AccountSwitchingContainerViewController.swift +++ b/Tusker/Screens/Main/AccountSwitchingContainerViewController.swift @@ -11,9 +11,13 @@ import ScreenCorners class AccountSwitchingContainerViewController: UIViewController { + private var currentAccountID: String 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 super.init(nibName: nil, bundle: nil) @@ -29,14 +33,27 @@ class AccountSwitchingContainerViewController: UIViewController { embedChild(root) } - func setRoot(_ newRoot: TuskerRootViewController, animating direction: AnimationDirection) { + func setRoot(_ newRoot: TuskerRootViewController, for account: LocalData.UserAccountInfo, animating direction: AnimationDirection) { let oldRoot = self.root if direction == .none { 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 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 UIAccessibility.prefersCrossFadeTransitions { newRoot.view.alpha = 0 diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 3e3d97fa..48fd88f1 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -32,15 +32,6 @@ class UserActivityManager { 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? { guard let id = activity.userInfo?["accountID"] as? String else { return nil