Skip to content

Instantly share code, notes, and snippets.

@danlozano
Last active September 26, 2018 07:19
Show Gist options
  • Save danlozano/95ae5e3ef28c6e22551f21845ee9cfcc to your computer and use it in GitHub Desktop.
Save danlozano/95ae5e3ef28c6e22551f21845ee9cfcc to your computer and use it in GitHub Desktop.
Coordinator Class
class Coordinator {
private(set) var childCoordinators: [Coordinator] = []
func start() {
preconditionFailure("This method needs to be overriden by concrete subclass.")
}
func finish() {
preconditionFailure("This method needs to be overriden by concrete subclass.")
}
func addChildCoordinator(_ coordinator: Coordinator) {
childCoordinators.append(coordinator)
}
func removeChildCoordinator(_ coordinator: Coordinator) {
if let index = childCoordinators.index(of: coordinator) {
childCoordinators.remove(at: index)
} else {
Print("Couldn't remove coordinator: \(coordinator). It's not a child coordinator.")
}
}
func removeAllChildCoordinatorsWith<T>(type: T.Type) {
childCoordinators = childCoordinators.filter { $0 is T == false }
}
func removeAllChildCoordinators() {
childCoordinators.removeAll()
}
}
extension Coordinator: Equatable {
static func == (lhs: Coordinator, rhs: Coordinator) -> Bool {
return lhs === rhs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment