diff --git a/Libraries/Coordination/Kit/Sources/Protocols/Coordinator.swift b/Libraries/Coordination/Kit/Sources/Protocols/Coordinator.swift new file mode 100644 index 0000000..61607d0 --- /dev/null +++ b/Libraries/Coordination/Kit/Sources/Protocols/Coordinator.swift @@ -0,0 +1,57 @@ +// +// Coordinator.swift +// ReviewsCoordinationKit +// +// Created by Javier Cicchelli on 21/03/2024. +// Copyright © 2024 Röck+Cöde. All rights reserved. +// + +public protocol Coordinator: AnyObject { + + // MARK: Properties + var children: [Coordinator] { get set } + var router: Router { get } + + // MARK: Functions + func present(animated: Bool, onDismiss: Router.OnDismissClosure?) + +} + +// MARK: - Implementations +public extension Coordinator { + + // MARK: Functions + func present( + child: Coordinator, + animated: Bool, + onDismiss: Router.OnDismissClosure? = nil + ) { + store(child) + + child.present(animated: animated) { [weak self, weak child] in + guard let self, let child else { return } + + self.free(child) + onDismiss?() + } + } + + func dismiss(animated: Bool) { + router.dismiss(animated: animated) + } + +} + +// MARK: - Helpers +private extension Coordinator { + + // MARK: Functions + func store(_ coordinator: Coordinator) { + children.append(coordinator) + } + + func free(_ coordinator: Coordinator) { + children = children.filter { $0 !== coordinator } + } + +}