USB-Meter / USB Meter / Views / Meter / RecordingView.swift
1 contributor
87 lines | 3.758kb
//
//  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("Device Recording")
                            .font(.headline)
                        Text(usbMeter.recording ? "Active" : "Idle")
                            .foregroundColor(usbMeter.recording ? .red : .secondary)
                    }
                    .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("Start 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")
                            }
                        }
                    }
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))

                    if usbMeter.supportsRecordingThreshold {
                        VStack(alignment: .leading, spacing: 10) {
                            Text("Start Threshold")
                                .fontWeight(.semibold)
                            Slider(value: $usbMeter.recordingTreshold, in: 0...0.30, step: 0.01)
                            if let hint = usbMeter.recordingThresholdHint {
                                Text(hint)
                                    .font(.footnote)
                                    .foregroundColor(.secondary)
                            }
                        }
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
                    } else {
                        Text("This model reports recording totals, but the app does not expose remote threshold control for it.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                            .multilineTextAlignment(.center)
                            .padding()
                            .background(RoundedRectangle(cornerRadius: 16).foregroundColor(.secondary).opacity(0.1))
                    }
                }
                .padding()
            }
            .navigationBarTitle("Device Recording", displayMode: .inline)
            .navigationBarItems(trailing: Button("Done") { visibility.toggle() })
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

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