NightscoutConfigRootView.swift 10 KB

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