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
@@ -30,7 +30,7 @@ class AuthenticationCoordinator: CoordinatorProtocol {
private let analytics: AnalyticsService
private let userIndicatorController: UserIndicatorControllerProtocol
private var cancellables: Set<AnyCancellable> = []
private var cancellables = Set<AnyCancellable>()
weak var delegate: AuthenticationCoordinatorDelegate?
@@ -57,15 +57,18 @@ class AuthenticationCoordinator: CoordinatorProtocol {
// MARK: - Private
private func showOnboarding() {
let coordinator = OnboardingCoordinator()
coordinator.callback = { [weak self] action in
guard let self else { return }
switch action {
case .login:
Task { await self.startAuthentication() }
let coordinator = OnboardingScreenCoordinator()
coordinator.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .login:
Task { await self.startAuthentication() }
}
}
}
.store(in: &cancellables)
navigationStackCoordinator.setRootCoordinator(coordinator)
}
@@ -92,29 +95,31 @@ class AuthenticationCoordinator: CoordinatorProtocol {
isModallyPresented: isModallyPresented)
let coordinator = ServerSelectionScreenCoordinator(parameters: parameters)
coordinator.callback = { [weak self] action in
guard let self else { return }
switch action {
case .updated:
if isModallyPresented {
navigationStackCoordinator.setSheetCoordinator(nil)
} else {
// We are here because the default server failed to respond.
if authenticationService.homeserver.value.loginMode == .password {
// Add the password login screen directly to the flow, its fine.
showLoginScreen()
coordinator.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .updated:
if isModallyPresented {
navigationStackCoordinator.setSheetCoordinator(nil)
} else {
// OIDC is presented from the confirmation screen so replace the
// server selection screen which was inserted to handle the failure.
navigationStackCoordinator.pop()
showServerConfirmationScreen()
// We are here because the default server failed to respond.
if authenticationService.homeserver.value.loginMode == .password {
// Add the password login screen directly to the flow, its fine.
showLoginScreen()
} else {
// OIDC is presented from the confirmation screen so replace the
// server selection screen which was inserted to handle the failure.
navigationStackCoordinator.pop()
showServerConfirmationScreen()
}
}
case .dismiss:
navigationStackCoordinator.setSheetCoordinator(nil)
}
case .dismiss:
navigationStackCoordinator.setSheetCoordinator(nil)
}
}
.store(in: &cancellables)
if isModallyPresented {
navigationCoordinator.setRootCoordinator(coordinator)
@@ -178,20 +183,22 @@ class AuthenticationCoordinator: CoordinatorProtocol {
userIndicatorController: userIndicatorController)
let coordinator = LoginScreenCoordinator(parameters: parameters)
coordinator.callback = { [weak self] action in
guard let self else { return }
coordinator.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .signedIn(let userSession):
userHasSignedIn(userSession: userSession)
case .configuredForOIDC:
// Pop back to the confirmation screen for OIDC login to continue.
navigationStackCoordinator.pop(animated: false)
case .isOnWaitlist(let credentials):
showWaitlistScreen(for: credentials)
switch action {
case .signedIn(let userSession):
userHasSignedIn(userSession: userSession)
case .configuredForOIDC:
// Pop back to the confirmation screen for OIDC login to continue.
navigationStackCoordinator.pop(animated: false)
case .isOnWaitlist(let credentials):
showWaitlistScreen(for: credentials)
}
}
}
.store(in: &cancellables)
navigationStackCoordinator.push(coordinator)
}
@@ -228,10 +235,18 @@ class AuthenticationCoordinator: CoordinatorProtocol {
completion()
return
}
let coordinator = AnalyticsPromptScreenCoordinator(analytics: analytics, termsURL: appSettings.analyticsConfiguration.termsURL)
coordinator.callback = {
completion()
}
coordinator.actions
.sink { action in
switch action {
case .done:
completion()
}
}
.store(in: &cancellables)
navigationStackCoordinator.push(coordinator)
}
@@ -42,7 +42,12 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
private var authenticationService: AuthenticationServiceProxyProtocol { parameters.authenticationService }
var callback: (@MainActor (LoginScreenCoordinatorAction) -> Void)?
private let actionsSubject: PassthroughSubject<LoginScreenCoordinatorAction, Never> = .init()
private var cancellables = Set<AnyCancellable>()
var actions: AnyPublisher<LoginScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
// MARK: - Setup
@@ -56,18 +61,20 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
// MARK: - Public
func start() {
viewModel.callback = { [weak self] action in
guard let self else { return }
switch action {
case .parseUsername(let username):
parseUsername(username)
case .forgotPassword:
showForgotPasswordScreen()
case .login(let username, let password):
login(username: username, password: password)
viewModel.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .parseUsername(let username):
parseUsername(username)
case .forgotPassword:
showForgotPasswordScreen()
case .login(let username, let password):
login(username: username, password: password)
}
}
}
.store(in: &cancellables)
}
func stop() {
@@ -126,7 +133,7 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
initialDeviceName: UIDevice.current.initialDeviceName,
deviceID: nil) {
case .success(let userSession):
callback?(.signedIn(userSession))
actionsSubject.send(.signedIn(userSession))
parameters.analytics.signpost.endLogin()
stopLoading()
case .failure(let error):
@@ -134,11 +141,11 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
parameters.analytics.signpost.endLogin()
switch error {
case .isOnWaitlist:
callback?(.isOnWaitlist(.init(username: username,
password: password,
initialDeviceName: UIDevice.current.initialDeviceName,
deviceID: nil,
homeserver: authenticationService.homeserver.value)))
actionsSubject.send(.isOnWaitlist(.init(username: username,
password: password,
initialDeviceName: UIDevice.current.initialDeviceName,
deviceID: nil,
homeserver: authenticationService.homeserver.value)))
default:
handleError(error)
}
@@ -159,7 +166,7 @@ final class LoginScreenCoordinator: CoordinatorProtocol {
case .success:
stopLoading()
if authenticationService.homeserver.value.loginMode == .oidc {
callback?(.configuredForOIDC)
actionsSubject.send(.configuredForOIDC)
} else {
updateViewModel()
}
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
typealias LoginScreenViewModelType = StateStoreViewModel<LoginScreenViewState, LoginScreenViewAction>
@@ -21,7 +22,11 @@ typealias LoginScreenViewModelType = StateStoreViewModel<LoginScreenViewState, L
class LoginScreenViewModel: LoginScreenViewModelType, LoginScreenViewModelProtocol {
private let slidingSyncLearnMoreURL: URL
var callback: (@MainActor (LoginScreenViewModelAction) -> Void)?
private var actionsSubject: PassthroughSubject<LoginScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<LoginScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(homeserver: LoginHomeserver, slidingSyncLearnMoreURL: URL) {
self.slidingSyncLearnMoreURL = slidingSyncLearnMoreURL
@@ -34,11 +39,11 @@ class LoginScreenViewModel: LoginScreenViewModelType, LoginScreenViewModelProtoc
override func process(viewAction: LoginScreenViewAction) {
switch viewAction {
case .parseUsername:
callback?(.parseUsername(state.bindings.username))
actionsSubject.send(.parseUsername(state.bindings.username))
case .forgotPassword:
callback?(.forgotPassword)
actionsSubject.send(.forgotPassword)
case .next:
callback?(.login(username: state.bindings.username, password: state.bindings.password))
actionsSubject.send(.login(username: state.bindings.username, password: state.bindings.password))
}
}
@@ -14,11 +14,11 @@
// limitations under the License.
//
import Foundation
import Combine
@MainActor
protocol LoginScreenViewModelProtocol {
var callback: (@MainActor (LoginScreenViewModelAction) -> Void)? { get set }
var actions: AnyPublisher<LoginScreenViewModelAction, Never> { get }
var context: LoginScreenViewModelType.Context { get }
/// Update the view to reflect that a new homeserver is being loaded.
@@ -31,7 +31,7 @@ final class ServerConfirmationScreenCoordinator: CoordinatorProtocol {
private let parameters: ServerConfirmationScreenCoordinatorParameters
private var viewModel: ServerConfirmationScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<ServerConfirmationScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
private var cancellables = Set<AnyCancellable>()
var actions: AnyPublisher<ServerConfirmationScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
struct ServerSelectionScreenCoordinatorParameters {
@@ -35,7 +36,12 @@ final class ServerSelectionScreenCoordinator: CoordinatorProtocol {
private var viewModel: ServerSelectionScreenViewModelProtocol
private var authenticationService: AuthenticationServiceProxyProtocol { parameters.authenticationService }
var callback: (@MainActor (ServerSelectionScreenCoordinatorAction) -> Void)?
private let actionsSubject: PassthroughSubject<ServerSelectionScreenCoordinatorAction, Never> = .init()
private var cancellables = Set<AnyCancellable>()
var actions: AnyPublisher<ServerSelectionScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: ServerSelectionScreenCoordinatorParameters) {
self.parameters = parameters
@@ -48,16 +54,18 @@ final class ServerSelectionScreenCoordinator: CoordinatorProtocol {
// MARK: - Public
func start() {
viewModel.callback = { [weak self] action in
guard let self else { return }
switch action {
case .confirm(let homeserverAddress):
self.useHomeserver(homeserverAddress)
case .dismiss:
self.callback?(.dismiss)
viewModel.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .confirm(let homeserverAddress):
self.useHomeserver(homeserverAddress)
case .dismiss:
actionsSubject.send(.dismiss)
}
}
}
.store(in: &cancellables)
}
func stop() {
@@ -88,7 +96,7 @@ final class ServerSelectionScreenCoordinator: CoordinatorProtocol {
switch await authenticationService.configure(for: homeserverAddress) {
case .success:
MXLog.info("Selected homeserver: \(homeserverAddress)")
callback?(.updated)
actionsSubject.send(.updated)
stopLoading()
case .failure(let error):
MXLog.info("Invalid homeserver: \(homeserverAddress)")
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
typealias ServerSelectionScreenViewModelType = StateStoreViewModel<ServerSelectionScreenViewState, ServerSelectionScreenViewAction>
@@ -21,7 +22,11 @@ typealias ServerSelectionScreenViewModelType = StateStoreViewModel<ServerSelecti
class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, ServerSelectionScreenViewModelProtocol {
private let slidingSyncLearnMoreURL: URL
var callback: (@MainActor (ServerSelectionScreenViewModelAction) -> Void)?
private var actionsSubject: PassthroughSubject<ServerSelectionScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<ServerSelectionScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(homeserverAddress: String, slidingSyncLearnMoreURL: URL, isModallyPresented: Bool) {
self.slidingSyncLearnMoreURL = slidingSyncLearnMoreURL
@@ -35,9 +40,9 @@ class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, Server
override func process(viewAction: ServerSelectionScreenViewAction) {
switch viewAction {
case .confirm:
callback?(.confirm(homeserverAddress: state.bindings.homeserverAddress))
actionsSubject.send(.confirm(homeserverAddress: state.bindings.homeserverAddress))
case .dismiss:
callback?(.dismiss)
actionsSubject.send(.dismiss)
case .clearFooterError:
clearFooterError()
}
@@ -14,11 +14,11 @@
// limitations under the License.
//
import Foundation
import Combine
@MainActor
protocol ServerSelectionScreenViewModelProtocol {
var callback: (@MainActor (ServerSelectionScreenViewModelAction) -> Void)? { get set }
var actions: AnyPublisher<ServerSelectionScreenViewModelAction, Never> { get }
var context: ServerSelectionScreenViewModelType.Context { get }
/// Displays an error to the user.
@@ -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.
@@ -37,7 +37,7 @@ final class WaitlistScreenCoordinator: CoordinatorProtocol {
private let parameters: WaitlistScreenCoordinatorParameters
private var viewModel: WaitlistScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<WaitlistScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
private var cancellables = Set<AnyCancellable>()
private var refreshCancellable: AnyCancellable?
var actions: AnyPublisher<WaitlistScreenCoordinatorAction, Never> {