Sample CodeiOS, iPadOS, Mac Catalyst, visionOSReviewed 2026-07-21View on Apple Developer

Visualizing HealthKit State of Mind in visionOS

At a glance

Item Summary
Purpose Incorporate HealthKit State of Mind into your app and visualize the data in visionOS.
App architecture A Swift sample with the source-visible chain HKStateOfMindDataSampleAppTabsViewHealthStoreCalendarQualityScoreProviderHealthKit APIs.
Main patterns Central store, Binding-based state propagation, Actor isolation
Project style 33 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: actor, async declaration or closure, @MainActor, Task, Sendable or @Sendable; none alone proves a background thread.
State/event model Source-visible mechanisms: @Observable, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, HealthKit, EventKit, Foundation, Charts; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── HKStateOfMindDataSampleApp/
    └── HKStateOfMindDataSampleApp/
        ├── HKStateOfMindDataSampleApp.swift
        ├── Models/
        │   ├── Insights/
        │   │   └── InsightsModels.swift
        │   ├── Calendars/
        │   │   └── CalendarModel.swift
        │   ├── Stores/
        │   │   └── HealthStore.swift
        │   └── Today/
        │       └── EventModel.swift
        ├── Views/
        │   ├── Charts/
        │   │   └── CalendarChartsView.swift
        │   ├── Authorization/
        │   │   └── AuthorizationGatedView.swift
        │   ├── Reflection/
        │   │   └── ReflectionCurrentEmojiPickerView.swift
        │   ├── TabsView.swift
        │   └── Today/
        │       └── CalendarScoreView.swift
        └── Data Sources/
            ├── CalendarQualityScoreProvider.swift
            └── WorkLifeBalanceScoreProvider.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 5 project/configuration file(s) and 48 source declaration(s).

Overall architecture

Reference code

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift:12 — architecture anchor

