adds chargers

This commit is contained in:
Stefan Lange-Hegermann
2025-10-23 14:09:16 +02:00
parent 6258a6a66f
commit 0720529821
23 changed files with 1460 additions and 263 deletions

View File

@@ -7,7 +7,8 @@ struct SystemComponentsPersistence {
for system: ElectricalSystem,
in context: ModelContext,
existingLoads: [SavedLoad],
existingBatteries: [SavedBattery]
existingBatteries: [SavedBattery],
existingChargers: [SavedCharger]
) -> SavedLoad {
let defaultName = String(
localized: "default.load.new",
@@ -16,7 +17,8 @@ struct SystemComponentsPersistence {
let loadName = uniqueName(
startingWith: defaultName,
loads: existingLoads,
batteries: existingBatteries
batteries: existingBatteries,
chargers: existingChargers
)
let newLoad = SavedLoad(
name: loadName,
@@ -40,14 +42,16 @@ struct SystemComponentsPersistence {
for system: ElectricalSystem,
in context: ModelContext,
existingLoads: [SavedLoad],
existingBatteries: [SavedBattery]
existingBatteries: [SavedBattery],
existingChargers: [SavedCharger]
) -> SavedLoad {
let localizedName = item.localizedName
let baseName = localizedName.isEmpty ? "Library Load" : localizedName
let loadName = uniqueName(
startingWith: baseName,
loads: existingLoads,
batteries: existingBatteries
batteries: existingBatteries,
chargers: existingChargers
)
let voltage = item.displayVoltage ?? 12.0
let power = item.watt ?? (item.current != nil ? item.current! * voltage : 0)
@@ -85,7 +89,8 @@ struct SystemComponentsPersistence {
static func makeBatteryDraft(
for system: ElectricalSystem,
existingLoads: [SavedLoad],
existingBatteries: [SavedBattery]
existingBatteries: [SavedBattery],
existingChargers: [SavedCharger]
) -> BatteryConfiguration {
let defaultName = NSLocalizedString(
"battery.editor.default_name",
@@ -96,7 +101,8 @@ struct SystemComponentsPersistence {
let batteryName = uniqueName(
startingWith: defaultName,
loads: existingLoads,
batteries: existingBatteries
batteries: existingBatteries,
chargers: existingChargers
)
return BatteryConfiguration(
name: batteryName,
@@ -106,6 +112,32 @@ struct SystemComponentsPersistence {
)
}
static func makeChargerDraft(
for system: ElectricalSystem,
existingLoads: [SavedLoad],
existingBatteries: [SavedBattery],
existingChargers: [SavedCharger]
) -> ChargerConfiguration {
let defaultName = NSLocalizedString(
"charger.editor.default_name",
bundle: .main,
value: "New Charger",
comment: "Default name when configuring a new charger"
)
let chargerName = uniqueName(
startingWith: defaultName,
loads: existingLoads,
batteries: existingBatteries,
chargers: existingChargers
)
return ChargerConfiguration(
name: chargerName,
iconName: "bolt.fill",
colorName: system.colorName,
system: system
)
}
static func saveBattery(
_ configuration: BatteryConfiguration,
for system: ElectricalSystem,
@@ -129,6 +161,30 @@ struct SystemComponentsPersistence {
}
}
static func saveCharger(
_ configuration: ChargerConfiguration,
for system: ElectricalSystem,
existingChargers: [SavedCharger],
in context: ModelContext
) {
if let existing = existingChargers.first(where: { $0.id == configuration.id }) {
configuration.apply(to: existing)
} else {
let newCharger = SavedCharger(
id: configuration.id,
name: configuration.name,
inputVoltage: configuration.inputVoltage,
outputVoltage: configuration.outputVoltage,
maxCurrentAmps: configuration.maxCurrentAmps,
maxPowerWatts: configuration.maxPowerWatts,
iconName: configuration.iconName,
colorName: configuration.colorName,
system: system
)
context.insert(newCharger)
}
}
static func deleteBatteries(
at offsets: IndexSet,
from batteries: [SavedBattery],
@@ -139,12 +195,27 @@ struct SystemComponentsPersistence {
}
}
static func deleteChargers(
at offsets: IndexSet,
from chargers: [SavedCharger],
in context: ModelContext
) {
for index in offsets {
context.delete(chargers[index])
}
}
static func uniqueName(
startingWith baseName: String,
loads: [SavedLoad],
batteries: [SavedBattery]
batteries: [SavedBattery],
chargers: [SavedCharger]
) -> String {
let existingNames = Set(loads.map { $0.name } + batteries.map { $0.name })
let existingNames = Set(
loads.map { $0.name } +
batteries.map { $0.name } +
chargers.map { $0.name }
)
if !existingNames.contains(baseName) {
return baseName
@@ -160,4 +231,35 @@ struct SystemComponentsPersistence {
return candidate
}
static func createDefaultCharger(
for system: ElectricalSystem,
in context: ModelContext,
existingLoads: [SavedLoad],
existingBatteries: [SavedBattery],
existingChargers: [SavedCharger]
) -> SavedCharger {
let defaultName = String(
localized: "charger.default.new",
bundle: .main,
comment: "Default name when creating a new charger from system view"
)
let chargerName = uniqueName(
startingWith: defaultName,
loads: existingLoads,
batteries: existingBatteries,
chargers: existingChargers
)
let charger = SavedCharger(
name: chargerName,
inputVoltage: 230,
outputVoltage: 14.4,
maxCurrentAmps: 30,
iconName: "bolt.fill",
colorName: system.colorName,
system: system
)
context.insert(charger)
return charger
}
}

View File

@@ -80,7 +80,7 @@ struct SystemsView: View {
HStack(spacing: 12) {
ZStack {
RoundedRectangle(cornerRadius: 10)
.fill(colorForName(system.colorName))
.fill(Color.componentColor(named: system.colorName))
.frame(width: 44, height: 44)
Image(systemName: system.iconName)
@@ -395,24 +395,6 @@ struct SystemsView: View {
return uniqueKeywords
}
private func colorForName(_ colorName: String) -> Color {
switch colorName {
case "blue": return .blue
case "green": return .green
case "orange": return .orange
case "red": return .red
case "purple": return .purple
case "yellow": return .yellow
case "pink": return .pink
case "teal": return .teal
case "indigo": return .indigo
case "mint": return .mint
case "cyan": return .cyan
case "brown": return .brown
case "gray": return .gray
default: return .blue
}
}
}
#Preview("Sample Systems") {