Newer Older
123 lines | 4.981kb
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
18
    @State var measurementsViewVisibility: Bool = false
19
    private var myBounds: CGRect { UIScreen.main.bounds }
20

            
21
    var body: some View {
22
        ScrollView {
23
            VStack (alignment: .center, spacing: 10) {
24
                // MARK: Name
25
                VStack {
26
                    Text("Meter")
27
                        .font(.headline)
28
                    Text("\(meter.name)")
29
                }
30
                .padding()
31
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(meter.color).opacity(0.1))
32
                // MARK: Mac
33
                VStack {
34
                    Text("MAC")
35
                        .font(.headline)
36
                    Text("\(meter.btSerial.macAddress.description)")
37
                }
38
                .padding()
39
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(.secondary).opacity(0.1))
40
            }
41
            // MARK: Connect/Disconnect
42
            connectionControlButton()
43
            // MARK: Show Data
44
            if ( meter.operationalState ==  .dataIsAvailable) {
45
                Text("Model: \(meter.modelString) - (\(meter.modelNumber))")
46
                HStack {
47
                    Button(action: {self.dataGroupsViewVisibility.toggle()}) {
48
                        VStack {
49
                            Image(systemName: "map")
50
                                .sheet(isPresented: self.$dataGroupsViewVisibility) {
51
                                    DataGroupsView(visibility: self.$dataGroupsViewVisibility)
52
                                        .environmentObject(self.meter)
53
                            }
54
                            Text("ceva")
55
                        }
56
                    }
57
                    Button(action: {self.measurementsViewVisibility.toggle()}) {
58
                        VStack {
59
                            Image(systemName: "recordingtape")
60
                                .sheet(isPresented: self.$measurementsViewVisibility) {
61
                                    MeasurementsView(visibility: self.$measurementsViewVisibility)
62
                                        .environmentObject(self.meter.measurements)
63
                            }
64
                            Text("altceva")
65
                        }
66
                    }
67
                }
68
                if self.meter.measurements.power.context.isValid {
69
                    MeasurementChartView()
70
                    .environmentObject(self.meter.measurements)
71
                        .frame(minHeight: myBounds.height/3)
72
                }
73
                ControlView()
74
                LiveView()
75
            }
76
        }
77
        //.frame(minWidth: 0, maxWidth: .greatestFiniteMagnitude, minHeight: 0, maxHeight: .greatestFiniteMagnitude)
78
        .navigationBarTitle("Meter")
79
        .navigationBarItems(trailing: HStack (spacing: 0) {
80
            if meter.operationalState > .notPresent {
81
                RSSIView(RSSI: meter.btSerial.RSSI)
82
                    .frame(width: 24)
83
                    .padding(.vertical)
84
            }
85
            NavigationLink(destination: MeterSettingsView().environmentObject(meter)) {
86
                Image( systemName: "gear" )
87
                    //.imageScale(.large)
88
                    .padding(.vertical)
89
                    .padding(.leading)
90
                //.background(RoundedRectangle(cornerRadius: 20)).opacity(0.05)
91
            }
92
        })
93
    }
94

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

            
109
                HStack {
110
                    if meter.operationalState < .peripheralConnectionPending {
111
                        Button (action: { self.meter.connect() } ) { Text("Connect") }
112
                    } else {
113
                        Button (action: { self.meter.disconnect() } ) { Text("Disconnect") }
114
                    }
115
                }
116
                .padding()
117
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(buttonColor).opacity(0.1))
118
                .frame(maxWidth: .greatestFiniteMagnitude)
119
            }
120
        }
121

            
122
    }
123
}