USB-Meter / USB Meter / Views / Meter / Tabs / Home / Subviews / MeterConnectionActionView.swift
Newer Older
49b9c2a 2 months ago History
80 lines | 2.211kb
Bogdan Timofte authored 2 months ago
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 {
Bogdan Timofte authored 2 months ago
50
        if connected {
51
            #if targetEnvironment(macCatalyst)
52
            return "bolt.slash.circle.fill"
53
            #else
54
            return "link.badge.minus"
55
            #endif
56
        }
57
        return "bolt.horizontal.circle.fill"
Bogdan Timofte authored 2 months ago
58
    }
59

            
60
    var body: some View {
61
        if operationalState != .notPresent {
62
            Button(action: {
63
                if connected {
64
                    disconnectAction()
65
                } else {
66
                    connectAction()
67
                }
68
            }) {
69
                if showsTitle {
70
                    Label(title, systemImage: systemImage)
71
                } else {
72
                    Image(systemName: systemImage)
73
                }
74
            }
75
            .foregroundStyle(actionTint)
76
            .accessibilityLabel(title)
77
            .help(title)
78
        }
79
    }
80
}