-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- moved business logic out of the coordinators - show ProgressHUD instead of custom loading screen - removed unnecessary login screen - show navigation bar title for all hub authentication screens
- Loading branch information
Showing
10 changed files
with
317 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
CryptomatorCommon/Sources/CryptomatorCommonCore/Hub/HubAuthenticationCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import AppAuthCore | ||
import CryptomatorCloudAccessCore | ||
import SwiftUI | ||
import UIKit | ||
|
||
public protocol HubAuthenticationCoordinatorDelegate: AnyObject { | ||
@MainActor | ||
func userDidCancelHubAuthentication() | ||
|
||
@MainActor | ||
func userDismissedHubAuthenticationErrorMessage() | ||
} | ||
|
||
public final class HubAuthenticationCoordinator: Coordinator { | ||
public var childCoordinators = [Coordinator]() | ||
public var navigationController: UINavigationController | ||
public weak var parent: Coordinator? | ||
|
||
private let vaultConfig: UnverifiedVaultConfig | ||
private let hubAuthenticator: HubAuthenticating | ||
private var progressHUD: ProgressHUD? | ||
private let unlockHandler: HubVaultUnlockHandler | ||
private weak var delegate: HubAuthenticationCoordinatorDelegate? | ||
|
||
public init(navigationController: UINavigationController, | ||
vaultConfig: UnverifiedVaultConfig, | ||
hubAuthenticator: HubAuthenticating, | ||
unlockHandler: HubVaultUnlockHandler, | ||
parent: Coordinator?, | ||
delegate: HubAuthenticationCoordinatorDelegate) { | ||
self.navigationController = navigationController | ||
self.vaultConfig = vaultConfig | ||
self.hubAuthenticator = hubAuthenticator | ||
self.unlockHandler = unlockHandler | ||
self.parent = parent | ||
self.delegate = delegate | ||
} | ||
|
||
public func start() { | ||
guard let hubConfig = vaultConfig.allegedHubConfig else { | ||
handleError(HubAuthenticationViewModelError.missingHubConfig, for: navigationController, onOKTapped: { [weak self] in | ||
guard let self else { return } | ||
parent?.childDidFinish(self) | ||
}) | ||
return | ||
} | ||
Task { @MainActor in | ||
let authenticator = HubUserAuthenticator(hubAuthenticator: hubAuthenticator, viewController: navigationController) | ||
let authState: OIDAuthState | ||
do { | ||
authState = try await authenticator.authenticate(with: hubConfig) | ||
} catch let error as NSError where error.domain == OIDGeneralErrorDomain && error.code == OIDErrorCode.userCanceledAuthorizationFlow.rawValue { | ||
// do not show alert if user canceled it on purpose | ||
delegate?.userDidCancelHubAuthentication() | ||
parent?.childDidFinish(self) | ||
return | ||
} catch { | ||
handleError(error, for: navigationController, onOKTapped: { [weak self] in | ||
guard let self else { return } | ||
delegate?.userDismissedHubAuthenticationErrorMessage() | ||
parent?.childDidFinish(self) | ||
}) | ||
return | ||
} | ||
let viewModel = HubAuthenticationViewModel(authState: authState, | ||
vaultConfig: vaultConfig, | ||
unlockHandler: unlockHandler, | ||
delegate: self) | ||
await viewModel.continueToAccessCheck() | ||
guard !viewModel.isLoggedIn else { | ||
// Do not show the authentication view if the user already authenticated successfully | ||
return | ||
} | ||
navigationController.setNavigationBarHidden(false, animated: false) | ||
let viewController = HubAuthenticationViewController(viewModel: viewModel) | ||
navigationController.pushViewController(viewController, animated: true) | ||
} | ||
} | ||
|
||
private func showProgressHUD() { | ||
assert(progressHUD == nil, "showProgressHUD called although one is already shown") | ||
progressHUD = ProgressHUD() | ||
progressHUD?.show(presentingViewController: navigationController) | ||
progressHUD?.showLoadingIndicator() | ||
} | ||
|
||
private func hideProgressHUD() async { | ||
await withCheckedContinuation { continuation in | ||
progressHUD?.dismiss(animated: true, completion: { [weak self] in | ||
continuation.resume() | ||
self?.progressHUD = nil | ||
}) | ||
} | ||
} | ||
} | ||
|
||
extension HubAuthenticationCoordinator: HubAuthenticationViewModelDelegate { | ||
public func hubAuthenticationViewModelWantsToShowLoadingIndicator() { | ||
showProgressHUD() | ||
} | ||
|
||
public func hubAuthenticationViewModelWantsToHideLoadingIndicator() async { | ||
await hideProgressHUD() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
CryptomatorCommon/Sources/CryptomatorCommonCore/Hub/HubUserAuthenticator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import AppAuthCore | ||
import CryptomatorCloudAccessCore | ||
import UIKit | ||
|
||
struct HubUserAuthenticator: HubUserLogin { | ||
private let hubAuthenticator: HubAuthenticating | ||
private let viewController: UIViewController | ||
|
||
init(hubAuthenticator: HubAuthenticating, viewController: UIViewController) { | ||
self.hubAuthenticator = hubAuthenticator | ||
self.viewController = viewController | ||
} | ||
|
||
func authenticate(with hubConfig: HubConfig) async throws -> OIDAuthState { | ||
try await hubAuthenticator.authenticate(with: hubConfig, from: viewController) | ||
} | ||
} |
Oops, something went wrong.