Code Monkey home page Code Monkey logo

routerman's Introduction

RouterMan GitHub license Cocoapods

RouterMan is a protocol-oriented url router, base on regular expressions. RouterMan is simple and extensible.

Requirements

  • iOS 10 or later
  • Swift 5

##Features

  • Regular expressions support
  • Storyboard controller support,you can open a storyboard based controller easily
  • Mutable source urls to a target behavior
  • Mutable transitions support
  • RouterDelegate support

Installation with CocoaPods

pod 'RouterMan'

Installation with Carthage

github "wanggang316/RouterMan"

Useage

It's simple to use RouterMan, implement RoutableType based protocol for your controller or other types, then registe it to RouteMan, done.

RoutableType is routable base protocol, it derivative three protocols

  • RoutableControllerType
  • RoutableStoryboardControllerType
  • RoutableActionType

Config RoutableType

  • RoutableControllerType
class CityViewController: UIViewController: RoutableControllerType {
    
    static var patterns: [String] {
        return ["abc://page/cities/\\d+\\?name=\\w+"]
    }
    
    required convenience init(_ url: URLConvertible) {
        self.init()
        let params = url.urlValue?.queryParameters
        let title = params?["name"]
        self.title = title
    }
}
  • RoutableStoryboardControllerType
extension StoryViewController: RoutableStoryboardControllerType {
    
    static var patterns: [String] {
        return ["abc://page/stories/\\d+\\?name=\\S+",
                "http://www.xxx.com/stories/\\d+\\?name=\\w+"]
    }
    
    static var storyboardName: String {
        return "Main"
    }
    
    static var identifier: String {
        return "StoryViewController"
    }
    
    func initViewController(_ url: URLConvertible) {
        self.storyId = url.urlValue?.pathComponents.last
        self.storyName = url.urlValue?.queryParameters["name"]
    }
}
  • RoutableActionType
class AlertActionRouter: RoutableActionType {
    static func handle(_ url: URLConvertible) -> Bool {
        let title = url.urlValue?.queryParameters["title"]
        let message = url.urlValue?.queryParameters["message"]

        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Yes", style: .cancel, handler: nil))

        let appdelegate = UIApplication.shared.delegate as? AppDelegate
        appdelegate?.window?.rootViewController?.present(alert, animated: true, completion: nil)

        return true
    }
    
    static var patterns: [String] {
        return ["abc://alert\\?title=\\w+&message=\\w+"]
    }
}
  • RouterDelegate
    • You can implement RouterDelegate globally

      Router.default.delegate = AClass()
      
      extension AClass: RouterDelegate {
          func shouldShowController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind, shouldShow: @escaping (Bool) -> Void) {
            // do something
              shouldShow(true)
          }
          func willShowController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind) {
              // do something
          }
          
          func didShownController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind) {
              // do something
          }
      }
    • Also, you can implement RouterDelegate locally in a RoutableType

      extension AViewController: RoutableTypeDelegate {
          func shouldShowController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind, shouldShow: @escaping (Bool) -> Void) {
            	let isAuthorized = ...
            	shouldShow(isAuthorized)
          }
          
          func willShowController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind) {
              // do something
          }
          
          func didShownController(_ controller: UIViewController, fromViewController: UIViewController, segueKind: SegueKind) {
              // do something
          }
      }

Registe

Router.default.registe(CityViewController.self)
Router.default.registe(StoryViewController.self)
Router.default.registe(AlertActionRouter.self)

Handle

try? Router.default.handle(url)

License

RouterMan is released under the MIT license.

routerman's People

Contributors

wanggang316 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

al1020119

routerman's Issues

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.