1 contributor
334 lines | 12.97kb
//
//  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 }

    private let actionColumns = [
        GridItem(.adaptive(minimum: 112, maximum: 180), spacing: 12)
    ]

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 16) {
                headerCard
                connectionControlButton()

                if meter.operationalState == .dataIsAvailable {
                    actionGrid

                    if meter.measurements.power.context.isValid {
                        MeasurementChartView()
                            .environmentObject(meter.measurements)
                            .frame(minHeight: myBounds.height / 3.4)
                            .padding(16)
                            .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                    }

                    ControlView()
                        .padding(16)
                        .meterCard(tint: .indigo, fillOpacity: 0.12, strokeOpacity: 0.20)

                    LiveView()
                        .padding(18)
                        .meterCard(tint: meter.color, fillOpacity: 0.12, strokeOpacity: 0.20)
                }
            }
            .padding()
        }
        .background(
            LinearGradient(
                colors: [
                    meter.color.opacity(0.22),
                    Color.secondary.opacity(0.08),
                    Color.clear
                ],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
        .navigationBarTitle("Meter")
        .navigationBarItems(trailing: HStack (spacing: 0) {
            if meter.operationalState > .notPresent {
                RSSIView(RSSI: meter.btSerial.RSSI)
                    .frame(width: 24)
                    .padding(.vertical)
            }
            NavigationLink(destination: MeterInfoView().environmentObject(meter)) {
                Image(systemName: "info.circle.fill")
                    .padding(.vertical)
                    .padding(.leading)
            }
            NavigationLink(destination: MeterSettingsView().environmentObject(meter)) {
                Image(systemName: "gearshape.fill")
                    .padding(.vertical)
                    .padding(.leading)
            }
        })
    }

    private var headerCard: some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack(alignment: .top) {
                VStack(alignment: .leading, spacing: 6) {
                    Text(meter.name)
                        .font(.system(.title2, design: .rounded).weight(.bold))
                    Text(meter.deviceModelSummary)
                        .font(.subheadline.weight(.semibold))
                        .foregroundColor(.secondary)
                }
                Spacer()
                VStack(alignment: .trailing, spacing: 6) {
                    Text(statusText)
                        .font(.caption.weight(.bold))
                        .padding(.horizontal, 12)
                        .padding(.vertical, 6)
                        .meterCard(tint: statusColor, fillOpacity: 0.24, strokeOpacity: 0.30, cornerRadius: 999)
                    if meter.operationalState > .notPresent {
                        Text("RSSI \(meter.btSerial.RSSI)")
                            .font(.caption)
                            .foregroundColor(.secondary)
                    }
                }
            }
        }
        .padding(20)
        .meterCard(tint: meter.color, fillOpacity: 0.22, strokeOpacity: 0.24)
    }

    private var actionGrid: some View {
        LazyVGrid(columns: actionColumns, spacing: 12) {
            meterSheetButton(icon: "square.grid.2x2.fill", title: meter.dataGroupsTitle, tint: .teal) {
                dataGroupsViewVisibility.toggle()
            }
            .sheet(isPresented: $dataGroupsViewVisibility) {
                DataGroupsView(visibility: $dataGroupsViewVisibility)
                    .environmentObject(meter)
            }

            if meter.supportsRecordingView {
                meterSheetButton(icon: "gauge.with.dots.needle.50percent", title: "Charge Record", tint: .pink) {
                    recordingViewVisibility.toggle()
                }
                .sheet(isPresented: $recordingViewVisibility) {
                    RecordingView(visibility: $recordingViewVisibility)
                        .environmentObject(meter)
                }
            }

            meterSheetButton(icon: "clock.arrow.circlepath", title: "App History", tint: .blue) {
                measurementsViewVisibility.toggle()
            }
            .sheet(isPresented: $measurementsViewVisibility) {
                MeasurementsView(visibility: $measurementsViewVisibility)
                    .environmentObject(meter.measurements)
            }
        }
    }
    
    fileprivate func connectionControlButton() -> some View {
        let connected = meter.operationalState >= .peripheralConnectionPending
        let tint = connected ? Color.red : Color.green

        return Group {
            if meter.operationalState == .notPresent {
                HStack(spacing: 10) {
                    Image(systemName: "exclamationmark.triangle.fill")
                        .foregroundColor(.orange)
                    Text("Not found at this time.")
                        .fontWeight(.semibold)
                    Spacer()
                }
                .padding(16)
                .meterCard(tint: .orange, fillOpacity: 0.18, strokeOpacity: 0.24)
            } else {
                Button(action: {
                    if meter.operationalState < .peripheralConnectionPending {
                        meter.connect()
                    } else {
                        meter.disconnect()
                    }
                }) {
                    HStack {
                        Image(systemName: connected ? "xmark.circle.fill" : "bolt.horizontal.circle.fill")
                        Text(connected ? "Disconnect" : "Connect")
                            .fontWeight(.semibold)
                        Spacer()
                        Text(statusText)
                            .font(.footnote.weight(.medium))
                            .foregroundColor(.secondary)
                    }
                    .padding(.horizontal, 18)
                    .padding(.vertical, 16)
                    .frame(maxWidth: .infinity)
                    .foregroundColor(tint)
                    .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24)
                }
                .buttonStyle(.plain)
            }
        }
    }

    fileprivate func meterSheetButton(icon: String, title: String, tint: Color, action: @escaping () -> Void) -> some View {
        Button(action: action) {
            VStack(spacing: 10) {
                Image(systemName: icon)
                    .font(.system(size: 20, weight: .semibold))
                    .frame(width: 40, height: 40)
                    .background(Circle().fill(tint.opacity(0.14)))
                Text(title)
                    .font(.footnote.weight(.semibold))
                    .multilineTextAlignment(.center)
                    .lineLimit(2)
                    .minimumScaleFactor(0.9)
            }
            .foregroundColor(tint)
            .frame(maxWidth: .infinity, minHeight: 106)
            .padding(.horizontal, 8)
            .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.22)
        }
        .buttonStyle(.plain)
    }

    private var statusText: String {
        switch meter.operationalState {
        case .notPresent:
            return "Missing"
        case .peripheralNotConnected:
            return "Ready"
        case .peripheralConnectionPending:
            return "Connecting"
        case .peripheralConnected:
            return "Linked"
        case .peripheralReady:
            return "Preparing"
        case .comunicating:
            return "Syncing"
        case .dataIsAvailable:
            return "Live"
        }
    }

    private var statusColor: Color {
        Meter.operationalColor(for: meter.operationalState)
    }
}

