CGMRootView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // @AppStorage(UserDefaults.BTKey.cgmTransmitterDeviceAddress.rawValue) private var cgmTransmitterDeviceAddress: String? = nil
  11. var body: some View {
  12. NavigationView {
  13. Form {
  14. Section(header: Text("CGM")) {
  15. Picker("Type", selection: $state.cgmCurrent) {
  16. ForEach(state.listOfCGM) { type in
  17. VStack(alignment: .leading) {
  18. Text(type.displayName)
  19. Text(type.subtitle).font(.caption).foregroundColor(.secondary)
  20. }.tag(type)
  21. }
  22. }
  23. if let link = state.cgmCurrent.type.externalLink {
  24. Button("About this source") {
  25. UIApplication.shared.open(link, options: [:], completionHandler: nil)
  26. }
  27. }
  28. }
  29. if let cgmFetchManager = state.cgmManager {
  30. if let appURL = state.urlOfApp()
  31. {
  32. Section {
  33. Button {
  34. UIApplication.shared.open(appURL, options: [:]) { success in
  35. if !success {
  36. self.router.alertMessage
  37. .send(MessageContent(content: "Unable to open the app", type: .warning))
  38. }
  39. }
  40. }
  41. label: {
  42. Label(state.displayNameOfApp(), systemImage: "waveform.path.ecg.rectangle").font(.title3) }
  43. .frame(maxWidth: .infinity, alignment: .center)
  44. .buttonStyle(.bordered)
  45. }
  46. .listRowBackground(Color.clear)
  47. } else if state.cgmCurrent.type == .nightscout && state.url != nil {
  48. Section {
  49. Button {
  50. UIApplication.shared.open(state.url!, options: [:]) { success in
  51. if !success {
  52. self.router.alertMessage
  53. .send(MessageContent(content: "No URL available", type: .warning))
  54. }
  55. }
  56. }
  57. label: { Label("Open URL", systemImage: "waveform.path.ecg.rectangle").font(.title3) }
  58. .frame(maxWidth: .infinity, alignment: .center)
  59. .buttonStyle(.bordered)
  60. }
  61. .listRowBackground(Color.clear)
  62. }
  63. }
  64. if state.cgmCurrent.type == .plugin {
  65. Section {
  66. Button("CGM Configuration") {
  67. setupCGM.toggle()
  68. }
  69. }
  70. }
  71. if state.cgmCurrent.type == .xdrip {
  72. Section(header: Text("Heartbeat")) {
  73. VStack(alignment: .leading) {
  74. if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
  75. Text("CGM address :")
  76. Text(cgmTransmitterDeviceAddress)
  77. } else {
  78. Text("CGM is not used as heartbeat.")
  79. }
  80. }
  81. }
  82. }
  83. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  84. Section(header: Text("Calibrations")) {
  85. Text("Calibrations").navigationLink(to: .calibrations, from: self)
  86. }
  87. }
  88. // }
  89. Section(header: Text("Calendar")) {
  90. Toggle("Create events in calendar", isOn: $state.createCalendarEvents)
  91. if state.calendarIDs.isNotEmpty {
  92. Picker("Calendar", selection: $state.currentCalendarID) {
  93. ForEach(state.calendarIDs, id: \.self) {
  94. Text($0).tag($0)
  95. }
  96. }
  97. }
  98. }
  99. Section(header: Text("Experimental")) {
  100. Toggle("Smooth Glucose Value", isOn: $state.smoothGlucose)
  101. }
  102. }
  103. .onAppear(perform: configureView)
  104. .navigationTitle("CGM")
  105. .navigationBarTitleDisplayMode(.automatic)
  106. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  107. .sheet(isPresented: $setupCGM) {
  108. if let cgmFetchManager = state.cgmManager,
  109. let cgmManager = cgmFetchManager.cgmManager,
  110. state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
  111. state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
  112. {
  113. CGMSettingsView(
  114. cgmManager: cgmManager,
  115. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  116. unit: state.settingsManager.settings.units,
  117. completionDelegate: state
  118. )
  119. } else {
  120. CGMSetupView(
  121. CGMType: state.cgmCurrent,
  122. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  123. unit: state.settingsManager.settings.units,
  124. completionDelegate: state,
  125. setupDelegate: state,
  126. pluginCGMManager: self.state.pluginCGMManager
  127. )
  128. }
  129. }
  130. .onChange(of: setupCGM) { setupCGM in
  131. state.setupCGM = setupCGM
  132. }
  133. .onChange(of: state.setupCGM) { setupCGM in
  134. self.setupCGM = setupCGM
  135. }
  136. }
  137. }
  138. }
  139. }