Код: Выделить всё
// MARK: - BaseCoordinator
@MainActor
class BaseCoordinator: Coordinator {
let navigator: NavigatorType
var childCoordinators: [Coordinator] = []
var cancellables = Set()
init(navigator: NavigatorType) {
self.navigator = navigator
}
func start() {
// Default implementation, can be overridden by subclasses
}
}
// MARK: - ExampleFlowCoordinator
final class ExampleFlowCoordinator: BaseCoordinator {
override func start() {
let coordinator = ExampleCoordinator()
add(coordinator)
coordinator.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .done:
remove(coordinator)
}
}
.store(in: &cancellables)
navigator.setRootViewController(coordinator.rootViewController, animated: true)
coordinator.start()
}
private func showExample2() {
let coordinator = ExampleCoordinator2()
coordinator.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .done:
remove(coordinator)
}
}
.store(in: &cancellables)
add(coordinator)
coordinator.start()
navigator.present(coordinator.rootViewController, animated: true) { [weak self] in
guard let self else { return }
remove(coordinator)
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... bscription