NightscoutConfigRootView.swift 11 KB

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