Fix crash when activating account in My Profile scene

This commit is contained in:
Shadowfacts 2022-12-14 21:24:54 -05:00
parent 23e4541eb7
commit a13d5d5a82
5 changed files with 35 additions and 29 deletions

View File

@ -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)

View File

@ -487,7 +487,6 @@ fileprivate extension MainTabBarViewController.Tab {
case .explore:
return "magnifyingglass"
case .myProfile:
// todo: use user avatar image
return "person"
}
}

View File

@ -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)
}
}
}

View File

@ -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?) {

View File

@ -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
}