Skip to content

Commit

Permalink
Replace error alert with server selection screen in startFlow.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Aug 9, 2022
1 parent 90ecedb commit 16cc569
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions Riot/Modules/Authentication/AuthenticationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc
// Start the flow using the default server (or a provisioning link if set).
try await authenticationService.startFlow(flow)
} catch {
MXLog.error("[AuthenticationCoordinator] start: Failed to start")
displayError(message: error.localizedDescription)
MXLog.error("[AuthenticationCoordinator] start: Failed to start, showing server selection.")
showServerSelectionScreen(for: flow)
return
}

Expand All @@ -155,6 +155,42 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc
}
}

/// Pushes the server selection screen into the flow (other screens may also present it modally later).
@MainActor private func showServerSelectionScreen(for flow: AuthenticationFlow) {
MXLog.debug("[AuthenticationCoordinator] showServerSelectionScreen")
let parameters = AuthenticationServerSelectionCoordinatorParameters(authenticationService: authenticationService,
flow: flow,
hasModalPresentation: false)
let coordinator = AuthenticationServerSelectionCoordinator(parameters: parameters)
coordinator.callback = { [weak self, weak coordinator] result in
guard let self = self, let coordinator = coordinator else { return }
self.serverSelectionCoordinator(coordinator, didCompleteWith: result, for: flow)
}

coordinator.start()
add(childCoordinator: coordinator)

navigationRouter.push(coordinator, animated: true) { [weak self] in
self?.remove(childCoordinator: coordinator)
}
}

/// Shows the next screen in the flow after the server selection screen.
@MainActor private func serverSelectionCoordinator(_ coordinator: AuthenticationServerSelectionCoordinator,
didCompleteWith result: AuthenticationServerSelectionCoordinatorResult,
for flow: AuthenticationFlow) {
switch result {
case .updated:
if flow == .register {
showRegistrationScreen()
} else {
showLoginScreen()
}
case .dismiss:
MXLog.failure("[AuthenticationCoordinator] AuthenticationServerSelectionScreen is requesting dismiss when part of a stack.")
}
}

/// Presents an alert on top of the navigation router with the supplied error message.
@MainActor private func displayError(message: String) {
let alert = UIAlertController(title: VectorL10n.error, message: message, preferredStyle: .alert)
Expand Down Expand Up @@ -307,48 +343,6 @@ final class AuthenticationCoordinator: NSObject, AuthenticationCoordinatorProtoc

// MARK: - Registration

#warning("Unused.")
/// Pushes the server selection screen into the flow (other screens may also present it modally later).
@MainActor private func showServerSelectionScreen() {
MXLog.debug("[AuthenticationCoordinator] showServerSelectionScreen")
let parameters = AuthenticationServerSelectionCoordinatorParameters(authenticationService: authenticationService,
flow: .register,
hasModalPresentation: false)
let coordinator = AuthenticationServerSelectionCoordinator(parameters: parameters)
coordinator.callback = { [weak self, weak coordinator] result in
guard let self = self, let coordinator = coordinator else { return }
self.serverSelectionCoordinator(coordinator, didCompleteWith: result)
}

coordinator.start()
add(childCoordinator: coordinator)

if navigationRouter.modules.isEmpty {
navigationRouter.setRootModule(coordinator) { [weak self] in
self?.remove(childCoordinator: coordinator)
}
} else {
navigationRouter.push(coordinator, animated: true) { [weak self] in
self?.remove(childCoordinator: coordinator)
}
}
}

/// Shows the next screen in the flow after the server selection screen.
@MainActor private func serverSelectionCoordinator(_ coordinator: AuthenticationServerSelectionCoordinator,
didCompleteWith result: AuthenticationServerSelectionCoordinatorResult) {
switch result {
case .updated:
if authenticationService.state.homeserver.needsRegistrationFallback {
showFallback(for: .register)
} else {
showRegistrationScreen()
}
case .dismiss:
MXLog.failure("[AuthenticationCoordinator] AuthenticationServerSelectionScreen is requesting dismiss when part of a stack.")
}
}

/// Shows the registration screen.
@MainActor private func showRegistrationScreen() {
MXLog.debug("[AuthenticationCoordinator] showRegistrationScreen")
Expand Down

0 comments on commit 16cc569

Please sign in to comment.