From ceb58f1d92f07ff42c280a7cd3d61f22fb7900c8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 6 Jun 2021 14:55:04 -0400 Subject: [PATCH] Add state restoration for current account in main scene --- Tusker/AppDelegate.swift | 3 +++ Tusker/Info.plist | 1 + Tusker/MainSceneDelegate.swift | 26 ++++++++++++++++++---- Tusker/Shortcuts/UserActivityManager.swift | 9 ++++++++ Tusker/Shortcuts/UserActivityType.swift | 3 +++ 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index a3ec319465..04691144a5 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -65,6 +65,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } switch type { + case .mainScene: + return "main-scene" + case .showConversation, .showTimeline, .checkNotifications, diff --git a/Tusker/Info.plist b/Tusker/Info.plist index 5e034cbba2..f778036920 100644 --- a/Tusker/Info.plist +++ b/Tusker/Info.plist @@ -69,6 +69,7 @@ $(PRODUCT_BUNDLE_IDENTIFIER).activity.bookmarks $(PRODUCT_BUNDLE_IDENTIFIER).activity.my-profile $(PRODUCT_BUNDLE_IDENTIFIER).activity.show-profile + $(PRODUCT_BUNDLE_IDENTIFIER).activity.main-scene UIApplicationSceneManifest diff --git a/Tusker/MainSceneDelegate.swift b/Tusker/MainSceneDelegate.swift index 4e51578398..30b7a3d103 100644 --- a/Tusker/MainSceneDelegate.swift +++ b/Tusker/MainSceneDelegate.swift @@ -15,12 +15,15 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? + private var launchActivity: NSUserActivity? + 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. - // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). guard let windowScene = scene as? UIWindowScene else { return } + if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity { + launchActivity = activity + } + window = UIWindow(windowScene: windowScene) if let report = AppDelegate.pendingCrashReport { @@ -91,6 +94,14 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate { DraftsManager.save() } + func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? { + if let mastodonController = window?.windowScene?.session.mastodonController { + return UserActivityManager.mainSceneActivity(accountID: mastodonController.accountInfo!.id) + } else { + return nil + } + } + func sceneWillEnterForeground(_ scene: UIScene) { // Called as the scene transitions from the background to the foreground. // Use this method to undo the changes made on entering the background. @@ -123,7 +134,14 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate { func showAppOrOnboardingUI(session: UISceneSession? = nil) { let session = session ?? window!.windowScene!.session if LocalData.shared.onboardingComplete { - let account = LocalData.shared.getMostRecentAccount()! + let account: LocalData.UserAccountInfo + if let activity = launchActivity, + let activityAccount = UserActivityManager.getAccount(from: activity) { + account = activityAccount + } else { + account = LocalData.shared.getMostRecentAccount()! + } + if session.mastodonController == nil { session.mastodonController = MastodonController.getForAccount(account) } diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 37795a4959..6db9c8e9de 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -37,6 +37,15 @@ class UserActivityManager { } return LocalData.shared.getAccount(id: id) } + + // MARK: - Main Scene + static func mainSceneActivity(accountID: String) -> NSUserActivity { + let activity = NSUserActivity(type: .mainScene) + activity.userInfo = [ + "accountID": accountID, + ] + return activity + } // MARK: - New Post static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity { diff --git a/Tusker/Shortcuts/UserActivityType.swift b/Tusker/Shortcuts/UserActivityType.swift index 77c9323dc1..d15e8e541d 100644 --- a/Tusker/Shortcuts/UserActivityType.swift +++ b/Tusker/Shortcuts/UserActivityType.swift @@ -9,6 +9,7 @@ import Foundation enum UserActivityType: String { + case mainScene = "space.vaccor.Tusker.activity.main-scene" case newPost = "space.vaccor.Tusker.activity.new-post" case checkNotifications = "space.vaccor.Tusker.activity.check-notifications" case showTimeline = "space.vaccor.Tusker.activity.show-timeline" @@ -22,6 +23,8 @@ enum UserActivityType: String { extension UserActivityType { var handle: (NSUserActivity) -> Void { switch self { + case .mainScene: + fatalError("cannot handle main scene activity") case .newPost: return UserActivityManager.handleNewPost case .checkNotifications: