# dimillian/approuter architecture

> Architecture diagram of the GitHub repository dimillian/approuter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dimillian/approuter
- Repository: https://github.com/dimillian/approuter

## Overview

##### Project type and purpose
- This is an open-source Swift Package (“AppRouter”) that provides a generic, reusable navigation router for SwiftUI apps.
- It exposes two main router types (SimpleRouter and Router) plus URL‐based deep linking, sheet presentation, and tab‐based navigation support.

##### File structure
- Top level:
- Sources/AppRouter – the core library code
- Examples – sample SwiftUI apps showing usage
- Tests – unit tests for SimpleRouter and Router
- Inside Sources/AppRouter:
- Protocols.swift – DestinationType, SheetType, TabType
- Router.swift / SimpleRouter.swift – the two router implementations
- URLNavigationHelper.swift – deep‐link parsing & dispatch
- Extensions.swift – helpers (e.g. URL builders)

##### Key README insights
- Two router classes annotated @Observable and @MainActor
- Generic protocols to allow apps to define their own Destination, Sheet, Tab enums
- SwiftUI integration via NavigationStack(path:), TabView(selection:), .sheet(item:) and .onOpenURL

a. Main components
- **Protocols module** (DestinationType, SheetType, TabType)
- **Core Routers**
- SimpleRouter<Destination,Sheet>
- Router<Tab,Destination,Sheet>
- **URLNavigationHelper** (parses URLs into Destination instances)
- **Extensions** (URL deep-link helper, type aliases)
- **SwiftUI Integration** layer—NavigationStack, TabView, .sheet, .onOpenURL
- **App code** (your Destination/Sheet/Tab implementations & Views)
b. Relationships & interactions
- App defines Destination/Sheet/Tab enums → conforms to Protocols → feeds into Routers
- Routers expose @Published path & presentedSheet → bound to SwiftUI views
- URLNavigationHelper parses incoming URLs → invokes router.navigate(to:)
- Router updates path/sheet → SwiftUI renders navigationDestination or .sheet
- In tab-based case, Router.selectedTab ↔ TabView(selection:)
c. Architectural patterns & design principles
- Generics + Protocols for high flexibility and type safety
- Reactive state management via @Observable and @MainActor for UI safety
- Separation of concerns: core routing logic decoupled from SwiftUI views
- Extensibility points via Protocols (apps plug in custom Destination/Sheet/Tab)
d. Relevant technologies
- Swift Package Manager (Package.swift)
- Swift 5.9, @Observable, @MainActor
- SwiftUI: NavigationStack, TabView, .sheet(item:), .onOpenURL

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% AppRouter Package
    subgraph "AppRouter Package"
        subgraph "Protocols Module"
            Protocols["Protocols Module<br/>(DestinationType, SheetType, TabType)"] 
        end
        subgraph "Core Routers"
            SimpleRouter["SimpleRouter<Destination,Sheet>"] 
            RouterNode["Router<Tab,Destination,Sheet>"] 
        end
        subgraph "Integration Adapters"
            URLHelper["URLNavigationHelper"] 
            Extensions["Extensions<br/>(URL builders, type aliases)"]
            SwiftUIIntegration["SwiftUI Integration<br/>(NavigationStack, TabView, .sheet, .onOpenURL)"]
        end
    end

    subgraph "App Code & Examples"
        App["App Code<br/>(Destination/Sheet/Tab enums & Views)"]
    end

    subgraph "Tests"
        Tests1["AppRouterTests"] 
        Tests2["SimpleRouterTests"] 
    end

    ExternalURL["Incoming URL"]:::external

    %% Relationships
    App -->|"defines enums conforming to"| Protocols
    Protocols -->|"provides types to"| SimpleRouter
    Protocols -->|"provides types to"| RouterNode
    App -->|"uses router"| SimpleRouter
    App -->|"uses router"| RouterNode
    SimpleRouter -->|"@Published path & sheet"| SwiftUIIntegration
    RouterNode -->|"@Published path & sheet"| SwiftUIIntegration
    RouterNode -->|"selectedTab binding"| SwiftUIIntegration
    ExternalURL -->|".onOpenURL"| URLHelper
    URLHelper -->|"parse & dispatch"| RouterNode
    App -->|"URL builders"| Extensions

    %% Click Events
    click Protocols "https://github.com/dimillian/approuter/blob/main/Sources/AppRouter/Protocols.swift"
    click SimpleRouter "https://github.com/dimillian/approuter/blob/main/Sources/AppRouter/SimpleRouter.swift"
    click RouterNode "https://github.com/dimillian/approuter/blob/main/Sources/AppRouter/Router.swift"
    click URLHelper "https://github.com/dimillian/approuter/blob/main/Sources/AppRouter/URLNavigationHelper.swift"
    click Extensions "https://github.com/dimillian/approuter/blob/main/Sources/AppRouter/Extensions.swift"
    click App "https://github.com/dimillian/approuter/tree/main/Examples/"
    click Tests1 "https://github.com/dimillian/approuter/blob/main/Tests/AppRouterTests/AppRouterTests.swift"
    click Tests2 "https://github.com/dimillian/approuter/blob/main/Tests/AppRouterTests/SimpleRouterTests.swift"

    %% Styles
    classDef protocols fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef routers fill:#DFFFD0,stroke:#2e7d32,color:#2e7d32
    classDef integration fill:#FFE8A1,stroke:#ed6a00,color:#ed6a00
    classDef appcolor fill:#E0E0E0,stroke:#424242,color:#424242
    classDef testcolor fill:#F5F5F5,stroke:#9E9E9E,color:#9E9E9E
    classDef external fill:#FFFFFF,stroke:#888888,color:#888888

    class Protocols protocols
    class SimpleRouter,Routernode routers
    class URLHelper,Extensions,SwiftUIIntegration integration
    class App appcolor
    class Tests1,Tests2 testcolor
    class ExternalURL external
```
