NightscoutConfigRootView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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("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: .top) {
  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. Text(
  114. "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"
  115. )
  116. )
  117. shouldDisplayHint.toggle()
  118. },
  119. label: {
  120. HStack {
  121. Image(systemName: "questionmark.circle")
  122. }
  123. }
  124. ).buttonStyle(BorderlessButtonStyle())
  125. }.padding(.top)
  126. }.padding(.vertical)
  127. }.listRowBackground(Color.chart)
  128. Section(
  129. content:
  130. {
  131. VStack {
  132. Button {
  133. Task {
  134. await state.backfillGlucose()
  135. }
  136. } label: {
  137. Text("Backfill Glucose")
  138. .font(.title3) }
  139. .frame(maxWidth: .infinity, alignment: .center)
  140. .buttonStyle(.bordered)
  141. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  142. HStack(alignment: .top) {
  143. Text(
  144. "Backfill missing glucose data from Nightscout"
  145. )
  146. .font(.footnote)
  147. .foregroundColor(.secondary)
  148. .lineLimit(nil)
  149. Spacer()
  150. Button(
  151. action: {
  152. hintLabel = "Backfill Glucose from Nightscout"
  153. selectedVerboseHint =
  154. AnyView(
  155. Text(
  156. "This will backfill 24 hours of glucose data from your connected Nightscout URL to Trio"
  157. )
  158. )
  159. shouldDisplayHint.toggle()
  160. },
  161. label: {
  162. HStack {
  163. Image(systemName: "questionmark.circle")
  164. }
  165. }
  166. ).buttonStyle(BorderlessButtonStyle())
  167. }.padding(.top)
  168. }.padding(.vertical)
  169. }
  170. ).listRowBackground(Color.chart)
  171. }.blur(radius: state.importStatus == .running ? 5 : 0)
  172. if state.importStatus == .running {
  173. CustomProgressView(text: "Importing Profile...")
  174. }
  175. }
  176. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  177. NightscoutImportResultView(resolver: resolver, state: state)
  178. })
  179. .sheet(isPresented: $shouldDisplayHint) {
  180. SettingInputHintView(
  181. hintDetent: $hintDetent,
  182. shouldDisplayHint: $shouldDisplayHint,
  183. hintLabel: hintLabel ?? "",
  184. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  185. sheetTitle: "Help"
  186. )
  187. }
  188. .navigationBarTitle("Nightscout")
  189. .navigationBarTitleDisplayMode(.automatic)
  190. .alert(isPresented: $isImportAlertPresented) {
  191. importAlert ?? Alert(title: Text("Unknown Error"))
  192. }
  193. .scrollContentBackground(.hidden).background(color)
  194. .onAppear(perform: configureView)
  195. }
  196. }
  197. }