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. |
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| 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.
Class and protocol design
EnergyKitSampleApp/EnergyKitSampleApp/EnergyKitSampleApp.swift:12 — representative type boundary
struct EnergyKitSampleApp: App {
// ...
}| 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 |
EnergyKitSampleApp |
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicleController.swift |
ElectricVehicleController |
EnergyKitSampleApp/EnergyKitSampleApp/Models/EnergyVenueManager.swift |
EnergyVenueManager |
EnergyKitSampleApp/EnergyKitSampleApp/Views/EV/EVDetailsView.swift |
EVDetailsView, DataCategory |
EnergyKitSampleApp/EnergyKitSampleApp/Models/ElectricVehicle/ElectricVehicle.swift |
ElectricVehicle, Properties, State |
EnergyKitSampleApp/EnergyKitSampleApp/ContentView.swift |
ContentView |
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 |