USB-Meter / USB Meter / Views / ChargedDevices / BatteryCheckpointEditorSheetView.swift
Newer Older
66 lines | 2.306kb
Bogdan Timofte authored a month ago
1
//
2
//  BatteryCheckpointEditorSheetView.swift
3
//  USB Meter
4
//
5
//  Created by Codex on 10/04/2026.
6
//
7

            
8
import SwiftUI
9

            
10
struct BatteryCheckpointEditorSheetView: View {
11
    @EnvironmentObject private var appData: AppData
12
    @EnvironmentObject private var meter: Meter
13
    @Environment(\.dismiss) private var dismiss
14

            
15
    @State private var batteryPercent = ""
16
    @State private var label = ""
17

            
18
    var body: some View {
19
        NavigationView {
20
            Form {
21
                Section(header: Text("Checkpoint")) {
22
                    TextField("Battery %", text: $batteryPercent)
23
                        .keyboardType(.decimalPad)
24
                    TextField("Label (optional)", text: $label)
25
                }
26

            
27
                Section {
28
                    Text("The checkpoint is stored on the active charge session and later used for capacity estimation and the typical charge curve.")
29
                        .font(.footnote)
30
                        .foregroundColor(.secondary)
31
                }
32
            }
33
            .navigationTitle("Battery Checkpoint")
34
            .navigationBarTitleDisplayMode(.inline)
35
            .toolbar {
36
                ToolbarItem(placement: .cancellationAction) {
37
                    Button("Cancel") {
38
                        dismiss()
39
                    }
40
                }
41
                ToolbarItem(placement: .confirmationAction) {
42
                    Button("Save") {
43
                        guard let percent = Double(batteryPercent) else {
44
                            return
45
                        }
46

            
47
                        let didSave = appData.addBatteryCheckpoint(
48
                            percent: percent,
49
                            label: label,
50
                            for: meter
51
                        )
52
                        if didSave {
53
                            dismiss()
54
                        }
55
                    }
56
                    .disabled(
57
                        (Double(batteryPercent) ?? -1) < 0
58
                            || (Double(batteryPercent) ?? 101) > 100
Bogdan Timofte authored a month ago
59
                            || appData.activeChargeSessionSummary(for: meter.btSerial.macAddress.description) == nil
Bogdan Timofte authored a month ago
60
                    )
61
                }
62
            }
63
        }
64
        .navigationViewStyle(StackNavigationViewStyle())
65
    }
66
}