ShortcutsConfigStateModel.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // ShortcutsConfigStateModel.swift
  3. // FreeAPS
  4. //
  5. // Created by Pierre LAGARDE on 01/05/2024.
  6. //
  7. import SwiftUI
  8. extension ShortcutsConfig {
  9. final class StateModel: BaseStateModel<Provider> {
  10. @Published var units: GlucoseUnits = .mgdL
  11. @Published var allowBolusByShortcuts: Bool = false
  12. @Published var maxBolusByShortcuts: BolusShortcutLimit = .notAllowed
  13. override func subscribe() {
  14. units = settingsManager.settings.units
  15. subscribeSetting(\.bolusShortcut, on: $maxBolusByShortcuts) {
  16. maxBolusByShortcuts = ($0 == .notAllowed) ? .limitBolusMax : $0
  17. allowBolusByShortcuts = ($0 != .notAllowed)
  18. }
  19. $allowBolusByShortcuts.receive(on: DispatchQueue.main)
  20. .sink { [weak self] value in
  21. if !value {
  22. // the bolus is not allowed
  23. self?.settingsManager.settings.bolusShortcut = .notAllowed
  24. } else {
  25. if let bs = self?.maxBolusByShortcuts {
  26. self?.settingsManager.settings.bolusShortcut = bs
  27. } else {
  28. self?.settingsManager.settings.bolusShortcut = .limitBolusMax
  29. }
  30. }
  31. }
  32. .store(in: &lifetime)
  33. }
  34. }
  35. }
  36. extension ShortcutsConfig.StateModel: SettingsObserver {
  37. func settingsDidChange(_: FreeAPSSettings) {
  38. units = settingsManager.settings.units
  39. }
  40. }