# n26/flowkit-ios architecture

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

- Interactive diagram: https://gitdiagram.com/n26/flowkit-ios
- Repository: https://github.com/n26/flowkit-ios

## Overview

##### Main Components and Their Relationships
- Core FlowKit Module (found in the flowkit-ios folder)
– Components: Flow, FlowDefinition, FlowData, FlowKit, FlowKitSwiftUI.
– Role: These handle the definition, creation, and execution of flows. The FlowData (made from JSON or programmatically) is used by FlowKit to initiate flows.
- Step Handling Infrastructure (located in flowkit-ios/Classes/Step, StepHandler, and related)
– Components: Step protocols (StepProtocol.swift), concrete step implementations (AnyStep, NextStep, etc.), StepFactory, and StepHandler.
– Role: They are responsible for generating and managing individual flow steps. The step factory creates step handlers based on step type (the “if stepInfo.type == …” logic in the README).
- Flow Output Typing
– Components: FlowOutput and its associated definitions.
– Role: Provides a typed final output for the entire flow, leveraging Swift’s type safety and autocompletion.
- FlowKitAdditions Module
– Components: Additional classes such as display conditions (BoolDisplayCondition, DateDisplayCondition, etc.), next step conditions, and utility extensions.
- Example Implementations
– Components: Directory under flowkitAdditions/Example which includes projects for both UIKit and SwiftUI integrations.
– Role: Demonstrates how to instantiate FlowKit, inject the step factory, start a flow, and handle flow output. It shows clear separation between the framework’s logic (inside FlowKit) and the UI integration layers (UIKit/AppDelegate, SwiftUI/FlowKitSwiftUIView).

##### Relationships and Interactions
- The external data source (remote JSON, local files, or programmatic creation) feeds into FlowData – an initiator for flows.
- This FlowData is then used by FlowKit (or FlowKitSwiftUIViewModel for SwiftUI) to start the flow.
- Internally, FlowKit uses the StepFactory to map flow steps (from the JSON or definition) to specific StepHandler implementations.
- Each step handler (created via the sugar syntax provided in the README) encapsulates the logic for presenting a step, handling user interactions, and signaling when it is complete.
- As steps complete, outputs are gathered and merged into a type-safe FlowOutput that ultimately becomes the final result when the flow finishes.
- The FlowKitAdditions module extends this architecture by providing common conditions (display and next step conditions) and utility functionalities that are used within step handlers.
- The Example projects then illustrate how to wire up these components in a real-world iOS application using navigation controllers (UIKit) or SwiftUI views/modals.

##### Key Architectural Patterns and Design Principles
- Modularity – Separation between the flow definition, step management, output typing, and UI integration.
- Extensibility – The design (e.g., through protocols like FlowDefinition, StepHandlerDefinition, and StepFactory) makes it easy to add custom steps and conditions.
- Loose Coupling – The framework is built around composition and delegation (e.g., step handlers and step factories create and manage steps), minimizing direct dependencies.
- Type Safety – The use of Swift generics and protocols to enforce that the flow output, step outputs, and content definitions are correctly used at compile time.
- Dual UI Support – The architecture supports both UIKit and SwiftUI integrations via separate entry points: FlowKit (for UIKit) and FlowKitSwiftUI.

