SwiduxRouter 0.2.0

SwiduxRouter 0.2.0

Clément Cyril 维护。



SwiduxRouter

由 Swidux store 驱动的路由器。

稳定性

这个库应被视为 alpha 版本,并不稳定。可能会经常发生破坏性变更。

用法

声明您的路由

extension Route {
    static let home = Route(type: HomeViewController.self)
    static let product: (Id<Product>) -> Route = { Route(type: HomeViewController.self, routeParam: $0) }
    // ...
}

准备您的 Swidux store 定义初始路由

struct AppState {
    var root = RootDescriptor(initialRoute: .home)
    // ...
}

let store = Store(
    initialState: AppState(),
    reducer: .combine(reducers: [
        routeReducer.lift(\.root),
        // ...
    ])
)

如果视图控制器依赖于参数(例如产品页面依赖于产品 ID),则将视图控制器声明为 RoutableParametricRoutable

class HomeViewController: Routable {/* ... */}

class ProductViewController: ParametricRoutable {
    var routeParam: Id<Product>!
    // ...
}

初始化您的 Router 并将其添加到视图控制器层次结构中

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    let window = UIWindow()

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        window.rootViewController = Router(
            store: store,
            keyPath: \.root
        )
        window.makeKeyAndVisible()
        return true
    }
}

为了导航到新屏幕,请使用 store.dispatch 并传递以下其中的一个路由操作

enum RouteAction: Action {
    case present(Route)
    case push(Route)
    case back
    case backToRoot
    case backTo(Route)
    case reset(RootDescriptor)
}

已知问题

  • 不包含对 UITabBarController 的支持

许可证

SwiduxRouter 采用 MIT 许可协议发布。详情请见 LICENSE