NightscoutConfigRootView.swift 10 KB

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