Skip to content
lynx56Public

About

A lightweight flow management library for iOS

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flow

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.

Installation

CocoaPods (the pod is named GFlow):

pod 'GFlow', '~> 1.0'
import GFlow

Swift Package Manager isn't supported yet. Requires iOS 10+ and Swift 5.

Protocols

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

Usage

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)
    }
}

Type erasure

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.

License

MIT. See LICENSE.

About

A lightweight flow management library for iOS

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages