Switch callbacks to combine (#1710)

* #750 - Convert the SoftLogoutScreen to combine

* #750 - Convert the UserSessionFlowCoordinator to Combine

* #750 - Convert the AnalyticsPromptScreen to Combine

* #750 - Convert the LoginScreen to Combine

* #750 - Convert the ServerSelectionScreen to Combine

* #750 - Convert the EmojiPickerScreen to Combine

* #750 - Convert the HomeScreen to Combine

* #750 - Convert the MediaUploadPreviewScreen to Combine

* #750 - Convert the OnboardingScreen to Combine

* Rename `Onboarding` to `OnboardingScreen`

* #750 - Convert the ReportContentScreen to Combine

* #750 - Convert the RoomDetailsSscreen to Combine

* #750 - Convert the RoomMemberDetailsScreen to Combine

* #750 - Convert the RoomMembersListScreen to Combine

* #750 - Convert the SessionVerificationScreen to Combine

* #750 - Convert the SettingsScreen to Combine

* #750 - Convert the AdvancedSettingsScreen to Combine

* #750 - Convert the DeveloperOptionsScreen to Combine

* Fix the unit tests

* Use .sink action and the same cancellables constructor everywhere

* Cleanup cancellables when setting up tests
This commit is contained in:
Stefan Ceriu
2023-09-14 12:53:33 +03:00
committed by GitHub
parent a0d40b6f0c
commit a4e5e4f0ed
89 changed files with 884 additions and 589 deletions
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
struct SoftLogoutScreenCoordinatorParameters {
@@ -43,10 +44,14 @@ enum SoftLogoutScreenCoordinatorResult: CustomStringConvertible {
final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
private let parameters: SoftLogoutScreenCoordinatorParameters
private var viewModel: SoftLogoutScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<SoftLogoutScreenCoordinatorResult, Never> = .init()
private var cancellables = Set<AnyCancellable>()
private var authenticationService: AuthenticationServiceProxyProtocol { parameters.authenticationService }
var callback: (@MainActor (SoftLogoutScreenCoordinatorResult) -> Void)?
var actions: AnyPublisher<SoftLogoutScreenCoordinatorResult, Never> {
actionsSubject.eraseToAnyPublisher()
}
@MainActor init(parameters: SoftLogoutScreenCoordinatorParameters) {
self.parameters = parameters
@@ -60,21 +65,23 @@ final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
// MARK: - Public
func start() {
viewModel.callback = { [weak self] result in
guard let self else { return }
MXLog.info("[SoftLogoutCoordinator] SoftLogoutViewModel did complete with result: \(result).")
viewModel.actions
.sink { [weak self] action in
guard let self else { return }
MXLog.info("[SoftLogoutCoordinator] SoftLogoutViewModel did complete with result: \(action).")
switch result {
case .login(let password):
self.login(withPassword: password)
case .forgotPassword:
self.showForgotPasswordScreen()
case .clearAllData:
self.callback?(.clearAllData)
case .continueWithOIDC:
self.continueWithOIDC(presentationAnchor: viewModel.context.viewState.window)
switch action {
case .login(let password):
login(withPassword: password)
case .forgotPassword:
showForgotPasswordScreen()
case .clearAllData:
actionsSubject.send(.clearAllData)
case .continueWithOIDC:
continueWithOIDC(presentationAnchor: viewModel.context.viewState.window)
}
}
}
.store(in: &cancellables)
}
func stop() {
@@ -119,7 +126,7 @@ final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
initialDeviceName: UIDevice.current.initialDeviceName,
deviceID: parameters.credentials.deviceID) {
case .success(let userSession):
callback?(.signedIn(userSession))
actionsSubject.send(.signedIn(userSession))
stopLoading()
case .failure(let error):
stopLoading()
@@ -146,7 +153,7 @@ final class SoftLogoutScreenCoordinator: CoordinatorProtocol {
presentationAnchor: presentationAnchor)
switch await presenter.authenticate(using: oidcData) {
case .success(let userSession):
callback?(.signedIn(userSession))
actionsSubject.send(.signedIn(userSession))
case .failure(let error):
handleError(error)
}
@@ -14,12 +14,17 @@
// limitations under the License.
//
import Combine
import SwiftUI
typealias SoftLogoutScreenViewModelType = StateStoreViewModel<SoftLogoutScreenViewState, SoftLogoutScreenViewAction>
class SoftLogoutScreenViewModel: SoftLogoutScreenViewModelType, SoftLogoutScreenViewModelProtocol {
var callback: (@MainActor (SoftLogoutScreenViewModelAction) -> Void)?
private var actionsSubject: PassthroughSubject<SoftLogoutScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<SoftLogoutScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(credentials: SoftLogoutScreenCredentials,
homeserver: LoginHomeserver,
@@ -36,13 +41,13 @@ class SoftLogoutScreenViewModel: SoftLogoutScreenViewModelType, SoftLogoutScreen
override func process(viewAction: SoftLogoutScreenViewAction) {
switch viewAction {
case .login:
callback?(.login(state.bindings.password))
actionsSubject.send(.login(state.bindings.password))
case .forgotPassword:
callback?(.forgotPassword)
actionsSubject.send(.forgotPassword)
case .clearAllData:
callback?(.clearAllData)
actionsSubject.send(.clearAllData)
case .continueWithOIDC:
callback?(.continueWithOIDC)
actionsSubject.send(.continueWithOIDC)
case .updateWindow(let window):
guard state.window != window else { return }
Task { state.window = window }
@@ -14,10 +14,10 @@
// limitations under the License.
//
import Foundation
import Combine
protocol SoftLogoutScreenViewModelProtocol {
var callback: (@MainActor (SoftLogoutScreenViewModelAction) -> Void)? { get set }
var actions: AnyPublisher<SoftLogoutScreenViewModelAction, Never> { get }
var context: SoftLogoutScreenViewModelType.Context { get }
/// Display an error to the user.