Implemented the Coordinator protocol in the Coordination library.

This commit is contained in:
Javier Cicchelli 2024-03-21 16:26:11 +01:00
parent 1b4fde9adb
commit 022f975d5e

View File

@ -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 }
}
}