USB-Meter / USB Meter / Views / Meter / RecordingView.swift
Newer Older
134 lines | 6.534kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  RecordingView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 09/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
11
struct RecordingView: View {
12

            
Bogdan Timofte authored 2 weeks ago
13
    @Binding var visibility: Bool
Bogdan Timofte authored 2 weeks ago
14
    @EnvironmentObject private var usbMeter: Meter
15

            
16
    var body: some View {
Bogdan Timofte authored 2 weeks ago
17
        NavigationView {
18
            ScrollView {
19
                VStack(spacing: 16) {
20
                    VStack(spacing: 6) {
Bogdan Timofte authored 2 weeks ago
21
                        Text("Charge Record")
Bogdan Timofte authored 2 weeks ago
22
                            .font(.headline)
Bogdan Timofte authored 2 weeks ago
23
                        Text(usbMeter.chargeRecordStatusText)
24
                            .foregroundColor(usbMeter.chargeRecordStatusColor)
Bogdan Timofte authored 2 weeks ago
25
                    }
26
                    .frame(maxWidth: .infinity)
27
                    .padding()
28
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
29

            
30
                    HStack(alignment: .top) {
31
                        VStack(alignment: .leading, spacing: 10) {
32
                            Text("Capacity")
33
                            Text("Energy")
34
                            Text("Duration")
Bogdan Timofte authored 2 weeks ago
35
                            Text("Stop Threshold")
Bogdan Timofte authored 2 weeks ago
36
                        }
37
                        Spacer()
38
                        VStack(alignment: .trailing, spacing: 10) {
Bogdan Timofte authored 2 weeks ago
39
                            Text("\(usbMeter.chargeRecordAH.format(decimalDigits: 3)) Ah")
40
                            Text("\(usbMeter.chargeRecordWH.format(decimalDigits: 3)) Wh")
41
                            Text(usbMeter.chargeRecordDurationDescription)
42
                            Text("\(usbMeter.chargeRecordStopThreshold.format(decimalDigits: 2)) A")
Bogdan Timofte authored 2 weeks ago
43
                        }
44
                    }
45
                    .padding()
46
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
47

            
Bogdan Timofte authored 2 weeks ago
48
                    if usbMeter.chargeRecordTimeRange != nil {
49
                        VStack(alignment: .leading, spacing: 12) {
50
                            HStack {
51
                                Text("Charge Curve")
52
                                    .fontWeight(.semibold)
53
                                Spacer()
54
                                Button("Reset Graph") {
55
                                    usbMeter.resetChargeRecordGraph()
56
                                }
Bogdan Timofte authored 2 weeks ago
57
                            }
Bogdan Timofte authored 2 weeks ago
58
                            MeasurementChartView(timeRange: usbMeter.chargeRecordTimeRange)
59
                                .environmentObject(usbMeter.measurements)
60
                                .frame(minHeight: 220)
61
                            Text("Reset Graph clears the current charge-record session and removes older shared samples that are no longer needed for this curve.")
62
                                .font(.footnote)
63
                                .foregroundColor(.secondary)
Bogdan Timofte authored 2 weeks ago
64
                        }
65
                        .padding()
Bogdan Timofte authored 2 weeks ago
66
                        .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
Bogdan Timofte authored 2 weeks ago
67
                    }
68

            
69
                    VStack(alignment: .leading, spacing: 12) {
70
                        Text("Stop Threshold")
71
                            .fontWeight(.semibold)
72
                        Slider(value: $usbMeter.chargeRecordStopThreshold, in: 0...0.30, step: 0.01)
73
                        Text("The app starts accumulating when current rises above this threshold and stops when it falls back to or below it.")
Bogdan Timofte authored 2 weeks ago
74
                            .font(.footnote)
75
                            .foregroundColor(.secondary)
Bogdan Timofte authored 2 weeks ago
76
                        Button("Reset") {
77
                            usbMeter.resetChargeRecord()
78
                        }
79
                        .frame(maxWidth: .infinity)
80
                    }
81
                    .padding()
82
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
83

            
84
                    if usbMeter.supportsDataGroupCommands || usbMeter.recordedAH > 0 || usbMeter.recordedWH > 0 || usbMeter.recordingDuration > 0 {
85
                        VStack(alignment: .leading, spacing: 12) {
86
                            Text("Meter Totals")
87
                                .fontWeight(.semibold)
88
                            HStack(alignment: .top) {
89
                                VStack(alignment: .leading, spacing: 10) {
90
                                    Text("Capacity")
91
                                    Text("Energy")
92
                                    Text("Duration")
93
                                    Text("Meter Threshold")
94
                                }
95
                                Spacer()
96
                                VStack(alignment: .trailing, spacing: 10) {
97
                                    Text("\(usbMeter.recordedAH.format(decimalDigits: 3)) Ah")
98
                                    Text("\(usbMeter.recordedWH.format(decimalDigits: 3)) Wh")
99
                                    Text(usbMeter.recordingDurationDescription)
100
                                    if usbMeter.supportsRecordingThreshold {
101
                                        Text("\(usbMeter.recordingTreshold.format(decimalDigits: 2)) A")
102
                                    } else {
103
                                        Text("Read-only")
104
                                    }
105
                                }
106
                            }
107
                            Text("These values are reported by the meter for the active data group.")
108
                                .font(.footnote)
109
                                .foregroundColor(.secondary)
110
                            if usbMeter.supportsDataGroupCommands {
111
                                Button("Reset Active Group") {
112
                                    usbMeter.clear()
113
                                }
114
                                .frame(maxWidth: .infinity)
115
                            }
116
                        }
117
                        .padding()
118
                        .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
Bogdan Timofte authored 2 weeks ago
119
                    }
Bogdan Timofte authored 2 weeks ago
120
                }
121
                .padding()
Bogdan Timofte authored 2 weeks ago
122
            }
Bogdan Timofte authored 2 weeks ago
123
            .navigationBarTitle("Charge Record", displayMode: .inline)
Bogdan Timofte authored 2 weeks ago
124
            .navigationBarItems(trailing: Button("Done") { visibility.toggle() })
Bogdan Timofte authored 2 weeks ago
125
        }
Bogdan Timofte authored 2 weeks ago
126
        .navigationViewStyle(StackNavigationViewStyle())
Bogdan Timofte authored 2 weeks ago
127
    }
128
}
129

            
130
struct RecordingView_Previews: PreviewProvider {
131
    static var previews: some View {
Bogdan Timofte authored 2 weeks ago
132
        RecordingView(visibility: .constant(true))
Bogdan Timofte authored 2 weeks ago
133
    }
134
}