mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
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:
+20
-8
@@ -14,8 +14,13 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
enum SessionVerificationScreenCoordinatorAction {
|
||||
case done
|
||||
}
|
||||
|
||||
struct SessionVerificationScreenCoordinatorParameters {
|
||||
let sessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol
|
||||
}
|
||||
@@ -24,7 +29,12 @@ final class SessionVerificationScreenCoordinator: CoordinatorProtocol {
|
||||
private let parameters: SessionVerificationScreenCoordinatorParameters
|
||||
private var viewModel: SessionVerificationScreenViewModelProtocol
|
||||
|
||||
var callback: (() -> Void)?
|
||||
private let actionsSubject: PassthroughSubject<SessionVerificationScreenCoordinatorAction, Never> = .init()
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
var actions: AnyPublisher<SessionVerificationScreenCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(parameters: SessionVerificationScreenCoordinatorParameters) {
|
||||
self.parameters = parameters
|
||||
@@ -35,14 +45,16 @@ final class SessionVerificationScreenCoordinator: CoordinatorProtocol {
|
||||
// MARK: - Public
|
||||
|
||||
func start() {
|
||||
viewModel.callback = { [weak self] action in
|
||||
guard let self else { return }
|
||||
|
||||
switch action {
|
||||
case .finished:
|
||||
self.callback?()
|
||||
viewModel.actions
|
||||
.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
|
||||
switch action {
|
||||
case .finished:
|
||||
actionsSubject.send(.done)
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func toPresentable() -> AnyView {
|
||||
|
||||
+14
-9
@@ -14,6 +14,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
typealias SessionVerificationViewModelType = StateStoreViewModel<SessionVerificationScreenViewState, SessionVerificationScreenViewAction>
|
||||
@@ -23,7 +24,11 @@ class SessionVerificationScreenViewModel: SessionVerificationViewModelType, Sess
|
||||
|
||||
private var stateMachine: SessionVerificationScreenStateMachine
|
||||
|
||||
var callback: ((SessionVerificationScreenViewModelAction) -> Void)?
|
||||
private var actionsSubject: PassthroughSubject<SessionVerificationScreenViewModelAction, Never> = .init()
|
||||
|
||||
var actions: AnyPublisher<SessionVerificationScreenViewModelAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(sessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol,
|
||||
initialState: SessionVerificationScreenViewState = SessionVerificationScreenViewState()) {
|
||||
@@ -79,7 +84,7 @@ class SessionVerificationScreenViewModel: SessionVerificationViewModelType, Sess
|
||||
return
|
||||
}
|
||||
|
||||
callback?(.finished)
|
||||
actionsSubject.send(.finished)
|
||||
case .accept:
|
||||
stateMachine.processEvent(.acceptChallenge)
|
||||
case .decline:
|
||||
@@ -93,24 +98,24 @@ class SessionVerificationScreenViewModel: SessionVerificationViewModelType, Sess
|
||||
stateMachine.addTransitionHandler { [weak self] context in
|
||||
guard let self else { return }
|
||||
|
||||
self.state.verificationState = context.toState
|
||||
state.verificationState = context.toState
|
||||
|
||||
switch (context.fromState, context.event, context.toState) {
|
||||
case (.initial, .requestVerification, .requestingVerification):
|
||||
self.requestVerification()
|
||||
requestVerification()
|
||||
case (.verificationRequestAccepted, .startSasVerification, .startingSasVerification):
|
||||
self.startSasVerification()
|
||||
startSasVerification()
|
||||
case (.showingChallenge, .acceptChallenge, .acceptingChallenge):
|
||||
self.acceptChallenge()
|
||||
acceptChallenge()
|
||||
case (.showingChallenge, .declineChallenge, .decliningChallenge):
|
||||
self.declineChallenge()
|
||||
declineChallenge()
|
||||
case (_, .cancel, .cancelling):
|
||||
self.cancelVerification()
|
||||
cancelVerification()
|
||||
case (_, _, .verified):
|
||||
// Dismiss the success screen automatically.
|
||||
Task {
|
||||
try? await Task.sleep(for: .seconds(2))
|
||||
self.callback?(.finished)
|
||||
self.actionsSubject.send(.finished)
|
||||
}
|
||||
default:
|
||||
break
|
||||
|
||||
+2
-2
@@ -14,10 +14,10 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
@MainActor
|
||||
protocol SessionVerificationScreenViewModelProtocol {
|
||||
var callback: ((SessionVerificationScreenViewModelAction) -> Void)? { get set }
|
||||
var actions: AnyPublisher<SessionVerificationScreenViewModelAction, Never> { get }
|
||||
var context: SessionVerificationViewModelType.Context { get }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user