Skip to content

Instantly share code, notes, and snippets.

@ethanjdiamond
Created January 29, 2017 19:39
Show Gist options
  • Save ethanjdiamond/4e558bb4e9b2c45bf6b09b61840e4d5b to your computer and use it in GitHub Desktop.
Save ethanjdiamond/4e558bb4e9b2c45bf6b09b61840e4d5b to your computer and use it in GitHub Desktop.
protocol Routable: class {
associatedtype I: Interactable
func configure(interactor: I, viewController: UIViewController?)
func attach(router: Router<I>)
func detach(router: Router<I>)
}
class Router<I: Interactable>: Routable {
var interactor: Interactable!
var viewController: UIViewController?
private var childRouters = Set<Router>()
func configure(interactor: I, viewController: UIViewController? = nil) {
self.interactor = interactor
self.viewController = viewController
}
func attach(router: Router) {
childRouters.insert(router)
router.interactor.interactorDidAttach()
}
func detach(router: Router) {
router.interactor.interactorWillDetach()
childRouters.remove(router)
}
}
extension Router: Hashable, Equatable {
var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
final class func == (lhs: Router, rhs: Router) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment