ModuleKit is a Swift Package for modularizing your Swift codebase with customizable routes and actions. It is compatible with iOS, macOS, watchOS, and tvOS.
To install ModuleKit, add the following to your Package.swift file:
dependencies:[.package(url:"https://github.com/jsambuo/ModuleKit.git", from:"1.0.0")]Define a module by conforming to the Module protocol. Each module must provide an array of routes and an array of actions.
import ModuleKit
import SwiftUI
structMyModule:Module{varroutes:[Route]{[Route(path:"/my/route"){ parameters inVStack{Text("Route matched with parameters: \(parameters)")}},Route(path:"/simple/route"){VStack{Text("Simple route matched with no parameters")}}]}varactions:[Action]{[Action(key:"myAction"){ parameters inprint("My action executed with parameters: \(parameters)")}]}}import SwiftUI
import ModuleKit
@mainstructMyApp:App{privateletmoduleManager=ModuleManager(modules:[MyModule()])varbody:someScene{WindowGroup{NavigationView{VStack{RouteView("/my/route", parameters:["param1":"value1"])RouteView("/simple/route")}.environmentObject(moduleManager)}}}}ModuleManager is responsible for managing and handling routes and actions for multiple modules.
publicclassModuleManager:ObservableObject{publicinit(modules:[anyModule]=[])publicfunc registerModule(_ module:anyModule)publicfunc registerModules(_ modules:[anyModule])publicfunc viewForRoute(path:String, parameters:[String:Any])->(anyView)?publicfunc performAction(key:String, parameters:[String:Any]=[:])}RouteView is a SwiftUI view that renders a view for a given route path using the ModuleManager.
publicstructRouteView:View{publicinit(_ path:String, parameters:[String:Any]=[:])}ModuleKit is released under the MIT license. See LICENSE for details.