Beam/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateScreenCoordinator.swift

55 lines
1.6 KiB
Swift
Raw Permalink Normal View History

2022-05-10 15:05:56 +03:00
//
// Copyright 2022-2024 New Vector Ltd.
2022-05-10 15:05:56 +03:00
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
2022-05-10 15:05:56 +03:00
//
2023-12-19 12:12:16 +01:00
// periphery:ignore:all - this is just a template remove this comment once generating the final file
import Combine
2022-05-10 15:05:56 +03:00
import SwiftUI
2023-08-02 13:24:57 +03:00
struct TemplateScreenCoordinatorParameters { }
2022-05-10 15:05:56 +03:00
enum TemplateScreenCoordinatorAction {
2023-08-02 13:24:57 +03:00
case done
// Consider adding CustomStringConvertible conformance if the actions contain PII
}
final class TemplateScreenCoordinator: CoordinatorProtocol {
private let parameters: TemplateScreenCoordinatorParameters
private let viewModel: TemplateScreenViewModelProtocol
2022-05-10 15:05:56 +03:00
private var cancellables = Set<AnyCancellable>()
private let actionsSubject: PassthroughSubject<TemplateScreenCoordinatorAction, Never> = .init()
var actionsPublisher: AnyPublisher<TemplateScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
2022-05-10 15:05:56 +03:00
init(parameters: TemplateScreenCoordinatorParameters) {
2022-05-10 15:05:56 +03:00
self.parameters = parameters
2023-08-02 13:24:57 +03:00
viewModel = TemplateScreenViewModel()
2022-05-10 15:05:56 +03:00
}
func start() {
viewModel.actionsPublisher.sink { [weak self] action in
2023-08-02 13:24:57 +03:00
MXLog.info("Coordinator: received view model action: \(action)")
guard let self else { return }
switch action {
2023-08-02 13:24:57 +03:00
case .done:
actionsSubject.send(.done)
}
2022-05-10 15:05:56 +03:00
}
.store(in: &cancellables)
2022-05-10 15:05:56 +03:00
}
func toPresentable() -> AnyView {
AnyView(TemplateScreen(context: viewModel.context))
2022-05-10 15:05:56 +03:00
}
}