// // Copyright 2025 Element Creations Ltd. // Copyright 2025 New Vector Ltd. // // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial // Please see LICENSE files in the repository root for full details. // import Combine import Foundation import SwiftState protocol StateMachineFactoryProtocol { func makeUserSessionFlowStateMachine(state: UserSessionFlowCoordinator.State) -> StateMachine func makeChatsFlowStateMachine() -> ChatsFlowCoordinatorStateMachine func makeMembersFlowStateMachine(state: RoomMembersFlowCoordinator.State) -> StateMachine } struct StateMachineFactory: StateMachineFactoryProtocol { func makeUserSessionFlowStateMachine(state: UserSessionFlowCoordinator.State) -> StateMachine { .init(state: state) } func makeChatsFlowStateMachine() -> ChatsFlowCoordinatorStateMachine { .init() } func makeMembersFlowStateMachine(state: RoomMembersFlowCoordinator.State) -> StateMachine { .init(state: state) } } // MARK: For testing class PublishedStateMachineFactory: StateMachineFactoryProtocol { let baseFactory = StateMachineFactory() // MARK: UserSessionFlowCoordinator let userSessionFlowStatePublisher = PassthroughSubject() func makeUserSessionFlowStateMachine(state: UserSessionFlowCoordinator.State) -> StateMachine { let stateMachine = baseFactory.makeUserSessionFlowStateMachine(state: state) stateMachine.addAnyHandler(.any => .any) { [weak self] in self?.userSessionFlowStatePublisher.send($0.toState) } return stateMachine } // MARK: ChatsFlowCoordinator let chatsFlowStatePublisher = PassthroughSubject() func makeChatsFlowStateMachine() -> ChatsFlowCoordinatorStateMachine { let stateMachine = baseFactory.makeChatsFlowStateMachine() stateMachine.addTransitionHandler { [weak self] in self?.chatsFlowStatePublisher.send($0.toState) } return stateMachine } // MARK: MembersFlowCoordinator let membersFlowStatePublisher = PassthroughSubject() func makeMembersFlowStateMachine(state: RoomMembersFlowCoordinator.State) -> StateMachine { let stateMachine = baseFactory.makeMembersFlowStateMachine(state: state) stateMachine.addAnyHandler(.any => .any) { [weak self] in self?.membersFlowStatePublisher.send($0.toState) } return stateMachine } }