UserInterfaceSettingsStateModel.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import SwiftUI
  2. extension UserInterfaceSettings {
  3. final class StateModel: BaseStateModel<Provider> {
  4. @Published var overrideHbA1cUnit = false
  5. @Published var low: Decimal = 70
  6. @Published var high: Decimal = 180
  7. @Published var hours: Decimal = 6
  8. @Published var xGridLines = false
  9. @Published var yGridLines: Bool = false
  10. @Published var oneDimensionalGraph = false
  11. @Published var rulerMarks: Bool = true
  12. @Published var totalInsulinDisplayType: TotalInsulinDisplayType = .totalDailyDose
  13. @Published var showCarbsRequiredBadge: Bool = true
  14. @Published var carbsRequiredThreshold: Decimal = 0
  15. var units: GlucoseUnits = .mgdL
  16. override func subscribe() {
  17. let units = settingsManager.settings.units
  18. self.units = units
  19. subscribeSetting(\.overrideHbA1cUnit, on: $overrideHbA1cUnit) { overrideHbA1cUnit = $0 }
  20. subscribeSetting(\.xGridLines, on: $xGridLines) { xGridLines = $0 }
  21. subscribeSetting(\.yGridLines, on: $yGridLines) { yGridLines = $0 }
  22. subscribeSetting(\.rulerMarks, on: $rulerMarks) { rulerMarks = $0 }
  23. subscribeSetting(\.oneDimensionalGraph, on: $oneDimensionalGraph) { oneDimensionalGraph = $0 }
  24. subscribeSetting(\.totalInsulinDisplayType, on: $totalInsulinDisplayType) { totalInsulinDisplayType = $0 }
  25. subscribeSetting(\.low, on: $low, initial: {
  26. let value = max(min($0, 90), 40)
  27. low = units == .mmolL ? value.asMmolL : value
  28. }, map: {
  29. guard units == .mmolL else { return $0 }
  30. return $0.asMgdL
  31. })
  32. subscribeSetting(\.high, on: $high, initial: {
  33. let value = max(min($0, 270), 110)
  34. high = units == .mmolL ? value.asMmolL : value
  35. }, map: {
  36. guard units == .mmolL else { return $0 }
  37. return $0.asMgdL
  38. })
  39. subscribeSetting(\.showCarbsRequiredBadge, on: $showCarbsRequiredBadge) { showCarbsRequiredBadge = $0 }
  40. subscribeSetting(
  41. \.carbsRequiredThreshold,
  42. on: $carbsRequiredThreshold
  43. ) { carbsRequiredThreshold = $0 }
  44. }
  45. }
  46. }
  47. enum TotalInsulinDisplayType: String, JSON, CaseIterable, Identifiable, Codable, Hashable {
  48. var id: String { rawValue }
  49. case totalDailyDose
  50. case totalInsulinInScope
  51. var displayName: String {
  52. switch self {
  53. case .totalDailyDose:
  54. return NSLocalizedString("Total Daily Dose", comment: "")
  55. case .totalInsulinInScope:
  56. return NSLocalizedString("Total Insulin in Scope", comment: "")
  57. }
  58. }
  59. }