Newer Older
138 lines | 5.516kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  MeterView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 04/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8
// MARK: Parent frame https://stackoverflow.com/questions/56832865/how-to-access-parents-frame-in-swiftui
9

            
10
import SwiftUI
11
import CoreBluetooth
12

            
13
struct MeterView: View {
14

            
15
    @EnvironmentObject private var meter: Meter
16

            
17
    @State var dataGroupsViewVisibility: Bool = false
Bogdan Timofte authored 2 weeks ago
18
    @State var recordingViewVisibility: Bool = false
Bogdan Timofte authored 2 weeks ago
19
    @State var measurementsViewVisibility: Bool = false
20
    private var myBounds: CGRect { UIScreen.main.bounds }
21

            
22
    var body: some View {
23
        ScrollView {
24
            VStack (alignment: .center, spacing: 10) {
25
                // MARK: Name
26
                VStack {
27
                    Text("Meter")
28
                        .font(.headline)
29
                    Text("\(meter.name)")
30
                }
31
                .padding()
32
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(meter.color).opacity(0.1))
33
                // MARK: Mac
34
                VStack {
35
                    Text("MAC")
36
                        .font(.headline)
37
                    Text("\(meter.btSerial.macAddress.description)")
38
                }
39
                .padding()
40
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(.secondary).opacity(0.1))
41
            }
42
            // MARK: Connect/Disconnect
43
            connectionControlButton()
44
            // MARK: Show Data
45
            if ( meter.operationalState ==  .dataIsAvailable) {
Bogdan Timofte authored 2 weeks ago
46
                Text("Model: \(meter.deviceModelSummary)")
Bogdan Timofte authored 2 weeks ago
47
                HStack(spacing: 24) {
Bogdan Timofte authored 2 weeks ago
48
                    meterSheetButton(icon: "map", title: meter.dataGroupsTitle) {
Bogdan Timofte authored 2 weeks ago
49
                        dataGroupsViewVisibility.toggle()
50
                    }
51
                    .sheet(isPresented: self.$dataGroupsViewVisibility) {
52
                        DataGroupsView(visibility: self.$dataGroupsViewVisibility)
53
                            .environmentObject(self.meter)
Bogdan Timofte authored 2 weeks ago
54
                    }
Bogdan Timofte authored 2 weeks ago
55

            
56
                    if meter.supportsRecordingView {
Bogdan Timofte authored 2 weeks ago
57
                        meterSheetButton(icon: "record.circle", title: "Charge Record") {
Bogdan Timofte authored 2 weeks ago
58
                            recordingViewVisibility.toggle()
59
                        }
60
                        .sheet(isPresented: self.$recordingViewVisibility) {
61
                            RecordingView(visibility: self.$recordingViewVisibility)
62
                                .environmentObject(self.meter)
Bogdan Timofte authored 2 weeks ago
63
                        }
64
                    }
Bogdan Timofte authored 2 weeks ago
65

            
Bogdan Timofte authored 2 weeks ago
66
                    meterSheetButton(icon: "recordingtape", title: "App History") {
Bogdan Timofte authored 2 weeks ago
67
                        measurementsViewVisibility.toggle()
68
                    }
69
                    .sheet(isPresented: self.$measurementsViewVisibility) {
70
                        MeasurementsView(visibility: self.$measurementsViewVisibility)
71
                            .environmentObject(self.meter.measurements)
72
                    }
Bogdan Timofte authored 2 weeks ago
73
                }
74
                if self.meter.measurements.power.context.isValid {
75
                    MeasurementChartView()
76
                    .environmentObject(self.meter.measurements)
77
                        .frame(minHeight: myBounds.height/3)
78
                }
79
                ControlView()
80
                LiveView()
81
            }
82
        }
83
        //.frame(minWidth: 0, maxWidth: .greatestFiniteMagnitude, minHeight: 0, maxHeight: .greatestFiniteMagnitude)
84
        .navigationBarTitle("Meter")
85
        .navigationBarItems(trailing: HStack (spacing: 0) {
86
            if meter.operationalState > .notPresent {
87
                RSSIView(RSSI: meter.btSerial.RSSI)
88
                    .frame(width: 24)
89
                    .padding(.vertical)
90
            }
91
            NavigationLink(destination: MeterSettingsView().environmentObject(meter)) {
92
                Image( systemName: "gear" )
93
                    //.imageScale(.large)
94
                    .padding(.vertical)
95
                    .padding(.leading)
96
                //.background(RoundedRectangle(cornerRadius: 20)).opacity(0.05)
97
            }
98
        })
99
    }
100

            
101
    fileprivate func connectionControlButton() -> some View {
102
        /*
103
         MARK: De adaugat si celelalte situatii
104
         case peripheralNotConnected
105
         case peripheralConnectionPending
106
         case peripheralConnected
107
         case ready
108
         */
109
        let buttonColor = meter.operationalState > .peripheralNotConnected ? Color.red : Color.green
110
        return Group {
111
            if meter.operationalState == .notPresent {
112
                Text("Not found at this time.").foregroundColor(.red)
113
            } else {
114

            
115
                HStack {
116
                    if meter.operationalState < .peripheralConnectionPending {
117
                        Button (action: { self.meter.connect() } ) { Text("Connect") }
118
                    } else {
119
                        Button (action: { self.meter.disconnect() } ) { Text("Disconnect") }
120
                    }
121
                }
122
                .padding()
123
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(buttonColor).opacity(0.1))
124
                .frame(maxWidth: .greatestFiniteMagnitude)
125
            }
126
        }
127

            
128
    }
Bogdan Timofte authored 2 weeks ago
129

            
130
    fileprivate func meterSheetButton(icon: String, title: String, action: @escaping () -> Void) -> some View {
131
        Button(action: action) {
132
            VStack {
133
                Image(systemName: icon)
134
                Text(title)
135
            }
136
        }
137
    }
Bogdan Timofte authored 2 weeks ago
138
}