private struct MeterInfoView: View {
    @EnvironmentObject private var meter: Meter

    var body: some View {
        ScrollView {
            VStack(spacing: 14) {
                MeterInfoCard(title: "Overview", tint: meter.color) {
                    MeterInfoRow(label: "Name", value: meter.name)
                    MeterInfoRow(label: "Displayed Model", value: meter.deviceModelSummary)
                    MeterInfoRow(label: "Advertised Model", value: meter.modelString)
                    MeterInfoRow(label: "Working Voltage", value: meter.documentedWorkingVoltage)
                    MeterInfoRow(label: "Temperature Unit", value: meter.temperatureUnitDescription)
                }

                MeterInfoCard(title: "Identifiers", tint: .blue) {
                    MeterInfoRow(label: "MAC", value: meter.btSerial.macAddress.description)
                    if meter.modelNumber != 0 {
                        MeterInfoRow(label: "Model Code", value: "\(meter.modelNumber)")
                    }
                }

                MeterInfoCard(title: "Screen Reporting", tint: .orange) {
                    if meter.reportsCurrentScreenIndex {
                        MeterInfoRow(label: "Current Screen", value: meter.currentScreenDescription)
                        Text("The active screen index is reported by the meter and mapped by the app to a known label.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                    } else {
                        MeterInfoRow(label: "Current Screen", value: "Not Reported")
                        Text("The current screen is not reported by the device payload, or we have not yet identified where and how the protocol announces it.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                    }
                }

                if meter.operationalState == .dataIsAvailable {
                    MeterInfoCard(title: "Live Device Details", tint: .indigo) {
                        if !meter.firmwareVersion.isEmpty {
                            MeterInfoRow(label: "Firmware", value: meter.firmwareVersion)
                        }
                        if meter.serialNumber != 0 {
                            MeterInfoRow(label: "Serial", value: "\(meter.serialNumber)")
                        }
                        if meter.bootCount != 0 {
                            MeterInfoRow(label: "Boot Count", value: "\(meter.bootCount)")
                        }
                    }
                } else {
                    MeterInfoCard(title: "Live Device Details", tint: .secondary) {
                        Text("Connect to the meter to load firmware, serial, and boot details.")
                            .font(.footnote)
                            .foregroundColor(.secondary)
                    }
                }
            }
            .padding()
        }
        .background(
            LinearGradient(
                colors: [meter.color.opacity(0.14), Color.clear],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
        .navigationBarTitle("Meter Info")
        .navigationBarItems(trailing: RSSIView(RSSI: meter.btSerial.RSSI).frame(width: 24))
    }
}

private struct MeterInfoCard<Content: View>: View {
    let title: String
    let tint: Color
    @ViewBuilder var content: Content

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text(title)
                .font(.headline)
            content
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(18)
        .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24)
    }
}

private struct MeterInfoRow: View {
    let label: String
    let value: String

    var body: some View {
        HStack {
            Text(label)
            Spacer()
            Text(value)
                .foregroundColor(.secondary)
                .multilineTextAlignment(.trailing)
        }
        .font(.footnote)
    }
}