NightscoutConfigRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. List {
  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: Text("Are you sure you want to import profile settings from Nightscout?\n\n")
  65. + Text("This will overwrite the following Trio therapy settings:\n")
  66. + Text("• Basal Rates\n")
  67. + Text("• Insulin Sensitivities\n")
  68. + Text("• Carb Ratios\n")
  69. + Text("• Target Glucose\n")
  70. + Text("• Duration of Insulin Action"),
  71. primaryButton: .default(
  72. Text("Yes, Import!"),
  73. action: {
  74. Task {
  75. await state.importSettings()
  76. if state.importStatus == .failed, state.importErrors.isNotEmpty,
  77. let errorMessage = state.importErrors.first
  78. {
  79. DispatchQueue.main.async {
  80. importAlert = Alert(
  81. title: Text("Import Failed"),
  82. message: Text(errorMessage.description),
  83. dismissButton: .default(Text("OK"))
  84. )
  85. isImportAlertPresented = true
  86. }
  87. }
  88. }
  89. }
  90. ),
  91. secondaryButton: .cancel()
  92. )
  93. isImportAlertPresented = true
  94. } label: {
  95. Text("Import Settings")
  96. .font(.title3) }
  97. .frame(maxWidth: .infinity, alignment: .center)
  98. .buttonStyle(.bordered)
  99. .disabled(state.url.isEmpty || state.connecting)
  100. HStack(alignment: .center) {
  101. Text(
  102. "Import therapy settings from Nightscout.\nSee hint for the list of settings available for import."
  103. )
  104. .font(.footnote)
  105. .foregroundColor(.secondary)
  106. .lineLimit(nil)
  107. Spacer()
  108. Button(
  109. action: {
  110. hintLabel = "Import Settings from Nightscout"
  111. selectedVerboseHint =
  112. AnyView(
  113. VStack(spacing: 10) {
  114. Text(
  115. "This will overwrite the following Trio therapy settings:"
  116. )
  117. VStack {
  118. Text("• Basal Rates")
  119. Text("• Insulin Sensitivities")
  120. Text("• Carb Ratios")
  121. Text("• Target Glucose")
  122. Text("• Duration of Insulin Action")
  123. }
  124. }
  125. )
  126. shouldDisplayHint.toggle()
  127. },
  128. label: {
  129. HStack {
  130. Image(systemName: "questionmark.circle")
  131. }
  132. }
  133. ).buttonStyle(BorderlessButtonStyle())
  134. }.padding(.top)
  135. }.padding(.vertical)
  136. }.listRowBackground(Color.chart)
  137. Section(
  138. content:
  139. {
  140. VStack {
  141. Button {
  142. Task {
  143. await state.backfillGlucose()
  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 = "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. }.padding(.top)
  177. }.padding(.vertical)
  178. }
  179. ).listRowBackground(Color.chart)
  180. }
  181. .listSectionSpacing(sectionSpacing)
  182. .blur(radius: state.importStatus == .running ? 5 : 0)
  183. if state.importStatus == .running {
  184. CustomProgressView(text: "Importing Profile...")
  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: "Help"
  197. )
  198. }
  199. .navigationBarTitle("Nightscout")
  200. .navigationBarTitleDisplayMode(.automatic)
  201. .alert(isPresented: $isImportAlertPresented) {
  202. importAlert ?? Alert(title: Text("Unknown Error"))
  203. }
  204. .scrollContentBackground(.hidden).background(color)
  205. .onAppear(perform: configureView)
  206. }
  207. }
  208. }