| 1 |
// |
|
| 2 |
// PowerbankEditorSheetView.swift |
|
| 3 |
// USB Meter |
|
| 4 |
// |
|
| 5 | ||
| 6 |
import SwiftUI |
|
| 7 | ||
| 8 |
struct PowerbankEditorSheetView: View {
|
|
| 9 |
@EnvironmentObject private var appData: AppData |
|
| 10 |
@Environment(\.dismiss) private var dismiss |
|
| 11 | ||
| 12 |
let powerbank: PowerbankSummary? |
|
| 13 |
let standalone: Bool |
|
| 14 | ||
| 15 |
@State private var name: String |
|
| 16 |
@State private var batteryLevelReporting: BatteryLevelReporting |
|
| 17 |
@State private var batteryBarsCount: Int |
|
| 18 |
@State private var notes: String |
|
| 19 | ||
| 20 |
init( |
|
| 21 |
powerbank: PowerbankSummary? = nil, |
|
| 22 |
standalone: Bool = true |
|
| 23 |
) {
|
|
| 24 |
self.powerbank = powerbank |
|
| 25 |
self.standalone = standalone |
|
| 26 |
_name = State(initialValue: powerbank?.name ?? "") |
|
| 27 |
_batteryLevelReporting = State(initialValue: powerbank?.batteryLevelReporting ?? .percent) |
|
| 28 |
_batteryBarsCount = State(initialValue: powerbank?.batteryBarsCount ?? 4) |
|
| 29 |
_notes = State(initialValue: powerbank?.notes ?? "") |
|
| 30 |
} |
|
| 31 | ||
| 32 |
var body: some View {
|
|
| 33 |
ChargedDeviceEditorScaffoldView( |
|
| 34 |
title: editorTitle, |
|
| 35 |
saveButtonTitle: saveButtonTitle, |
|
| 36 |
canSave: canSave, |
|
| 37 |
standalone: standalone, |
|
| 38 |
save: save |
|
| 39 |
) {
|
|
| 40 |
Section(header: Text("Identity")) {
|
|
| 41 |
TextField("Powerbank name", text: $name)
|
|
| 42 | ||
| 43 |
if let powerbank {
|
|
| 44 |
Text(powerbank.qrIdentifier) |
|
| 45 |
.font(.caption.monospaced()) |
|
| 46 |
.foregroundColor(.secondary) |
|
| 47 |
.textSelection(.enabled) |
|
| 48 |
} |
|
| 49 |
} |
|
| 50 | ||
| 51 |
Section( |
|
| 52 |
header: ContextInfoHeader( |
|
| 53 |
title: "Battery Level Reporting", |
|
| 54 |
message: "Powerbanks report battery in different ways: 0–100%, discrete bars (e.g. 4 of 4), a single LED that lights only when full, or not at all. This selection drives how checkpoints are entered and how capacity is learned." |
|
| 55 |
) |
|
| 56 |
) {
|
|
| 57 |
Picker("Reporting", selection: $batteryLevelReporting) {
|
|
| 58 |
ForEach(BatteryLevelReporting.allCases) { reporting in
|
|
| 59 |
Text(reporting.title).tag(reporting) |
|
| 60 |
} |
|
| 61 |
} |
|
| 62 |
.pickerStyle(.menu) |
|
| 63 | ||
| 64 |
if batteryLevelReporting == .bars {
|
|
| 65 |
Stepper(value: $batteryBarsCount, in: 1...10) {
|
|
| 66 |
Text("Bars resolution: \(batteryBarsCount)")
|
|
| 67 |
} |
|
| 68 |
} |
|
| 69 | ||
| 70 |
Text(batteryLevelReporting.description) |
|
| 71 |
.font(.caption) |
|
| 72 |
.foregroundColor(.secondary) |
|
| 73 |
} |
|
| 74 | ||
| 75 |
Section(header: Text("Notes")) {
|
|
| 76 |
TextField("Optional notes", text: $notes)
|
|
| 77 |
} |
|
| 78 |
} |
|
| 79 |
} |
|
| 80 | ||
| 81 |
private var editorTitle: String {
|
|
| 82 |
powerbank == nil ? "New Powerbank" : "Edit Powerbank" |
|
| 83 |
} |
|
| 84 | ||
| 85 |
private var saveButtonTitle: String {
|
|
| 86 |
powerbank == nil ? "Save" : "Update" |
|
| 87 |
} |
|
| 88 | ||
| 89 |
private var canSave: Bool {
|
|
| 90 |
!name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
|
| 91 |
} |
|
| 92 | ||
| 93 |
private func save() {
|
|
| 94 |
let trimmedNotes = notes.trimmingCharacters(in: .whitespacesAndNewlines) |
|
| 95 |
let notesValue: String? = trimmedNotes.isEmpty ? nil : trimmedNotes |
|
| 96 | ||
| 97 |
let didSave: Bool |
|
| 98 |
if let powerbank {
|
|
| 99 |
didSave = appData.updatePowerbank( |
|
| 100 |
id: powerbank.id, |
|
| 101 |
name: name, |
|
| 102 |
templateID: powerbank.deviceTemplateID, |
|
| 103 |
batteryLevelReporting: batteryLevelReporting, |
|
| 104 |
batteryBarsCount: batteryBarsCount, |
|
| 105 |
notes: notesValue |
|
| 106 |
) |
|
| 107 |
} else {
|
|
| 108 |
didSave = appData.createPowerbank( |
|
| 109 |
name: name, |
|
| 110 |
templateID: nil, |
|
| 111 |
batteryLevelReporting: batteryLevelReporting, |
|
| 112 |
batteryBarsCount: batteryBarsCount, |
|
| 113 |
notes: notesValue |
|
| 114 |
) |
|
| 115 |
} |
|
| 116 | ||
| 117 |
if didSave {
|
|
| 118 |
dismiss() |
|
| 119 |
} |
|
| 120 |
} |
|
| 121 |
} |