diff --git a/Mastodon/Coordinator/SceneCoordinator.swift b/Mastodon/Coordinator/SceneCoordinator.swift index 49419d97f..bd485b820 100644 --- a/Mastodon/Coordinator/SceneCoordinator.swift +++ b/Mastodon/Coordinator/SceneCoordinator.swift @@ -599,7 +599,7 @@ extension SceneCoordinator: MastodonLoginViewControllerDelegate { //MARK: - SettingsCoordinatorDelegate extension SceneCoordinator: SettingsCoordinatorDelegate { - func logout(_ settingsCoordinator: SettingsCoordinator) { + func logout(_ user: MastodonAuthentication, presentingFrom viewController: UIViewController) { let preferredStyle: UIAlertController.Style @@ -617,17 +617,17 @@ extension SceneCoordinator: SettingsCoordinatorDelegate { let cancelAction = UIAlertAction(title: L10n.Common.Controls.Actions.cancel, style: .cancel) let signOutAction = UIAlertAction(title: L10n.Common.Alerts.SignOut.confirm, style: .destructive) { [weak self] _ in - guard let self, let authenticationBox = self.authenticationBox else { return } + guard let self else { return } NotificationService.shared.clearNotificationCountForActiveUser() Task { @MainActor in try await AuthenticationServiceProvider.shared.signOutMastodonUser( - authentication: authenticationBox.authentication + authentication: user ) - let userIdentifier = authenticationBox - PersistenceManager.shared.removeAllCaches(forUser: userIdentifier) self.setup() + PersistenceManager.shared.removeAllCaches(forUser: user) + try await BodegaPersistence.removeUser(user) } } @@ -635,7 +635,7 @@ extension SceneCoordinator: SettingsCoordinatorDelegate { alertController.addAction(cancelAction) alertController.addAction(signOutAction) - settingsCoordinator.navigationController.present(alertController, animated: true) + (viewController.navigationController ?? viewController).present(alertController, animated: true) } @MainActor diff --git a/Mastodon/Scene/Account/AccountViewController.swift b/Mastodon/Scene/Account/AccountViewController.swift index f9ff8198d..ec6e8aba8 100644 --- a/Mastodon/Scene/Account/AccountViewController.swift +++ b/Mastodon/Scene/Account/AccountViewController.swift @@ -103,20 +103,7 @@ extension AccountListViewController: UITableViewDelegate { guard let self else { return } UserDefaults.shared.setNotificationCountWithAccessToken(accessToken: record.userAccessToken, value: 0) - - Task { @MainActor in - do { - let userIdentifier = record - try await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: record) - PersistenceManager.shared.removeAllCaches(forUser: userIdentifier) - - self.sceneCoordinator?.setup() - - } catch { - assertionFailure("Failed to delete Authentication: \(error)") - } - - } + self.sceneCoordinator?.logout(record, presentingFrom: self) }) logoutAction.image = UIImage(systemName: "rectangle.portrait.and.arrow.forward") @@ -153,10 +140,15 @@ extension AccountListViewController: UITableViewDelegate { Task { @MainActor in self.sceneCoordinator?.showLoading() for authenticationBox in AuthenticationServiceProvider.shared.mastodonAuthenticationBoxes { - try? await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: authenticationBox.authentication) - let userIdentifier = authenticationBox.authentication.userIdentifier() - PersistenceManager.shared.removeAllCaches(forUser: userIdentifier) - self.sceneCoordinator?.setup() + do { + try await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: authenticationBox.authentication) + let userIdentifier = authenticationBox.authentication.userIdentifier() + self.sceneCoordinator?.setup() + PersistenceManager.shared.removeAllCaches(forUser: userIdentifier) + try await BodegaPersistence.removeUser(userIdentifier) + } catch { + assertionFailure("failed to sign out") + } } self.sceneCoordinator?.hideLoading() diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift index 0f7c27d38..9d0643bb5 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift @@ -645,17 +645,6 @@ extension HomeTimelineViewController { return } } - - @objc func signOutAction(_ sender: UIAction) { - - Task { @MainActor in - try await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: authenticationBox.authentication) - let userIdentifier = authenticationBox - PersistenceManager.shared.removeAllCaches(forUser: userIdentifier) - self.sceneCoordinator?.setup() - self.sceneCoordinator?.setup() - } - } @objc private func timelinePillTouched(_ sender: TimelineStatusPill) { UIView.animate(withDuration: 0.05) { diff --git a/Mastodon/Scene/Settings/SettingsCoordinator.swift b/Mastodon/Scene/Settings/SettingsCoordinator.swift index 376a3dd25..3d9f7d36d 100644 --- a/Mastodon/Scene/Settings/SettingsCoordinator.swift +++ b/Mastodon/Scene/Settings/SettingsCoordinator.swift @@ -10,7 +10,7 @@ import MetaTextKit import MastodonUI protocol SettingsCoordinatorDelegate: AnyObject { - func logout(_ settingsCoordinator: SettingsCoordinator) + func logout(_ user: MastodonAuthentication, presentingFrom viewController: UIViewController) func openGithubURL(_ settingsCoordinator: SettingsCoordinator) func openPrivacyURL(_ settingsCoordinator: SettingsCoordinator) func openProfileSettingsURL(_ settingsCoordinator: SettingsCoordinator) @@ -142,7 +142,8 @@ extension SettingsCoordinator: SettingsViewControllerDelegate { navigationController.pushViewController(aboutViewController, animated: true) case .logout(_): - delegate?.logout(self) + guard let user = AuthenticationServiceProvider.shared.currentActiveUser.value?.authentication else { return } + delegate?.logout(user, presentingFrom: self.navigationController) case .manageBetaFeatures: let betaTestSettingsViewController = BetaTestSettingsViewController()