NightscoutConfigRootView.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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("Connect", destination: NightscoutConnectView(state: state))
  42. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  43. NavigationLink("Fetch & Remote Control", destination: NightscoutFetchView(state: state))
  44. }
  45. ).listRowBackground(Color.chart)
  46. Section {
  47. VStack {
  48. Button {
  49. importAlert = Alert(
  50. title: Text("Import Therapy Settings?"),
  51. message: Text(
  52. "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."
  53. ),
  54. primaryButton: .default(
  55. Text("Yes, Import!"),
  56. action: {
  57. Task {
  58. await state.importSettings()
  59. // Check the import status and errors after the import process finishes
  60. if state.importStatus == .failed, state.importErrors.isNotEmpty,
  61. let errorMessage = state.importErrors.first
  62. {
  63. DispatchQueue.main.async {
  64. importAlert = Alert(
  65. title: Text("Import Failed"),
  66. message: Text(errorMessage.description),
  67. dismissButton: .default(Text("OK"))
  68. )
  69. isImportAlertPresented = true
  70. }
  71. }
  72. }
  73. }
  74. ),
  75. secondaryButton: .cancel()
  76. )
  77. isImportAlertPresented = true
  78. } label: {
  79. Text("Import Settings")
  80. .font(.title3) }
  81. .frame(maxWidth: .infinity, alignment: .center)
  82. .buttonStyle(.bordered)
  83. .disabled(state.url.isEmpty || state.connecting)
  84. HStack(alignment: .top) {
  85. Text(
  86. "You can import therapy settings from Nightscout. See hint for more information which settings will be overwritten."
  87. )
  88. .font(.footnote)
  89. .foregroundColor(.secondary)
  90. .lineLimit(nil)
  91. Spacer()
  92. Button(
  93. action: {
  94. hintLabel = "Import Settings from Nightscout"
  95. selectedVerboseHint =
  96. "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"
  97. shouldDisplayHint.toggle()
  98. },
  99. label: {
  100. HStack {
  101. Image(systemName: "questionmark.circle")
  102. }
  103. }
  104. ).buttonStyle(BorderlessButtonStyle())
  105. }.padding(.top)
  106. }.padding(.vertical)
  107. }.listRowBackground(Color.chart)
  108. Section(
  109. content:
  110. {
  111. VStack {
  112. Button {
  113. Task {
  114. await state.backfillGlucose()
  115. }
  116. } label: {
  117. Text("Backfill Glucose")
  118. .font(.title3) }
  119. .frame(maxWidth: .infinity, alignment: .center)
  120. .buttonStyle(.bordered)
  121. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  122. HStack(alignment: .top) {
  123. Text(
  124. "You can backfill missing glucose data from Nightscout."
  125. )
  126. .font(.footnote)
  127. .foregroundColor(.secondary)
  128. .lineLimit(nil)
  129. Spacer()
  130. Button(
  131. action: {
  132. hintLabel = "Backfill Glucose from Nightscout"
  133. selectedVerboseHint =
  134. "Explanation… limitation… etc."
  135. shouldDisplayHint.toggle()
  136. },
  137. label: {
  138. HStack {
  139. Image(systemName: "questionmark.circle")
  140. }
  141. }
  142. ).buttonStyle(BorderlessButtonStyle())
  143. }.padding(.top)
  144. }.padding(.vertical)
  145. }
  146. ).listRowBackground(Color.chart)
  147. }.blur(radius: state.importStatus == .running ? 5 : 0)
  148. if state.importStatus == .running {
  149. CustomProgressView(text: "Importing Profile...")
  150. }
  151. }
  152. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  153. NightscoutImportResultView(resolver: resolver, state: state)
  154. })
  155. .sheet(isPresented: $shouldDisplayHint) {
  156. SettingInputHintView(
  157. hintDetent: $hintDetent,
  158. shouldDisplayHint: $shouldDisplayHint,
  159. hintLabel: hintLabel ?? "",
  160. hintText: selectedVerboseHint ?? "",
  161. sheetTitle: "Help"
  162. )
  163. }
  164. .navigationBarTitle("Nightscout")
  165. .navigationBarTitleDisplayMode(.automatic)
  166. .alert(isPresented: $isImportAlertPresented) {
  167. importAlert ?? Alert(title: Text("Unknown Error"))
  168. }
  169. .scrollContentBackground(.hidden).background(color)
  170. .onAppear(perform: configureView)
  171. }
  172. }
  173. }