Build a workout app for Apple Watch
At a glance
| Item | Summary |
|---|---|
| Purpose | Create your own workout app, quickly and easily, with HealthKit and SwiftUI. |
| App architecture | A Swift sample with the source-visible chain MyWorkoutsApp → StartView → WorkoutManager → HealthKit APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks, SwiftUI environment injection, Publisher-backed observable state |
| Project style | 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── MyWorkouts WatchKit Extension/
│ ├── MyWorkoutsApp.swift
│ ├── Views/
│ │ ├── ElapsedTimeView.swift
│ │ ├── MetricsView.swift
│ │ ├── SessionPagingView.swift
│ │ ├── SummaryView.swift
│ │ ├── ControlsView.swift
│ │ ├── StartView.swift
│ │ └── ActivityRingsView.swift
│ ├── ComplicationController.swift
│ └── Controller/
│ └── WorkoutManager.swift
├── Configuration/
│ └── SampleCode.xcconfig
└── MyWorkouts WatchKit App/
└── Info.plist
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 6 project/configuration file(s) and 14 source declaration(s).
Overall architecture
flowchart LR
N1["MyWorkoutsApp"]
N2["StartView"]
N3["WorkoutManager"]
N4["HealthKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:10 — architecture anchor
@main
struct MyWorkoutsApp: App {
// ...
}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
classDiagram
MyWorkoutsApp *-- WorkoutManager : workoutManager
ElapsedTimeView o-- TimeInterval : elapsedTime
ElapsedTimeView *-- Bool : showSubseconds
ElapsedTimeView *-- ElapsedTimeFormatter : timeFormatter
Ownership evidence
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:12 — stored dependency or nearest verified ownership anchor
@StateObject private var workoutManager = WorkoutManager()| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
MyWorkoutsApp |
WorkoutManager (workoutManager) |
owns wrapper-managed state | Owning lexical scope |
ElapsedTimeView |
TimeInterval (elapsedTime) |
stores or receives | App/module collaborators |
ElapsedTimeView |
Bool (showSubseconds) |
owns value state | App/module collaborators |
ElapsedTimeView |
ElapsedTimeFormatter (timeFormatter) |
owns wrapper-managed state | Owning lexical scope |
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
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:11 — representative type boundary
struct MyWorkoutsApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
MyWorkoutsApp |
Application entry and top-level composition | App |
ElapsedTimeView |
User-interface presentation and input forwarding | View |
MetricsView |
User-interface presentation and input forwarding | View |
SessionPagingView |
User-interface presentation and input forwarding | View |
SummaryView |
User-interface presentation and input forwarding | View |
SummaryMetricView |
User-interface presentation and input forwarding | View |
ControlsView |
User-interface presentation and input forwarding | View |
StartView |
User-interface presentation and input forwarding | View |
ComplicationController |
View lifecycle, callbacks, and feature coordination | NSObject, CLKComplicationDataSource |
WorkoutManager |
Long-lived feature or framework coordination | NSObject, ObservableObject |
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 |
|---|---|---|---|
workoutManager (MyWorkouts WatchKit Extension/MyWorkoutsApp.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. |
timeFormatter (MyWorkouts WatchKit Extension/Views/ElapsedTimeView.swift:13) |
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. |
selection (MyWorkouts WatchKit Extension/Views/SessionPagingView.swift:14) |
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. |
displayMetricsView (MyWorkouts WatchKit Extension/Views/SessionPagingView.swift:38) |
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
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:12 — representative boundary
@StateObject private var workoutManager = WorkoutManager()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 | MyWorkoutsApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | ComplicationController |
The source’s Controller suffix makes this role explicit. |
| Long-lived feature or framework coordination | WorkoutManager |
The source’s Manager suffix makes this role explicit. |
| User-interface presentation and input forwarding | ActivityRingsView, ControlsView, ElapsedTimeView, MetricsView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | MyWorkouts WatchKit Extension/ComplicationController.swift:10 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | MyWorkouts WatchKit Extension/ComplicationController.swift:10 |
Callback protocols invert event delivery back into the sample’s owner. |
| SwiftUI environment injection | MyWorkouts WatchKit Extension/Views/ControlsView.swift:11 |
The environment supplies state or a capability without threading it through every initializer. |
| Publisher-backed observable state | MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:19 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: MyWorkoutsApp; Controller: ComplicationController; Manager: WorkoutManager; View: ActivityRingsView, ControlsView, ElapsedTimeView, MetricsView, SessionPagingView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
string,entries,displayMetricsView,getComplicationDescriptors,handleSharedComplicationDescriptors,getTimelineEndDate,getPrivacyBehavior,getCurrentTimelineEntry. - Files:
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift,MyWorkouts WatchKit Extension/Views/ElapsedTimeView.swift,MyWorkouts WatchKit Extension/Views/MetricsView.swift,MyWorkouts WatchKit Extension/Views/SessionPagingView.swift,MyWorkouts WatchKit Extension/Views/SummaryView.swift,MyWorkouts WatchKit Extension/Views/ControlsView.swift.
Architecture takeaways
MyWorkoutsAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, HealthKit, WatchKit, ClockKit 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 |
|---|---|
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift |
MyWorkoutsApp |
MyWorkouts WatchKit Extension/Views/ElapsedTimeView.swift |
ElapsedTimeView, ElapsedTimeFormatter, ElapsedTime_Previews |
MyWorkouts WatchKit Extension/Views/MetricsView.swift |
MetricsView, MetricsView_Previews, MetricsTimelineSchedule |
MyWorkouts WatchKit Extension/Views/SessionPagingView.swift |
SessionPagingView, Tab, PagingView_Previews |
MyWorkouts WatchKit Extension/Views/SummaryView.swift |
SummaryView, SummaryView_Previews, SummaryMetricView |
MyWorkouts WatchKit Extension/Views/ControlsView.swift |
ControlsView, ControlsView_Previews |
MyWorkouts WatchKit Extension/Views/StartView.swift |
StartView, StartView_Previews |
MyWorkouts WatchKit Extension/ComplicationController.swift |
ComplicationController |
MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift |
WorkoutManager |
MyWorkouts WatchKit Extension/Views/ActivityRingsView.swift |
ActivityRingsView |