EIS-BLE-S3/cue-ios/CueIOS/Views/ConnectionView.swift

88 lines
2.8 KiB
Swift

import SwiftUI
struct ConnectionView: View {
var state: AppState
var body: some View {
VStack(spacing: 0) {
header
Divider()
deviceList
}
.navigationTitle("Connection")
}
private var header: some View {
HStack {
Circle()
.fill(statusColor)
.frame(width: 10, height: 10)
Text(state.ble.state.rawValue)
.font(.headline)
Spacer()
if state.ble.state == .connected {
Button("Disconnect") { state.ble.disconnect() }
.buttonStyle(.bordered)
.tint(.red)
} else if state.ble.state == .scanning {
Button("Stop") { state.ble.stopScanning() }
.buttonStyle(.bordered)
} else {
Button("Scan") { state.ble.startScanning() }
.buttonStyle(.borderedProminent)
}
}
.padding()
}
private var statusColor: Color {
switch state.ble.state {
case .connected: .green
case .scanning: .orange
case .connecting: .yellow
case .disconnected: .red
}
}
private var deviceList: some View {
List {
if state.ble.discoveredDevices.isEmpty && state.ble.state == .scanning {
HStack {
ProgressView()
Text("Scanning for devices...")
.foregroundStyle(.secondary)
}
}
ForEach(state.ble.discoveredDevices) { device in
Button {
state.ble.connectTo(device)
} label: {
HStack {
VStack(alignment: .leading, spacing: 2) {
Text(device.name)
.font(.body.weight(.medium))
if !device.serviceUUIDs.isEmpty {
Text(device.serviceUUIDs.map(\.uuidString).joined(separator: ", "))
.font(.caption2)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
Spacer()
Text("\(device.rssi) dBm")
.font(.caption)
.foregroundStyle(.secondary)
if device.name == "EIS4" {
Image(systemName: "star.fill")
.foregroundStyle(.yellow)
.font(.caption)
}
}
}
.disabled(state.ble.state == .connecting)
}
}
}
}