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 parentIDs = self.getDirectParents(inReplyTo: mainStatusInReplyToID, from: context.ancestors)
let parentStatuses = context.ancestors.filter { parentIDs.contains($0.id) } 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) await mastodonController.persistentContainer.addAll(statuses: parentStatuses + context.descendants)
self.contextLoaded(mainStatus: mainStatus, context: context, parentIDs: parentIDs) self.contextLoaded(mainStatus: mainStatus, context: context, parentIDs: parentIDs)

View File

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

View File

@ -61,42 +61,50 @@ class PreferencesNavigationController: UINavigationController {
// when switching accounts shortly after adding a new one, there is an old instance of PreferncesNavigationController still around // 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) // 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 // the propper fix would be to figure out what's leaking instances of this class
guard let window = self.view.window, guard let windowScene = self.view.window?.windowScene else {
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 return
} }
let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo
if let sceneDelegate = windowScene.delegate as? MainSceneDelegate {
isSwitchingAccounts = true isSwitchingAccounts = true
dismiss(animated: true) { // dismiss preferences dismiss(animated: true) { // dismiss preferences
sceneDelegate.activateAccount(account, animated: true) sceneDelegate.activateAccount(account, animated: true)
} }
} else {
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: UserActivityManager.mainSceneActivity(accountID: account.id), options: nil)
}
} }
@objc func userLoggedOut() { @objc func userLoggedOut() {
guard let window = self.view.window, guard let windowScene = self.view.window?.windowScene else {
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 return
} }
if let sceneDelegate = windowScene.delegate as? MainSceneDelegate {
isSwitchingAccounts = true isSwitchingAccounts = true
dismiss(animated: true) { // dismiss preferences dismiss(animated: true) { // dismiss preferences
sceneDelegate.logoutCurrent() 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)
}
} }
} }
extension PreferencesNavigationController: OnboardingViewControllerDelegate { extension PreferencesNavigationController: OnboardingViewControllerDelegate {
func didFinishOnboarding(account: LocalData.UserAccountInfo) { func didFinishOnboarding(account: LocalData.UserAccountInfo) {
DispatchQueue.main.async { guard let windowScene = self.view.window?.windowScene else {
// todo: my profile can be torn off into a separate window, this will crash return
let sceneDelegate = self.view.window!.windowScene!.delegate as! MainSceneDelegate }
self.dismiss(animated: true) { // dismiss instance selector self.dismiss(animated: true) { // dismiss instance selector
self.dismiss(animated: true) { // dismiss preferences self.dismiss(animated: true) { // dismiss preferences
if let sceneDelegate = windowScene.delegate as? MainSceneDelegate {
sceneDelegate.activateAccount(account, animated: false) 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 { if let sourceDataSource = source.dataSource {
dataSource.apply(sourceDataSource.snapshot()) dataSource.apply(sourceDataSource.snapshot())
} }
// todo: check if the search needs to be performed before searching
// performSearch(query: currentQuery)
} }
func performSearch(query: String?) { func performSearch(query: String?) {

View File

@ -46,11 +46,13 @@ class UserActivityManager {
} }
// MARK: - Main Scene // MARK: - Main Scene
static func mainSceneActivity(accountID: String) -> NSUserActivity { static func mainSceneActivity(accountID: String?) -> NSUserActivity {
let activity = NSUserActivity(type: .mainScene) let activity = NSUserActivity(type: .mainScene)
if let accountID {
activity.userInfo = [ activity.userInfo = [
"accountID": accountID, "accountID": accountID,
] ]
}
return activity return activity
} }