RemoteSettingsViewModel.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // RemoteSettingsViewModel.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-08-25.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import Combine
  10. import HealthKit
  11. class RemoteSettingsViewModel: ObservableObject {
  12. @Published var remoteType: RemoteType
  13. @Published var user: String
  14. @Published var sharedSecret: String
  15. @Published var apnsKey: String
  16. @Published var keyId: String
  17. @Published var maxBolus: HKQuantity
  18. @Published var maxCarbs: HKQuantity
  19. @Published var maxProtein: HKQuantity
  20. @Published var maxFat: HKQuantity
  21. @Published var mealWithBolus: Bool
  22. @Published var mealWithFatProtein: Bool
  23. @Published var isTrioDevice: Bool = (ObservableUserDefaults.shared.device.value == "Trio")
  24. private var storage = Storage.shared
  25. private var cancellables = Set<AnyCancellable>()
  26. init() {
  27. self.remoteType = storage.remoteType.value
  28. self.user = storage.user.value
  29. self.sharedSecret = storage.sharedSecret.value
  30. self.apnsKey = storage.apnsKey.value
  31. self.keyId = storage.keyId.value
  32. self.maxBolus = storage.maxBolus.value
  33. self.maxCarbs = storage.maxCarbs.value
  34. self.maxProtein = storage.maxProtein.value
  35. self.maxFat = storage.maxFat.value
  36. self.mealWithBolus = storage.mealWithBolus.value
  37. self.mealWithFatProtein = storage.mealWithFatProtein.value
  38. setupBindings()
  39. }
  40. private func setupBindings() {
  41. $remoteType
  42. .sink { [weak self] in self?.storage.remoteType.value = $0 }
  43. .store(in: &cancellables)
  44. $user
  45. .sink { [weak self] in self?.storage.user.value = $0 }
  46. .store(in: &cancellables)
  47. $sharedSecret
  48. .sink { [weak self] in self?.storage.sharedSecret.value = $0 }
  49. .store(in: &cancellables)
  50. $apnsKey
  51. .sink { [weak self] in self?.storage.apnsKey.value = $0 }
  52. .store(in: &cancellables)
  53. $keyId
  54. .sink { [weak self] in self?.storage.keyId.value = $0 }
  55. .store(in: &cancellables)
  56. $maxBolus
  57. .sink { [weak self] in self?.storage.maxBolus.value = $0 }
  58. .store(in: &cancellables)
  59. $maxCarbs
  60. .sink { [weak self] in self?.storage.maxCarbs.value = $0 }
  61. .store(in: &cancellables)
  62. $maxProtein
  63. .sink { [weak self] in self?.storage.maxProtein.value = $0 }
  64. .store(in: &cancellables)
  65. $maxFat
  66. .sink { [weak self] in self?.storage.maxFat.value = $0 }
  67. .store(in: &cancellables)
  68. $mealWithBolus
  69. .sink { [weak self] in self?.storage.mealWithBolus.value = $0 }
  70. .store(in: &cancellables)
  71. $mealWithFatProtein
  72. .sink { [weak self] in self?.storage.mealWithFatProtein.value = $0 }
  73. .store(in: &cancellables)
  74. ObservableUserDefaults.shared.device.$value
  75. .receive(on: DispatchQueue.main )
  76. .sink { [weak self] newValue in
  77. self?.isTrioDevice = (newValue == "Trio")
  78. }
  79. .store(in: &cancellables)
  80. }
  81. }