NightscoutConfigRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 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: VStack(spacing: 10) {
  65. Text("Are you sure you want to import profile settings from Nightscout?")
  66. Text("This will overwrite the following Trio therapy settings:")
  67. VStack(alignment: .leading, spacing: 5) {
  68. Text("• Basal Rates")
  69. Text("• Insulin Sensitivities")
  70. Text("• Carb Ratios")
  71. Text("• Target Glucose")
  72. Text("• Duration of Insulin Action")
  73. }
  74. }
  75. ),
  76. primaryButton: .default(
  77. Text("Yes, Import!"),
  78. action: {
  79. Task {
  80. await state.importSettings()
  81. // Check the import status and errors after the import process finishes
  82. if state.importStatus == .failed, state.importErrors.isNotEmpty,
  83. let errorMessage = state.importErrors.first
  84. {
  85. DispatchQueue.main.async {
  86. importAlert = Alert(
  87. title: Text("Import Failed"),
  88. message: Text(errorMessage.description),
  89. dismissButton: .default(Text("OK"))
  90. )
  91. isImportAlertPresented = true
  92. }
  93. }
  94. }
  95. }
  96. ),
  97. secondaryButton: .cancel()
  98. )
  99. isImportAlertPresented = true
  100. } label: {
  101. Text("Import Settings")
  102. .font(.title3) }
  103. .frame(maxWidth: .infinity, alignment: .center)
  104. .buttonStyle(.bordered)
  105. .disabled(state.url.isEmpty || state.connecting)
  106. HStack(alignment: .top) {
  107. Text("Import therapy settings from Nightscout\nSee hint for the list of settings available for import")
  108. .font(.footnote)
  109. .foregroundColor(.secondary)
  110. .lineLimit(nil)
  111. Spacer()
  112. Button(
  113. action: {
  114. hintLabel = "Import Settings from Nightscout"
  115. selectedVerboseHint =
  116. AnyView(
  117. Text("This will overwrite the following Trio therapy settings:\n\n• Basal Rates\n• Insulin Sensitivities\n• Carb Ratios\n• Target Glucose\n• Duration of Insulin Action")
  118. )
  119. shouldDisplayHint.toggle()
  120. },
  121. label: {
  122. HStack {
  123. Image(systemName: "questionmark.circle")
  124. }
  125. }
  126. ).buttonStyle(BorderlessButtonStyle())
  127. }.padding(.top)
  128. }.padding(.vertical)
  129. }.listRowBackground(Color.chart)
  130. Section(
  131. content:
  132. {
  133. VStack {
  134. Button {
  135. Task {
  136. await state.backfillGlucose()
  137. }
  138. } label: {
  139. Text("Backfill Glucose")
  140. .font(.title3) }
  141. .frame(maxWidth: .infinity, alignment: .center)
  142. .buttonStyle(.bordered)
  143. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  144. HStack(alignment: .top) {
  145. Text(
  146. "Backfill missing glucose data from Nightscout"
  147. )
  148. .font(.footnote)
  149. .foregroundColor(.secondary)
  150. .lineLimit(nil)
  151. Spacer()
  152. Button(
  153. action: {
  154. hintLabel = "Backfill Glucose from Nightscout"
  155. selectedVerboseHint =
  156. AnyView(
  157. Text(
  158. "This will backfill 24 hours of glucose data from your connected Nightscout URL to Trio"
  159. )
  160. )
  161. shouldDisplayHint.toggle()
  162. },
  163. label: {
  164. HStack {
  165. Image(systemName: "questionmark.circle")
  166. }
  167. }
  168. ).buttonStyle(BorderlessButtonStyle())
  169. }.padding(.top)
  170. }.padding(.vertical)
  171. }
  172. ).listRowBackground(Color.chart)
  173. }.blur(radius: state.importStatus == .running ? 5 : 0)
  174. if state.importStatus == .running {
  175. CustomProgressView(text: "Importing Profile...")
  176. }
  177. }
  178. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  179. NightscoutImportResultView(resolver: resolver, state: state)
  180. })
  181. .sheet(isPresented: $shouldDisplayHint) {
  182. SettingInputHintView(
  183. hintDetent: $hintDetent,
  184. shouldDisplayHint: $shouldDisplayHint,
  185. hintLabel: hintLabel ?? "",
  186. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  187. sheetTitle: "Help"
  188. )
  189. }
  190. .navigationBarTitle("Nightscout")
  191. .navigationBarTitleDisplayMode(.automatic)
  192. .alert(isPresented: $isImportAlertPresented) {
  193. importAlert ?? Alert(title: Text("Unknown Error"))
  194. }
  195. .scrollContentBackground(.hidden).background(color)
  196. .onAppear(perform: configureView)
  197. }
  198. }
  199. }