CGMRootView.swift 5.4 KB

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