| 1 |
// |
|
| 2 |
// MeterConnectionActionView.swift |
|
| 3 |
// USB Meter |
|
| 4 |
// |
|
| 5 |
// Created by Bogdan Timofte on 29/03/2026. |
|
| 6 |
// Co-authored-by: GPT-5.3-Codex. |
|
| 7 |
// Copyright © 2026 Bogdan Timofte. All rights reserved. |
|
| 8 |
// |
|
| 9 | ||
| 10 |
import SwiftUI |
|
| 11 | ||
| 12 |
struct MeterConnectionActionView: View {
|
|
| 13 |
let operationalState: Meter.OperationalState |
|
| 14 |
let compact: Bool |
|
| 15 | ||
| 16 |
var body: some View {
|
|
| 17 |
if operationalState == .notPresent {
|
|
| 18 |
HStack(spacing: 10) {
|
|
| 19 |
Image(systemName: "exclamationmark.triangle.fill") |
|
| 20 |
.foregroundColor(.orange) |
|
| 21 |
Text("Not found at this time.")
|
|
| 22 |
.fontWeight(.semibold) |
|
| 23 |
Spacer() |
|
| 24 |
} |
|
| 25 |
.padding(compact ? 12 : 16) |
|
| 26 |
.meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.18) |
|
| 27 |
} |
|
| 28 |
} |
|
| 29 |
} |
|
| 30 | ||
| 31 |
struct MeterConnectionToolbarButton: View {
|
|
| 32 |
let operationalState: Meter.OperationalState |
|
| 33 |
let showsTitle: Bool |
|
| 34 |
let connectAction: () -> Void |
|
| 35 |
let disconnectAction: () -> Void |
|
| 36 | ||
| 37 |
private var connected: Bool {
|
|
| 38 |
operationalState >= .peripheralConnectionPending |
|
| 39 |
} |
|
| 40 | ||
| 41 |
private var actionTint: Color {
|
|
| 42 |
connected ? Color(red: 0.66, green: 0.39, blue: 0.35) : Color(red: 0.20, green: 0.46, blue: 0.43) |
|
| 43 |
} |
|
| 44 | ||
| 45 |
private var title: String {
|
|
| 46 |
connected ? "Disconnect" : "Connect" |
|
| 47 |
} |
|
| 48 | ||
| 49 |
private var systemImage: String {
|
|
| 50 |
connected ? "link.badge.minus" : "bolt.horizontal.circle.fill" |
|
| 51 |
} |
|
| 52 | ||
| 53 |
var body: some View {
|
|
| 54 |
if operationalState != .notPresent {
|
|
| 55 |
Button(action: {
|
|
| 56 |
if connected {
|
|
| 57 |
disconnectAction() |
|
| 58 |
} else {
|
|
| 59 |
connectAction() |
|
| 60 |
} |
|
| 61 |
}) {
|
|
| 62 |
if showsTitle {
|
|
| 63 |
Label(title, systemImage: systemImage) |
|
| 64 |
} else {
|
|
| 65 |
Image(systemName: systemImage) |
|
| 66 |
} |
|
| 67 |
} |
|
| 68 |
.foregroundStyle(actionTint) |
|
| 69 |
.accessibilityLabel(title) |
|
| 70 |
.help(title) |
|
| 71 |
} |
|
| 72 |
} |
|
| 73 |
} |