USB-Meter / USB Meter / Views / Meter / RecordingView.swift
1 contributor
134 lines | 6.534kb
//
//  RecordingView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 09/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct RecordingView: View {
    
    @Binding var visibility: Bool
    @EnvironmentObject private var usbMeter: Meter
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 16) {
                    VStack(spacing: 6) {
                        Text("Charge Record")
                            .font(.headline)
                        Text(usbMeter.chargeRecordStatusText)
                            .foregroundColor(usbMeter.chargeRecordStatusColor)
                    }
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))

                    HStack(alignment: .top) {
                        VStack(alignment: .leading, spacing: 10) {
                            Text("Capacity")
                            Text("Energy")
                            Text("Duration")
                            Text("Stop Threshold")
                        }
                        Spacer()
                        VStack(alignment: .trailing, spacing: 10) {
                            Text("\(usbMeter.chargeRecordAH.format(decimalDigits: 3)) Ah")
                            Text("\(usbMeter.chargeRecordWH.format(decimalDigits: 3)) Wh")
                            Text(usbMeter.chargeRecordDurationDescription)
                            Text("\(usbMeter.chargeRecordStopThreshold.format(decimalDigits: 2)) A")
                        }
                    }
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))

                    if usbMeter.chargeRecordTimeRange != nil {
                        VStack(alignment: .leading, spacing: 12) {
                            HStack {
                                Text("Charge Curve")
                                    .fontWeight(.semibold)
                                Spacer()
                                Button("Reset Graph") {
                                    usbMeter.resetChargeRecordGraph()
                                }
                            }
                            MeasurementChartView(timeRange: usbMeter.chargeRecordTimeRange)
                                .environmentObject(usbMeter.measurements)
                                .frame(minHeight: 220)
                            Text("Reset Graph clears the current charge-record session and removes older shared samples that are no longer needed for this curve.")
                                .font(.footnote)
                                .foregroundColor(.secondary)
                        }
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
                    }

                    VStack(alignment: .leading, spacing: 12) {
                        Text("Stop Threshold")
                            .fontWeight(.semibold)
                        Slider(value: $usbMeter.chargeRecordStopThreshold, in: 0...0.30, step: 0.01)
                        Text("The app starts accumulating when current rises above this threshold and stops when it falls back to or below it.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                        Button("Reset") {
                            usbMeter.resetChargeRecord()
                        }
                        .frame(maxWidth: .infinity)
                    }
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))

                    if usbMeter.supportsDataGroupCommands || usbMeter.recordedAH > 0 || usbMeter.recordedWH > 0 || usbMeter.recordingDuration > 0 {
                        VStack(alignment: .leading, spacing: 12) {
                            Text("Meter Totals")
                                .fontWeight(.semibold)
                            HStack(alignment: .top) {
                                VStack(alignment: .leading, spacing: 10) {
                                    Text("Capacity")
                                    Text("Energy")
                                    Text("Duration")
                                    Text("Meter Threshold")
                                }
                                Spacer()
                                VStack(alignment: .trailing, spacing: 10) {
                                    Text("\(usbMeter.recordedAH.format(decimalDigits: 3)) Ah")
                                    Text("\(usbMeter.recordedWH.format(decimalDigits: 3)) Wh")
                                    Text(usbMeter.recordingDurationDescription)
                                    if usbMeter.supportsRecordingThreshold {
                                        Text("\(usbMeter.recordingTreshold.format(decimalDigits: 2)) A")
                                    } else {
                                        Text("Read-only")
                                    }
                                }
                            }
                            Text("These values are reported by the meter for the active data group.")
                                .font(.footnote)
                                .foregroundColor(.secondary)
                            if usbMeter.supportsDataGroupCommands {
                                Button("Reset Active Group") {
                                    usbMeter.clear()
                                }
                                .frame(maxWidth: .infinity)
                            }
                        }
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
                    }
                }
                .padding()
            }
            .navigationBarTitle("Charge Record", displayMode: .inline)
            .navigationBarItems(trailing: Button("Done") { visibility.toggle() })
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct RecordingView_Previews: PreviewProvider {
    static var previews: some View {
        RecordingView(visibility: .constant(true))
    }
}