From a13d5d5a82d5b6dd4cc53f7dcb77ab2b2920480c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 14 Dec 2022 21:24:54 -0500 Subject: [PATCH] Fix crash when activating account in My Profile scene --- .../ConversationTableViewController.swift | 1 - .../Main/MainSidebarViewController.swift | 1 - .../PreferencesNavigationController.swift | 50 +++++++++++-------- .../Search/SearchResultsViewController.swift | 2 - Tusker/Shortcuts/UserActivityManager.swift | 10 ++-- 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index 8290634d..60e98682 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -193,7 +193,6 @@ class ConversationTableViewController: EnhancedTableViewController { let parentIDs = self.getDirectParents(inReplyTo: mainStatusInReplyToID, from: context.ancestors) let parentStatuses = context.ancestors.filter { parentIDs.contains($0.id) } - // todo: should this really be blindly adding all the descendants? await mastodonController.persistentContainer.addAll(statuses: parentStatuses + context.descendants) self.contextLoaded(mainStatus: mainStatus, context: context, parentIDs: parentIDs) diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index 07d2d7d1..18888afe 100644 --- a/Tusker/Screens/Main/MainSidebarViewController.swift +++ b/Tusker/Screens/Main/MainSidebarViewController.swift @@ -487,7 +487,6 @@ fileprivate extension MainTabBarViewController.Tab { case .explore: return "magnifyingglass" case .myProfile: - // todo: use user avatar image return "person" } } diff --git a/Tusker/Screens/Preferences/PreferencesNavigationController.swift b/Tusker/Screens/Preferences/PreferencesNavigationController.swift index c3446174..910b413f 100644 --- a/Tusker/Screens/Preferences/PreferencesNavigationController.swift +++ b/Tusker/Screens/Preferences/PreferencesNavigationController.swift @@ -61,29 +61,34 @@ class PreferencesNavigationController: UINavigationController { // 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, - // todo: my profile can be torn off into a separate window, this doesn't work - let sceneDelegate = windowScene.delegate as? MainSceneDelegate else { - return + guard let windowScene = self.view.window?.windowScene else { + return } let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo - isSwitchingAccounts = true - dismiss(animated: true) { // dismiss preferences - sceneDelegate.activateAccount(account, animated: true) + if let sceneDelegate = windowScene.delegate as? MainSceneDelegate { + isSwitchingAccounts = true + dismiss(animated: true) { // dismiss preferences + sceneDelegate.activateAccount(account, animated: true) + } + } else { + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: UserActivityManager.mainSceneActivity(accountID: account.id), options: nil) } } @objc func userLoggedOut() { - guard let window = self.view.window, - let windowScene = window.windowScene, - // todo: my profile can be torn off into a separate window, this doesn't work - let sceneDelegate = windowScene.delegate as? MainSceneDelegate else { - return + guard let windowScene = self.view.window?.windowScene else { + return } - isSwitchingAccounts = true - dismiss(animated: true) { // dismiss preferences - sceneDelegate.logoutCurrent() + if let sceneDelegate = windowScene.delegate as? MainSceneDelegate { + isSwitchingAccounts = true + dismiss(animated: true) { // dismiss preferences + sceneDelegate.logoutCurrent() + } + } else { + LocalData.shared.removeAccount(LocalData.shared.getMostRecentAccount()!) + let accountID = LocalData.shared.getMostRecentAccount()?.id + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: UserActivityManager.mainSceneActivity(accountID: accountID), options: nil) + UIApplication.shared.requestSceneSessionDestruction(windowScene.session, options: nil) } } @@ -91,12 +96,15 @@ class PreferencesNavigationController: UINavigationController { extension PreferencesNavigationController: OnboardingViewControllerDelegate { func didFinishOnboarding(account: LocalData.UserAccountInfo) { - DispatchQueue.main.async { - // todo: my profile can be torn off into a separate window, this will crash - let sceneDelegate = self.view.window!.windowScene!.delegate as! MainSceneDelegate - self.dismiss(animated: true) { // dismiss instance selector - self.dismiss(animated: true) { // dismiss preferences + guard let windowScene = self.view.window?.windowScene else { + return + } + self.dismiss(animated: true) { // dismiss instance selector + self.dismiss(animated: true) { // dismiss preferences + if let sceneDelegate = windowScene.delegate as? MainSceneDelegate { sceneDelegate.activateAccount(account, animated: false) + } else { + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: UserActivityManager.mainSceneActivity(accountID: account.id), options: nil) } } } diff --git a/Tusker/Screens/Search/SearchResultsViewController.swift b/Tusker/Screens/Search/SearchResultsViewController.swift index b4ce3b72..e261b681 100644 --- a/Tusker/Screens/Search/SearchResultsViewController.swift +++ b/Tusker/Screens/Search/SearchResultsViewController.swift @@ -136,8 +136,6 @@ class SearchResultsViewController: EnhancedTableViewController { if let sourceDataSource = source.dataSource { dataSource.apply(sourceDataSource.snapshot()) } - // todo: check if the search needs to be performed before searching -// performSearch(query: currentQuery) } func performSearch(query: String?) { diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 931091ed..f457131b 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -46,11 +46,13 @@ class UserActivityManager { } // MARK: - Main Scene - static func mainSceneActivity(accountID: String) -> NSUserActivity { + static func mainSceneActivity(accountID: String?) -> NSUserActivity { let activity = NSUserActivity(type: .mainScene) - activity.userInfo = [ - "accountID": accountID, - ] + if let accountID { + activity.userInfo = [ + "accountID": accountID, + ] + } return activity }