CGMRootView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import LoopKitUI
  2. import SwiftUI
  3. import Swinject
  4. extension CGMSettings {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. let displayClose: Bool
  8. @StateObject var state = StateModel()
  9. @State private var shouldDisplayHint: Bool = false
  10. @State var hintDetent = PresentationDetent.large
  11. @State var selectedVerboseHint: AnyView?
  12. @State var hintLabel: String?
  13. @State private var decimalPlaceholder: Decimal = 0.0
  14. @State private var booleanPlaceholder: Bool = false
  15. @State var showCGMSelection: Bool = false
  16. @Environment(\.colorScheme) var colorScheme
  17. @Environment(AppState.self) var appState
  18. var cgmSelectionButtons: some View {
  19. ForEach(cgmOptions, id: \.name) { option in
  20. if let cgm = state.listOfCGM.first(where: option.predicate) {
  21. Button(option.name) {
  22. state.addCGM(cgm: cgm)
  23. }
  24. }
  25. }
  26. }
  27. var body: some View {
  28. NavigationView {
  29. Form {
  30. Section(
  31. header: Text("CGM Integration to Trio"),
  32. content: {
  33. let cgmState = state.cgmCurrent
  34. if cgmState.type != .none {
  35. Button {
  36. state.shouldDisplayCGMSetupSheet = true
  37. } label: {
  38. HStack {
  39. Image(systemName: "sensor.tag.radiowaves.forward.fill")
  40. Text(cgmState.displayName)
  41. }
  42. .frame(maxWidth: .infinity, minHeight: 50, alignment: .center)
  43. .font(.title2)
  44. }.padding()
  45. } else {
  46. VStack {
  47. Button {
  48. showCGMSelection.toggle()
  49. } label: {
  50. Text("Add CGM")
  51. .font(.title3) }
  52. .frame(maxWidth: .infinity, alignment: .center)
  53. .buttonStyle(.bordered)
  54. HStack(alignment: .center) {
  55. Text(
  56. "Pair your CGM with Trio. See hint for compatible devices."
  57. )
  58. .font(.footnote)
  59. .foregroundColor(.secondary)
  60. .lineLimit(nil)
  61. Spacer()
  62. Button(
  63. action: {
  64. shouldDisplayHint.toggle()
  65. },
  66. label: {
  67. HStack {
  68. Image(systemName: "questionmark.circle")
  69. }
  70. }
  71. ).buttonStyle(BorderlessButtonStyle())
  72. }.padding(.top)
  73. }.padding(.vertical)
  74. }
  75. }
  76. )
  77. .listRowBackground(Color.chart)
  78. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  79. Section {
  80. NavigationLink(
  81. destination: Calibrations.RootView(resolver: resolver),
  82. label: { Text("Libre Calibrations") }
  83. )
  84. }.listRowBackground(Color.chart)
  85. }
  86. SettingInputSection(
  87. decimalValue: $decimalPlaceholder,
  88. booleanValue: $state.smoothGlucose,
  89. shouldDisplayHint: $shouldDisplayHint,
  90. selectedVerboseHint: Binding(
  91. get: { selectedVerboseHint },
  92. set: {
  93. selectedVerboseHint = $0.map { AnyView($0) }
  94. hintLabel = "Smooth Glucose Value"
  95. }
  96. ),
  97. units: state.units,
  98. type: .boolean,
  99. label: "Smooth Glucose Value",
  100. miniHint: "Smooth CGM readings using Savitzky-Golay filtering.",
  101. verboseHint:
  102. VStack(alignment: .leading, spacing: 10) {
  103. Text("Default: OFF").bold()
  104. Text(
  105. "This filter looks at small groups of nearby readings and fits them to a simple mathematical curve. This process doesn't change the overall pattern of your glucose data but helps smooth out the \"noise\" or irregular fluctuations that could lead to false highs or lows."
  106. )
  107. Text(
  108. "It's designed to keep the important trends in your data while minimizing those small, misleading variations, giving you and Trio a clearer sense of where your blood sugar is really headed. This type of filtering is useful in Trio, as it can help prevent over-corrections based on inaccurate glucose readings. This can help reduce the impact of sudden spikes or dips that might not reflect your true blood glucose levels."
  109. )
  110. Text(
  111. "Note: If enabled, the smoothed values you see in Trio may differ from what is shown in your CGM app."
  112. )
  113. }
  114. )
  115. }
  116. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  117. .onAppear(perform: configureView)
  118. .navigationTitle("CGM")
  119. .navigationBarTitleDisplayMode(.automatic)
  120. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  121. .sheet(isPresented: $state.shouldDisplayCGMSetupSheet) {
  122. switch state.cgmCurrent.type {
  123. case .enlite,
  124. .nightscout,
  125. .none,
  126. .simulator,
  127. .xdrip:
  128. CustomCGMOptionsView(
  129. resolver: self.resolver,
  130. state: state,
  131. cgmCurrent: state.cgmCurrent,
  132. deleteCGM: state.deleteCGM
  133. )
  134. case .plugin:
  135. if let fetchGlucoseManager = state.fetchGlucoseManager,
  136. let cgmManager = fetchGlucoseManager.cgmManager,
  137. state.cgmCurrent.type == fetchGlucoseManager.cgmGlucoseSourceType,
  138. state.cgmCurrent.id == fetchGlucoseManager.cgmGlucosePluginId
  139. {
  140. CGMSettingsView(
  141. cgmManager: cgmManager,
  142. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  143. unit: state.settingsManager.settings.units,
  144. completionDelegate: state
  145. )
  146. } else {
  147. CGMSetupView(
  148. CGMType: state.cgmCurrent,
  149. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  150. unit: state.settingsManager.settings.units,
  151. completionDelegate: state,
  152. setupDelegate: state,
  153. pluginCGMManager: self.state.pluginCGMManager
  154. )
  155. }
  156. }
  157. }
  158. .sheet(isPresented: $shouldDisplayHint) {
  159. SettingInputHintView(
  160. hintDetent: $hintDetent,
  161. shouldDisplayHint: $shouldDisplayHint,
  162. hintLabel: hintLabel ?? "",
  163. hintText: AnyView(
  164. VStack(alignment: .leading, spacing: 10) {
  165. Text(
  166. "Current CGM Models Supported:"
  167. )
  168. VStack(alignment: .leading) {
  169. Text("• Dexcom G5")
  170. Text("• Dexcom G6 / ONE")
  171. Text("• Dexcom G7 / ONE+")
  172. Text("• Dexcom Share")
  173. Text("• Freestyle Libre")
  174. Text("• Freestyle Libre Demo")
  175. Text("• Glucose Simulator")
  176. Text("• Medtronic Enlite")
  177. Text("• Nightscout")
  178. Text("• xDrip4iOS")
  179. }
  180. Text(
  181. "Note: The CGM Heartbeat can come from either a CGM or a pump to wake up Trio when phone is locked or in the background. If CGM is on the same phone as Trio and xDrip4iOS is configured to use the same AppGroup as Trio and the heartbeat feature is turned on in xDrip4iOS, then the CGM can provide a heartbeat to wake up Trio when phone is locked or app is in the background."
  182. )
  183. }
  184. ),
  185. sheetTitle: "Help"
  186. )
  187. }
  188. .confirmationDialog("CGM Model", isPresented: $showCGMSelection) {
  189. cgmSelectionButtons
  190. } message: {
  191. Text("Select CGM Model")
  192. }
  193. }
  194. }
  195. }
  196. }