GraphSettingsView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // LoopFollow
  2. // GraphSettingsView.swift
  3. import SwiftUI
  4. struct GraphSettingsView: View {
  5. @ObservedObject private var showDots = Storage.shared.showDots
  6. @ObservedObject private var showLines = Storage.shared.showLines
  7. @ObservedObject private var showValues = Storage.shared.showValues
  8. @ObservedObject private var showAbsorption = Storage.shared.showAbsorption
  9. @ObservedObject private var showDIALines = Storage.shared.showDIALines
  10. @ObservedObject private var show30MinLine = Storage.shared.show30MinLine
  11. @ObservedObject private var show90MinLine = Storage.shared.show90MinLine
  12. @ObservedObject private var showMidnightLines = Storage.shared.showMidnightLines
  13. @ObservedObject private var showYesterdayLine = Storage.shared.showYesterdayLine
  14. @ObservedObject private var smallGraphTreatments = Storage.shared.smallGraphTreatments
  15. @ObservedObject private var smallGraphHeight = Storage.shared.smallGraphHeight
  16. @ObservedObject private var predictionToLoad = Storage.shared.predictionToLoad
  17. @ObservedObject private var predictionDisplayType = Storage.shared.predictionDisplayType
  18. @ObservedObject private var minBasalScale = Storage.shared.minBasalScale
  19. @ObservedObject private var minBGScale = Storage.shared.minBGScale
  20. @ObservedObject private var downloadDays = Storage.shared.downloadDays
  21. private var nightscoutEnabled: Bool { IsNightscoutEnabled() }
  22. var body: some View {
  23. Form {
  24. // ── Graph Display ────────────────────────────────────────────
  25. Section("Graph Display") {
  26. Toggle("Display Dots", isOn: $showDots.value)
  27. .onChange(of: showDots.value) { _ in markDirty() }
  28. Toggle("Display Lines", isOn: $showLines.value)
  29. .onChange(of: showLines.value) { _ in markDirty() }
  30. if nightscoutEnabled {
  31. Toggle("Show DIA Lines", isOn: $showDIALines.value)
  32. .onChange(of: showDIALines.value) { _ in markDirty() }
  33. Toggle("Show −30 min Line", isOn: $show30MinLine.value)
  34. .onChange(of: show30MinLine.value) { _ in markDirty() }
  35. Toggle("Show −90 min Line", isOn: $show90MinLine.value)
  36. .onChange(of: show90MinLine.value) { _ in markDirty() }
  37. Toggle("Show Yesterday's BG", isOn: $showYesterdayLine.value)
  38. .onChange(of: showYesterdayLine.value) { _ in markDirty() }
  39. }
  40. Toggle("Show Midnight Lines", isOn: $showMidnightLines.value)
  41. .onChange(of: showMidnightLines.value) { _ in markDirty() }
  42. }
  43. // ── Treatments ───────────────────────────────────────────────
  44. if nightscoutEnabled {
  45. Section("Treatments") {
  46. Toggle("Show Carb/Bolus Values", isOn: $showValues.value)
  47. .onChange(of: showValues.value) { _ in markDirty() }
  48. Toggle("Show Carb Absorption", isOn: $showAbsorption.value)
  49. .onChange(of: showAbsorption.value) { _ in markDirty() }
  50. Toggle("Treatments on Small Graph",
  51. isOn: $smallGraphTreatments.value)
  52. .onChange(of: smallGraphTreatments.value) { _ in markDirty() }
  53. }
  54. }
  55. // ── Small Graph ──────────────────────────────────────────────
  56. Section("Small Graph") {
  57. SettingsStepperRow(
  58. title: String(localized: "Height"),
  59. range: 40 ... 80,
  60. step: 5,
  61. value: $smallGraphHeight.value,
  62. format: { "\(Int($0)) pt" }
  63. )
  64. .onChange(of: smallGraphHeight.value) { _ in markDirty() }
  65. }
  66. // ── Prediction ───────────────────────────────────────────────
  67. if nightscoutEnabled {
  68. Section("Prediction") {
  69. SettingsStepperRow(
  70. title: String(localized: "Hours of Prediction"),
  71. range: 0 ... 6,
  72. step: 0.25,
  73. value: $predictionToLoad.value,
  74. format: { "\($0.localized(maxFractionDigits: 2)) h" }
  75. )
  76. if Storage.shared.device.value != "Loop" {
  77. Picker("Prediction Style", selection: $predictionDisplayType.value) {
  78. ForEach(PredictionDisplayType.allCases, id: \.self) { type in
  79. Text(type.displayName).tag(type)
  80. }
  81. }
  82. .onChange(of: predictionDisplayType.value) { _ in markDirty() }
  83. }
  84. }
  85. }
  86. // ── Basal / BG scale ─────────────────────────────────────────
  87. if nightscoutEnabled {
  88. Section("Basal / BG Scale") {
  89. SettingsStepperRow(
  90. title: String(localized: "Min Basal"),
  91. range: 0.5 ... 20,
  92. step: 0.5,
  93. value: $minBasalScale.value,
  94. format: { "\($0.localized(maxFractionDigits: 1)) U/h" }
  95. )
  96. BGPicker(
  97. title: String(localized: "Min BG Scale"),
  98. range: 40 ... 400,
  99. value: $minBGScale.value
  100. )
  101. .onChange(of: minBGScale.value) { _ in markDirty() }
  102. }
  103. }
  104. // ── History window ───────────────────────────────────────────
  105. if nightscoutEnabled {
  106. Section("History") {
  107. SettingsStepperRow(
  108. title: String(localized: "Show Days Back"),
  109. range: 1 ... 4,
  110. step: 1,
  111. value: $downloadDays.value,
  112. format: { "\(Int($0)) d" }
  113. )
  114. }
  115. }
  116. }
  117. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  118. .navigationBarTitle("Graph Settings", displayMode: .inline)
  119. }
  120. /// Marks the chart as needing a redraw
  121. private func markDirty() {
  122. Observable.shared.chartSettingsChanged.value = true
  123. }
  124. }