BLEDeviceSelectionView.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if bleManager.devices.filter({ selectedFilter.matches($0) && !isSelected($0) }).isEmpty {
  14. Text("No devices found yet. They'll appear here when discovered.")
  15. .foregroundColor(.secondary)
  16. .multilineTextAlignment(.center)
  17. .padding()
  18. } else {
  19. ForEach(bleManager.devices.filter { selectedFilter.matches($0) && !isSelected($0) }, id: \.id) { device in
  20. HStack {
  21. VStack(alignment: .leading) {
  22. Text(device.name ?? "Unknown")
  23. Text("RSSI: \(device.rssi) dBm")
  24. .foregroundColor(.secondary)
  25. .font(.footnote)
  26. }
  27. Spacer()
  28. }
  29. .contentShape(Rectangle())
  30. .onTapGesture {
  31. onSelectDevice(device)
  32. }
  33. }
  34. }
  35. }
  36. }
  37. .onAppear {
  38. bleManager.startScanning()
  39. }
  40. .onDisappear {
  41. bleManager.stopScanning()
  42. }
  43. }
  44. private func isSelected(_ device: BLEDevice) -> Bool {
  45. guard let selectedDevice = Storage.shared.selectedBLEDevice.value else {
  46. return false
  47. }
  48. return selectedDevice.id == device.id
  49. }
  50. }