Fruta: Building a feature-rich app with SwiftUI
At a glance
| Item | Summary |
|---|---|
| Purpose | Create a shared codebase to build a multiplatform app that offers widgets and an App Clip. |
| App architecture | A Swift sample bundle with entry-bearing project variants Shared, Widgets, iOS, each leading to SwiftUI / StoreKit APIs. |
| Main patterns | Protocol-oriented abstraction, Coordinator, Builder, Publisher-backed observable state |
| Project style | 56 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Task, await suspension point, DispatchQueue.main.async, @MainActor, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | SwiftUI, Foundation, StoreKit, AuthenticationServices, WidgetKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Shared/
│ ├── FrutaApp.swift
│ ├── Orders/
│ │ ├── OrderPlacedView.swift
│ │ └── RewardsView.swift
│ ├── Components/
│ │ ├── FlipView.swift
│ │ ├── StepperView.swift
│ │ └── BubbleView.swift
│ ├── Recipe/
│ │ └── RecipeView.swift
│ ├── Navigation/
│ │ ├── AppSidebarNavigation.swift
│ │ └── ContentView.swift
│ └── NutritionFacts/
│ └── NutritionFactView.swift
├── iOS/
│ └── FrutaAppClip.swift
└── Widgets/
└── FrutaWidgets.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 15 project/configuration file(s) and 78 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Shared"]
V2["Widgets"]
V3["iOS"]
Boundary["SwiftUI / StoreKit APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Bundle --> V3
V3 --> Boundary
Reference code
Shared/FrutaApp.swift:10 — architecture anchor
@main
struct FrutaApp: App {
@StateObject private var model = Model()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
}
.commands {
SidebarCommands()
SmoothieCommands(model: model)
}
}
}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
FrutaApp *-- Model : model
FlipView o-- FlipViewSide : visibleSide
FlipView o-- Front : front
FlipView o-- Back : back
Ownership evidence
Shared/FrutaApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
struct FrutaApp: App {
@StateObject private var model = Model()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
FrutaApp |
Model (model) |
owns wrapper-managed state | Owning lexical scope |
FlipView |
FlipViewSide (visibleSide) |
stores or receives | App/module collaborators |
FlipView |
Front (front) |
stores or receives | App/module collaborators |
FlipView |
Back (back) |
stores or receives | 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.
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. | Shared/Model/Model.swift:48 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Shared/Model/Model.swift:49 |
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Shared/Model/Model.swift:57 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | Shared/Model/Model.swift:146 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | Shared/Model/Model.swift:146 |
@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
Shared/Model/Model.swift:48 — representative execution boundary
updatesHandler = Task {
await listenForStoreUpdates()
}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 | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | Shared/Components/BubbleView.swift:16 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | Shared/Model/Model.swift:12 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | Shared/Model/Model.swift:13 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Components/AnimatableFontModifier.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Model/Model.swift:8 |
| Source import | StoreKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Components/FlipView.swift:9 |
| Source import | AuthenticationServices |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Model/Model.swift:9 |
| Source import | WidgetKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Widgets/FeaturedSmoothieWidget.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
Shared/NutritionFacts/UnitIcons.swift:22 — representative type boundary
private protocol UnitIconProvider {
var customUnitIcon: Image { get }
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
FrutaApp |
Application entry and top-level composition | App |
UnitIconProvider |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
OrderPlacedView |
User-interface presentation and input forwarding | View |
FlipView |
User-interface presentation and input forwarding | Concrete collaborators/imported frameworks |
StepperView |
User-interface presentation and input forwarding | View |
RecipeView |
User-interface presentation and input forwarding | View |
BubbleView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
NutritionFactView |
User-interface presentation and input forwarding | View |
RewardsView |
User-interface presentation and input forwarding | View |
The source explicitly defines local protocol relationships: Measurement → DisplayableMeasurement, UnitMass → UnitIconProvider, UnitVolume → UnitIconProvider.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
shimmer (Shared/Components/BubbleView.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. |
shimmerDelay (Shared/Components/BubbleView.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. |
float (Shared/Components/BubbleView.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. |
floatDelay (Shared/Components/BubbleView.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
Shared/Components/BubbleView.swift:16 — representative boundary
struct BubbleView: View {
// ...
@State private var shimmer: Bool = .random()
// ...
}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 | FrutaApp |
The source’s App suffix makes this role explicit. |
| Incrementally constructs a framework value or graph | SmoothieArrayBuilder, SmoothieBuilder |
The source’s Builder suffix makes this role explicit. |
| Cross-object flow or session coordination | Coordinator |
The source’s Coordinator suffix makes this role explicit. |
| Feature data or observable state | Model |
The source’s Model suffix makes this role explicit. |
| Supplies a capability or framework resource | Provider, UnitIconProvider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | BubbleView, ContentView, FeaturedSmoothieEntryView, FlipView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Protocol-oriented abstraction | Shared/NutritionFacts/DisplayableMeasurement.swift:22 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Coordinator | Shared/Orders/PaymentButton.swift:64 |
A role-named coordinator centralizes cross-object flow. |
| Builder | Shared/Smoothie/Smoothie.swift:237 |
A builder-named type owns incremental construction. |
| Publisher-backed observable state | Shared/Model/Model.swift:13 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: FrutaApp; Builder: SmoothieArrayBuilder, SmoothieBuilder; Coordinator: Coordinator; Model: Model; Provider: Provider, UnitIconProvider; View: BubbleView, ContentView, FeaturedSmoothieEntryView, FlipView, MeasurementView.
- Protocols:
UnitIconProvider,DisplayableMeasurement. - Methods:
handleUserActivity,toggle,body,increment,decrement. - Files:
Shared/FrutaApp.swift,iOS/FrutaAppClip.swift,Shared/Orders/OrderPlacedView.swift,Widgets/FrutaWidgets.swift,Shared/Components/FlipView.swift,Shared/Components/StepperView.swift.
Architecture takeaways
FrutaAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, StoreKit, AuthenticationServices, 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 |
|---|---|
Shared/FrutaApp.swift |
Cited implementation, FrutaApp |
Shared/NutritionFacts/UnitIcons.swift |
UnitIconProvider |
Shared/Components/BubbleView.swift |
Cited implementation, SwiftUI state property wrapper, BubbleView, BubbleView_Previews |
Shared/NutritionFacts/DisplayableMeasurement.swift |
Cited implementation, DisplayableMeasurement |
Shared/Orders/PaymentButton.swift |
Coordinator, PaymentButton, Representable, PaymentButton_Previews |
Shared/Smoothie/Smoothie.swift |
SmoothieArrayBuilder, Smoothie, SmoothieBuilder |
Shared/Model/Model.swift |
Cited implementation, Task, await suspension point, DispatchQueue.main.async, @MainActor, Task closure isolated to MainActor, ObservableObject, @Published, Foundation, AuthenticationServices, Model |
Shared/Components/AnimatableFontModifier.swift |
SwiftUI, AnimatableFontModifier |
Shared/Components/FlipView.swift |
StoreKit, FlipView, FlipViewSide, FlipModifier, FlipView_Previews |
Widgets/FeaturedSmoothieWidget.swift |
WidgetKit, FeaturedSmoothieWidget, Provider, Entry, FeaturedSmoothieEntryView, FeaturedSmoothieWidget_Previews |
iOS/FrutaAppClip.swift |
FrutaAppClip |
Shared/Orders/OrderPlacedView.swift |
OrderPlacedView, Card, OrderPlacedView_Previews |
Widgets/FrutaWidgets.swift |
FrutaWidgets |
Shared/Components/StepperView.swift |
StepperView, Configuration, StepperView_Previews |
Shared/Recipe/RecipeView.swift |
RecipeView, RecipeIngredientRow, RecipeView_Previews |
Shared/Navigation/AppSidebarNavigation.swift |
AppSidebarNavigation, NavigationItem, Pocket, AppSidebarNavigation_Previews, AppSidebarNavigationPocket_Previews |