NightscoutConfigRootView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. Task {
  74. await state.importSettings()
  75. importedHasRun = true
  76. }
  77. }
  78. ),
  79. secondaryButton: .cancel()
  80. )
  81. isImportAlertPresented.toggle()
  82. } label: {
  83. Text("Import Settings")
  84. .font(.title3) }
  85. .frame(maxWidth: .infinity, alignment: .center)
  86. .buttonStyle(.bordered)
  87. .disabled(state.url.isEmpty || state.connecting)
  88. .alert(isPresented: $importedHasRun) {
  89. Alert(
  90. title: Text(
  91. (fetchedErrors.first?.error ?? "")
  92. .count < 4 ? "Settings imported" : "Import Error"
  93. ),
  94. message: Text(
  95. (fetchedErrors.first?.error ?? "").count < 4 ?
  96. NSLocalizedString(
  97. "\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.",
  98. comment: "Imported Profiles Alert"
  99. ) :
  100. NSLocalizedString(
  101. fetchedErrors.first?.error ?? "",
  102. comment: "Import Error"
  103. )
  104. ),
  105. primaryButton: .destructive(
  106. Text("OK")
  107. ),
  108. secondaryButton: .cancel()
  109. )
  110. }
  111. HStack(alignment: .top) {
  112. Text(
  113. "You can import therapy settings from Nightscout. See hint for more information which settings will be overwritten."
  114. )
  115. .font(.footnote)
  116. .foregroundColor(.secondary)
  117. .lineLimit(nil)
  118. Spacer()
  119. Button(
  120. action: {
  121. hintLabel = "Import Settings from Nightscout"
  122. selectedVerboseHint =
  123. "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"
  124. shouldDisplayHint.toggle()
  125. },
  126. label: {
  127. HStack {
  128. Image(systemName: "questionmark.circle")
  129. }
  130. }
  131. ).buttonStyle(BorderlessButtonStyle())
  132. }.padding(.top)
  133. }.padding(.vertical)
  134. }.listRowBackground(Color.chart)
  135. Section(
  136. content:
  137. {
  138. VStack {
  139. Button {
  140. Task {
  141. await state.backfillGlucose()
  142. }
  143. } label: {
  144. Text("Backfill Glucose")
  145. .font(.title3) }
  146. .frame(maxWidth: .infinity, alignment: .center)
  147. .buttonStyle(.bordered)
  148. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  149. HStack(alignment: .top) {
  150. Text(
  151. "You can backfill missing glucose data from Nightscout."
  152. )
  153. .font(.footnote)
  154. .foregroundColor(.secondary)
  155. .lineLimit(nil)
  156. Spacer()
  157. Button(
  158. action: {
  159. hintLabel = "Backfill Glucose from Nightscout"
  160. selectedVerboseHint =
  161. "Explanation… limitation… etc."
  162. shouldDisplayHint.toggle()
  163. },
  164. label: {
  165. HStack {
  166. Image(systemName: "questionmark.circle")
  167. }
  168. }
  169. ).buttonStyle(BorderlessButtonStyle())
  170. }.padding(.top)
  171. }.padding(.vertical)
  172. }
  173. ).listRowBackground(Color.chart)
  174. }
  175. .sheet(isPresented: $shouldDisplayHint) {
  176. SettingInputHintView(
  177. hintDetent: $hintDetent,
  178. shouldDisplayHint: $shouldDisplayHint,
  179. hintLabel: hintLabel ?? "",
  180. hintText: selectedVerboseHint ?? "",
  181. sheetTitle: "Help"
  182. )
  183. }
  184. .navigationBarTitle("Nightscout")
  185. .navigationBarTitleDisplayMode(.automatic)
  186. .alert(isPresented: $isImportAlertPresented) {
  187. importAlert!
  188. }
  189. .scrollContentBackground(.hidden).background(color)
  190. .onAppear(perform: configureView)
  191. }
  192. }
  193. }