Newer Older
111 lines | 3.49kb
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
    let meterMACAddress: String?
14
    /// When false the view omits its own NavigationView (used as a push destination).
15
    let standalone: Bool
16

            
17
    @State private var name: String
18
    @State private var chargerType: ChargerType
19
    @State private var notes: String
20

            
21
    init(
22
        chargedDevice: ChargedDeviceSummary? = nil,
23
        meterMACAddress: String? = nil,
24
        standalone: Bool = true
25
    ) {
26
        self.chargedDevice = chargedDevice
27
        self.meterMACAddress = meterMACAddress
28
        self.standalone = standalone
29
        _name = State(initialValue: chargedDevice?.name ?? "")
30
        _chargerType = State(initialValue: chargedDevice?.chargerType ?? .genericQi)
31
        _notes = State(initialValue: chargedDevice?.notes ?? "")
32
    }
33

            
34
    var body: some View {
Bogdan Timofte authored a month ago
35
        ChargedDeviceEditorScaffoldView(
36
            title: editorTitle,
37
            saveButtonTitle: saveButtonTitle,
38
            canSave: canSave,
39
            standalone: standalone,
40
            save: save
41
        ) {
Bogdan Timofte authored a month ago
42
            Section(header: Text("Identity")) {
43
                TextField("Charger name", text: $name)
44

            
45
                if let chargedDevice {
46
                    Text(chargedDevice.qrIdentifier)
47
                        .font(.caption.monospaced())
48
                        .foregroundColor(.secondary)
49
                        .textSelection(.enabled)
50
                }
51
            }
52

            
53
            Section(
54
                header: ContextInfoHeader(
55
                    title: "Charger Type",
56
                    message: "MagSafe and Watch chargers use magnetic alignment, enabling accurate efficiency calibration. Standby current and efficiency are learned automatically from sessions."
57
                )
58
            ) {
59
                Picker("Type", selection: $chargerType) {
60
                    ForEach(ChargerType.allCases) { type in
61
                        Label(type.title, systemImage: type.symbolName)
62
                            .tag(type)
63
                    }
64
                }
65
                .pickerStyle(.menu)
66
            }
67

            
68
            Section(header: Text("Notes")) {
69
                TextField("Optional notes", text: $notes)
70
            }
71
        }
Bogdan Timofte authored a month ago
72
    }
73

            
74
    private var editorTitle: String {
75
        chargedDevice == nil ? "New Charger" : "Edit Charger"
76
    }
77

            
78
    private var saveButtonTitle: String {
79
        chargedDevice == nil ? "Save" : "Update"
80
    }
81

            
82
    private var canSave: Bool {
83
        !name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
Bogdan Timofte authored a month ago
84
    }
85

            
86
    private func save() {
87
        let trimmedNotes = notes.trimmingCharacters(in: .whitespacesAndNewlines)
88
        let notesValue: String? = trimmedNotes.isEmpty ? nil : trimmedNotes
89

            
90
        let didSave: Bool
91
        if let chargedDevice {
92
            didSave = appData.updateCharger(
93
                id: chargedDevice.id,
94
                name: name,
95
                chargerType: chargerType,
96
                notes: notesValue
97
            )
98
        } else {
99
            didSave = appData.createCharger(
100
                name: name,
101
                chargerType: chargerType,
102
                notes: notesValue,
103
                meterMACAddress: meterMACAddress
104
            )
105
        }
106

            
107
        if didSave {
108
            dismiss()
109
        }
110
    }
111
}