Coder Social home page Coder Social logo

modulekit's Introduction

ModuleKit

ModuleKit is a Swift Package for modularizing your Swift codebase with customizable routes and actions. It is compatible with iOS, macOS, watchOS, and tvOS.

Installation

To install ModuleKit, add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/jsambuo/ModuleKit.git", from: "1.0.0")
]

Usage

Defining a Module

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

struct MyModule: Module {
    var routes: [Route] {
        [
            Route(path: "/my/route") { parameters in
                VStack {
                    Text("Route matched with parameters: \(parameters)")
                }
            },
            Route(path: "/simple/route") {
                VStack {
                    Text("Simple route matched with no parameters")
                }
            }
        ]
    }

    var actions: [Action] {
        [
            Action(key: "myAction") { parameters in
                print("My action executed with parameters: \(parameters)")
            }
        ]
    }
}

Using ModuleManager and RouteView

import SwiftUI
import ModuleKit

@main
struct MyApp: App {
    private let moduleManager = ModuleManager(modules: [MyModule()])

    var body: some Scene {
        WindowGroup {
            NavigationView {
                VStack {
                    RouteView("/my/route", parameters: ["param1": "value1"])
                    RouteView("/simple/route")
                }
                .environmentObject(moduleManager)
            }
        }
    }
}

API

ModuleManager

ModuleManager is responsible for managing and handling routes and actions for multiple modules.

public class ModuleManager: ObservableObject {
    public init(modules: [any Module] = [])
    public func registerModule(_ module: any Module)
    public func registerModules(_ modules: [any Module])
    public func viewForRoute(path: String, parameters: [String: Any]) -> (any View)?
    public func performAction(key: String, parameters: [String: Any] = [:])
}

RouteView

RouteView is a SwiftUI view that renders a view for a given route path using the ModuleManager.

public struct RouteView: View {
    public init(_ path: String, parameters: [String: Any] = [:])
}

License

ModuleKit is released under the MIT license. See LICENSE for details.

modulekit's People

Contributors

jsambuo avatar

Watchers

 avatar

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.