element-ios/RiotSwiftUI/Modules/Authentication/Common/Service/MatrixSDK/RegistrationFlowHandling.swift
Doug 3161d3d535 Add Authentication Flow WIP.
- Add Registration Screen.
- Add Server Selection Screen.
- Rename AuthenticationCoordinator to LegacyAuthenticationCoordinator.
- Add AuthenticationService and RegistrationWizard.
- Async extensions.
- Add global white and EMS colors to the themes.
- Add tests for server selection and registration screens.
- Accessibility and iPad layout tweaks.
- Remove MainActor from Auth Coordinators/VMs/Views.
(It broke the protocol conformances so now the methods and properties are marked individually.)
2022-05-04 19:26:38 +01:00

72 lines
3 KiB
Swift

//
// 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 {
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
}
}