ShortcutsConfigStateModel.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 allowBolusByShortcuts: Bool = false
  11. @Published var maxBolusByShortcuts: BolusShortcutLimit = .notAllowed
  12. override func subscribe() {
  13. subscribeSetting(\.bolusShortcut, on: $maxBolusByShortcuts) {
  14. maxBolusByShortcuts = ($0 == .notAllowed) ? .limitBolusMax : $0
  15. allowBolusByShortcuts = ($0 != .notAllowed)
  16. }
  17. $allowBolusByShortcuts.receive(on: DispatchQueue.main)
  18. .sink { [weak self] value in
  19. if !value {
  20. // the bolus is not allowed
  21. self?.settingsManager.settings.bolusShortcut = .notAllowed
  22. } else {
  23. if let bs = self?.maxBolusByShortcuts {
  24. self?.settingsManager.settings.bolusShortcut = bs
  25. } else {
  26. self?.settingsManager.settings.bolusShortcut = .limitBolusMax
  27. }
  28. }
  29. }
  30. .store(in: &lifetime)
  31. }
  32. }
  33. }