USB-Meter / USB Meter / Views / MeterRowView.swift
Newer Older
97 lines | 2.994kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  MeterComunicationView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 05/05/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
11
struct MeterRowView: View {
12

            
13
    @EnvironmentObject private var meter: Meter
14

            
15
    var body: some View {
Bogdan Timofte authored 2 weeks ago
16
        HStack(spacing: 14) {
17
            Image(systemName: "sensor.tag.radiowaves.forward.fill")
18
                .font(.system(size: 18, weight: .semibold))
Bogdan Timofte authored 2 weeks ago
19
                .foregroundColor(meter.color)
Bogdan Timofte authored 2 weeks ago
20
                .frame(width: 42, height: 42)
21
                .background(
22
                    Circle()
Bogdan Timofte authored 2 weeks ago
23
                        .fill(meter.color.opacity(0.18))
Bogdan Timofte authored 2 weeks ago
24
                )
Bogdan Timofte authored 2 weeks ago
25
                .overlay(alignment: .bottomTrailing) {
26
                    Circle()
27
                        .fill(connectivityTint)
28
                        .frame(width: 12, height: 12)
29
                        .overlay(
30
                            Circle()
31
                                .stroke(Color(uiColor: .systemBackground), lineWidth: 2)
32
                        )
33
                }
Bogdan Timofte authored 2 weeks ago
34

            
35
            VStack(alignment: .leading, spacing: 4) {
36
                Text(meter.name)
37
                    .font(.headline)
38
                Text(meter.deviceModelSummary)
39
                    .font(.caption)
40
                    .foregroundColor(.secondary)
41
            }
42

            
43
            Spacer()
44

            
45
            VStack(alignment: .trailing, spacing: 4) {
Bogdan Timofte authored 2 weeks ago
46
                HStack(spacing: 6) {
47
                    Circle()
48
                        .fill(connectivityTint)
49
                        .frame(width: 8, height: 8)
50
                    Text(statusText)
51
                        .font(.caption.weight(.semibold))
52
                        .foregroundColor(.secondary)
53
                }
54
                .padding(.horizontal, 10)
55
                .padding(.vertical, 6)
56
                .background(
57
                    Capsule(style: .continuous)
58
                        .fill(connectivityTint.opacity(0.12))
59
                )
60
                .overlay(
61
                    Capsule(style: .continuous)
62
                        .stroke(connectivityTint.opacity(0.22), lineWidth: 1)
63
                )
Bogdan Timofte authored 2 weeks ago
64
            }
65
        }
66
        .padding(14)
67
        .meterCard(
Bogdan Timofte authored 2 weeks ago
68
            tint: meter.color,
Bogdan Timofte authored 2 weeks ago
69
            fillOpacity: 0.16,
70
            strokeOpacity: 0.22,
71
            cornerRadius: 18
72
        )
73
    }
74

            
Bogdan Timofte authored 2 weeks ago
75
    private var connectivityTint: Color {
76
        Meter.operationalColor(for: meter.operationalState)
77
    }
78

            
Bogdan Timofte authored 2 weeks ago
79
    private var statusText: String {
80
        switch meter.operationalState {
81
        case .notPresent:
82
            return "Missing"
83
        case .peripheralNotConnected:
84
            return "Available"
85
        case .peripheralConnectionPending:
86
            return "Connecting"
87
        case .peripheralConnected:
88
            return "Linked"
89
        case .peripheralReady:
90
            return "Ready"
91
        case .comunicating:
92
            return "Syncing"
93
        case .dataIsAvailable:
94
            return "Live"
Bogdan Timofte authored 2 weeks ago
95
        }
96
    }
97
}