WatchConfigStateModel.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Combine
  2. import ConnectIQ
  3. import SwiftUI
  4. extension WatchConfig {
  5. final class StateModel: BaseStateModel<Provider> {
  6. @Injected() private var garmin: GarminManager!
  7. @Published var units: GlucoseUnits = .mgdL
  8. @Published var devices: [IQDevice] = []
  9. @Published var confirmBolusFaster = false
  10. /// Garmin watch settings containing all watch-related configuration
  11. @Published var garminSettings = GarminWatchSettings()
  12. private(set) var preferences = Preferences()
  13. override func subscribe() {
  14. preferences = provider.preferences
  15. units = settingsManager.settings.units
  16. // Subscribe to the entire garminSettings struct from TrioSettings
  17. subscribeSetting(\.garminSettings, on: $garminSettings) { garminSettings = $0 }
  18. subscribeSetting(\.confirmBolusFaster, on: $confirmBolusFaster) { confirmBolusFaster = $0 }
  19. devices = garmin.devices
  20. }
  21. /// Prompts the user to select Garmin devices and updates the device list
  22. func selectGarminDevices() {
  23. garmin.selectDevices()
  24. .receive(on: DispatchQueue.main)
  25. .weakAssign(to: \.devices, on: self)
  26. .store(in: &lifetime)
  27. }
  28. /// Updates the Garmin manager with the current device list
  29. func deleteGarminDevice() {
  30. garmin.updateDeviceList(devices)
  31. }
  32. /// Handles watchface selection changes by automatically disabling data transmission
  33. /// to allow the user to switch watchfaces on their Garmin device without data conflicts
  34. func handleWatchfaceChange() {
  35. garminSettings.isWatchfaceDataEnabled = false
  36. }
  37. /// Resumes data transmission after user confirms they have switched watchface on their device
  38. func resumeDataTransmission() {
  39. garminSettings.isWatchfaceDataEnabled = true
  40. }
  41. }
  42. }
  43. extension WatchConfig.StateModel: SettingsObserver {
  44. func settingsDidChange(_: TrioSettings) {
  45. units = settingsManager.settings.units
  46. }
  47. }