BLEDeviceSelectionView.swift 2.1 KB

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