Customizing workouts with WorkoutKit
At a glance
| Item | Summary |
|---|---|
| Purpose | Create, preview, and sync workouts for use in the Workout app on Apple Watch. |
| App architecture | A Swift sample with the source-visible chain SamplePlannerApp → SamplePlannerView → WorkoutStore → WorkoutKit APIs. |
| Main patterns | Central store |
| Project style | 5 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── Sample Planner/
│ ├── SamplePlannerApp.swift
│ ├── SamplePlannerView.swift
│ ├── WorkoutStore.swift
│ ├── PresentPreviewDemo.swift
│ └── HKWorkoutConfiguration+Displaying.swift
├── Configuration/
│ └── SampleCode.xcconfig
└── Sample Planner.xcodeproj/
├── .xcodesamplecode.plist
└── project.pbxproj
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 3 project/configuration file(s) and 4 source declaration(s).
Overall architecture
flowchart LR
N1["SamplePlannerApp"]
N2["SamplePlannerView"]
N3["WorkoutStore"]
N4["WorkoutKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Sample Planner/SamplePlannerApp.swift:10 — architecture anchor
@main
struct SamplePlannerApp: App {
var body: some Scene {
WindowGroup {
SamplePlannerView()
}
}
}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 WorkoutKit.
Ownership and state
classDiagram
SamplePlannerView *-- AuthorizationState : authorizationState
SamplePlannerView *-- Array : scheduledWorkouts
SamplePlannerView o-- RelativeDateTimeFormatter : dateFormatter
PresentPreviewDemo o-- WorkoutPlan : cyclingWorkoutPlan
Ownership evidence
Sample Planner/SamplePlannerView.swift:12 — stored dependency or nearest verified ownership anchor
struct SamplePlannerView: View {
@State var authorizationState: WorkoutScheduler.AuthorizationState = .notDetermined
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
SamplePlannerView |
AuthorizationState (authorizationState) |
owns wrapper-managed state | App/module collaborators |
SamplePlannerView |
Array (scheduledWorkouts) |
owns wrapper-managed state | App/module collaborators |
SamplePlannerView |
RelativeDateTimeFormatter (dateFormatter) |
stores or receives | Initialized by the owner; the binding is immutable |
PresentPreviewDemo |
WorkoutPlan (cyclingWorkoutPlan) |
stores or receives | 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.
Class and protocol design
Sample Planner/SamplePlannerApp.swift:11 — representative type boundary
struct SamplePlannerApp: App {
var body: some Scene {
WindowGroup {
SamplePlannerView()
}
}
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SamplePlannerApp |
Application entry and top-level composition | App |
SamplePlannerView |
User-interface presentation and input forwarding | View |
WorkoutStore |
Centralized state or persistence access | Concrete collaborators/imported frameworks |
PresentPreviewDemo |
Represents a feature value or composable behavior | 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 |
|---|---|---|---|
cyclingWorkoutPlan (Sample Planner/PresentPreviewDemo.swift:12) |
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. |
schedule (Sample Planner/SamplePlannerView.swift:116) |
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. |
update (Sample Planner/SamplePlannerView.swift:134) |
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. |
scheduleMenuOptions (Sample Planner/SamplePlannerView.swift:142) |
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
Sample Planner/PresentPreviewDemo.swift:12 — representative boundary
private let cyclingWorkoutPlan: WorkoutPlan
@State var showPreview: Bool = false
init() {
cyclingWorkoutPlan = WorkoutPlan(.custom(WorkoutStore.createCyclingCustomWorkout()))
}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 | SamplePlannerApp |
The source’s App suffix makes this role explicit. |
| Centralized state or persistence access | WorkoutStore |
The source’s Store suffix makes this role explicit. |
| User-interface presentation and input forwarding | SamplePlannerView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Central store | Sample Planner/WorkoutStore.swift:11 |
A store-named type centralizes feature state or persistence. |
Naming conventions
- Types: App: SamplePlannerApp; Store: WorkoutStore; View: SamplePlannerView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
schedule,update,scheduleMenuOptions,createCyclingCustomWorkout,cyclingBlockOne,cyclingBlockTwo,createGolfWorkout,createRunningCustomWorkout. - Files:
Sample Planner/SamplePlannerApp.swift,Sample Planner/SamplePlannerView.swift,Sample Planner/WorkoutStore.swift,Sample Planner/PresentPreviewDemo.swift.
Architecture takeaways
SamplePlannerAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, WorkoutKit, HealthKit 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 |
|---|---|
Sample Planner/SamplePlannerApp.swift |
SamplePlannerApp |
Sample Planner/SamplePlannerView.swift |
SamplePlannerView, SamplePlannerView_Previews |
Sample Planner/WorkoutStore.swift |
WorkoutStore |
Sample Planner/PresentPreviewDemo.swift |
PresentPreviewDemo, PresentPreviewDemo_Previews |
Sample Planner/HKWorkoutConfiguration+Displaying.swift |
Feature implementation |