[Library] Coordination library (#15)
This PR contains the work done to implement the necessary protocols and router to implement the Coordinators pattern in the app. Reviewed-on: rock-n-code/app-reviews#15 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #15.
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Router.swift
|
||||
// ReviewsCoordinationKet
|
||||
//
|
||||
// Created by Javier Cicchelli on 21/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
public protocol Router: AnyObject {
|
||||
|
||||
// MARK: Type aliases
|
||||
typealias OnDismissClosure = () -> Void
|
||||
|
||||
// MARK: Functions
|
||||
func present(
|
||||
_ viewController: UIViewController,
|
||||
animated: Bool,
|
||||
onDismiss: OnDismissClosure?
|
||||
)
|
||||
|
||||
func dismiss(animated: Bool)
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Implementations
|
||||
public extension Router {
|
||||
|
||||
// MARK: Functions
|
||||
func present(
|
||||
_ viewController: UIViewController,
|
||||
animated: Bool
|
||||
) {
|
||||
present(
|
||||
viewController,
|
||||
animated: animated,
|
||||
onDismiss: nil
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user