CGMRootView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: [:], completionHandler: nil) }
  35. label: {
  36. Label(state.displayNameOfApp(), systemImage: "waveform.path.ecg.rectangle").font(.title3) }
  37. .frame(maxWidth: .infinity, alignment: .center)
  38. .buttonStyle(.bordered)
  39. }
  40. .listRowBackground(Color.clear)
  41. } else if state.cgmCurrent.type == .nightscout && state.url != nil {
  42. Section {
  43. Button {
  44. UIApplication.shared.open(state.url!, options: [:], completionHandler: nil) }
  45. label: { Label("Open URL", systemImage: "waveform.path.ecg.rectangle").font(.title3) }
  46. .frame(maxWidth: .infinity, alignment: .center)
  47. .buttonStyle(.bordered)
  48. }
  49. .listRowBackground(Color.clear)
  50. }
  51. }
  52. if state.cgmCurrent.type == .plugin {
  53. Section {
  54. Button("CGM Configuration") {
  55. setupCGM.toggle()
  56. }
  57. }
  58. }
  59. if state.cgmCurrent.type == .xdrip {
  60. Section(header: Text("Heartbeat")) {
  61. VStack(alignment: .leading) {
  62. if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
  63. Text("CGM address :")
  64. Text(cgmTransmitterDeviceAddress)
  65. } else {
  66. Text("CGM is not used as heartbeat.")
  67. }
  68. }
  69. }
  70. }
  71. if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
  72. Section(header: Text("Calibrations")) {
  73. Text("Calibrations").navigationLink(to: .calibrations, from: self)
  74. }
  75. }
  76. // }
  77. Section(header: Text("Calendar")) {
  78. Toggle("Create events in calendar", isOn: $state.createCalendarEvents)
  79. if state.calendarIDs.isNotEmpty {
  80. Picker("Calendar", selection: $state.currentCalendarID) {
  81. ForEach(state.calendarIDs, id: \.self) {
  82. Text($0).tag($0)
  83. }
  84. }
  85. }
  86. }
  87. Section(header: Text("Experimental")) {
  88. Toggle("Smooth Glucose Value", isOn: $state.smoothGlucose)
  89. }
  90. }
  91. .onAppear(perform: configureView)
  92. .navigationTitle("CGM")
  93. .navigationBarTitleDisplayMode(.automatic)
  94. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  95. .sheet(isPresented: $setupCGM) {
  96. if let cgmFetchManager = state.cgmManager,
  97. let cgmManager = cgmFetchManager.cgmManager,
  98. state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
  99. state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
  100. {
  101. CGMSettingsView(
  102. cgmManager: cgmManager,
  103. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  104. unit: state.settingsManager.settings.units,
  105. completionDelegate: state
  106. )
  107. } else {
  108. CGMSetupView(
  109. CGMType: state.cgmCurrent,
  110. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  111. unit: state.settingsManager.settings.units,
  112. completionDelegate: state,
  113. setupDelegate: state,
  114. pluginCGMManager: self.state.pluginCGMManager
  115. )
  116. }
  117. }
  118. .onChange(of: setupCGM) { setupCGM in
  119. state.setupCGM = setupCGM
  120. }
  121. .onChange(of: state.setupCGM) { setupCGM in
  122. self.setupCGM = setupCGM
  123. }
  124. }
  125. }
  126. }
  127. }