CGMRootView.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import LoopKitUI
  2. import SwiftUI
  3. import Swinject
  4. extension CGM {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var setupCGM = false
  9. @Environment(\.colorScheme) var colorScheme
  10. var color: LinearGradient {
  11. colorScheme == .dark ? LinearGradient(
  12. gradient: Gradient(colors: [
  13. Color.bgDarkBlue,
  14. Color.bgDarkerDarkBlue
  15. ]),
  16. startPoint: .top,
  17. endPoint: .bottom
  18. )
  19. :
  20. LinearGradient(
  21. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  22. startPoint: .top,
  23. endPoint: .bottom
  24. )
  25. }
  26. // @AppStorage(UserDefaults.BTKey.cgmTransmitterDeviceAddress.rawValue) private var cgmTransmitterDeviceAddress: String? = nil
  27. var body: some View {
  28. NavigationView {
  29. Form {
  30. Section(header: Text("CGM")) {
  31. Picker("Type", selection: $state.cgm) {
  32. ForEach(CGMType.allCases) { type in
  33. VStack(alignment: .leading) {
  34. Text(type.displayName)
  35. Text(type.subtitle).font(.caption).foregroundColor(.secondary)
  36. }.tag(type)
  37. }
  38. }
  39. if let link = state.cgm.externalLink {
  40. Button("About this source") {
  41. UIApplication.shared.open(link, options: [:], completionHandler: nil)
  42. }
  43. }
  44. }
  45. if [.dexcomG5, .dexcomG6, .dexcomG7].contains(state.cgm) {
  46. Section {
  47. Button("CGM Configuration") {
  48. setupCGM.toggle()
  49. }
  50. }
  51. }
  52. if state.cgm == .xdrip {
  53. Section(header: Text("Heartbeat")) {
  54. VStack(alignment: .leading) {
  55. if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
  56. Text("CGM address :")
  57. Text(cgmTransmitterDeviceAddress)
  58. } else {
  59. Text("CGM is not used as heartbeat.")
  60. }
  61. }
  62. }
  63. }
  64. if state.cgm == .libreTransmitter {
  65. Button("Configure Libre Transmitter") {
  66. state.showModal(for: .libreConfig)
  67. }
  68. Text("Calibrations").navigationLink(to: .calibrations, from: self)
  69. }
  70. Section(header: Text("Calendar")) {
  71. Toggle("Create Events in Calendar", isOn: $state.createCalendarEvents)
  72. if state.calendarIDs.isNotEmpty {
  73. Picker("Calendar", selection: $state.currentCalendarID) {
  74. ForEach(state.calendarIDs, id: \.self) {
  75. Text($0).tag($0)
  76. }
  77. }
  78. Toggle("Display Emojis as Labels", isOn: $state.displayCalendarEmojis)
  79. Toggle("Display IOB and COB", isOn: $state.displayCalendarIOBandCOB)
  80. } else if state.createCalendarEvents {
  81. if #available(iOS 17.0, *) {
  82. Text(
  83. "If you are not seeing calendars to choose here, please go to Settings -> iAPS -> Calendars and change permissions to \"Full Access\""
  84. ).font(.footnote)
  85. Button("Open Settings") {
  86. // Get the settings URL and open it
  87. if let url = URL(string: UIApplication.openSettingsURLString) {
  88. UIApplication.shared.open(url)
  89. }
  90. }
  91. }
  92. }
  93. }
  94. Section(header: Text("Experimental")) {
  95. Toggle("Smooth Glucose Value", isOn: $state.smoothGlucose)
  96. }
  97. }
  98. .scrollContentBackground(.hidden).background(color)
  99. .onAppear(perform: configureView)
  100. .navigationTitle("CGM")
  101. .navigationBarTitleDisplayMode(.automatic)
  102. .sheet(isPresented: $setupCGM) {
  103. if let cgmFetchManager = state.cgmManager, cgmFetchManager.glucoseSource.cgmType == state.cgm {
  104. CGMSettingsView(
  105. cgmManager: cgmFetchManager.glucoseSource.cgmManager!,
  106. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  107. unit: state.settingsManager.settings.units,
  108. completionDelegate: state
  109. )
  110. } else {
  111. CGMSetupView(
  112. CGMType: state.cgm,
  113. bluetoothManager: state.provider.apsManager.bluetoothManager!,
  114. unit: state.settingsManager.settings.units,
  115. completionDelegate: state,
  116. setupDelegate: state
  117. )
  118. }
  119. }
  120. .onChange(of: setupCGM) { setupCGM in
  121. state.setupCGM = setupCGM
  122. }
  123. .onChange(of: state.setupCGM) { setupCGM in
  124. self.setupCGM = setupCGM
  125. }
  126. }
  127. }
  128. }
  129. }