A tiny protocol-based way to structure navigation in UIKit apps. A flow owns a piece of navigation (onboarding, checkout, settings), builds its first screen and reports when it's done, optionally with a result. Parents decide what happens next, so screens stay unaware of each other.
CocoaPods (the pod is named GFlow):
pod 'GFlow', '~> 1.0'import GFlowSwift Package Manager isn't supported yet. Requires iOS 10+ and Swift 5.
| Protocol | Use for |
|---|---|
Flow |
Any flow: startViewController() builds its first screen |
FiniteFlow |
A flow that ends: didFinish: () -> Void |
FiniteFlowWithOutput |
A flow that ends with a result: didFinish: (Output) -> Void |
AnyFiniteFlow<Output> |
Type-erased finite flow, optionally mapping the output |
final class SignInFlow: FiniteFlowWithOutput {
var didFinish: (User) -> Void = { _ in }
func startViewController() -> UIViewController {
let signIn = SignInViewController()
signIn.onSignedIn = { [weak self] user in
self?.didFinish(user)
}
return UINavigationController(rootViewController: signIn)
}
}
final class AppFlow {
private let window: UIWindow
private var current: AnyObject? // keep the active flow alive
init(window: UIWindow) {
self.window = window
}
func start() {
let signIn = SignInFlow()
signIn.didFinish = { [weak self] user in
self?.showHome(for: user)
}
current = signIn
window.rootViewController = signIn.startViewController()
}
private func showHome(for user: User) {
window.rootViewController = HomeViewController(user: user)
}
}Store flows with different types but the same output, or adapt an output:
let signIn = AnyFiniteFlow<User>(flow: SignInFlow())
let didSignIn = AnyFiniteFlow<Bool>(flow: SignInFlow(), mapper: { _ in true })
didSignIn.didFinish = { success in print("Signed in:", success) }Flows are classes: keep a strong reference to the active one while it's on screen, because view controllers usually capture it weakly.
MIT. See LICENSE.