Optimizing home electricity usage
At a glance
| Item | Summary |
|---|---|
| Purpose | Shift electric vehicle charging schedules to times when the grid is cleaner and potentially less expensive. |
| App architecture | A Swift sample with the source-visible chain EnergyKitSampleApp → ContentView → EnergyVenueManager → EnergyKit APIs. |
| Main patterns | View-controller organization |
| Project style | 25 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, Task.detached, Task; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, EnergyKit, Foundation, Charts, OSLog; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── EnergyKitSampleApp/
└── EnergyKitSampleApp/
├── EnergyKitSampleApp.swift
├── Models/
│ ├── ElectricVehicle/
│ │ ├── ElectricVehicleController.swift
│ │ └── ElectricVehicle.swift
│ └── EnergyVenueManager.swift
├── Views/
│ ├── EV/
│ │ ├── EVDetailsView.swift
│ │ ├── EVView.swift
│ │ └── Load Event/
│ │ ├── LoadEventDetailView.swift
│ │ └── LoadEventsView.swift
│ ├── ChargingLocation/
│ │ └── ChargingLocationView.swift
│ └── Common/
│ ├── AttributeValueTextView.swift
│ └── UUIDTextView.swift
└── ContentView.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 4 project/configuration file(s) and 28 source declaration(s).
Overall architecture
flowchart LR
N1["EnergyKitSampleApp"]
N2["ContentView"]
N3["EnergyVenueManager"]
N4["EnergyKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
EnergyKitSampleApp/EnergyKitSampleApp/EnergyKitSampleApp.swift:11 — architecture anchor
@main
struct EnergyKitSampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: ChargingLocation.self)
}
}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 EnergyKit.
Ownership and state
classDiagram
ElectricVehicleController *-- DateInterval : chargingWindow
ElectricVehicleController o-- TimeInterval : timestep
ElectricVehicleController o-- ElectricVehicle : configuration
ElectricVehicleController *-- Array : snapshots
Ownership evidence
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:15 — stored dependency or nearest verified ownership anchor
var chargingWindow: DateInterval = .init(start: Date(), end: Date().addingTimeInterval(60 * 60 * 10))
/// An incremental portion of the simulation, in seconds.
var timestep: TimeInterval = 60
// MARK: Electric Vehicle (EV)
/// The configuration of the EV.
var configuration: ElectricVehicle
/// The EV snapshots for UI purposes.
var snapshots: [ElectricVehicle] = []
// MARK: EnergyKit
/// The EV load event session.
var session: ElectricVehicleLoadEvent.Session?
// Electricity Guidance
/// The guidance stored at the EV.
var currentGuidance: ElectricityGuidance {
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
ElectricVehicleController |
DateInterval (chargingWindow) |
owns value state | App/module collaborators |
ElectricVehicleController |
TimeInterval (timestep) |
stores or receives | App/module collaborators |
ElectricVehicleController |
ElectricVehicle (configuration) |
stores or receives | App/module collaborators |
ElectricVehicleController |
Array (snapshots) |
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.
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 |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:318 |
| Detached task | Task.detached |
The source creates a detached task; no specific operating-system thread is established. | EnergyKitSampleApp/EnergyKitSampleApp/Models/EnergyVenueManager.swift:49 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVDetailsView.swift:40 |
@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
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:318 — representative execution boundary
func submitEvents() async throws {
try await currentVenue.submitEvents(events)
}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 | @Observable |
Observation macro publishes source-visible changes. | EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:12 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | EnergyKitSampleApp/EnergyKitSampleApp/Views/ChargingLocation/ChargingLocationListItem.swift:14 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/ContentView.swift:8 |
| Source import | EnergyKit |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/ContentView.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/Models/ChargingLocation.swift:9 |
| Source import | Charts |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVDetailsView.swift:8 |
| Source import | OSLog |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:9 |
| Source import | SwiftData |
The cited file imports this module; runtime use and architectural role are not inferred. | EnergyKitSampleApp/EnergyKitSampleApp/Models/ChargingLocation.swift:10 |
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
EnergyKitSampleApp/EnergyKitSampleApp/EnergyKitSampleApp.swift:12 — representative type boundary
@main
struct EnergyKitSampleApp: App {
// ...
ContentView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
EnergyKitSampleApp |
Application entry and top-level composition | App |
ElectricVehicleController |
View lifecycle, callbacks, and feature coordination | Concrete collaborators/imported frameworks |
EnergyVenueManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
EVDetailsView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
ChargingLocationView |
User-interface presentation and input forwarding | View |
AttributeValueTextView |
User-interface presentation and input forwarding | View |
UUIDTextView |
User-interface presentation and input forwarding | View |
EVView |
User-interface presentation and input forwarding | View |
LoadEventDetailView |
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 |
|---|---|---|---|
energyRequiredToFullCharge (EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:91) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
calculateEVChargingSchedule (EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:98) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
selectingChargingIntervals (EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:102) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
selectCleanestChargingIntervals (EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:122) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
Reference code
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:91 — representative boundary
fileprivate func energyRequiredToFullCharge() -> Double {
if configuration.properties.desiredStateOfCharge <= configuration.state.stateOfCharge {
return 0
}
return configuration.properties.batteryCapacity * (configuration.properties.desiredStateOfCharge - configuration.state.stateOfCharge) / 100
}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 | EnergyKitSampleApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | ElectricVehicleController |
The source’s Controller suffix makes this role explicit. |
| Long-lived feature or framework coordination | EnergyVenueManager |
The source’s Manager suffix makes this role explicit. |
| User-interface presentation and input forwarding | AttributeValueTextView, ChargingLocationView, ContentView, EVDetailsView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift:12 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
Naming conventions
- Types: App: EnergyKitSampleApp; Controller: ElectricVehicleController; Manager: EnergyVenueManager; View: AttributeValueTextView, ChargingLocationView, ContentView, EVDetailsView, EVView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
setElectricityGuidance,setEnergyVenue,energyRequiredToFullCharge,calculateEVChargingSchedule,selectingChargingIntervals,selectCleanestChargingIntervals,guidanceIntervalIsInChargingWindow,simulateCharging. - Files:
EnergyKitSampleApp/EnergyKitSampleApp/EnergyKitSampleApp.swift,EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift,EnergyKitSampleApp/EnergyKitSampleApp/Models/EnergyVenueManager.swift,EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVDetailsView.swift,EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicle.swift,EnergyKitSampleApp/EnergyKitSampleApp/ContentView.swift.
Architecture takeaways
EnergyKitSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, EnergyKit, Charts, SwiftData 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 |
|---|---|
EnergyKitSampleApp/EnergyKitSampleApp/EnergyKitSampleApp.swift |
Cited implementation, EnergyKitSampleApp |
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift |
Cited implementation, ElectricVehicleController, async declaration or closure, @Observable, OSLog |
EnergyKitSampleApp/EnergyKitSampleApp/Models/EnergyVenueManager.swift |
Task.detached, EnergyVenueManager |
EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVDetailsView.swift |
Task, Charts, EVDetailsView, DataCategory |
EnergyKitSampleApp/EnergyKitSampleApp/Views/ChargingLocation/ChargingLocationListItem.swift |
SwiftUI state property wrapper, ChargingLocationListItem |
EnergyKitSampleApp/EnergyKitSampleApp/ContentView.swift |
SwiftUI, EnergyKit, ContentView |
EnergyKitSampleApp/EnergyKitSampleApp/Models/ChargingLocation.swift |
Foundation, SwiftData, ChargingLocation |
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicle.swift |
ElectricVehicle, Properties, State |
EnergyKitSampleApp/EnergyKitSampleApp/Views/ChargingLocation/ChargingLocationView.swift |
ChargingLocationView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/Common/AttributeValueTextView.swift |
AttributeValueTextView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/Common/UUIDTextView.swift |
UUIDTextView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVView.swift |
EVView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/Load Event/LoadEventDetailView.swift |
LoadEventDetailView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/Load Event/LoadEventsView.swift |
LoadEventsView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/Venue/EnergyKitErrorView.swift |
EnergyKitErrorView |
EnergyKitSampleApp/EnergyKitSampleApp/Views/Venue/VenueDetailView.swift |
VenueDetailView |