Code Monkey home page Code Monkey logo

coordinatorkit's Introduction

CoordinatorKit

A tool to implement navigation via coordinator + router for both UIKit and SwiftUI screens.

Installation

Add the package via Swift Package Manager, specify the URL to the repository.

Usage

Base protocols

  • RootCoordinator is responsible for the root screen

  • RootRouter is capable of setting the root window

  • FlowCoordinator is responsible for all navigation flows

  • FlowRouter is capable of pushing, presenting, dismissing, popping view controllers etc.

Tutorial

  1. A coordinator needs a router to do the job
  2. FlowCoordinator does not necessarily need a freshly created router - you can make use of its parent coordinator's router
  3. To process different endings of coordinator's flow you may specify the result type in completionHandler, for example enum FlowFinish:
final class MyFlowCoordinator: FlowCoordinator {
    var completionHandler: ((Result<FlowFinish, FlowCoordinatorError>) -> Void)?
    /* some other code */
}

extension MyFlowCoordinator {
    enum FlowFinish {
        /// Simply close the screen
        case quit
        /// Go to the next step
        case goToTheNextStep
    }
}
  1. To push a new coordinator from an existing one, you can simply pass a parent coordinator's router to a new one, for example:
final class MenuCoordinator: FlowCoordinator {
    let router: FlowRouter
    /* some other code */
}

extension MenuCoordinator {
    func makeSettings() {
        /// Here we pass the `MenuCoordinator`'s router to `SettingsCoordinator`
        let settingsCoordinator = SettingsCoordinator(router: router)
        start(settingsCoordinator)
    }
}
  1. To start a new coordinator in a modal view with some complex navigation flow you can do the following in the parent coordinator:
  • Create a new UINavigationController
  • Create a new FlowRouter
  • Create a new FlowCoordinator
  • Start your new coordinator
  • Present the navigation controller

For example, lets open MyNewCoordinator in a modal view from MyParentCoordinator:

final class MyParentCoordinator: FlowCoordinator {
    /* some other code */
    
    func startModalCoordinator() {
        let navigationController = UINavigationController()
        let newRouter = FlowRouter(navigationController: navigationController)
        let newCoordinator = MyNewCoordinator(router: newRouter)
        start(newCoordinator) { [weak self] result in
            /// You can do something with the result here if it affects your navigation flow
            self?.router.dismiss(animated: true)
        }
        router.present(navigationController, animated: true)
    }
}

Then you can start your normal navigation flow inside MyNewCoordinator:

final class MyNewCoordinator: FlowCoordinator {
    /* some other code */
    
    func start() {
        let view = SwiftUIView(
            goToNextScreen: { [weak self] in
                /// Continue your navigation flow
                self?.openNextScreen()
            },
            goBack: { [weak self] in
                /// Finish your navigation flow with a `cancel` result
                self?.finish(.cancel)
            }
        )
        let host = UIHostingController(rootView: view)
        router.push(host, animated: true)
    }
}

coordinatorkit's People

Contributors

baevra avatar

Stargazers

Allen Lee avatar Xer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.