RemoteSettingsViewModel.swift 3.0 KB

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