Newer Older
81 lines | 3.155kb
Bogdan Timofte authored 2 months ago
1
//
Bogdan Timofte authored 2 months ago
2
//  MeasurementSeriesSheetView.swift
Bogdan Timofte authored 2 months ago
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 13/04/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
Bogdan Timofte authored 2 months ago
11
struct MeasurementSeriesSheetView: View {
Bogdan Timofte authored 2 months ago
12

            
13
    @EnvironmentObject private var measurements: Measurements
14

            
15
    @Binding var visibility: Bool
16

            
17
    var body: some View {
Bogdan Timofte authored 2 months ago
18
        let seriesPoints = measurements.power.samplePoints
19

            
Bogdan Timofte authored 2 months ago
20
        NavigationView {
Bogdan Timofte authored 2 months ago
21
            ScrollView {
22
                VStack(alignment: .leading, spacing: 14) {
23
                    VStack(alignment: .leading, spacing: 8) {
Bogdan Timofte authored a month ago
24
                        HStack {
25
                            Text("Measurement Series")
26
                                .font(.system(.title3, design: .rounded).weight(.bold))
27
                            ContextInfoButton(
28
                                title: "Measurement Series",
29
                                message: "Buffered measurement series captured from the meter for analysis, charts, and correlations."
30
                            )
31
                            Spacer(minLength: 0)
32
                        }
Bogdan Timofte authored 2 months ago
33
                    }
34
                    .padding(18)
35
                    .meterCard(tint: .blue, fillOpacity: 0.18, strokeOpacity: 0.24)
36

            
Bogdan Timofte authored 2 months ago
37
                    if seriesPoints.isEmpty {
38
                        Text("No measurement samples have been captured yet.")
Bogdan Timofte authored 2 months ago
39
                            .font(.footnote)
40
                            .foregroundColor(.secondary)
41
                            .padding(18)
42
                            .meterCard(tint: .secondary, fillOpacity: 0.14, strokeOpacity: 0.20)
43
                    } else {
44
                        LazyVStack(spacing: 12) {
Bogdan Timofte authored 2 months ago
45
                            ForEach(seriesPoints) { point in
46
                                MeasurementSeriesSampleView(
Bogdan Timofte authored 2 months ago
47
                                    power: point,
48
                                    voltage: measurements.voltage.points[point.id],
Bogdan Timofte authored 2 months ago
49
                                    current: measurements.current.points[point.id],
50
                                    energy: energyPoint(for: point.timestamp)
Bogdan Timofte authored 2 months ago
51
                                )
52
                            }
Bogdan Timofte authored 2 months ago
53
                        }
54
                    }
55
                }
Bogdan Timofte authored 2 months ago
56
                .padding()
Bogdan Timofte authored 2 months ago
57
            }
Bogdan Timofte authored 2 months ago
58
            .background(
59
                LinearGradient(
60
                    colors: [.blue.opacity(0.14), Color.clear],
61
                    startPoint: .topLeading,
62
                    endPoint: .bottomTrailing
63
                )
64
                .ignoresSafeArea()
65
            )
66
            .navigationBarItems(
67
                leading: Button("Done") { visibility.toggle() },
Bogdan Timofte authored 2 months ago
68
                trailing: Button("Reset Series") {
69
                    measurements.resetSeries()
Bogdan Timofte authored 2 months ago
70
                }
71
                .foregroundColor(.red)
72
            )
Bogdan Timofte authored 2 months ago
73
            .navigationBarTitle("Measurement Series", displayMode: .inline)
Bogdan Timofte authored 2 months ago
74
        }
75
        .navigationViewStyle(StackNavigationViewStyle())
76
    }
Bogdan Timofte authored 2 months ago
77

            
78
    private func energyPoint(for timestamp: Date) -> Measurements.Measurement.Point? {
79
        measurements.energy.samplePoints.last { $0.timestamp == timestamp }
80
    }
Bogdan Timofte authored 2 months ago
81
}