Building a multidevice workout app
At a glance
| Item | Summary |
|---|---|
| Purpose | Mirror a workout from a watchOS app to its companion iOS app, and perform bidirectional communication between them. |
| App architecture | A Swift sample bundle with entry-bearing project variants MirroringWorkoutsSample Watch App, MirroringWorkoutsSample, each leading to HealthKit / HealthKitUI APIs. |
| Main patterns | Delegate or data-source callbacks, SwiftUI environment injection, Binding-based state propagation, Publisher-backed observable state, Actor-isolated state |
| Project style | 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── MirroringWorkoutsSample Watch App/
│ ├── MirroringWorkoutsSampleApp.swift
│ ├── PagingView.swift
│ ├── SummaryView.swift
│ ├── ActivityRingsView.swift
│ ├── AppDelegate.swift
│ ├── ControlsView.swift
│ └── MetricsView.swift
├── MirroringWorkoutsSample/
│ ├── MirroringWorkoutsSampleApp.swift
│ ├── ChartView.swift
│ └── SummaryView.swift
└── Shared/
├── WorkoutManager.swift
└── ElapsedTimeView.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 6 project/configuration file(s) and 26 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["MirroringWorkoutsSample Watch App"]
V2["MirroringWorkoutsSample"]
Boundary["HealthKit / HealthKitUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift:10 — architecture anchor
@main
struct MirroringWorkoutsSampleWatchApp: App {
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
SessionSateChange o-- HKWorkoutSessionState : newState
SessionSateChange *-- Date : date
SessionSateChange o-- HKWorkoutSessionState : sessionState
SessionSateChange *-- Double : heartRate
Ownership evidence
Shared/WorkoutManager.swift:15 — stored dependency or nearest verified ownership anchor
struct SessionSateChange {
let newState: HKWorkoutSessionState
let date: Date
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
SessionSateChange |
HKWorkoutSessionState (newState) |
stores or receives | Initialized by the owner; the binding is immutable |
SessionSateChange |
Date (date) |
owns value state | Initialized by the owner; the binding is immutable |
SessionSateChange |
HKWorkoutSessionState (sessionState) |
stores or receives | App/module collaborators |
SessionSateChange |
Double (heartRate) |
owns value state | App/module collaborators |
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
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift:11 — representative type boundary
struct MirroringWorkoutsSampleWatchApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
MirroringWorkoutsSampleWatchApp |
Application entry and top-level composition | App |
MirroringWorkoutsSampleApp |
Application entry and top-level composition | App |
WorkoutManager |
Long-lived feature or framework coordination | NSObject, ObservableObject |
PagingView |
User-interface presentation and input forwarding | View |
SummaryView |
User-interface presentation and input forwarding | View |
SummaryMetricView |
User-interface presentation and input forwarding | View |
ChartView |
User-interface presentation and input forwarding | View |
SummaryView |
User-interface presentation and input forwarding | View |
GridItemView |
User-interface presentation and input forwarding | View |
ElapsedTimeView |
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 |
|---|---|---|---|
calendar (MirroringWorkoutsSample Watch App/ActivityRingsView.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. |
startWorkout (MirroringWorkoutsSample Watch App/ControlsView.swift:45) |
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. |
appDelegate (MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.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. |
workoutManager (MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.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. |
Reference code
MirroringWorkoutsSample Watch App/ActivityRingsView.swift:13 — representative boundary
@Environment(\.calendar) private var calendarSwift 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 | MirroringWorkoutsSampleApp, MirroringWorkoutsSampleWatchApp |
The source’s App suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate |
The source’s Delegate 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, ChartView, ControlsView, ElapsedTimeView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | MirroringWorkoutsSample Watch App/AppDelegate.swift:13 |
Callback protocols invert event delivery back into the sample’s owner. |
| SwiftUI environment injection | MirroringWorkoutsSample Watch App/ControlsView.swift:13 |
The environment supplies state or a capability without threading it through every initializer. |
| Binding-based state propagation | MirroringWorkoutsSample/ChartView.swift:22 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Publisher-backed observable state | Shared/WorkoutManager.swift:21 |
Published properties notify observers while mutation remains with the state object. |
| Actor-isolated state | Shared/WorkoutManager+iOS.swift:27 |
Actor annotations make the concurrency ownership boundary explicit. |
Naming conventions
- Types: App: MirroringWorkoutsSampleApp, MirroringWorkoutsSampleWatchApp; Delegate: AppDelegate; Manager: WorkoutManager; View: ActivityRingsView, ChartView, ControlsView, ElapsedTimeView, GridItemView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
consumeSessionStateChange,resetWorkout,sendData,updateForStatistics,workoutSession,displayMetricsView,summaryListView,updateLineEntries. - Files:
MirroringWorkoutsSample/MirroringWorkoutsSampleApp.swift,Shared/WorkoutManager.swift,MirroringWorkoutsSample Watch App/PagingView.swift,MirroringWorkoutsSample Watch App/SummaryView.swift,MirroringWorkoutsSample/ChartView.swift,MirroringWorkoutsSample/SummaryView.swift.
Architecture takeaways
MirroringWorkoutsSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, HealthKit, Charts, HealthKitUI 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 |
|---|---|
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift |
MirroringWorkoutsSampleWatchApp |
MirroringWorkoutsSample/MirroringWorkoutsSampleApp.swift |
MirroringWorkoutsSampleApp |
Shared/WorkoutManager.swift |
WorkoutManager, SessionSateChange, WorkoutElapsedTime |
MirroringWorkoutsSample Watch App/PagingView.swift |
PagingView, Tab |
MirroringWorkoutsSample Watch App/SummaryView.swift |
SummaryView, SummaryMetricView |
MirroringWorkoutsSample/ChartView.swift |
LineChartEntry, ChartView |
MirroringWorkoutsSample/SummaryView.swift |
SummaryView, GridItemView |
Shared/ElapsedTimeView.swift |
ElapsedTimeView, ElapsedTimeFormatter |
MirroringWorkoutsSample Watch App/ActivityRingsView.swift |
ActivityRingsView |
MirroringWorkoutsSample Watch App/AppDelegate.swift |
AppDelegate |