NightscoutConfigRootView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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: String?
  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. @FetchRequest(
  36. entity: ImportError.entity(),
  37. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)], predicate: NSPredicate(
  38. format: "date > %@", Date().addingTimeInterval(-1.minutes.timeInterval) as NSDate
  39. )
  40. ) var fetchedErrors: FetchedResults<ImportError>
  41. private var portFormater: NumberFormatter {
  42. let formatter = NumberFormatter()
  43. formatter.allowsFloats = false
  44. formatter.usesGroupingSeparator = false
  45. return formatter
  46. }
  47. var body: some View {
  48. Form {
  49. Section(
  50. header: Text("Nightscout Integration"),
  51. content: {
  52. NavigationLink("Connect", destination: NightscoutConnectView(state: state))
  53. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  54. NavigationLink("Fetch & Remote Control", destination: NightscoutFetchView(state: state))
  55. }
  56. ).listRowBackground(Color.chart)
  57. Section {
  58. VStack {
  59. Button {
  60. importAlert = Alert(
  61. title: Text("Import settings?"),
  62. message: Text(
  63. "\n" +
  64. NSLocalizedString(
  65. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  66. comment: "Profile Import Alert"
  67. ) +
  68. "\n"
  69. ),
  70. primaryButton: .destructive(
  71. Text("Yes, Import"),
  72. action: {
  73. state.importSettings()
  74. importedHasRun = true
  75. }
  76. ),
  77. secondaryButton: .cancel()
  78. )
  79. isImportAlertPresented.toggle()
  80. } label: {
  81. Text("Import Settings")
  82. .font(.title3) }
  83. .frame(maxWidth: .infinity, alignment: .center)
  84. .buttonStyle(.bordered)
  85. .disabled(state.url.isEmpty || state.connecting)
  86. .alert(isPresented: $importedHasRun) {
  87. Alert(
  88. title: Text(
  89. (fetchedErrors.first?.error ?? "")
  90. .count < 4 ? "Settings imported" : "Import Error"
  91. ),
  92. message: Text(
  93. (fetchedErrors.first?.error ?? "").count < 4 ?
  94. NSLocalizedString(
  95. "\nNow please verify all of your new settings thoroughly: \n\n • DIA (Pump settings)\n • Basal Rates\n • Insulin Sensitivities\n • Carb Ratios\n • Target Glucose\n\n in Trio Settings -> Configuration.\n\nBad or invalid profile settings could have disastrous effects.",
  96. comment: "Imported Profiles Alert"
  97. ) :
  98. NSLocalizedString(
  99. fetchedErrors.first?.error ?? "",
  100. comment: "Import Error"
  101. )
  102. ),
  103. primaryButton: .destructive(
  104. Text("OK")
  105. ),
  106. secondaryButton: .cancel()
  107. )
  108. }
  109. HStack(alignment: .top) {
  110. Text(
  111. "You can import therapy settings from Nightscout. See hint for more information which settings will be overwritten."
  112. )
  113. .font(.footnote)
  114. .foregroundColor(.secondary)
  115. .lineLimit(nil)
  116. Spacer()
  117. Button(
  118. action: {
  119. hintLabel = "Import Settings from Nightscout"
  120. selectedVerboseHint =
  121. "Importing settings from Nightscout will overwrite the following Trio therapy settings: \n • DIA (Pump settings) \n • Basal Profile \n • Insulin Sensitivities \n • Carb Ratios \n • Target Glucose"
  122. shouldDisplayHint.toggle()
  123. },
  124. label: {
  125. HStack {
  126. Image(systemName: "questionmark.circle")
  127. }
  128. }
  129. ).buttonStyle(BorderlessButtonStyle())
  130. }.padding(.top)
  131. }.padding(.vertical)
  132. }.listRowBackground(Color.chart)
  133. Section(
  134. content:
  135. {
  136. VStack {
  137. Button {
  138. Task {
  139. await state.backfillGlucose()
  140. }
  141. } label: {
  142. Text("Backfill Glucose")
  143. .font(.title3) }
  144. .frame(maxWidth: .infinity, alignment: .center)
  145. .buttonStyle(.bordered)
  146. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  147. HStack(alignment: .top) {
  148. Text(
  149. "You can backfill missing glucose data from Nightscout."
  150. )
  151. .font(.footnote)
  152. .foregroundColor(.secondary)
  153. .lineLimit(nil)
  154. Spacer()
  155. Button(
  156. action: {
  157. hintLabel = "Backfill Glucose from Nightscout"
  158. selectedVerboseHint =
  159. "Explanation… limitation… etc."
  160. shouldDisplayHint.toggle()
  161. },
  162. label: {
  163. HStack {
  164. Image(systemName: "questionmark.circle")
  165. }
  166. }
  167. ).buttonStyle(BorderlessButtonStyle())
  168. }.padding(.top)
  169. }.padding(.vertical)
  170. }
  171. ).listRowBackground(Color.chart)
  172. }
  173. .sheet(isPresented: $shouldDisplayHint) {
  174. SettingInputHintView(
  175. hintDetent: $hintDetent,
  176. shouldDisplayHint: $shouldDisplayHint,
  177. hintLabel: hintLabel ?? "",
  178. hintText: selectedVerboseHint ?? "",
  179. sheetTitle: "Help"
  180. )
  181. }
  182. .sheet(isPresented: $shouldDisplayHint) {
  183. SettingInputHintView(
  184. hintDetent: $hintDetent,
  185. shouldDisplayHint: $shouldDisplayHint,
  186. hintLabel: hintLabel ?? "",
  187. hintText: selectedVerboseHint ?? "",
  188. sheetTitle: "Help"
  189. )
  190. }
  191. .navigationBarTitle("Nightscout")
  192. .navigationBarTitleDisplayMode(.automatic)
  193. .alert(isPresented: $isImportAlertPresented) {
  194. importAlert!
  195. }
  196. .scrollContentBackground(.hidden).background(color)
  197. .onAppear(perform: configureView)
  198. }
  199. }
  200. }