##### Final Checklist for the Engineer
- Every major component (Flow Data, Core Engine, Step Handler, Output, Extensions, Example Integration) is represented.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Flow Data Provider
    subgraph "Flow Data Provider"
        FDP("Flow Data Provider")
    end

    %% Core Engine
    subgraph "FlowKit Core Engine – Handles flow execution"
        FD("FlowData (JSON/Definition)"):::core
        FDfn("FlowDefinition (Definition)"):::core
        FKit("FlowKit (UIKit Entry)"):::core
        FKitSwiftUI("FlowKitSwiftUI (SwiftUI Entry)"):::core
    end

    %% Step Handling Subsystem
    subgraph "Step Handling Subsystem"
        SP("StepProtocol"):::step
        AS("AnyStep"):::step
        NS("NextStep"):::step
        SF("StepFactory"):::step
        ST("StepsTree"):::step
        SHD("StepHandlerDefinition"):::step
        SH("StepHandler"):::step
        ASH("AnyStepHandler"):::step
    end

    %% Flow Output Module
    subgraph "Flow Output Module"
        FO("FlowOutput (Typed)"):::output
    end

    %% Extensions / FlowKitAdditions
    subgraph "FlowKitAdditions – Provides additional conditions"
        DC("DisplayCondition"):::addon
        BDC("BoolDisplayCondition"):::addon
        DDC("DateDisplayCondition"):::addon
        NSC("NextStepsCondition"):::addon
        DNSC("DateNextStepsCondition"):::addon
        NNSC("NumericNextStepsCondition"):::addon
        TNSC("TextNextStepsCondition"):::addon
        UE("Utility Extensions"):::addon
    end

    %% Example Integration Layer
    subgraph "Example Implementations – Integration Layer"
        UIE("UIKit Example"):::example
        SWE("SwiftUI Example"):::example
    end

    %% Data Flow Relationships
    FDP -->|"provides flow definitions"| FD
    FDP -->|"provides flow definitions"| FDfn

    FD -->|"triggers"| FDfn
    FDfn -->|"initiates flow via"| FKit
    FDfn -->|"initiates flow via"| FKitSwiftUI

    FKit -->|"delegates step creation to"| SF
    FKitSwiftUI -->|"delegates step creation to"| SF

    SF -->|"creates steps using"| SP
    SP -->|"has concrete implementation"| AS
    SP -->|"has concrete implementation"| NS
    SF -->|"assembles"| ST
    SHD -->|"defines handling for"| SH
    SH -->|"implements logic in"| ASH

    SH -- "contributes output" --> FO
    FO -- "aggregates step outputs" --> SH

    %% Extension dotted relationships
    FKit -.->|"extended by"| DC
    FKit -.->|"extended by"| NSC
    SF -.->|"applies"| DC
    SF -.->|"applies"| NSC

    %% Integration Layer relationships
    FKit -->|"used in"| UIE
    FKitSwiftUI -->|"used in"| SWE

    %% Click Events for Core Engine
    click FD "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Flow/FlowData.swift"
    click FDfn "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Flow/FlowDefinition.swift"
    click FKit "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Flow/FlowKit.swift"
    click FKitSwiftUI "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Flow/FlowKitSwiftUI.swift"

    %% Click Event for Flow Output
    click FO "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/FlowOutput/FlowOutput.swift"

    %% Click Events for Step Handling Infrastructure
    click SP "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Step/StepProtocol.swift"
    click AS "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Step/AnyStep.swift"
    click NS "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Step/NextStep.swift"
    click SF "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Step/StepFactory.swift"
    click ST "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/Step/StepsTree.swift"
    click SHD "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/StepHandler/StepHandlerDefinition.swift"
    click SH "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/StepHandler/StepHandler.swift"
    click ASH "https://github.com/n26/flowkit-ios/blob/main/flowkit-ios/Classes/StepHandler/AnyStepHandler.swift"

    %% Click Events for FlowKitAdditions
    click DC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Display Conditions/DisplayCondition.swift"
    click BDC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Display Conditions/Conditions/BoolDisplayCondition.swift"
    click DDC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Display Conditions/Conditions/DateDispayCondition.swift"
    click NSC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Next Step Condition/NextStepsCondition.swift"
    click DNSC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Next Step Condition/DateNextStepsCondition.swift"
    click NNSC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Next Step Condition/NumericNextStepsCondition.swift"
    click TNSC "https://github.com/n26/flowkit-ios/blob/main/flowkitAdditions/Classes/Next Step Condition/TextNextStepsCondition.swift"
    click UE "https://github.com/n26/flowkit-ios/tree/main/flowkitAdditions/Classes/Utility"

    %% Click Events for Example Implementations
    click UIE "https://github.com/n26/flowkit-ios/tree/main/flowkitAdditions/Example/flowkitAdditionsUIKit"
    click SWE "https://github.com/n26/flowkit-ios/tree/main/flowkitAdditions/Example/flowkitAdditionsSwiftUI"

    %% Styles
    classDef core fill:#a6cee3,stroke:#1f78b4,stroke-width:2px;
    classDef step fill:#b2df8a,stroke:#33a02c,stroke-width:2px;
    classDef output fill:#fb9a99,stroke:#e31a1c,stroke-width:2px;
    classDef addon fill:#fdbf6f,stroke:#ff7f00,stroke-width:2px;
    classDef example fill:#cab2d6,stroke:#6a3d9a,stroke-width:2px;
```
