Improve account switching.

This commit is contained in:
Marcin Czachursk 2023-02-12 09:13:04 +01:00
parent c0566e5ec9
commit c48da9c95d
4 changed files with 40 additions and 15 deletions

View File

@ -72,7 +72,8 @@ public extension MastodonClient {
) )
oauthClient?.authorizeURLHandler = ASWebAuthenticationURLHandler(callbackUrlScheme: callbackUrlScheme, oauthClient?.authorizeURLHandler = ASWebAuthenticationURLHandler(callbackUrlScheme: callbackUrlScheme,
presentationContextProvider: presentationContextProvider) presentationContextProvider: presentationContextProvider,
prefersEphemeralWebBrowserSession: true)
return try await withCheckedThrowingContinuation { [weak self] continuation in return try await withCheckedThrowingContinuation { [weak self] continuation in
self?.oAuthContinuation = continuation self?.oAuthContinuation = continuation

View File

@ -14,9 +14,7 @@ public class AuthorizationService {
private init() { } private init() { }
/// Access token verification. /// Access token verification.
public func verifyAccount(session: AuthorizationSession, _ result: @escaping (AccountData?) -> Void) async { public func verifyAccount(session: AuthorizationSession, currentAccount: AccountData?, _ result: @escaping (AccountData?) -> Void) async {
let currentAccount = AccountDataHandler.shared.getCurrentAccountData()
// When we dont have even one account stored in database then we have to ask user to enter server and sign in. // When we dont have even one account stored in database then we have to ask user to enter server and sign in.
guard let currentAccount, let accessToken = currentAccount.accessToken else { guard let currentAccount, let accessToken = currentAccount.accessToken else {
result(nil) result(nil)
@ -133,8 +131,16 @@ public class AuthorizationService {
group.addTask { group.addTask {
do { do {
try await self.refreshAccessToken(accountData: account) try await self.refreshAccessToken(accountData: account)
#if DEBUG
ToastrService.shared.showSuccess("New access tokens has been retrieved", imageSystemName: "key.fill")
#endif
} catch { } catch {
ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.") #if DEBUG
ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.", showToastr: true)
#else
ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.")
#endif
} }
} }
} }

View File

@ -65,7 +65,8 @@ struct VernissageApp: App {
// Verify access token correctness. // Verify access token correctness.
let authorizationSession = AuthorizationSession() let authorizationSession = AuthorizationSession()
await AuthorizationService.shared.verifyAccount(session: authorizationSession) { accountData in let currentAccount = AccountDataHandler.shared.getCurrentAccountData()
await AuthorizationService.shared.verifyAccount(session: authorizationSession, currentAccount: currentAccount) { accountData in
guard let accountData = accountData else { guard let accountData = accountData else {
self.applicationViewMode = .signIn self.applicationViewMode = .signIn
return return

View File

@ -153,15 +153,7 @@ struct MainView: View {
Menu { Menu {
ForEach(self.dbAccounts) { account in ForEach(self.dbAccounts) { account in
Button { Button {
HapticService.shared.touch() self.tryToSwitch(account)
let accountModel = AccountModel(accountData: account)
self.applicationState.account = accountModel
self.client.setAccount(account: accountModel)
self.applicationState.lastSeenStatusId = account.lastSeenStatusId
self.applicationState.amountOfNewStatuses = 0
ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: account)
} label: { } label: {
if self.applicationState.account?.id == account.id { if self.applicationState.account?.id == account.id {
Label(account.displayName ?? account.acct, systemImage: "checkmark") Label(account.displayName ?? account.acct, systemImage: "checkmark")
@ -232,5 +224,30 @@ struct MainView: View {
return "Notifications" return "Notifications"
} }
} }
private func tryToSwitch(_ account: AccountData) {
HapticService.shared.touch()
Task {
// Verify access token correctness.
let authorizationSession = AuthorizationSession()
await AuthorizationService.shared.verifyAccount(session: authorizationSession, currentAccount: account) { accountData in
guard let accountData = accountData else {
ToastrService.shared.showError(subtitle: "Cannot switch accounts.")
return
}
Task { @MainActor in
let accountModel = AccountModel(accountData: accountData)
self.applicationState.account = accountModel
self.client.setAccount(account: accountModel)
self.applicationState.lastSeenStatusId = account.lastSeenStatusId
self.applicationState.amountOfNewStatuses = 0
ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: accountData)
}
}
}
}
} }