BackgroundRefreshSettingsView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // BackgroundRefreshSettingsView.swift
  3. // LoopFollow
  4. //
  5. import SwiftUI
  6. struct BackgroundRefreshSettingsView: View {
  7. @ObservedObject var viewModel: BackgroundRefreshSettingsViewModel
  8. @Environment(\.presentationMode) var presentationMode
  9. @ObservedObject var bleManager = BLEManager.shared
  10. var body: some View {
  11. NavigationView {
  12. Form {
  13. refreshTypeSection
  14. if viewModel.backgroundRefreshType.isBluetooth {
  15. selectedDeviceSection
  16. availableDevicesSection
  17. }
  18. }
  19. .navigationBarTitle("Background Refresh Settings", displayMode: .inline)
  20. .toolbar {
  21. ToolbarItem(placement: .navigationBarTrailing) {
  22. Button("Done") {
  23. presentationMode.wrappedValue.dismiss()
  24. }
  25. }
  26. }
  27. }
  28. }
  29. // MARK: - Subviews / Computed Properties
  30. private var refreshTypeSection: some View {
  31. Section {
  32. Picker("Background Refresh Type", selection: $viewModel.backgroundRefreshType) {
  33. ForEach(BackgroundRefreshType.allCases, id: \.self) { type in
  34. Text(type.rawValue).tag(type)
  35. }
  36. }
  37. .pickerStyle(MenuPickerStyle())
  38. Text("Adjust the background refresh type.")
  39. .font(.footnote)
  40. .foregroundColor(.secondary)
  41. }
  42. }
  43. @ViewBuilder
  44. private var selectedDeviceSection: some View {
  45. if let storedDevice = Storage.shared.selectedBLEDevice.value {
  46. Section(header: Text("Selected Device")) {
  47. VStack(alignment: .leading, spacing: 4) {
  48. Text(storedDevice.name ?? "Unknown Device")
  49. .font(.headline)
  50. deviceConnectionStatus(for: storedDevice)
  51. HStack {
  52. Spacer()
  53. Button(action: {
  54. bleManager.disconnect()
  55. }) {
  56. Text("Disconnect")
  57. .foregroundColor(.blue)
  58. }
  59. .buttonStyle(BorderlessButtonStyle())
  60. Spacer()
  61. }
  62. }
  63. .padding(.vertical, 8)
  64. }
  65. }
  66. }
  67. private var availableDevicesSection: some View {
  68. Section(header: Text("Available Devices")) {
  69. BLEDeviceSelectionView(
  70. bleManager: bleManager,
  71. selectedFilter: viewModel.backgroundRefreshType,
  72. onSelectDevice: { device in
  73. bleManager.connect(device: device)
  74. }
  75. )
  76. }
  77. }
  78. private func deviceConnectionStatus(for device: BLEDevice) -> some View {
  79. if device.isConnected {
  80. return Text("Connected")
  81. .foregroundColor(.green)
  82. } else if let lastConnected = device.lastConnected {
  83. let date = dateTimeUtils.formattedDate(from: lastConnected)
  84. return Text("Last connection: \(date)")
  85. .foregroundColor(.orange)
  86. } else if let item = bleManager.devices.first(where: { $0.id == device.id }) {
  87. let date = dateTimeUtils.formattedDate(from: item.lastSeen)
  88. return Text("Last seen: \(date)")
  89. .foregroundColor(.orange)
  90. } else {
  91. return Text("Not found")
  92. .foregroundColor(.red)
  93. }
  94. }
  95. }