GraphSettingsView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // LoopFollow
  2. // GraphSettingsView.swift
  3. // Created by Jonas Björkert on 2025-05-26.
  4. import SwiftUI
  5. struct GraphSettingsView: View {
  6. @ObservedObject private var showDots = Storage.shared.showDots
  7. @ObservedObject private var showLines = Storage.shared.showLines
  8. @ObservedObject private var showValues = Storage.shared.showValues
  9. @ObservedObject private var showAbsorption = Storage.shared.showAbsorption
  10. @ObservedObject private var showDIALines = Storage.shared.showDIALines
  11. @ObservedObject private var show30MinLine = Storage.shared.show30MinLine
  12. @ObservedObject private var show90MinLine = Storage.shared.show90MinLine
  13. @ObservedObject private var showMidnightLines = Storage.shared.showMidnightLines
  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 minBasalScale = Storage.shared.minBasalScale
  18. @ObservedObject private var minBGScale = Storage.shared.minBGScale
  19. @ObservedObject private var lowLine = Storage.shared.lowLine
  20. @ObservedObject private var highLine = Storage.shared.highLine
  21. @ObservedObject private var downloadDays = Storage.shared.downloadDays
  22. private var nightscoutEnabled: Bool { IsNightscoutEnabled() }
  23. var body: some View {
  24. NavigationView {
  25. Form {
  26. // ── Graph Display ────────────────────────────────────────────
  27. Section("Graph Display") {
  28. Toggle("Display Dots", isOn: $showDots.value)
  29. .onChange(of: showDots.value) { _ in markDirty() }
  30. Toggle("Display Lines", isOn: $showLines.value)
  31. .onChange(of: showLines.value) { _ in markDirty() }
  32. if nightscoutEnabled {
  33. Toggle("Show DIA Lines", isOn: $showDIALines.value)
  34. .onChange(of: showDIALines.value) { _ in markDirty() }
  35. Toggle("Show −30 min Line", isOn: $show30MinLine.value)
  36. .onChange(of: show30MinLine.value) { _ in markDirty() }
  37. Toggle("Show −90 min Line", isOn: $show90MinLine.value)
  38. .onChange(of: show90MinLine.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. Toggle("Show Carb Absorption", isOn: $showAbsorption.value)
  48. Toggle("Treatments on Small Graph",
  49. isOn: $smallGraphTreatments.value)
  50. }
  51. }
  52. // ── Small Graph ──────────────────────────────────────────────
  53. Section("Small Graph") {
  54. SettingsStepperRow(
  55. title: "Height",
  56. range: 40 ... 80,
  57. step: 5,
  58. value: $smallGraphHeight.value,
  59. format: { "\(Int($0)) pt" }
  60. )
  61. .onChange(of: smallGraphHeight.value) { _ in markDirty() }
  62. }
  63. // ── Prediction ───────────────────────────────────────────────
  64. if nightscoutEnabled {
  65. Section("Prediction") {
  66. SettingsStepperRow(
  67. title: "Hours of Prediction",
  68. range: 0 ... 6,
  69. step: 0.25,
  70. value: $predictionToLoad.value,
  71. format: { "\($0.localized(maxFractionDigits: 2)) h" }
  72. )
  73. }
  74. }
  75. // ── Basal / BG scale ─────────────────────────────────────────
  76. if nightscoutEnabled {
  77. Section("Basal / BG Scale") {
  78. SettingsStepperRow(
  79. title: "Min Basal",
  80. range: 0.5 ... 20,
  81. step: 0.5,
  82. value: $minBasalScale.value,
  83. format: { "\($0.localized(maxFractionDigits: 1)) U/h" }
  84. )
  85. BGPicker(
  86. title: "Min BG Scale",
  87. range: 40 ... 400,
  88. value: $minBGScale.value
  89. )
  90. .onChange(of: minBGScale.value) { _ in markDirty() }
  91. }
  92. }
  93. // ── Target lines ─────────────────────────────────────────────
  94. Section("Target Lines") {
  95. BGPicker(title: "Low BG Line",
  96. range: 40 ... 120,
  97. value: $lowLine.value)
  98. .onChange(of: lowLine.value) { _ in markDirty() }
  99. BGPicker(title: "High BG Line",
  100. range: 120 ... 400,
  101. value: $highLine.value)
  102. .onChange(of: highLine.value) { _ in markDirty() }
  103. }
  104. // ── History window ───────────────────────────────────────────
  105. if nightscoutEnabled {
  106. Section("History") {
  107. SettingsStepperRow(
  108. title: "Show Days Back",
  109. range: 1 ... 4,
  110. step: 1,
  111. value: $downloadDays.value,
  112. format: { "\(Int($0)) d" }
  113. )
  114. }
  115. }
  116. }
  117. }
  118. .preferredColorScheme(Storage.shared.forceDarkMode.value ? .dark : nil)
  119. .navigationBarTitle("Graph Settings", displayMode: .inline)
  120. }
  121. /// Marks the chart as needing a redraw
  122. private func markDirty() {
  123. Observable.shared.chartSettingsChanged.value = true
  124. }
  125. }