Skip to content

RakuyoKit/RaRouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RaRouter

RaRouter

δΈ­ζ–‡

RaRouter is a lightweight protocol-oriented router framework.

By using the framework to provide default types, or custom router, you can quickly build your own routing components, and then decouple your own projects.

Prerequisites

  • iOS 10 or later.
  • Xcode 10.0 or later required.
  • Swift 5.0 or later required.

Install

CocoaPods

pod 'RaRouter'

Features

  • Support to execute the corresponding content according to router string, or get some return value.
  • Protocol-oriented, providing a very high degree of freedom for you to customize router operations.
  • When adding/removing router components, no need to add/remove any registration related codes.
  • Through encapsulation, collectively define and hide router strings to reduce the risk of hard coding.
  • You can keep the type strict when performing router through encapsulation.

Usage

For related content, please refer to wiki: Quick Start.

Preview

The following code shows the module encapsulated with RaRouter: ModuleA.

With this code, you can have a preliminary impression of RaRouter:

For more functions and complete sample code, please refer to the demo provided with the warehouse (under the Examples directory).

// In the `Interface.swift` file of the router project
public enum ModuleA: ModuleRouter {
    
    public struct Factory: RouterFactory {
        public init() {}
    }
    
    public enum Table: String, RouterTable {
        
        case create         = "RaRouter://ModuleA/create"
        case doSomething    = "RaRouter://ModuleA/do/something"
        case calculateFrame = "RaRouter://ModuleA/calculate/frame" 
    }
}

public extension Router where Module == ModuleA {
    
    static func doSomething(start: Date, end: Date) -> DoResult {
        return Router.do(.doSomething, param: (start, end))
    }

    static func calculateFrame(with screenWidth: CGFloat) -> GetResult<CGRect> {
        return Router.get(of: CGRect.self, from: .calculateFrame, param: screenWidth)
    }
    
    static func create() -> ViewControllerResult {
        return Router.viewController(from: .create)
    }
}

// In the `Register.swift` file of the core project
extension Test.Factory: FactoryMediator {
    
    public var source: RouterFactory { RealFactory() }

    private struct RealFactory: RouterFactory {
        
        lazy var doHandlerFactories: [String : DoHandlerFactory]? = [
            
            ModuleA.Table.doSomething.rawValue : { (url, value) -> DoResult in
                
                guard let param = value as? (start: Date, end: Date) else {
                    return .failure(.parameterError(url: url, parameter: value))
                }
                
                print("We are doing these things from \(param.start) to \(param.end)")
                return .success(())
            }
        ]
        
        lazy var getHandlerFactories: [String : GetHandlerFactory]? = [
            
            ModuleA.Table.calculateFrame.rawValue : { (url, value) -> GetResult<AnyResult> in
                
                guard let screenWidth = value as? CGFloat else {
                    return .failure(.parameterError(url: url, parameter: value))
                }
                
                return .success(CGRect(x: 0, y: 0, width: screenWidth * 0.25, height: screenWidth))
            }
        ]
        
        lazy var viewControllerHandlerFactories: [String : ViewControllerHandlerFactory]? = [
            
            ModuleA.Table.create.rawValue : { (url, value) -> ViewControllerResult in
                return .success(UIViewController())
            }
        ]
    }
}

// Execute

_ = Router<ModuleA>.doSomething(start: Date(), end: Date())

let frame = Router<ModuleA>.calculateFrame(with: UIScreen.main.bounds.width).get(default: .zero)

if case .success(let controller) = Router<ModuleA>.create() {
    navigationController?.pushViewController(controller, animated: true)
}

Reference

During the development process I referred to URLNavigator, a great open source component. Feel the author's contribution!

License

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