Created
January 29, 2017 19:39
-
-
Save ethanjdiamond/4e558bb4e9b2c45bf6b09b61840e4d5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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