Building Widgets Using WidgetKit and SwiftUI
At a glance
| Item | Summary |
|---|---|
| Purpose | Create widgets to show your app’s content on the Home screen, with custom intents for user-customizable settings. |
| App architecture | A Swift sample bundle with entry-bearing project variants Game Status Final, Game Status Part 2, Game Status Part 3, Shared Sources, each leading to WidgetKit APIs. |
| Main patterns | No named application pattern supported by the extracted structure |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, WidgetKit, Foundation, Intents; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Game Status Final/
│ ├── EmojiRangerWidget/
│ │ ├── LeaderboardWidget.swift
│ │ └── EmojiRangerWidget.swift
│ └── EmojiRangerIntentHandler/
│ └── IntentHandler.swift
├── Game Status Part 2/
│ └── EmojiRangerWidget/
│ └── EmojiRangerWidget.swift
├── Game Status Part 3/
│ └── EmojiRangerWidget/
│ ├── LeaderboardWidget.swift
│ └── EmojiRangerWidget.swift
└── Shared Sources/
├── App/
│ ├── EmojiRangers.swift
│ ├── ContentView.swift
│ └── DetailView.swift
└── Views/
├── AvatarView.swift
├── AllCharactersView.swift
└── CharacterNameView.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 31 project/configuration file(s) and 40 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Game Status Final"]
V2["Game Status Part 2"]
V3["Game Status Part 3"]
V4["Shared Sources"]
Boundary["WidgetKit APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Bundle --> V3
V3 --> Boundary
Bundle --> V4
V4 --> Boundary
Reference code
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:92 — architecture anchor
@main
struct EmojiRangerBundle: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
EmojiRangerWidget()
LeaderboardWidget()
}
}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
LeaderboardEntry *-- Date : date
LeaderboardEntry *-- Array : characters
LeaderboardWidgetEntryView o-- Entry : entry
LeaderboardWidget *-- String : kind
Ownership evidence
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:40 — stored dependency or nearest verified ownership anchor
struct LeaderboardEntry: TimelineEntry {
public let date: Date
var characters: [CharacterDetail]?
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
LeaderboardEntry |
Date (date) |
owns value state | Initialized by the owner; the binding is immutable |
LeaderboardEntry |
Array (characters) |
owns value state | App/module collaborators |
LeaderboardWidgetEntryView |
Entry (entry) |
stores or receives | App/module collaborators |
LeaderboardWidget |
String (kind) |
owns value state | 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.
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.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@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.
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. | Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:71 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:9 |
| Source import | WidgetKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared Sources/Data/CharacterDetail.swift:7 |
| Source import | Intents |
The cited file imports this module; runtime use and architectural role are not inferred. | Game Status Final/EmojiRangerIntentHandler/IntentHandler.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
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:11 — representative type boundary
struct LeaderboardProvider: TimelineProvider {
// ...
public typealias Entry = LeaderboardEntry
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
LeaderboardProvider |
Supplies a capability or framework resource | TimelineProvider |
LeaderboardPlaceholderView |
User-interface presentation and input forwarding | View |
LeaderboardWidgetEntryView |
User-interface presentation and input forwarding | View |
Provider |
Supplies a capability or framework resource | TimelineProvider |
PlaceholderView |
User-interface presentation and input forwarding | View |
EmojiRangerWidgetEntryView |
User-interface presentation and input forwarding | View |
LeaderboardProvider |
Supplies a capability or framework resource | TimelineProvider |
LeaderboardPlaceholderView |
User-interface presentation and input forwarding | View |
LeaderboardWidgetEntryView |
User-interface presentation and input forwarding | View |
Provider |
Supplies a capability or framework resource | IntentTimelineProvider |
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 |
|---|---|---|---|
date (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:57) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
kind (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:101) |
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. |
body (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:103) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
date (Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:40) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:57 — representative boundary
struct SimpleEntry: TimelineEntry {
public let date: Date
let relevance: TimelineEntryRelevance?
let character: CharacterDetail
}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 |
|---|---|---|
| Handles callbacks or feature events | IntentHandler |
The source’s Handler suffix makes this role explicit. |
| Supplies a capability or framework resource | LeaderboardProvider, Provider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | AllCharactersView, AvatarView, CharacterNameView, ContentView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| No named application pattern | Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:92 |
The verified source directly composes concrete framework types; this document avoids forcing a pattern name. |
Naming conventions
- Types: Handler: IntentHandler; Provider: LeaderboardProvider, Provider; View: AllCharactersView, AvatarView, CharacterNameView, ContentView, DetailView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
placeholder,getSnapshot,getTimeline,character. - Files:
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift,Game Status Part 2/EmojiRangerWidget/EmojiRangerWidget.swift,Game Status Part 3/EmojiRangerWidget/LeaderboardWidget.swift,Shared Sources/App/EmojiRangers.swift,Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift,Game Status Part 3/EmojiRangerWidget/EmojiRangerWidget.swift.
Architecture takeaways
LeaderboardWidgetis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, WidgetKit, Intents 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 |
|---|---|
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift |
Cited implementation, LeaderboardProvider, LeaderboardEntry, LeaderboardPlaceholderView, LeaderboardWidgetEntryView, LeaderboardWidget, LeaderboardWidget_Previews, EmojiRangerBundle |
Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift |
Cited implementation, SwiftUI state property wrapper, SwiftUI, WidgetKit, Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget, Widget_Previews |
Shared Sources/Data/CharacterDetail.swift |
Foundation, CharacterDetail |
Game Status Final/EmojiRangerIntentHandler/IntentHandler.swift |
Intents, IntentHandler |
Game Status Part 2/EmojiRangerWidget/EmojiRangerWidget.swift |
Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget, Widget_Previews |
Game Status Part 3/EmojiRangerWidget/LeaderboardWidget.swift |
LeaderboardProvider, LeaderboardEntry, LeaderboardPlaceholderView, LeaderboardWidgetEntryView, LeaderboardWidget, LeaderboardWidget_Previews |
Shared Sources/App/EmojiRangers.swift |
EmojiRangers |
Game Status Part 3/EmojiRangerWidget/EmojiRangerWidget.swift |
Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget, Widget_Previews |
Shared Sources/App/ContentView.swift |
ContentView, ContentView_Previews, TableRow |
Shared Sources/Views/AvatarView.swift |
Avatar, AvatarView, AvatarView_Previews |
Shared Sources/App/DetailView.swift |
DetailView, DetailView_Previews |
Shared Sources/Views/AllCharactersView.swift |
AllCharactersView, AllCharactersView_Previews |
Shared Sources/Views/CharacterNameView.swift |
CharacterNameView, CharacterNameView_Previews |
Game Status Part 3/EmojiRangerIntentHandler/IntentHandler.swift |
IntentHandler |
Shared Sources/Mock API/ImageURLProtocol.swift |
ImageURLProtocol, override |
Shared Sources/Views/HealthLevelShape.swift |
HealthLevelShape, HealthLevelShape_Previews |