Created
May 26, 2017 15:42
-
-
Save ksmandersen/b0eaebd7d0832687602a8331fe4c67d2 to your computer and use it in GitHub Desktop.
Coordinator Pattern in Swift
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
final class ApplicationContext { | |
// This is where you initialize most of the application stack | |
// that needs to be passed down to the coordinator chain for | |
// fetching data, and other important stuff. | |
// Example: | |
// let networkClient = NetworkClient() | |
} |
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 Coordinator: class { | |
var childCoordinators: [Coordinator] { get set } | |
var context: ApplicationContext { get } | |
func start() | |
} | |
extension Coordinator { | |
func addChild(coordinator: Coordinator) { | |
childCoordinators.append(coordinator) | |
} | |
func removeChild(coordinator: Coordinator) { | |
childCoordinators = childCoordinators.filter { $0 !== coordinator } | |
} | |
} |
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
class RootCoordinator: Coordinator { | |
var childCoordinators: [Coordinator] = [] | |
let context: ApplicationContext | |
init(context: ApplicationContext) { | |
self.context = context | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment