CGMRootView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 setupCGM = false
  10. @State private var shouldDisplayHint: Bool = false
  11. @State var hintDetent = PresentationDetent.large
  12. @State var selectedVerboseHint: AnyView?
  13. @State var hintLabel: String?
  14. @State private var decimalPlaceholder: Decimal = 0.0
  15. @State private var booleanPlaceholder: Bool = false
  16. @Environment(\.colorScheme) var colorScheme
  17. @Environment(AppState.self) var appState
  18. var body: some View {
  19. NavigationView {
  20. List {
  21. Section(
  22. header: Text("CGM Integration to Trio"),
  23. content: {
  24. VStack {
  25. Picker("Type", selection: $state.cgmCurrent) {
  26. ForEach(state.listOfCGM) { type in
  27. VStack(alignment: .leading) {
  28. Text(type.displayName)
  29. Text(type.subtitle).font(.caption).foregroundColor(.secondary)
  30. }.tag(type)
  31. }
  32. }.padding(.top)
  33. HStack(alignment: .center) {
  34. Text(
  35. "Select your CGM. See hint for compatible devices."
  36. )
  37. .font(.footnote)
  38. .foregroundColor(.secondary)
  39. .lineLimit(nil)
  40. Spacer()
  41. Button(
  42. action: {
  43. hintLabel = "Available CGM Types for Trio"
  44. selectedVerboseHint =
  45. AnyView(
  46. Text(
  47. "• Dexcom G5 \n• Dexcom G6 / ONE \n• Dexcom G7 / ONE+ \n• Dexcom Share \n• Freestyle Libre \n• Freestyle Libre Demo \n• Glucose Simulator \n• Medtronic Enlite \n• Nightscout \n• xDrip4iOS"
  48. )
  49. )
  50. shouldDisplayHint.toggle()
  51. },
  52. label: {
  53. HStack {
  54. Image(systemName: "questionmark.circle")
  55. }
  56. }
  57. ).buttonStyle(BorderlessButtonStyle())
  58. }.padding(.top)
  59. }.padding(.bottom)
  60. if let link = state.cgmCurrent.type.externalLink {
  61. Button {
  62. UIApplication.shared.open(link, options: [:], completionHandler: nil)
  63. } label: {
  64. HStack {
  65. Text("About this source")
  66. Spacer()
  67. Image(systemName: "chevron.right")
  68. }
  69. }
  70. .frame(maxWidth: .infinity, alignment: .leading)
  71. }
  72. if state.cgmCurrent.type == .plugin {
  73. Button {
  74. setupCGM.toggle()
  75. } label: {
  76. HStack {
  77. Text("CGM Configuration")
  78. Spacer()
  79. Image(systemName: "chevron.right")
  80. }
  81. }
  82. .frame(maxWidth: .infinity, alignment: .leading)
  83. }
  84. }
  85. ).listRowBackground(Color.chart)
  86. if let appURL = state.urlOfApp() {
  87. Section {
  88. Button {
  89. UIApplication.shared.open(appURL, options: [:]) { success in
  90. if !success {
  91. self.router.alertMessage
  92. .send(MessageContent(content: "Unable to open the app", type: .warning))
  93. }
  94. }
  95. }
  96. label: {
  97. Label(state.displayNameOfApp() ?? "-", systemImage: "waveform.path.ecg.rectangle").font(.title3)
  98. .padding() }
  99. .frame(maxWidth: .infinity, alignment: .center)
  100. .buttonStyle(.bordered)
  101. }
  102. .listRowBackground(Color.clear)
  103. } else if state.cgmCurrent.type == .nightscout {
  104. if let url = state.url {
  105. Section {
  106. Button {
  107. UIApplication.shared.open(url, options: [:]) { success in
  108. if !success {
  109. self.router.alertMessage
  110. .send(MessageContent(content: "No URL available", type: .warning))
  111. }
  112. }
  113. }
  114. label: { Label("Open URL", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding() }
  115. .frame(maxWidth: .infinity, alignment: .center)
  116. .buttonStyle(.bordered)
  117. }
  118. .listRowBackground(Color.clear)
  119. } else {
  120. Section {
  121. Button {
  122. state.showModal(for: .nighscoutConfigDirect)
  123. }
  124. label: {
  125. Label("Config Nightscout", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding()
  126. }
  127. .frame(maxWidth: .infinity, alignment: .center)
  128. .buttonStyle(.bordered)
  129. }
  130. .listRowBackground(Color.clear)
  131. }
  132. }
  133. if state.cgmCurrent.type == .xdrip {
  134. Section(header: Text("Heartbeat")) {
  135. VStack(alignment: .leading) {
  136. if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
  137. Text("CGM address :").padding(.top)
  138. Text(cgmTransmitterDeviceAddress)
  139. } else {
  140. Text("CGM is not used as heartbeat.").padding(.top)
  141. }
  142. HStack(alignment: .center) {
  143. Text(
  144. "A heartbeat tells Trio to start a loop cycle. This is required for closed loop."
  145. )
  146. .font(.footnote)
  147. .foregroundColor(.secondary)
  148. .lineLimit(nil)
  149. Spacer()
  150. Button(
  151. action: {
  152. hintLabel = "CGM Heartbeat"
  153. selectedVerboseHint =
  154. AnyView(
  155. Text(
  156. "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."
  157. )
  158. )
  159. shouldDisplayHint.toggle()
  160. },
  161. label: {
  162. HStack {
  163. Image(systemName: "questionmark.circle")
  164. }
  165. }
  166. ).buttonStyle(BorderlessButtonStyle())
  167. }.padding(.vertical)
  168. }
  169. }.listRowBackground(Color.chart)
  170. }
  171. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  172. Section {
  173. Text("Libre Calibrations").navigationLink(to: .calibrations, from: self)
  174. }.listRowBackground(Color.chart)
  175. }
  176. SettingInputSection(
  177. decimalValue: $decimalPlaceholder,
  178. booleanValue: $state.smoothGlucose,
  179. shouldDisplayHint: $shouldDisplayHint,
  180. selectedVerboseHint: Binding(
  181. get: { selectedVerboseHint },
  182. set: {
  183. selectedVerboseHint = $0.map { AnyView($0) }
  184. hintLabel = "Smooth Glucose Value"
  185. }
  186. ),
  187. units: state.units,
  188. type: .boolean,
  189. label: "Smooth Glucose Value",
  190. miniHint: "Smooth CGM readings using Savitzky-Golay filtering.",
  191. verboseHint:
  192. VStack(alignment: .leading, spacing: 10) {
  193. Text("Default: OFF").bold()
  194. Text(
  195. "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."
  196. )
  197. Text(
  198. "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."
  199. )
  200. Text(
  201. "Note: If enabled, the smoothed values you see in Trio may differ from what is shown in your CGM app."
  202. )
  203. }
  204. )
  205. }
  206. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  207. .onAppear(perform: configureView)
  208. .navigationTitle("CGM")
  209. .navigationBarTitleDisplayMode(.automatic)
  210. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  211. .sheet(isPresented: $shouldDisplayHint) {
  212. SettingInputHintView(
  213. hintDetent: $hintDetent,
  214. shouldDisplayHint: $shouldDisplayHint,
  215. hintLabel: hintLabel ?? "",
  216. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  217. sheetTitle: "Help"
  218. )
  219. }
  220. .onChange(of: setupCGM) { _, setupCGM in
  221. state.setupCGM = setupCGM
  222. }
  223. .onChange(of: state.setupCGM) { _, setupCGM in
  224. self.setupCGM = setupCGM
  225. }
  226. .screenNavigation(self)
  227. }
  228. .sheet(isPresented: $setupCGM) {
  229. if let cgmFetchManager = state.cgmManager,
  230. let cgmManager = cgmFetchManager.cgmManager,
  231. state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
  232. state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
  233. {
  234. CGMSettingsView(
  235. cgmManager: cgmManager,
  236. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  237. unit: state.settingsManager.settings.units,
  238. completionDelegate: state
  239. )
  240. } else {
  241. CGMSetupView(
  242. CGMType: state.cgmCurrent,
  243. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  244. unit: state.settingsManager.settings.units,
  245. completionDelegate: state,
  246. setupDelegate: state,
  247. pluginCGMManager: self.state.pluginCGMManager
  248. )
  249. }
  250. }
  251. }
  252. }
  253. }