Skip to content

Latest commit

History

64 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

GoodCoordinator

🧭 Programmatic navigation made simple, 100% SwiftUI

In just 3 lines of code

@Push(makeHome)varhomefunc makeHome(model:HomeModel)->HomeView{HomeView(model: model)}route(to: \.home,HomeModel(username: username, password: password))

Usage

  1. Implement a SwiftUI App
    @mainstructNavigationApp:App{privateletcoordinator=AppCoordinator(())varbody:someScene{WindowGroup{
    coordinator.makeView()}}}

    This is a standard implementation for a 100% SwiftUI app.

  2. Implement your first coordinator
    import GoodCoordinator
    finalclassAppCoordinator:NavigationCoordinator{typealiasInput=VoidtypealiasOutput=Voidvarstate:NavigationStack=.init()@Root(makeRoot)varrootfunc makeRoot()->InitialView{InitialView() /// SwiftUI View!
    }}

    Don't worry about code you don't understand yet. Feel free to name the makeRoot function in any way you prefer. The function returns the view of your choice. However, to get this code to compile, you need to do one more thing:

  3. Modify your SwiftUI View a bit
    import GoodCoordinator
    struct InitialView:View,Screen{ /// Extend from Screen as well
    ...
  4. Add a navigation step
    @Push(makeNewScreen)varopenNewScreenfunc makeNewScreen()->NewScreen{NewScreen(viewModel:NewScreenViewModel())}

    Add a step into your coordinator and specify, how the view will get constructed.

  5. Navigate!
    @EnvironmentObjectvarrouter:Router<AppCoordinator>
    // SwiftUI view code
    Button("Go to new screen"){
    router.coordinator.route(to: \.openNewScreen)}
    // More SwiftUI view code

    Launch the navigation step from your view.

Logging

structLogger:CoordinatorLogger{func logCoordinatorEvent(_ message:Any, level:LogLevel, fileName:String, lineNumber:Int){print("[\(level.rawValue)] \(message) [\(fileName):\(lineNumber)]")}}CoordinatorConfiguration.logger =Logger()

Installation

You can use Swift Package Manager (SPM) to import this package. Use the following URL:

https://github.com/GoodRequest/GoodCoordinator-iOS.git

Advanced examples

A: Pushing data (forwards data flow)

  1. Set your datatype as an Input

    Modify the navigation step that requires input data - add the data as a parameter.

    Note: navigation steps are limited to 1 input parameter only. This is due to practical reasons, as adding 2 or more input parameters would require disproportional amount of additional function overloads in the code base. If you require multiple input parameters, consider making a struct out of them and pass the struct.

    finalclassMyCoordinator:NavigationCoordinator{...func makeRoot(someData:String)->RootView{RootView(model:RootModel(data: someData))}...}

    If your input view is the @Root step of a coordinator, change the whole coordinator Input typealias to reflect your datatype:

    typealiasInput=String
  2. Navigate

    In a SwiftUI view:

    @Stateprivatevarusername:String=""...Button("Push with data"){
    router.coordinator.route(to: \.step, username)}

    Call the route function with additional parameter as an input. Xcode should automatically autocomplete this function with expected data type.

About

🧭 Simplified programmatic navigation in SwiftUI

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages