Skip to content

SwiftUI Usage

ChiefVenzox edited this page Jun 5, 2026 · 1 revision

SwiftUI Usage

Import NotebookFlowKit and render a flow from a JSON file in your app bundle.

import SwiftUI
import NotebookFlowKit

struct OnboardingView: View {
    var body: some View {
        NotebookFlowView(fileName: "starter_flow") { result in
            print(result.answers)
        }
    }
}

Loading From A Bundle

By default, NotebookFlowView loads from .main.

NotebookFlowView(fileName: "starter_flow") { result in
    print(result.answers)
}

You can also pass a bundle explicitly:

NotebookFlowView(
    fileName: "starter_flow",
    bundle: .main,
    onComplete: { result in
        print(result.answers)
    }
)

Rendering An In-Memory Flow

You can construct a flow directly in Swift:

let flow = NotebookFlow(
    name: "StarterFlow",
    screens: [
        FlowScreen(
            id: "welcome",
            type: .text,
            title: "Welcome",
            next: "goal"
        ),
        FlowScreen(
            id: "goal",
            type: .choice,
            title: "What is your goal?",
            options: ["Focus", "Learning"],
            next: "done"
        ),
        FlowScreen(
            id: "done",
            type: .completion,
            title: "You're ready"
        )
    ]
)

NotebookFlowView(flow: flow) { result in
    print(result.answers)
}

Completion Result

The completion callback receives a FlowResult.

NotebookFlowView(fileName: "starter_flow") { result in
    print(result.flowName)
    print(result.answers)
}

Typed Answer Helpers

Use typed helpers when you know the expected answer type.

let name = result.text("name")
let goal = result.choice("goal")
let topics = result.choices("topics")

Raw answers are also available:

switch result.answers["goal"] {
case .choice(let value):
    print(value)
case .choices(let values):
    print(values)
case .text(let value):
    print(value)
case nil:
    break
}

Progress Indicator

NotebookFlowKit shows progress by default.

Disable it when your app already has its own progress UI:

NotebookFlowView(
    fileName: "starter_flow",
    showsProgress: false,
    onComplete: { result in
        print(result.answers)
    }
)

FlowStateManager

FlowStateManager owns navigation state and collected answers.

It exposes:

  • currentScreen
  • answers
  • isCompleted
  • canGoBack
  • canAdvance
  • currentStepIndex
  • totalScreens
  • progressFraction

This keeps navigation logic testable outside SwiftUI.

Clone this wiki locally