Latest 0.2.3
Homepage https://github.com/orangeince/SortingHat
License MIT
Platforms ios 10.0
Authors

Build Status
Version
License
Platform

SortingHat是一个纯Swift的路由库。提供普适的URL调用方案和枚举传参的内部调用方案。

Features

  • [x] 协议声明式的路由规则定义,省心
  • [x] 安全可靠的内部调用方式,参数变更引发的错误会在编译阶段暴露,放心
  • [ ] 代码自动生成,无需手动注册,贴心

Example

  1. 定义路由节点
extension DetailViewController: URLRoutable {
    static var urlPattern: String {
        return "x://detail/:id"
    }
    struct Parameters: ParametersDecodable {
        // This is a type wrapper like Box<Int>: Decodable
        let id: ValueType.Int
    }
    static func constructViewController(params: Parameters) -> UIViewController? {
        let vc = DetailViewController()
        vc.title = String(params.id.value)
        return vc
    }
}
  1. 路由注册
// 注册ViewController节点
SortingHat.register(node: RouteNode<DetailViewController>())

// 注册Handler节点
SortingHat.register(url: "x://handler/:target/:action") { (params) -> String? in
    guard let target = params["target"] as? String,
        let action = params["action"] as? String
        let content = params["content"] as? String
        else { return nil }
    return "target: (target)naction: (action)ncontent:(content)"
}
  1. 模块中枢增开路由节点(以下代码可自动生成 In future
extension ModuleCenter {
    enum Demo {
        case detail(title: String)
        case list(title: String, id: String)
    }
}
extension ModuleCenter.Demo: RouteTargetType {
    var node: RouteNodeType {
        switch self {
        case .detail: return RouteNode<DetailViewController>()
        case .list: return RouteNode<ListViewController>()
        }
    }
}
  1. 业务场景使用
// URL调用方式
SortingHat.show(targetUrl: "x://detail?title=SortingHat.detail", from: self)

// 内部Target调用方式
SortingHat.show(target: ModuleCenter.Demo.list(title: "SortingHat.list", id: "BJ2019"), from: self)

// Handler for url
SortingHat.handle(url: "x://handler/storyTarget/commentAction?content=Hello,SortingHat")

// ViewController之间的传值问题. Handle message between viewControllers.
SortingHat.show(targetUrl: "x://detail?title=MessageSender", from: self) { message in
    guard if let content = message as? String else { return print("Not match the message.") }
    print("Content is (content)")
}

RoadMap

  • [x] Unit Test.
  • [ ] Register URLs at module load point.
  • [ ] Auto generate extension code for submodule.
  • [ ] Documentation.

Installation

pod 'SortingHat', '~> 0.2'

License

SortingHat is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SortingHat",
    "version": "0.2.3",
    "summary": "A lightweight and pure swift library for router.",
    "description": "SortingHat is a powerful and pure swift library for router.nIt support not only URL routing, but also internal invoke between modules.",
    "homepage": "https://github.com/orangeince/SortingHat",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Shao": "[email protected]"
    },
    "source": {
        "git": "https://github.com/orangeince/SortingHat.git",
        "tag": "0.2.3"
    },
    "social_media_url": "https://twitter.com/xiaoyipie",
    "swift_versions": "4.2",
    "platforms": {
        "ios": "10.0"
    },
    "source_files": [
        "Sources/**/*.swift",
        "Sources/SortingHat.h"
    ]
}

Pin It on Pinterest

Share This