Improve account switching.
This commit is contained in:
parent
c0566e5ec9
commit
c48da9c95d
|
@ -72,7 +72,8 @@ public extension MastodonClient {
|
|||
)
|
||||
|
||||
oauthClient?.authorizeURLHandler = ASWebAuthenticationURLHandler(callbackUrlScheme: callbackUrlScheme,
|
||||
presentationContextProvider: presentationContextProvider)
|
||||
presentationContextProvider: presentationContextProvider,
|
||||
prefersEphemeralWebBrowserSession: true)
|
||||
|
||||
return try await withCheckedThrowingContinuation { [weak self] continuation in
|
||||
self?.oAuthContinuation = continuation
|
||||
|
|
|
@ -14,9 +14,7 @@ public class AuthorizationService {
|
|||
private init() { }
|
||||
|
||||
/// Access token verification.
|
||||
public func verifyAccount(session: AuthorizationSession, _ result: @escaping (AccountData?) -> Void) async {
|
||||
let currentAccount = AccountDataHandler.shared.getCurrentAccountData()
|
||||
|
||||
public func verifyAccount(session: AuthorizationSession, currentAccount: AccountData?, _ result: @escaping (AccountData?) -> Void) async {
|
||||
// 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 {
|
||||
result(nil)
|
||||
|
@ -133,8 +131,16 @@ public class AuthorizationService {
|
|||
group.addTask {
|
||||
do {
|
||||
try await self.refreshAccessToken(accountData: account)
|
||||
|
||||
#if DEBUG
|
||||
ToastrService.shared.showSuccess("New access tokens has been retrieved", imageSystemName: "key.fill")
|
||||
#endif
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,8 @@ struct VernissageApp: App {
|
|||
|
||||
// Verify access token correctness.
|
||||
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 {
|
||||
self.applicationViewMode = .signIn
|
||||
return
|
||||
|
|
|
@ -153,15 +153,7 @@ struct MainView: View {
|
|||
Menu {
|
||||
ForEach(self.dbAccounts) { account in
|
||||
Button {
|
||||
HapticService.shared.touch()
|
||||
|
||||
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)
|
||||
self.tryToSwitch(account)
|
||||
} label: {
|
||||
if self.applicationState.account?.id == account.id {
|
||||
Label(account.displayName ?? account.acct, systemImage: "checkmark")
|
||||
|
@ -232,5 +224,30 @@ struct MainView: View {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue