NightscoutConfigRootView.swift 12 KB

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