Remove RegistrationFlowHandling, handling the dummy stage in the wizard.

This commit is contained in:
Doug
2022-05-03 10:16:27 +01:00
committed by Doug
parent 82984381b3
commit 95d1a2a39e
5 changed files with 44 additions and 100 deletions
@@ -1,77 +0,0 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
@available(iOS 14.0, *)
/// A protocol with a default implementation that allows a coordinator to execute and handle registration flow steps.
protocol RegistrationFlowHandling {
var authenticationService: AuthenticationService { get }
var registrationWizard: RegistrationWizard? { get }
@MainActor var completion: ((AuthenticationRegistrationCoordinatorResult) -> Void)? { get }
/// Executes a registration step using the `RegistrationWizard` to complete any additional steps automatically.
@MainActor func executeRegistrationStep(step: @escaping (RegistrationWizard) async throws -> RegistrationResult) -> Task<Void, Error>
}
@available(iOS 14.0, *)
@MainActor extension RegistrationFlowHandling {
func executeRegistrationStep(step: @escaping (RegistrationWizard) async throws -> RegistrationResult) -> Task<Void, Error> {
return Task {
guard let registrationWizard = registrationWizard else {
MXLog.failure("[RegistrationFlowHandling] executeRegistrationStep: The registration wizard is nil.")
return
}
do {
let result = try await step(registrationWizard)
guard !Task.isCancelled else { return }
switch result {
case .success(let mxSession):
completion?(.sessionCreated(session: mxSession, isAccountCreated: true))
case .flowResponse(let flowResult):
await processFlowResponse(flowResult: flowResult)
}
} catch {
// An error is thrown only to indicate that a task failed,
// however it should be handled in the closure rather than here.
}
}
}
/// Processes flow responses making sure the dummy stage is handled automatically when possible.
func processFlowResponse(flowResult: FlowResult) async {
// If dummy stage is mandatory, and password is already sent, do the dummy stage now
if authenticationService.isRegistrationStarted && flowResult.missingStages.contains(where: { $0.isDummyAndMandatory }) {
await handleRegisterDummy()
} else {
// Notify the user
completion?(.flowResponse(flowResult))
}
}
/// Handle the dummy stage of the flow.
func handleRegisterDummy() async {
let task = executeRegistrationStep { wizard in
try await wizard.dummy()
}
// await the result to suspend until the request is complete.
let _ = await task.result
}
}
@@ -102,7 +102,7 @@ class RegistrationWizard {
password: String?,
initialDeviceDisplayName: String?) async throws -> RegistrationResult {
let parameters = RegistrationParameters(username: username, password: password, initialDeviceDisplayName: initialDeviceDisplayName)
let result = try await performRegistrationRequest(parameters: parameters)
let result = try await performRegistrationRequest(parameters: parameters, isCreatingAccount: true)
state.isRegistrationStarted = true
return result
}
@@ -231,7 +231,7 @@ class RegistrationWizard {
return try await performRegistrationRequest(parameters: parameters)
}
private func performRegistrationRequest(parameters: RegistrationParameters) async throws -> RegistrationResult {
private func performRegistrationRequest(parameters: RegistrationParameters, isCreatingAccount: Bool = false) async throws -> RegistrationResult {
do {
let response = try await client.register(parameters: parameters)
let credentials = MXCredentials(loginResponse: response, andDefaultCredentials: client.credentials)
@@ -245,7 +245,20 @@ class RegistrationWizard {
else { throw error }
state.currentSession = authenticationSession.session
return .flowResponse(authenticationSession.flowResult)
let flowResult = authenticationSession.flowResult
if isCreatingAccount || isRegistrationStarted {
return try await handleMandatoryDummyStage(flowResult: flowResult)
}
return .flowResponse(flowResult)
}
}
/// Checks for a mandatory dummy stage and handles it automatically when possible.
private func handleMandatoryDummyStage(flowResult: FlowResult) async throws -> RegistrationResult {
// If the dummy stage is mandatory, do the dummy stage now
guard flowResult.missingStages.contains(where: { $0.isDummyAndMandatory }) else { return .flowResponse(flowResult) }
return try await dummy()
}
}