GithubHelp home page GithubHelp logo

timoliver / beeline Goto Github PK

View Code? Open in Web Editor NEW
62.0 3.0 2.0 215 KB

An extremely lean implementation on the classic iOS router pattern.

Swift 96.00% Ruby 4.00%
uikit swift ios tvos mac-catalyst

beeline's Introduction

Beeline

CI Version GitHub license Platform

Beeline is a very small library that aims to provide a lean, automatic implementation of the classic iOS router pattern. It extends UIViewController to retain a Router object that serves as the source of truth for controlling navigation flows for all of the view controller's children. When a child view controller wishes to transition to a new screen, it can call show() on itself and this request is passed up the view controller chain to the routing view controller.

Instructions

A very basic custom implementation looks as the following. First, we create a Swift enum conforming to Route where we can define the types of destinations with which we want to move:

enum AppRoute: Route {
    case viewController(number: Int)
}

Thanks to Swift associated enums, we can also include any custom parameters the new destination may need.

We then also make a new class which subclasses Router, which serves as our single point of truth for controlling the app flow based off the destinations we defined above:

public class AppRouter: Router {
    override func show(_ route: Route,
                        from sourceViewController: UIViewController?) -> Bool {

        // Optionally, filter out routes we don't support
        guard let appRoute = route as? AppRoute else { return false }

        // Check the requested enum, and perform the transition
        switch appRoute {
        case .viewController(let number):
            let newViewController = ViewController(number: number)
            sourceViewController?
                .navigationController?
                .pushViewController(newViewController, animated: true)
        }

        return true
    }
}

Using Objective-C associated objects, we can assign this router to any parent view controller that contains all of the view controllers that might want to perform these transitions:

let navigationController = UINavigationController(rootViewController: ViewController())
navigationController.router = AppRouter()

Finally, without any further modification to any of the child view controllers, they can start a transition by simply calling show() with the desired destination:

class ViewController: UIViewController {
    func moveToNewViewController() {
        show(AppRoute.viewController(number: 2))
    } 
} 

And that's the entire library! ๐ŸŽ‰

Requirements

  • Swift 5
  • UIKit-compatible platforms (iOS, tvOS, Mac Catalyst)

Installation

Beeline is a very small framework, with all of its code contained in Router.swift. You can install it in the following ways:

Manual Installation

Drag the Beeline/Router.swift file into your Xcode project.

CocoaPods

pod 'Beeline'

SPM

You can add Beeline to an Xcode project by adding it as a package dependency.

https://github.com/TimOliver/Beeline

or, add the following to the dependencies in package.swift.

dependencies: [
  .package(url: "https://github.com/TimOliver/Beeline", from: "1.0.2")
]

Carthage

No plans to support Carthage at the moment, but please consider filing a PR if you would like it!

Credits

Beeline was built as a component of iComics 2 by Tim Oliver

License

Beeline is available under the MIT License. Please check the LICENSE file for more information.

beeline's People

Contributors

alexkobachijp avatar timoliver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.