Food Truck: Building a SwiftUI multiplatform app
At a glance
| Item | Summary |
|---|---|
| Purpose | Create a single codebase and app target for Mac, iPad, and iPhone. |
| App architecture | A Swift sample bundle with entry-bearing project variants App, Widgets, each leading to SwiftUI APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, Central store, SwiftUI environment injection, Binding-based state propagation |
| Project style | 82 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Task, await suspension point, @MainActor, Task closure isolated to MainActor, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | SwiftUI, FoodTruckKit, Foundation, StoreKit, WidgetKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── App/
│ ├── App.swift
│ ├── Store/
│ │ ├── SubscriptionStoreView.swift
│ │ └── UnlockFeatureView.swift
│ ├── Account/
│ │ └── SignUpView.swift
│ └── Truck/
│ └── TruckView.swift
├── Widgets/
│ └── Widgets.swift
└── FoodTruckKit/
└── Sources/
├── Donut/
│ ├── DonutBoxView.swift
│ └── DonutView.swift
├── Model/
│ └── FoodTruckModel.swift
├── Order/
│ └── OrderGenerator.swift
├── Store/
│ └── StoreMessagesManager.swift
└── Account/
└── AccountStore.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 11 project/configuration file(s) and 160 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["App"]
V2["Widgets"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
App/App.swift:15 — architecture anchor
@main
struct FoodTruckApp: App {
// ...
@StateObject private var model = FoodTruckModel()
// ...
}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
FoodTruckApp *-- FoodTruckModel : model
FoodTruckApp *-- AccountStore : accountStore
SignUpView o-- FoodTruckModel : model
SignUpView o-- AccountStore : accountStore
Ownership evidence
App/App.swift:18 — stored dependency or nearest verified ownership anchor
@main
struct FoodTruckApp: App {
// ...
@StateObject private var model = FoodTruckModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
FoodTruckApp |
FoodTruckModel (model) |
owns wrapper-managed state | Owning lexical scope |
FoodTruckApp |
AccountStore (accountStore) |
owns wrapper-managed state | Owning lexical scope |
SignUpView |
FoodTruckModel (model) |
observes externally owned state | The observed object is authoritative |
SignUpView |
AccountStore (accountStore) |
receives environment-provided state | The environment provider is authoritative |
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 |
|---|---|---|---|
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | App/Account/AccountView.swift:48 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | App/Account/AccountView.swift:49 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | App/Donut/ShowTopDonutsIntent.swift:19 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | App/Orders/OrderCompleteView.swift:37 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | FoodTruckKit/Sources/Model/FoodTruckModel.swift:201 |
@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
App/Account/AccountView.swift:48 — representative execution boundary
#if os(iOS)
// ...
#else
Task(priority: .userInitiated) {
try await AppStore.sync()
}
#endifState 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 | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | App/Account/AccountView.swift:14 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | FoodTruckKit/Sources/Account/AccountStore.swift:38 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | FoodTruckKit/Sources/Account/AccountStore.swift:39 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | App/Account/AccountView.swift:8 |
| Source import | FoodTruckKit |
The cited file imports this module; runtime use and architectural role are not inferred. | App/Account/AccountView.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | App/Store/RefundView.swift:11 |
| Source import | StoreKit |
The cited file imports this module; runtime use and architectural role are not inferred. | App/Account/AccountView.swift:10 |
| Source import | WidgetKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Widgets/DailyDonutWidget.swift:8 |
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
FoodTruckKit/Sources/Donut/Ingredients/Ingredient.swift:10 — representative type boundary
public protocol Ingredient: Identifiable, Hashable {
var id: String { get }
var name: String { get }
var flavors: FlavorProfile { get }
var imageAssetName: String { get }
static var imageAssetPrefix: String { get }
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
FoodTruckApp |
Application entry and top-level composition | App |
SubscriptionStoreView |
User-interface presentation and input forwarding | View |
SubscriptionStoreHeaderView |
User-interface presentation and input forwarding | View |
SubscriptionStoreOptionsView |
User-interface presentation and input forwarding | View |
SubscriptionOptionView |
User-interface presentation and input forwarding | View |
SubscriptionPurchaseView |
User-interface presentation and input forwarding | View |
SignUpView |
User-interface presentation and input forwarding | View |
DonutBoxView |
User-interface presentation and input forwarding | Concrete collaborators/imported frameworks |
DonutView |
User-interface presentation and input forwarding | View |
FoodTruckModel |
Feature data or observable state | ObservableObject |
The source explicitly defines local protocol relationships: Dough → Ingredient, Glaze → Ingredient, Topping → Ingredient.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
accountStore (App/Account/AccountView.swift:16) |
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. |
authorizationController (App/Account/AccountView.swift:17) |
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. |
isSignUpSheetPresented (App/Account/AccountView.swift:19) |
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. |
isSignOutAlertPresented (App/Account/AccountView.swift:20) |
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
App/Account/AccountView.swift:16 — representative boundary
struct AccountView: View {
// ...
@EnvironmentObject private var accountStore: AccountStore
// ...
}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 | FoodTruckApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | Controller, StoreProductController, StoreSubscriptionController |
The source’s Controller suffix makes this role explicit. |
| Generates feature data or resources | OrderGenerator, SeededRandomGenerator |
The source’s Generator suffix makes this role explicit. |
| Long-lived feature or framework coordination | StoreMessagesManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | FoodTruckModel |
The source’s Model suffix makes this role explicit. |
| Supplies a capability or framework resource | Provider, TruckActivityPreviewProvider |
The source’s Provider suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | DonutRenderer |
The source’s Renderer suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | App/City/DetailedMapView.swift:30 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | FoodTruckKit/Sources/Donut/Ingredients/Dough.swift:11 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | FoodTruckKit/Sources/Account/AccountStore.swift:38 |
Callback protocols invert event delivery back into the sample’s owner. |
| Central store | FoodTruckKit/Sources/Account/AccountStore.swift:38 |
A store-named type centralizes feature state or persistence. |
| SwiftUI environment injection | App/Account/AccountView.swift:16 |
The environment supplies state or a capability without threading it through every initializer. |
| Binding-based state propagation | App/Donut/DonutEditor.swift:12 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Main application flow
sequenceDiagram
participant StoreActor
participant Task
participant StoreKit
participant Product
participant subscriptionController
participant Storefront
StoreActor->>Task: Start asynchronous work
StoreKit-->>StoreActor: update() stream
StoreActor->>StoreActor: handle()
StoreActor->>Task: Start asynchronous work
Product-->>StoreActor: update() stream
StoreActor->>subscriptionController: handle()
StoreActor->>Task: Start asynchronous work
Storefront-->>StoreActor: update() stream
Reference code
FoodTruckKit/Sources/Store/StoreActor.swift:64 — setupListenerTasksIfNecessary()
private func setupListenerTasksIfNecessary() {
if transactionUpdatesTask == nil {
transactionUpdatesTask = Task(priority: .background) {
for await update in StoreKit.Transaction.updates {
await self.handle(transaction: update)
}
}
}
if statusUpdatesTask == nil {
statusUpdatesTask = Task(priority: .background) {
for await update in Product.SubscriptionInfo.Status.updates {
await subscriptionController.handle(update: update)
}
}
}
if storefrontUpdatesTask == nil {
storefrontUpdatesTask = Task(priority: .background) {
for await update in Storefront.updates {
self.handle(storefrontUpdate: update)
}
}
}
}Naming conventions
- Types: App: FoodTruckApp; Controller: Controller, StoreProductController, StoreSubscriptionController; Generator: OrderGenerator, SeededRandomGenerator; Manager: StoreMessagesManager; Model: FoodTruckModel; Provider: Provider, TruckActivityPreviewProvider; Renderer: DonutRenderer; Store: AccountStore.
- Protocols:
Ingredient. - Methods:
binding,subscriptionOptionCell,savings,applyKerning,signUp,dailyOrderSummaries,monthlyOrderSummaries,donuts. - Files:
App/Store/SubscriptionStoreView.swift,Widgets/Widgets.swift,App/Account/SignUpView.swift,FoodTruckKit/Sources/Donut/DonutBoxView.swift,FoodTruckKit/Sources/Donut/DonutView.swift,FoodTruckKit/Sources/Model/FoodTruckModel.swift.
Architecture takeaways
Appis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, FoodTruckKit, StoreKit, WidgetKit 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
App/App.swift |
Cited implementation, FoodTruckApp |
FoodTruckKit/Sources/Donut/Ingredients/Ingredient.swift |
Ingredient |
App/Account/AccountView.swift |
Cited implementation, Task, await suspension point, SwiftUI state property wrapper, SwiftUI, FoodTruckKit, StoreKit, AccountView, AccountView_Previews, Preview |
App/City/DetailedMapView.swift |
Controller, DetailedMapView, DetailedMapView_Previews |
FoodTruckKit/Sources/Donut/Ingredients/Dough.swift |
Cited implementation, Dough |
FoodTruckKit/Sources/Account/AccountStore.swift |
Cited implementation, AccountStore, ObservableObject, @Published, AuthorizationHandlingError |
App/Donut/DonutEditor.swift |
Cited implementation, DonutEditor, DonutEditor_Previews, Preview |
App/Donut/ShowTopDonutsIntent.swift |
@MainActor, ShowTopDonutsIntent, ShowTopDonutsIntentTimeframe, FoodTruckShortcuts |
App/Orders/OrderCompleteView.swift |
Task closure isolated to MainActor, OrderCompleteView, OrderCompleteView_Previews |
FoodTruckKit/Sources/Model/FoodTruckModel.swift |
Sendable or @Sendable, FoodTruckModel, DonutSortOrder, Timeframe |
App/Store/RefundView.swift |
Foundation, RefundView, TransactionRowView, RefundView_Previews |
Widgets/DailyDonutWidget.swift |
WidgetKit, DailyDonutWidget, Provider, Entry, DailyDonutWidgetView, DailyDonutWidget_Previews |
App/Store/SubscriptionStoreView.swift |
SubscriptionStoreView, SubscriptionStoreHeaderView, SubscriptionStoreOptionsView, SubscriptionOptionView, SubscriptionPurchaseView, SubscriptionStoreView_Previews |
Widgets/Widgets.swift |
Widgets |
App/Account/SignUpView.swift |
SignUpView, FocusElement, SignUpType, SignUpView_Previews, Preview |
FoodTruckKit/Sources/Donut/DonutBoxView.swift |
DonutBoxView, DonutBoxView_Previews, Preview |
FoodTruckKit/Sources/Store/StoreActor.swift |
setupListenerTasksIfNecessary |