Skip to content

Instantly share code, notes, and snippets.

@irace
Last active December 20, 2017 18:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irace/f4b82aae1e6f1fe7705e3f3b5df83434 to your computer and use it in GitHub Desktop.
Save irace/f4b82aae1e6f1fe7705e3f3b5df83434 to your computer and use it in GitHub Desktop.
import Foundation
/**
Coordinators are a design pattern that encourages decoupling view controllers such that they know as little as possible
about how they are presented, and don’t directly manipulate data or present other view controllers. Coordinators can be
“nested” such that child coordinators encapsulate different flows and present any from becoming too large.
- https://vimeo.com/144116310
- http://khanlou.com/2015/10/coordinators-redux/
- http://khanlou.com/2015/01/the-coordinator/
*/
public protocol Coordinator: class {
/**
Open a URL, or return `false` if unable to open.
- parameter URL: URL that the application is attempting to open.
- returns: `true` if the URL was openable by the coordinator, otherwise `false`.
*/
func openURL(URL: NSURL) -> Bool
}
public extension Coordinator {
func openURL(URL: NSURL) -> Bool {
return false
}
}
/**
A helper class that a coordinator can use to store it’s child coordinators. Swift doesn’t allow arrays of heterogenous
types (even if they all conform to the same protocol, e.g. `Coordinator`) to conform to `Equatable`, so we implement
our own `removeObject:` functionality that doesn’t rely on it. It does rely on coordinators being reference types,
however.
*/
public struct CoordinatorArray {
private var coordinators: [Coordinator] = []
public init() {
}
public mutating func append(coordinator: Coordinator) {
coordinators.append(coordinator)
}
public mutating func removeObject(coordinator: Coordinator) -> Coordinator? {
guard let index = coordinators.indexOf({ $0 === coordinator }) else { return nil }
return coordinators.removeAtIndex(index)
}
}
@radianttap
Copy link

Given that you only implement openURL, wouldn't Router be a better name..?
( I see the links in the comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment