NightscoutConfigRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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: AnyView?
  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. @Environment(AppState.self) var appState
  20. var body: some View {
  21. ZStack {
  22. List {
  23. Section(
  24. header: Text("Nightscout Integration"),
  25. content: {
  26. NavigationLink(destination: NightscoutConnectView(state: state), label: {
  27. HStack {
  28. Text("Connect")
  29. ZStack {
  30. if state.isConnectedToNS {
  31. Image(systemName: "network")
  32. Image(systemName: "checkmark.circle.fill").foregroundColor(.green).font(.caption2)
  33. .offset(x: 9, y: 6)
  34. } else {
  35. Image(systemName: "network.slash")
  36. }
  37. }
  38. }
  39. })
  40. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  41. NavigationLink("Fetch", destination: NightscoutFetchView(state: state))
  42. }
  43. ).listRowBackground(Color.chart)
  44. Section {
  45. VStack {
  46. Button {
  47. importAlert = Alert(
  48. title: Text("Import Therapy Settings?"),
  49. message: Text("Are you sure you want to import profile settings from Nightscout?\n\n")
  50. + Text("This will overwrite the following Trio therapy settings:\n")
  51. + Text("• Basal Rates\n")
  52. + Text("• Insulin Sensitivities\n")
  53. + Text("• Carb Ratios\n")
  54. + Text("• Glucose Targets\n")
  55. + Text("• Duration of Insulin Action"),
  56. primaryButton: .default(
  57. Text("Yes, Import!"),
  58. action: {
  59. Task {
  60. await state.importSettings()
  61. if state.importStatus == .failed, state.importErrors.isNotEmpty,
  62. let errorMessage = state.importErrors.first
  63. {
  64. DispatchQueue.main.async {
  65. importAlert = Alert(
  66. title: Text("Import Failed"),
  67. message: Text(errorMessage.description),
  68. dismissButton: .default(Text("OK"))
  69. )
  70. isImportAlertPresented = true
  71. }
  72. }
  73. }
  74. }
  75. ),
  76. secondaryButton: .cancel()
  77. )
  78. isImportAlertPresented = true
  79. } label: {
  80. Text("Import Settings")
  81. .font(.title3) }
  82. .frame(maxWidth: .infinity, alignment: .center)
  83. .buttonStyle(.bordered)
  84. .disabled(state.url.isEmpty || state.connecting)
  85. HStack(alignment: .center) {
  86. Text(
  87. "Import therapy settings from Nightscout.\nSee hint for the list of settings available for import."
  88. )
  89. .font(.footnote)
  90. .foregroundColor(.secondary)
  91. .lineLimit(nil)
  92. Spacer()
  93. Button(
  94. action: {
  95. hintLabel = "Import Settings from Nightscout"
  96. selectedVerboseHint =
  97. AnyView(
  98. VStack(alignment: .leading, spacing: 10) {
  99. Text(
  100. "This will overwrite the following Trio therapy settings:"
  101. )
  102. VStack(alignment: .leading) {
  103. Text("• Basal Rates")
  104. Text("• Insulin Sensitivities")
  105. Text("• Carb Ratios")
  106. Text("• Glucose Targets")
  107. Text("• Duration of Insulin Action")
  108. }
  109. }
  110. )
  111. shouldDisplayHint.toggle()
  112. },
  113. label: {
  114. HStack {
  115. Image(systemName: "questionmark.circle")
  116. }
  117. }
  118. ).buttonStyle(BorderlessButtonStyle())
  119. }.padding(.top)
  120. }.padding(.vertical)
  121. }.listRowBackground(Color.chart)
  122. Section(
  123. content:
  124. {
  125. VStack {
  126. Button {
  127. Task {
  128. await state.backfillGlucose()
  129. }
  130. } label: {
  131. Text("Backfill Glucose")
  132. .font(.title3) }
  133. .frame(maxWidth: .infinity, alignment: .center)
  134. .buttonStyle(.bordered)
  135. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  136. HStack(alignment: .center) {
  137. Text(
  138. "Backfill missing glucose data from Nightscout."
  139. )
  140. .font(.footnote)
  141. .foregroundColor(.secondary)
  142. .lineLimit(nil)
  143. Spacer()
  144. Button(
  145. action: {
  146. hintLabel = "Backfill Glucose from Nightscout"
  147. selectedVerboseHint =
  148. AnyView(
  149. Text(
  150. "This will backfill 24 hours of glucose data from your connected Nightscout URL to Trio"
  151. )
  152. )
  153. shouldDisplayHint.toggle()
  154. },
  155. label: {
  156. HStack {
  157. Image(systemName: "questionmark.circle")
  158. }
  159. }
  160. ).buttonStyle(BorderlessButtonStyle())
  161. }.padding(.top)
  162. }.padding(.vertical)
  163. }
  164. ).listRowBackground(Color.chart)
  165. }
  166. .listSectionSpacing(sectionSpacing)
  167. .blur(radius: state.importStatus == .running ? 5 : 0)
  168. if state.importStatus == .running {
  169. CustomProgressView(text: "Importing Profile...")
  170. }
  171. }
  172. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  173. NightscoutImportResultView(resolver: resolver, state: state)
  174. })
  175. .sheet(isPresented: $shouldDisplayHint) {
  176. SettingInputHintView(
  177. hintDetent: $hintDetent,
  178. shouldDisplayHint: $shouldDisplayHint,
  179. hintLabel: hintLabel ?? "",
  180. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  181. sheetTitle: "Help"
  182. )
  183. }
  184. .navigationBarTitle("Nightscout")
  185. .navigationBarTitleDisplayMode(.automatic)
  186. .alert(isPresented: $isImportAlertPresented) {
  187. importAlert ?? Alert(title: Text("Unknown Error"))
  188. }
  189. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  190. .onAppear(perform: configureView)
  191. }
  192. }
  193. }