|
Bogdan Timofte
authored
a month ago
|
1
|
//
|
|
|
2
|
// ChargerEditorSheetView.swift
|
|
|
3
|
// USB Meter
|
|
|
4
|
//
|
|
|
5
|
|
|
|
6
|
import SwiftUI
|
|
|
7
|
|
|
|
8
|
struct ChargerEditorSheetView: View {
|
|
Bogdan Timofte
authored
a month ago
|
9
|
@EnvironmentObject private var appData: AppData
|
|
Bogdan Timofte
authored
a month ago
|
10
|
@Environment(\.dismiss) private var dismiss
|
|
|
11
|
|
|
|
12
|
let chargedDevice: ChargedDeviceSummary?
|
|
|
13
|
/// When false the view omits its own NavigationView (used as a push destination).
|
|
|
14
|
let standalone: Bool
|
|
|
15
|
|
|
|
16
|
@State private var name: String
|
|
|
17
|
@State private var chargerType: ChargerType
|
|
|
18
|
@State private var notes: String
|
|
|
19
|
|
|
|
20
|
init(
|
|
|
21
|
chargedDevice: ChargedDeviceSummary? = nil,
|
|
|
22
|
standalone: Bool = true
|
|
|
23
|
) {
|
|
|
24
|
self.chargedDevice = chargedDevice
|
|
|
25
|
self.standalone = standalone
|
|
|
26
|
_name = State(initialValue: chargedDevice?.name ?? "")
|
|
|
27
|
_chargerType = State(initialValue: chargedDevice?.chargerType ?? .genericQi)
|
|
|
28
|
_notes = State(initialValue: chargedDevice?.notes ?? "")
|
|
|
29
|
}
|
|
|
30
|
|
|
|
31
|
var body: some View {
|
|
Bogdan Timofte
authored
a month ago
|
32
|
ChargedDeviceEditorScaffoldView(
|
|
|
33
|
title: editorTitle,
|
|
|
34
|
saveButtonTitle: saveButtonTitle,
|
|
|
35
|
canSave: canSave,
|
|
|
36
|
standalone: standalone,
|
|
|
37
|
save: save
|
|
|
38
|
) {
|
|
Bogdan Timofte
authored
a month ago
|
39
|
Section(header: Text("Identity")) {
|
|
|
40
|
TextField("Charger name", text: $name)
|
|
|
41
|
|
|
|
42
|
if let chargedDevice {
|
|
|
43
|
Text(chargedDevice.qrIdentifier)
|
|
|
44
|
.font(.caption.monospaced())
|
|
|
45
|
.foregroundColor(.secondary)
|
|
|
46
|
.textSelection(.enabled)
|
|
|
47
|
}
|
|
|
48
|
}
|
|
|
49
|
|
|
|
50
|
Section(
|
|
|
51
|
header: ContextInfoHeader(
|
|
|
52
|
title: "Charger Type",
|
|
|
53
|
message: "MagSafe and Watch chargers use magnetic alignment, enabling accurate efficiency calibration. Standby current and efficiency are learned automatically from sessions."
|
|
|
54
|
)
|
|
|
55
|
) {
|
|
|
56
|
Picker("Type", selection: $chargerType) {
|
|
|
57
|
ForEach(ChargerType.allCases) { type in
|
|
|
58
|
Label(type.title, systemImage: type.symbolName)
|
|
|
59
|
.tag(type)
|
|
|
60
|
}
|
|
|
61
|
}
|
|
|
62
|
.pickerStyle(.menu)
|
|
|
63
|
}
|
|
|
64
|
|
|
|
65
|
Section(header: Text("Notes")) {
|
|
|
66
|
TextField("Optional notes", text: $notes)
|
|
|
67
|
}
|
|
|
68
|
}
|
|
Bogdan Timofte
authored
a month ago
|
69
|
}
|
|
|
70
|
|
|
|
71
|
private var editorTitle: String {
|
|
|
72
|
chargedDevice == nil ? "New Charger" : "Edit Charger"
|
|
|
73
|
}
|
|
|
74
|
|
|
|
75
|
private var saveButtonTitle: String {
|
|
|
76
|
chargedDevice == nil ? "Save" : "Update"
|
|
|
77
|
}
|
|
|
78
|
|
|
|
79
|
private var canSave: Bool {
|
|
|
80
|
!name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
Bogdan Timofte
authored
a month ago
|
81
|
}
|
|
|
82
|
|
|
|
83
|
private func save() {
|
|
|
84
|
let trimmedNotes = notes.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
85
|
let notesValue: String? = trimmedNotes.isEmpty ? nil : trimmedNotes
|
|
|
86
|
|
|
|
87
|
let didSave: Bool
|
|
|
88
|
if let chargedDevice {
|
|
|
89
|
didSave = appData.updateCharger(
|
|
|
90
|
id: chargedDevice.id,
|
|
|
91
|
name: name,
|
|
|
92
|
chargerType: chargerType,
|
|
|
93
|
notes: notesValue
|
|
|
94
|
)
|
|
|
95
|
} else {
|
|
|
96
|
didSave = appData.createCharger(
|
|
|
97
|
name: name,
|
|
|
98
|
chargerType: chargerType,
|
|
Bogdan Timofte
authored
a month ago
|
99
|
notes: notesValue
|
|
Bogdan Timofte
authored
a month ago
|
100
|
)
|
|
|
101
|
}
|
|
|
102
|
|
|
|
103
|
if didSave {
|
|
|
104
|
dismiss()
|
|
|
105
|
}
|
|
|
106
|
}
|
|
|
107
|
}
|