USB-Meter / USB Meter / Views / ChargedDevices / ChargeSessionCompletionSheetView.swift
Newer Older
86 lines | 3.158kb
Bogdan Timofte authored a month ago
1
//
2
//  ChargeSessionCompletionSheetView.swift
3
//  USB Meter
4
//
5

            
6
import SwiftUI
7

            
8
struct ChargeSessionCompletionSheetView: View {
9
    @EnvironmentObject private var appData: AppData
10
    @Environment(\.dismiss) private var dismiss
11

            
12
    let sessionID: UUID
13
    let title: String
14
    let confirmTitle: String
15
    let explanation: String
16

            
17
    @State private var batteryPercent = ""
18

            
19
    var body: some View {
20
        NavigationView {
21
            Form {
22
                Section(
23
                    header: ContextInfoHeader(
24
                        title: "Final Checkpoint",
25
                        message: explanation
26
                    )
27
                ) {
28
                    TextField("Battery %", text: $batteryPercent)
29
                        .keyboardType(.decimalPad)
30
                }
31

            
32
                Section {
33
                    if let sessionWarning {
34
                        Text(sessionWarning)
35
                            .font(.footnote)
36
                            .foregroundColor(.orange)
37
                    } else if (parsedBatteryPercent ?? 0) >= 99.5 {
38
                        Text("A final checkpoint at 100% lets the app learn the stop current for this exact charging type when the session data is reliable.")
39
                            .font(.footnote)
40
                            .foregroundColor(.secondary)
41
                    }
42
                }
43
            }
44
            .navigationTitle(title)
45
            .navigationBarTitleDisplayMode(.inline)
46
            .toolbar {
47
                ToolbarItem(placement: .cancellationAction) {
48
                    Button("Cancel") {
49
                        dismiss()
50
                    }
51
                }
52
                ToolbarItem(placement: .confirmationAction) {
53
                    Button(confirmTitle) {
54
                        guard let percent = parsedBatteryPercent else { return }
55
                        if appData.stopChargeSession(sessionID: sessionID, finalBatteryPercent: percent) {
56
                            dismiss()
57
                        }
58
                    }
59
                    .disabled(parsedBatteryPercent == nil)
60
                }
61
            }
62
        }
63
        .navigationViewStyle(StackNavigationViewStyle())
64
    }
65

            
66
    private var parsedBatteryPercent: Double? {
67
        let normalized = batteryPercent
68
            .trimmingCharacters(in: .whitespacesAndNewlines)
69
            .replacingOccurrences(of: ",", with: ".")
70
        guard let value = Double(normalized), value >= 0, value <= 100 else { return nil }
71
        return value
72
    }
73

            
74
    private var sessionWarning: String? {
75
        guard let session = appData.chargedDevices
76
            .flatMap(\.sessions)
77
            .first(where: { $0.id == sessionID }),
78
              session.chargingTransportMode == .wireless,
79
              let chargerID = session.chargerID,
80
              let charger = appData.chargedDeviceSummary(id: chargerID),
81
              charger.chargerIdleCurrentAmps == nil else {
82
            return nil
83
        }
84
        return "This charger has no idle-current measurement, so the final checkpoint will stop the session but will not learn a wireless stop threshold yet."
85
    }
86
}