Add state restoration for current account in main scene

This commit is contained in:
Shadowfacts 2021-06-06 14:55:04 -04:00
parent 806591f5b7
commit ceb58f1d92
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 38 additions and 4 deletions

View File

@ -65,6 +65,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
switch type { switch type {
case .mainScene:
return "main-scene"
case .showConversation, case .showConversation,
.showTimeline, .showTimeline,
.checkNotifications, .checkNotifications,

View File

@ -69,6 +69,7 @@
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.bookmarks</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.bookmarks</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.my-profile</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.my-profile</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.show-profile</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.show-profile</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.main-scene</string>
</array> </array>
<key>UIApplicationSceneManifest</key> <key>UIApplicationSceneManifest</key>
<dict> <dict>

View File

@ -15,12 +15,15 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? var window: UIWindow?
private var launchActivity: NSUserActivity?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 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 } guard let windowScene = scene as? UIWindowScene else { return }
if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
launchActivity = activity
}
window = UIWindow(windowScene: windowScene) window = UIWindow(windowScene: windowScene)
if let report = AppDelegate.pendingCrashReport { if let report = AppDelegate.pendingCrashReport {
@ -91,6 +94,14 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate {
DraftsManager.save() 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) { func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground. // Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background. // 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) { func showAppOrOnboardingUI(session: UISceneSession? = nil) {
let session = session ?? window!.windowScene!.session let session = session ?? window!.windowScene!.session
if LocalData.shared.onboardingComplete { 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 { if session.mastodonController == nil {
session.mastodonController = MastodonController.getForAccount(account) session.mastodonController = MastodonController.getForAccount(account)
} }

View File

@ -37,6 +37,15 @@ class UserActivityManager {
} }
return LocalData.shared.getAccount(id: id) 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 // MARK: - New Post
static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity { static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity {

View File

@ -9,6 +9,7 @@
import Foundation import Foundation
enum UserActivityType: String { enum UserActivityType: String {
case mainScene = "space.vaccor.Tusker.activity.main-scene"
case newPost = "space.vaccor.Tusker.activity.new-post" case newPost = "space.vaccor.Tusker.activity.new-post"
case checkNotifications = "space.vaccor.Tusker.activity.check-notifications" case checkNotifications = "space.vaccor.Tusker.activity.check-notifications"
case showTimeline = "space.vaccor.Tusker.activity.show-timeline" case showTimeline = "space.vaccor.Tusker.activity.show-timeline"
@ -22,6 +23,8 @@ enum UserActivityType: String {
extension UserActivityType { extension UserActivityType {
var handle: (NSUserActivity) -> Void { var handle: (NSUserActivity) -> Void {
switch self { switch self {
case .mainScene:
fatalError("cannot handle main scene activity")
case .newPost: case .newPost:
return UserActivityManager.handleNewPost return UserActivityManager.handleNewPost
case .checkNotifications: case .checkNotifications: