Rename completion to callback and simplify actor usage. (#6141)

This commit is contained in:
Doug
2022-05-11 13:18:26 +01:00
committed by GitHub
parent 42d2108ac9
commit bb9d94a59d
12 changed files with 113 additions and 95 deletions
@@ -28,7 +28,7 @@ class AuthenticationServerSelectionViewModel: AuthenticationServerSelectionViewM
// MARK: Public
var completion: ((AuthenticationServerSelectionViewModelResult) -> Void)?
@MainActor var callback: ((AuthenticationServerSelectionViewModelResult) -> Void)?
// MARK: - Setup
@@ -41,20 +41,15 @@ class AuthenticationServerSelectionViewModel: AuthenticationServerSelectionViewM
// MARK: - Public
override func process(viewAction: AuthenticationServerSelectionViewAction) {
Task {
await MainActor.run {
switch viewAction {
case .confirm:
completion?(.confirm(homeserverAddress: state.bindings.homeserverAddress))
case .dismiss:
completion?(.dismiss)
case .getInTouch:
getInTouch()
case .clearFooterError:
guard state.footerErrorMessage != nil else { return }
withAnimation { state.footerErrorMessage = nil }
}
}
switch viewAction {
case .confirm:
Task { await callback?(.confirm(homeserverAddress: state.bindings.homeserverAddress)) }
case .dismiss:
Task { await callback?(.dismiss) }
case .getInTouch:
Task { await getInTouch() }
case .clearFooterError:
Task { await clearFooterError() }
}
}
@@ -71,6 +66,12 @@ class AuthenticationServerSelectionViewModel: AuthenticationServerSelectionViewM
// MARK: - Private
/// Clear any errors shown in the text field footer.
@MainActor private func clearFooterError() {
guard state.footerErrorMessage != nil else { return }
withAnimation { state.footerErrorMessage = nil }
}
/// Opens the EMS link in the user's browser.
@MainActor private func getInTouch() {
let url = BuildSettings.onboardingHostYourOwnServerLink
@@ -18,7 +18,7 @@ import Foundation
protocol AuthenticationServerSelectionViewModelProtocol {
@MainActor var completion: ((AuthenticationServerSelectionViewModelResult) -> Void)? { get set }
@MainActor var callback: ((AuthenticationServerSelectionViewModelResult) -> Void)? { get set }
var context: AuthenticationServerSelectionViewModelType.Context { get }
/// Displays an error to the user.
@@ -48,7 +48,7 @@ final class AuthenticationServerSelectionCoordinator: Coordinator, Presentable {
// Must be used only internally
var childCoordinators: [Coordinator] = []
@MainActor var completion: ((AuthenticationServerSelectionCoordinatorResult) -> Void)?
@MainActor var callback: ((AuthenticationServerSelectionCoordinatorResult) -> Void)?
// MARK: - Setup
@@ -70,22 +70,8 @@ final class AuthenticationServerSelectionCoordinator: Coordinator, Presentable {
// MARK: - Public
func start() {
Task {
await MainActor.run {
MXLog.debug("[AuthenticationServerSelectionCoordinator] did start.")
authenticationServerSelectionViewModel.completion = { [weak self] result in
guard let self = self else { return }
MXLog.debug("[AuthenticationServerSelectionCoordinator] AuthenticationServerSelectionViewModel did complete with result: \(result).")
switch result {
case .confirm(let homeserverAddress):
self.useHomeserver(homeserverAddress)
case .dismiss:
self.completion?(.dismiss)
}
}
}
}
MXLog.debug("[AuthenticationServerSelectionCoordinator] did start.")
Task { await setupViewModel() }
}
func toPresentable() -> UIViewController {
@@ -94,6 +80,21 @@ final class AuthenticationServerSelectionCoordinator: Coordinator, Presentable {
// MARK: - Private
/// Set up the view model. This method is extracted from `start()` so it can run on the `MainActor`.
@MainActor private func setupViewModel() {
authenticationServerSelectionViewModel.callback = { [weak self] result in
guard let self = self else { return }
MXLog.debug("[AuthenticationServerSelectionCoordinator] AuthenticationServerSelectionViewModel did complete with result: \(result).")
switch result {
case .confirm(let homeserverAddress):
self.useHomeserver(homeserverAddress)
case .dismiss:
self.callback?(.dismiss)
}
}
}
/// Show an activity indicator whilst loading.
/// - Parameters:
/// - label: The label to show on the indicator.
@@ -120,7 +121,7 @@ final class AuthenticationServerSelectionCoordinator: Coordinator, Presentable {
try await authenticationService.startFlow(.register, for: homeserverAddress)
stopLoading()
completion?(.updated)
callback?(.updated)
} catch {
stopLoading()