1 contributor
65 lines | 2.318kb
//
//  ConnectionPrimaryActionView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 29/03/2026.
//  Co-authored-by: GPT-5.3-Codex.
//  Copyright © 2026 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct ConnectionPrimaryActionView: View {
    let operationalState: Meter.OperationalState
    let compact: Bool
    let connectAction: () -> Void
    let disconnectAction: () -> Void

    private var connected: Bool {
        operationalState >= .peripheralConnectionPending
    }

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

    var body: some View {
        Group {
            if operationalState == .notPresent {
                HStack(spacing: 10) {
                    Image(systemName: "exclamationmark.triangle.fill")
                        .foregroundColor(.orange)
                    Text("Not found at this time.")
                        .fontWeight(.semibold)
                    Spacer()
                }
                .padding(compact ? 12 : 16)
                .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.18)
            } else {
                Button(action: {
                    if connected {
                        disconnectAction()
                    } else {
                        connectAction()
                    }
                }) {
                    HStack(spacing: 12) {
                        Image(systemName: connected ? "xmark.circle.fill" : "bolt.horizontal.circle.fill")
                            .foregroundColor(actionTint)
                            .frame(width: 30, height: 30)
                            .background(Circle().fill(actionTint.opacity(0.12)))
                        Text(connected ? "Disconnect" : "Connect")
                            .fontWeight(.semibold)
                            .foregroundColor(.primary)
                        Spacer()
                    }
                    .padding(.horizontal, 18)
                    .padding(.vertical, compact ? 10 : 14)
                    .frame(maxWidth: .infinity)
                    .meterCard(tint: actionTint, fillOpacity: 0.14, strokeOpacity: 0.20)
                }
                .buttonStyle(.plain)
            }
        }
    }
}