CGMRootView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import LoopKitUI
  2. import SwiftUI
  3. import Swinject
  4. extension CGM {
  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 body: some View {
  19. NavigationView {
  20. Form {
  21. Section(
  22. header: Text("CGM Integration to Trio"),
  23. content: {
  24. let cgmState = state.cgmCurrent
  25. if cgmState.type != .none {
  26. Button {
  27. state.setupCGM = true
  28. } label: {
  29. HStack {
  30. Image(systemName: "sensor.tag.radiowaves.forward.fill")
  31. Text(cgmState.displayName)
  32. }
  33. .frame(maxWidth: .infinity, minHeight: 50, alignment: .center)
  34. .font(.title2)
  35. }.padding()
  36. } else {
  37. VStack {
  38. Button {
  39. showCGMSelection.toggle()
  40. } label: {
  41. Text("Add CGM")
  42. .font(.title3) }
  43. .frame(maxWidth: .infinity, alignment: .center)
  44. .buttonStyle(.bordered)
  45. HStack(alignment: .center) {
  46. Text(
  47. "Pair your CGM with Trio. See hint for compatible devices."
  48. )
  49. .font(.footnote)
  50. .foregroundColor(.secondary)
  51. .lineLimit(nil)
  52. Spacer()
  53. Button(
  54. action: {
  55. shouldDisplayHint.toggle()
  56. },
  57. label: {
  58. HStack {
  59. Image(systemName: "questionmark.circle")
  60. }
  61. }
  62. ).buttonStyle(BorderlessButtonStyle())
  63. }.padding(.top)
  64. }.padding(.vertical)
  65. }
  66. }
  67. )
  68. .listRowBackground(Color.chart)
  69. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  70. Section {
  71. NavigationLink(
  72. destination: Calibrations.RootView(resolver: resolver),
  73. label: { Text("Libre Calibrations") }
  74. )
  75. }.listRowBackground(Color.chart)
  76. }
  77. SettingInputSection(
  78. decimalValue: $decimalPlaceholder,
  79. booleanValue: $state.smoothGlucose,
  80. shouldDisplayHint: $shouldDisplayHint,
  81. selectedVerboseHint: Binding(
  82. get: { selectedVerboseHint },
  83. set: {
  84. selectedVerboseHint = $0.map { AnyView($0) }
  85. hintLabel = "Smooth Glucose Value"
  86. }
  87. ),
  88. units: state.units,
  89. type: .boolean,
  90. label: "Smooth Glucose Value",
  91. miniHint: "Smooth CGM readings using Savitzky-Golay filtering.",
  92. verboseHint:
  93. VStack(alignment: .leading, spacing: 10) {
  94. Text("Default: OFF").bold()
  95. Text(
  96. "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."
  97. )
  98. Text(
  99. "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."
  100. )
  101. Text(
  102. "Note: If enabled, the smoothed values you see in Trio may differ from what is shown in your CGM app."
  103. )
  104. }
  105. )
  106. }
  107. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  108. .onAppear(perform: configureView)
  109. .navigationTitle("CGM")
  110. .navigationBarTitleDisplayMode(.automatic)
  111. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  112. .sheet(isPresented: $state.setupCGM) {
  113. switch state.cgmCurrent.type {
  114. case .enlite,
  115. .nightscout,
  116. .none,
  117. .simulator,
  118. .xdrip:
  119. OtherCGMView(resolver: self.resolver, state: state)
  120. case .plugin:
  121. if let cgmFetchManager = state.cgmManager,
  122. let cgmManager = cgmFetchManager.cgmManager,
  123. state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
  124. state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
  125. {
  126. CGMSettingsView(
  127. cgmManager: cgmManager,
  128. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  129. unit: state.settingsManager.settings.units,
  130. completionDelegate: state
  131. )
  132. } else {
  133. CGMSetupView(
  134. CGMType: state.cgmCurrent,
  135. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  136. unit: state.settingsManager.settings.units,
  137. completionDelegate: state,
  138. setupDelegate: state,
  139. pluginCGMManager: self.state.pluginCGMManager
  140. )
  141. }
  142. }
  143. }
  144. .sheet(isPresented: $shouldDisplayHint) {
  145. SettingInputHintView(
  146. hintDetent: $hintDetent,
  147. shouldDisplayHint: $shouldDisplayHint,
  148. hintLabel: hintLabel ?? "",
  149. hintText: AnyView(
  150. VStack(alignment: .leading, spacing: 10) {
  151. Text(
  152. "Current CGM Models Supported:"
  153. )
  154. VStack(alignment: .leading) {
  155. Text("• Dexcom G5")
  156. Text("• Dexcom G6 / ONE")
  157. Text("• Dexcom G7 / ONE+")
  158. Text("• Dexcom Share")
  159. Text("• Freestyle Libre")
  160. Text("• Freestyle Libre Demo")
  161. Text("• Glucose Simulator")
  162. Text("• Medtronic Enlite")
  163. Text("• Nightscout")
  164. Text("• xDrip4iOS")
  165. }
  166. Text(
  167. "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."
  168. )
  169. }
  170. ),
  171. sheetTitle: "Help"
  172. )
  173. }
  174. .confirmationDialog("CGM Model", isPresented: $showCGMSelection) {
  175. Button("Nightscout") { state.addCGM(cgm: state.listOfCGM.first(where: { $0.type == .nightscout })!) }
  176. Button("Dexcom G5") {
  177. state.addCGM(cgm: state.listOfCGM.first(where: { $0.type == .plugin && $0.displayName.contains("G5") })!)
  178. }
  179. Button("Dexcom G6 / ONE") {
  180. state
  181. .addCGM(
  182. cgm: state.listOfCGM
  183. .first(where: { $0.type == .plugin && $0.displayName.contains("G6") })!
  184. )
  185. }
  186. Button("Dexcom G7 / ONE+") {
  187. state
  188. .addCGM(
  189. cgm: state.listOfCGM
  190. .first(where: { $0.type == .plugin && $0.displayName.contains("G7") })!
  191. )
  192. }
  193. Button("Dexcom Share") {
  194. state.addCGM(
  195. cgm: state.listOfCGM
  196. .first(where: { $0.type == .plugin && $0.displayName.contains("Dexcom Share") })!
  197. ) }
  198. Button("FreeStyle Libre") {
  199. state.addCGM(
  200. cgm: state.listOfCGM
  201. .first(
  202. where: { $0.type == .plugin && $0.displayName == "FreeStyle Libre" }
  203. )!
  204. ) }
  205. Button("FreeStyle Libre Demo") {
  206. state.addCGM(
  207. cgm: state.listOfCGM
  208. .first(where: { $0.type == .plugin && $0.displayName == "FreeStyle Libre Demo" })!
  209. ) }
  210. Button("Medtronic Enlite") {
  211. state.addCGM(cgm: state.listOfCGM.first(where: { $0.type == .enlite })!) }
  212. Button("xDrip4iOS") {
  213. state.addCGM(cgm: state.listOfCGM.first(where: { $0.type == .xdrip })!) }
  214. Button("Glucose Simulator") {
  215. state
  216. .addCGM(
  217. cgm: state.listOfCGM
  218. .first(where: { $0.type == .simulator })!
  219. ) }
  220. } message: { Text("Select CGM Model") }
  221. }
  222. }
  223. }
  224. }