NightscoutConfigRootView.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension NightscoutConfig {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. let displayClose: Bool
  8. @StateObject var state = StateModel()
  9. @State var importAlert: Alert?
  10. @State var isImportAlertPresented = false
  11. @State var importedHasRun = false
  12. @State private var shouldDisplayHint: Bool = false
  13. @State var hintDetent = PresentationDetent.large
  14. @State var selectedVerboseHint: String?
  15. @State var hintLabel: String?
  16. @State private var decimalPlaceholder: Decimal = 0.0
  17. @State private var booleanPlaceholder: Bool = false
  18. @Environment(\.colorScheme) var colorScheme
  19. var color: LinearGradient {
  20. colorScheme == .dark ? LinearGradient(
  21. gradient: Gradient(colors: [
  22. Color.bgDarkBlue,
  23. Color.bgDarkerDarkBlue
  24. ]),
  25. startPoint: .top,
  26. endPoint: .bottom
  27. )
  28. :
  29. LinearGradient(
  30. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  31. startPoint: .top,
  32. endPoint: .bottom
  33. )
  34. }
  35. var body: some View {
  36. ZStack {
  37. Form {
  38. Section(
  39. header: Text("Nightscout Integration"),
  40. content: {
  41. NavigationLink(destination: NightscoutConnectView(state: state), label: {
  42. HStack {
  43. Text("Connect")
  44. ZStack {
  45. if state.isConnectedToNS {
  46. Image(systemName: "network")
  47. Image(systemName: "checkmark.circle.fill").foregroundColor(.green).font(.caption2)
  48. .offset(x: 9, y: 6)
  49. } else {
  50. Image(systemName: "network.slash")
  51. }
  52. }
  53. }
  54. })
  55. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  56. NavigationLink("Fetch & Remote Control", destination: NightscoutFetchView(state: state))
  57. }
  58. ).listRowBackground(Color.chart)
  59. Section {
  60. VStack {
  61. Button {
  62. importAlert = Alert(
  63. title: Text("Import Therapy Settings?"),
  64. message: Text(
  65. "Are you sure you want to import profile settings from Nightscout?\n\nThis will overwrite the following Trio therapy settings: Basal Rates, Insulin Sensitivities, Carb Ratios, Target Glucose, and Duration of Insulin Action."
  66. ),
  67. primaryButton: .default(
  68. Text("Yes, Import!"),
  69. action: {
  70. Task {
  71. await state.importSettings()
  72. // Check the import status and errors after the import process finishes
  73. if state.importStatus == .failed, state.importErrors.isNotEmpty,
  74. let errorMessage = state.importErrors.first
  75. {
  76. DispatchQueue.main.async {
  77. importAlert = Alert(
  78. title: Text("Import Failed"),
  79. message: Text(errorMessage.description),
  80. dismissButton: .default(Text("OK"))
  81. )
  82. isImportAlertPresented = true
  83. }
  84. }
  85. }
  86. }
  87. ),
  88. secondaryButton: .cancel()
  89. )
  90. isImportAlertPresented = true
  91. } label: {
  92. Text("Import Settings")
  93. .font(.title3) }
  94. .frame(maxWidth: .infinity, alignment: .center)
  95. .buttonStyle(.bordered)
  96. .disabled(state.url.isEmpty || state.connecting)
  97. HStack(alignment: .top) {
  98. Text(
  99. "You can import therapy settings from Nightscout. See hint for more information which settings will be overwritten."
  100. )
  101. .font(.footnote)
  102. .foregroundColor(.secondary)
  103. .lineLimit(nil)
  104. Spacer()
  105. Button(
  106. action: {
  107. hintLabel = "Import Settings from Nightscout"
  108. selectedVerboseHint =
  109. "This will overwrite the following Trio therapy settings: \n • Basal Rates \n • Insulin Sensitivities \n • Carb Ratios \n • Target Glucose \n • Duration of Insulin Action"
  110. shouldDisplayHint.toggle()
  111. },
  112. label: {
  113. HStack {
  114. Image(systemName: "questionmark.circle")
  115. }
  116. }
  117. ).buttonStyle(BorderlessButtonStyle())
  118. }.padding(.top)
  119. }.padding(.vertical)
  120. }.listRowBackground(Color.chart)
  121. Section(
  122. content:
  123. {
  124. VStack {
  125. Button {
  126. Task {
  127. await state.backfillGlucose()
  128. }
  129. } label: {
  130. Text("Backfill Glucose")
  131. .font(.title3) }
  132. .frame(maxWidth: .infinity, alignment: .center)
  133. .buttonStyle(.bordered)
  134. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  135. HStack(alignment: .top) {
  136. Text(
  137. "You can backfill missing glucose data from Nightscout."
  138. )
  139. .font(.footnote)
  140. .foregroundColor(.secondary)
  141. .lineLimit(nil)
  142. Spacer()
  143. Button(
  144. action: {
  145. hintLabel = "Backfill Glucose from Nightscout"
  146. selectedVerboseHint =
  147. "Explanation… limitation… etc."
  148. shouldDisplayHint.toggle()
  149. },
  150. label: {
  151. HStack {
  152. Image(systemName: "questionmark.circle")
  153. }
  154. }
  155. ).buttonStyle(BorderlessButtonStyle())
  156. }.padding(.top)
  157. }.padding(.vertical)
  158. }
  159. ).listRowBackground(Color.chart)
  160. }.blur(radius: state.importStatus == .running ? 5 : 0)
  161. if state.importStatus == .running {
  162. CustomProgressView(text: "Importing Profile...")
  163. }
  164. }
  165. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  166. NightscoutImportResultView(resolver: resolver, state: state)
  167. })
  168. .sheet(isPresented: $shouldDisplayHint) {
  169. SettingInputHintView(
  170. hintDetent: $hintDetent,
  171. shouldDisplayHint: $shouldDisplayHint,
  172. hintLabel: hintLabel ?? "",
  173. hintText: selectedVerboseHint ?? "",
  174. sheetTitle: "Help"
  175. )
  176. }
  177. .navigationBarTitle("Nightscout")
  178. .navigationBarTitleDisplayMode(.automatic)
  179. .alert(isPresented: $isImportAlertPresented) {
  180. importAlert ?? Alert(title: Text("Unknown Error"))
  181. }
  182. .scrollContentBackground(.hidden).background(color)
  183. .onAppear(perform: configureView)
  184. }
  185. }
  186. }