1 contributor
138 lines | 5.516kb
//
//  MeterView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 04/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//
// MARK: Parent frame https://stackoverflow.com/questions/56832865/how-to-access-parents-frame-in-swiftui

import SwiftUI
import CoreBluetooth

struct MeterView: View {
    
    @EnvironmentObject private var meter: Meter
    
    @State var dataGroupsViewVisibility: Bool = false
    @State var recordingViewVisibility: Bool = false
    @State var measurementsViewVisibility: Bool = false
    private var myBounds: CGRect { UIScreen.main.bounds }

    var body: some View {
        ScrollView {
            VStack (alignment: .center, spacing: 10) {
                // MARK: Name
                VStack {
                    Text("Meter")
                        .font(.headline)
                    Text("\(meter.name)")
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(meter.color).opacity(0.1))
                // MARK: Mac
                VStack {
                    Text("MAC")
                        .font(.headline)
                    Text("\(meter.btSerial.macAddress.description)")
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(.secondary).opacity(0.1))
            }
            // MARK: Connect/Disconnect
            connectionControlButton()
            // MARK: Show Data
            if ( meter.operationalState ==  .dataIsAvailable) {
                Text("Model: \(meter.deviceModelSummary)")
                HStack(spacing: 24) {
                    meterSheetButton(icon: "map", title: meter.dataGroupsTitle) {
                        dataGroupsViewVisibility.toggle()
                    }
                    .sheet(isPresented: self.$dataGroupsViewVisibility) {
                        DataGroupsView(visibility: self.$dataGroupsViewVisibility)
                            .environmentObject(self.meter)
                    }

                    if meter.supportsRecordingView {
                        meterSheetButton(icon: "record.circle", title: "Charge Record") {
                            recordingViewVisibility.toggle()
                        }
                        .sheet(isPresented: self.$recordingViewVisibility) {
                            RecordingView(visibility: self.$recordingViewVisibility)
                                .environmentObject(self.meter)
                        }
                    }

                    meterSheetButton(icon: "recordingtape", title: "App History") {
                        measurementsViewVisibility.toggle()
                    }
                    .sheet(isPresented: self.$measurementsViewVisibility) {
                        MeasurementsView(visibility: self.$measurementsViewVisibility)
                            .environmentObject(self.meter.measurements)
                    }
                }
                if self.meter.measurements.power.context.isValid {
                    MeasurementChartView()
                    .environmentObject(self.meter.measurements)
                        .frame(minHeight: myBounds.height/3)
                }
                ControlView()
                LiveView()
            }
        }
        //.frame(minWidth: 0, maxWidth: .greatestFiniteMagnitude, minHeight: 0, maxHeight: .greatestFiniteMagnitude)
        .navigationBarTitle("Meter")
        .navigationBarItems(trailing: HStack (spacing: 0) {
            if meter.operationalState > .notPresent {
                RSSIView(RSSI: meter.btSerial.RSSI)
                    .frame(width: 24)
                    .padding(.vertical)
            }
            NavigationLink(destination: MeterSettingsView().environmentObject(meter)) {
                Image( systemName: "gear" )
                    //.imageScale(.large)
                    .padding(.vertical)
                    .padding(.leading)
                //.background(RoundedRectangle(cornerRadius: 20)).opacity(0.05)
            }
        })
    }
    
    fileprivate func connectionControlButton() -> some View {
        /*
         MARK: De adaugat si celelalte situatii
         case peripheralNotConnected
         case peripheralConnectionPending
         case peripheralConnected
         case ready
         */
        let buttonColor = meter.operationalState > .peripheralNotConnected ? Color.red : Color.green
        return Group {
            if meter.operationalState == .notPresent {
                Text("Not found at this time.").foregroundColor(.red)
            } else {
                
                HStack {
                    if meter.operationalState < .peripheralConnectionPending {
                        Button (action: { self.meter.connect() } ) { Text("Connect") }
                    } else {
                        Button (action: { self.meter.disconnect() } ) { Text("Disconnect") }
                    }
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(buttonColor).opacity(0.1))
                .frame(maxWidth: .greatestFiniteMagnitude)
            }
        }
        
    }

    fileprivate func meterSheetButton(icon: String, title: String, action: @escaping () -> Void) -> some View {
        Button(action: action) {
            VStack {
                Image(systemName: icon)
                Text(title)
            }
        }
    }
}