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. }.padding(.top)
  122. }.padding(.vertical)
  123. }.listRowBackground(Color.chart)
  124. Section(
  125. content:
  126. {
  127. VStack {
  128. Button {
  129. Task {
  130. await state.backfillGlucose()
  131. if !state.message.isEmpty && state.message.hasPrefix("Error:") {
  132. DispatchQueue.main.async {
  133. backfillAlert = Alert(
  134. title: Text("Backfill Failed"),
  135. message: Text(state.message),
  136. dismissButton: .default(Text("OK"))
  137. )
  138. isBackfillAlertPresented = true
  139. }
  140. }
  141. }
  142. } label: {
  143. Text("Backfill Glucose")
  144. .font(.title3) }
  145. .frame(maxWidth: .infinity, alignment: .center)
  146. .buttonStyle(.bordered)
  147. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  148. HStack(alignment: .center) {
  149. Text(
  150. "Backfill missing glucose data from Nightscout."
  151. )
  152. .font(.footnote)
  153. .foregroundColor(.secondary)
  154. .lineLimit(nil)
  155. Spacer()
  156. Button(
  157. action: {
  158. hintLabel = String(localized: "Backfill Glucose from Nightscout")
  159. selectedVerboseHint =
  160. AnyView(
  161. Text(
  162. "This will backfill 24 hours of glucose data from your connected Nightscout URL to Trio"
  163. )
  164. )
  165. shouldDisplayHint.toggle()
  166. },
  167. label: {
  168. HStack {
  169. Image(systemName: "questionmark.circle")
  170. }
  171. }
  172. ).buttonStyle(BorderlessButtonStyle())
  173. }.padding(.top)
  174. }.padding(.vertical)
  175. }
  176. ).listRowBackground(Color.chart)
  177. }
  178. .listSectionSpacing(sectionSpacing)
  179. .blur(radius: state.importStatus == .running ? 5 : 0)
  180. if state.importStatus == .running {
  181. CustomProgressView(text: String(
  182. localized: "Importing Profile...",
  183. comment: "Progress text when importing profile via Nightscout"
  184. ))
  185. }
  186. }
  187. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  188. NightscoutImportResultView(resolver: resolver, state: state)
  189. })
  190. .sheet(isPresented: $shouldDisplayHint) {
  191. SettingInputHintView(
  192. hintDetent: $hintDetent,
  193. shouldDisplayHint: $shouldDisplayHint,
  194. hintLabel: hintLabel ?? "",
  195. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  196. sheetTitle: String(localized: "Help", comment: "Help sheet title")
  197. )
  198. }
  199. .navigationBarTitle("Nightscout")
  200. .navigationBarTitleDisplayMode(.automatic)
  201. .alert(isPresented: $isImportAlertPresented) {
  202. importAlert ?? Alert(title: Text("Unknown Error"))
  203. }
  204. .alert(isPresented: $isBackfillAlertPresented) {
  205. backfillAlert ?? Alert(title: Text("Unknown Error"))
  206. }
  207. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  208. .onAppear(perform: configureView)
  209. }
  210. }
  211. }