BLEDeviceSelectionView.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // LoopFollow
  2. // BLEDeviceSelectionView.swift
  3. import SwiftUI
  4. struct BLEDeviceSelectionView: View {
  5. @ObservedObject var bleManager: BLEManager
  6. var selectedFilter: BackgroundRefreshType
  7. var onSelectDevice: (BLEDevice) -> Void
  8. var body: some View {
  9. VStack {
  10. List {
  11. let filteredDevices = bleManager.devices.filter { selectedFilter.matches($0) && !isSelected($0) }
  12. if filteredDevices.isEmpty {
  13. Text("No devices found yet. They'll appear here when discovered.")
  14. .foregroundColor(.secondary)
  15. .multilineTextAlignment(.center)
  16. .padding()
  17. } else {
  18. ForEach(filteredDevices, id: \.id) { device in
  19. HStack {
  20. VStack(alignment: .leading) {
  21. Text(device.name ?? "Unknown")
  22. Text("RSSI: \(device.rssi) dBm")
  23. .foregroundColor(.secondary)
  24. .font(.footnote)
  25. if let offset = BLEManager.shared.expectedSensorFetchOffsetString(for: device) {
  26. Text("Expected bg delay: \(offset)")
  27. .foregroundColor(.secondary)
  28. .font(.footnote)
  29. }
  30. }
  31. Spacer()
  32. }
  33. .contentShape(Rectangle())
  34. .onTapGesture {
  35. onSelectDevice(device)
  36. }
  37. }
  38. }
  39. }
  40. }
  41. .onAppear {
  42. bleManager.startScanning()
  43. }
  44. .onDisappear {
  45. bleManager.stopScanning()
  46. }
  47. }
  48. private func isSelected(_ device: BLEDevice) -> Bool {
  49. guard let selectedDevice = Storage.shared.selectedBLEDevice.value else {
  50. return false
  51. }
  52. return selectedDevice.id == device.id
  53. }
  54. }