CGMRootView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: String?
  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. Form {
  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: .top) {
  34. Text(
  35. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  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. "CGM Types… bla bla \n\nLorem ipsum dolor sit amet, consetetur sadipscing elitr."
  46. shouldDisplayHint.toggle()
  47. },
  48. label: {
  49. HStack {
  50. Image(systemName: "questionmark.circle")
  51. }
  52. }
  53. ).buttonStyle(BorderlessButtonStyle())
  54. }.padding(.top)
  55. }.padding(.bottom)
  56. if let link = state.cgmCurrent.type.externalLink {
  57. Button {
  58. UIApplication.shared.open(link, options: [:], completionHandler: nil)
  59. } label: {
  60. HStack {
  61. Text("About this source")
  62. Spacer()
  63. Image(systemName: "chevron.right")
  64. }
  65. }
  66. .frame(maxWidth: .infinity, alignment: .leading)
  67. }
  68. if state.cgmCurrent.type == .plugin {
  69. Button {
  70. setupCGM.toggle()
  71. } label: {
  72. HStack {
  73. Text("CGM Configuration")
  74. Spacer()
  75. Image(systemName: "chevron.right")
  76. }
  77. }
  78. .frame(maxWidth: .infinity, alignment: .leading)
  79. }
  80. }
  81. ).listRowBackground(Color.chart)
  82. if let appURL = state.urlOfApp() {
  83. Section {
  84. Button {
  85. UIApplication.shared.open(appURL, options: [:]) { success in
  86. if !success {
  87. self.router.alertMessage
  88. .send(MessageContent(content: "Unable to open the app", type: .warning))
  89. }
  90. }
  91. }
  92. label: {
  93. Label(state.displayNameOfApp() ?? "-", systemImage: "waveform.path.ecg.rectangle").font(.title3)
  94. .padding() }
  95. .frame(maxWidth: .infinity, alignment: .center)
  96. .buttonStyle(.bordered)
  97. }
  98. .listRowBackground(Color.clear)
  99. } else if state.cgmCurrent.type == .nightscout {
  100. if let url = state.url {
  101. Section {
  102. Button {
  103. UIApplication.shared.open(url, options: [:]) { success in
  104. if !success {
  105. self.router.alertMessage
  106. .send(MessageContent(content: "No URL available", type: .warning))
  107. }
  108. }
  109. }
  110. label: { Label("Open URL", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding() }
  111. .frame(maxWidth: .infinity, alignment: .center)
  112. .buttonStyle(.bordered)
  113. }
  114. .listRowBackground(Color.clear)
  115. } else {
  116. Section {
  117. Button {
  118. state.showModal(for: .nighscoutConfigDirect)
  119. }
  120. label: {
  121. Label("Config Nightscout", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding()
  122. }
  123. .frame(maxWidth: .infinity, alignment: .center)
  124. .buttonStyle(.bordered)
  125. }
  126. .listRowBackground(Color.clear)
  127. }
  128. }
  129. if state.cgmCurrent.type == .xdrip {
  130. Section(header: Text("Heartbeat")) {
  131. VStack(alignment: .leading) {
  132. if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
  133. Text("CGM address :").padding(.top)
  134. Text(cgmTransmitterDeviceAddress)
  135. } else {
  136. Text("CGM is not used as heartbeat.").padding(.top)
  137. }
  138. HStack(alignment: .top) {
  139. Text(
  140. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  141. )
  142. .font(.footnote)
  143. .foregroundColor(.secondary)
  144. .lineLimit(nil)
  145. Spacer()
  146. Button(
  147. action: {
  148. hintLabel = "CGM Heartbeat"
  149. selectedVerboseHint = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  150. shouldDisplayHint.toggle()
  151. },
  152. label: {
  153. HStack {
  154. Image(systemName: "questionmark.circle")
  155. }
  156. }
  157. ).buttonStyle(BorderlessButtonStyle())
  158. }.padding(.vertical)
  159. }
  160. }.listRowBackground(Color.chart)
  161. }
  162. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  163. Section {
  164. Text("Libre Calibrations").navigationLink(to: .calibrations, from: self)
  165. }.listRowBackground(Color.chart)
  166. }
  167. SettingInputSection(
  168. decimalValue: $decimalPlaceholder,
  169. booleanValue: $state.smoothGlucose,
  170. shouldDisplayHint: $shouldDisplayHint,
  171. selectedVerboseHint: Binding(
  172. get: { selectedVerboseHint },
  173. set: {
  174. selectedVerboseHint = $0
  175. hintLabel = "Smooth Glucose Value"
  176. }
  177. ),
  178. units: state.units,
  179. type: .boolean,
  180. label: "Smooth Glucose Value",
  181. miniHint: "Smooth CGM readings using Savitzky–Golay filtering.",
  182. verboseHint: "Smooth Glucose Value… bla bla bla"
  183. )
  184. }
  185. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  186. .onAppear(perform: configureView)
  187. .navigationTitle("CGM")
  188. .navigationBarTitleDisplayMode(.automatic)
  189. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  190. .sheet(isPresented: $shouldDisplayHint) {
  191. SettingInputHintView(
  192. hintDetent: $hintDetent,
  193. shouldDisplayHint: $shouldDisplayHint,
  194. hintLabel: hintLabel ?? "",
  195. hintText: selectedVerboseHint ?? "",
  196. sheetTitle: "Help"
  197. )
  198. }
  199. .onChange(of: setupCGM) { _, setupCGM in
  200. state.setupCGM = setupCGM
  201. }
  202. .onChange(of: state.setupCGM) { _, setupCGM in
  203. self.setupCGM = setupCGM
  204. }
  205. .screenNavigation(self)
  206. }
  207. .sheet(isPresented: $setupCGM) {
  208. if let cgmFetchManager = state.cgmManager,
  209. let cgmManager = cgmFetchManager.cgmManager,
  210. state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
  211. state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
  212. {
  213. CGMSettingsView(
  214. cgmManager: cgmManager,
  215. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  216. unit: state.settingsManager.settings.units,
  217. completionDelegate: state
  218. )
  219. } else {
  220. CGMSetupView(
  221. CGMType: state.cgmCurrent,
  222. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  223. unit: state.settingsManager.settings.units,
  224. completionDelegate: state,
  225. setupDelegate: state,
  226. pluginCGMManager: self.state.pluginCGMManager
  227. )
  228. }
  229. }
  230. }
  231. }
  232. }