@main
struct HKStateOfMindDataSampleApp: App {
    // ...
    let healthStore = HealthStore.shared.healthStore
    // ...
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into HealthKit.

Ownership and state

Ownership evidence

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift:17 — stored dependency or nearest verified ownership anchor

@main
struct HKStateOfMindDataSampleApp: App {
    // ...
    @State var calendars = Calendars(calendarModels: [])
    // ...
}
Owner Object or state Relationship Mutation authority
HKStateOfMindDataSampleApp Calendars (calendars) owns wrapper-managed state App/module collaborators
HKStateOfMindDataSampleApp Bool (eventsAuthorized) owns wrapper-managed state App/module collaborators
HKStateOfMindDataSampleApp Bool (healthDataAuthorized) owns wrapper-managed state App/module collaborators
InsightModel UUID (id) owns value state Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Actor isolation actor The cited type is actor-isolated; this does not select a background thread. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:13
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:30
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarStateOfMindData.swift:13
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarStateOfMindData.swift:95
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift:54

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:13 — representative execution boundary

actor CalendarFetcher {
    static let shared = CalendarFetcher(eventStore: EKEventStore())
    // ...
}

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation @Observable Observation macro publishes source-visible changes. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarStateOfMindData.swift:13
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift:17
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:10
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarQualityScoreProvider.swift:9
Source import EventKit The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:9
Source import Charts The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Today/EmojiType.swift:11
Source import HealthKitUI The cited file imports this module; runtime use and architectural role are not inferred. HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Authorization/AuthorizationGatedView.swift:9

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift:13 — representative type boundary

@main
struct HKStateOfMindDataSampleApp: App {
    // ...
    let healthStore = HealthStore.shared.healthStore
    // ...
}
Type Responsibility Depends on or conforms to
HKStateOfMindDataSampleApp Application entry and top-level composition App
InsightModel Feature data or observable state Identifiable
CalendarChartsView User-interface presentation and input forwarding View
CalendarModel Feature data or observable state Identifiable, Equatable, Hashable, Sendable
HealthStore Centralized state or persistence access Sendable
EventModel Feature data or observable state Sendable, Identifiable
HealthKitAuthorizationGatedView User-interface presentation and input forwarding Concrete collaborators/imported frameworks
EventKitAuthorizationGatedView User-interface presentation and input forwarding Concrete collaborators/imported frameworks
ReflectionCurrentEmojiPickerView User-interface presentation and input forwarding View
TabsView User-interface presentation and input forwarding View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
eventStore (HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:21) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
deviceCalendar (HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:22) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
CalendarFetcher (HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:24) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
sourceToUse (HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:152) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.

Reference code

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:21 — representative boundary

actor CalendarFetcher {
    // ...
    private let eventStore: EKEventStore
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition HKStateOfMindDataSampleApp The source’s App suffix makes this role explicit.
Feature data or observable state CalendarModel, EventModel, InsightModel The source’s Model suffix makes this role explicit.
Supplies a capability or framework resource CalendarQualityScoreProvider, CalendarStateOfMindDataProvider, WorkLifeBalanceScoreProvider The source’s Provider suffix makes this role explicit.
Scene lifecycle or scene-level composition ReflectionScene The source’s Scene suffix makes this role explicit.
Centralized state or persistence access HealthStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding CalendarChartsView, CalendarScoreView, CalendarSelectorView, ChartView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Stores/HealthStore.swift:10 A store-named type centralizes feature state or persistence.
Binding-based state propagation HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Authorization/AuthorizationGatedView.swift:17 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Actor isolation HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift:13 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Main application flow

Reference code

HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Insights/InsightsGridView.swift:136calculateMetrics()

    private func calculateMetrics() async throws {
        // Weekly scores
        weeklyWorkLifeBalanceScore = await WorkLifeBalanceScoreProvider.calculateWorkLifeBalanceScore(
            from: calendars,
            numberOfDays: 7
        )

        let healthStore = HealthStore.shared.healthStore
        weeklyCalendarQualityScore = try await CalendarQualityScoreProvider.calendarQualityScore(
            forNumberOfDays: 7,
            associations: [.work],
            healthStore: healthStore
        )

        // Event highlights
        mostMeaningfulEvent = try await dataFetcher.event(matching: .happy,
                                                          calendarModels: calendars.calendarModels,
                                                          dateInterval: .eventHighlightInterval)
        mostBoringEvent = try await dataFetcher.event(matching: .indifferent,
                                                      calendarModels: calendars.calendarModels,
                                                      dateInterval: .eventHighlightInterval)
        proudestEvent = try await dataFetcher.event(matching: .proud,
                                                    calendarModels: calendars.calendarModels,
                                                    dateInterval: .eventHighlightInterval)
    }

Naming conventions

  • Types: App: HKStateOfMindDataSampleApp; Model: CalendarModel, EventModel, InsightModel; Provider: CalendarQualityScoreProvider, CalendarStateOfMindDataProvider, WorkLifeBalanceScoreProvider; Scene: ReflectionScene; Store: HealthStore; View: CalendarChartsView, CalendarScoreView, CalendarSelectorView, ChartView, DateIntervalPaginationView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: decrement, increment.
  • Files: HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift, HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Charts/CalendarChartsView.swift, HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Calendars/CalendarModel.swift, HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Stores/HealthStore.swift, HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Today/EventModel.swift, HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Reflection/ReflectionCurrentEmojiPickerView.swift.

Architecture takeaways

  • HKStateOfMindDataSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, HealthKit, EventKit, Charts through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp.swift Cited implementation, HKStateOfMindDataSampleApp, Sendable or @Sendable, SwiftUI state property wrapper, WindowGroupID
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarFetcher.swift Cited implementation, CalendarFetcher, actor, async declaration or closure, SwiftUI, EventKit, Foundation, Failure
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Stores/HealthStore.swift HealthStore
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Authorization/AuthorizationGatedView.swift Cited implementation, HealthKitUI, HealthKitAuthorizationGatedView, EventKitAuthorizationGatedView
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarStateOfMindData.swift @MainActor, Task, @Observable, CalendarStateOfMindData, CalendarStateOfMindDataProvider
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/CalendarQualityScoreProvider.swift HealthKit, CalendarQualityScoreProvider
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Today/EmojiType.swift Charts, EmojiType, SaveDetails
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Insights/InsightsModels.swift InsightDateInterval, InsightType, InsightModel, InsightSectionType, InsightSection
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Charts/CalendarChartsView.swift CalendarChartsView, NewChartViewerButton, DateConfiguration
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Calendars/CalendarModel.swift CalendarModel
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Models/Today/EventModel.swift EventModel
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Reflection/ReflectionCurrentEmojiPickerView.swift ReflectionCurrentEmojiPickerView, OptionButton
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/TabsView.swift TabsView, TabKind
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Today/CalendarScoreView.swift CalendarScoreView, Defaults
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Data Sources/WorkLifeBalanceScoreProvider.swift WorkLifeBalanceScoreProvider
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Charts/CalendarChartsView+CalendarSelectorView.swift CalendarSelectorView
HKStateOfMindDataSampleApp/HKStateOfMindDataSampleApp/Views/Insights/InsightsGridView.swift calculateMetrics