Newer Older
65 lines | 2.318kb
Bogdan Timofte authored a week ago
1
//
2
//  ConnectionPrimaryActionView.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 ConnectionPrimaryActionView: View {
13
    let operationalState: Meter.OperationalState
14
    let compact: Bool
15
    let connectAction: () -> Void
16
    let disconnectAction: () -> Void
17

            
18
    private var connected: Bool {
19
        operationalState >= .peripheralConnectionPending
20
    }
21

            
22
    private var actionTint: Color {
23
        connected ? Color(red: 0.66, green: 0.39, blue: 0.35) : Color(red: 0.20, green: 0.46, blue: 0.43)
24
    }
25

            
26
    var body: some View {
27
        Group {
28
            if operationalState == .notPresent {
29
                HStack(spacing: 10) {
30
                    Image(systemName: "exclamationmark.triangle.fill")
31
                        .foregroundColor(.orange)
32
                    Text("Not found at this time.")
33
                        .fontWeight(.semibold)
34
                    Spacer()
35
                }
36
                .padding(compact ? 12 : 16)
37
                .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.18)
38
            } else {
39
                Button(action: {
40
                    if connected {
41
                        disconnectAction()
42
                    } else {
43
                        connectAction()
44
                    }
45
                }) {
46
                    HStack(spacing: 12) {
47
                        Image(systemName: connected ? "xmark.circle.fill" : "bolt.horizontal.circle.fill")
48
                            .foregroundColor(actionTint)
49
                            .frame(width: 30, height: 30)
50
                            .background(Circle().fill(actionTint.opacity(0.12)))
51
                        Text(connected ? "Disconnect" : "Connect")
52
                            .fontWeight(.semibold)
53
                            .foregroundColor(.primary)
54
                        Spacer()
55
                    }
56
                    .padding(.horizontal, 18)
57
                    .padding(.vertical, compact ? 10 : 14)
58
                    .frame(maxWidth: .infinity)
59
                    .meterCard(tint: actionTint, fillOpacity: 0.14, strokeOpacity: 0.20)
60
                }
61
                .buttonStyle(.plain)
62
            }
63
        }
64
    }
65
}