NightscoutConfigRootView.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. ZStack {
  49. Form {
  50. Section(
  51. header: Text("Nightscout Integration"),
  52. content: {
  53. NavigationLink("Connect", destination: NightscoutConnectView(state: state))
  54. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  55. NavigationLink("Fetch & Remote Control", destination: NightscoutFetchView(state: state))
  56. }
  57. ).listRowBackground(Color.chart)
  58. Section {
  59. VStack {
  60. Button {
  61. importAlert = Alert(
  62. title: Text("Import Therapy Settings?"),
  63. message: Text(
  64. NSLocalizedString(
  65. "This will replace some or all of your current therapy settings. Are you sure you want to import profile settings from Nightscout?",
  66. comment: "Nightscout Settings Import Alert"
  67. )
  68. ),
  69. primaryButton: .default(
  70. Text("Yes, Import!"),
  71. action: {
  72. Task {
  73. await state.importSettings()
  74. }
  75. }
  76. ),
  77. secondaryButton: .cancel()
  78. )
  79. isImportAlertPresented = true
  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. HStack(alignment: .top) {
  87. Text(
  88. "You can import therapy settings from Nightscout. See hint for more information which settings will be overwritten."
  89. )
  90. .font(.footnote)
  91. .foregroundColor(.secondary)
  92. .lineLimit(nil)
  93. Spacer()
  94. Button(
  95. action: {
  96. hintLabel = "Import Settings from Nightscout"
  97. selectedVerboseHint =
  98. "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"
  99. shouldDisplayHint.toggle()
  100. },
  101. label: {
  102. HStack {
  103. Image(systemName: "questionmark.circle")
  104. }
  105. }
  106. ).buttonStyle(BorderlessButtonStyle())
  107. }.padding(.top)
  108. }.padding(.vertical)
  109. }.listRowBackground(Color.chart)
  110. Section(
  111. content:
  112. {
  113. VStack {
  114. Button {
  115. Task {
  116. await state.backfillGlucose()
  117. }
  118. } label: {
  119. Text("Backfill Glucose")
  120. .font(.title3) }
  121. .frame(maxWidth: .infinity, alignment: .center)
  122. .buttonStyle(.bordered)
  123. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  124. HStack(alignment: .top) {
  125. Text(
  126. "You can backfill missing glucose data from Nightscout."
  127. )
  128. .font(.footnote)
  129. .foregroundColor(.secondary)
  130. .lineLimit(nil)
  131. Spacer()
  132. Button(
  133. action: {
  134. hintLabel = "Backfill Glucose from Nightscout"
  135. selectedVerboseHint =
  136. "Explanation… limitation… etc."
  137. shouldDisplayHint.toggle()
  138. },
  139. label: {
  140. HStack {
  141. Image(systemName: "questionmark.circle")
  142. }
  143. }
  144. ).buttonStyle(BorderlessButtonStyle())
  145. }.padding(.top)
  146. }.padding(.vertical)
  147. }
  148. ).listRowBackground(Color.chart)
  149. }.blur(radius: state.importStatus == .running ? 5 : 0)
  150. if state.importStatus == .running {
  151. CustomProgressView(text: "Importing Profile...")
  152. }
  153. }
  154. .fullScreenCover(isPresented: $state.isImportResultReviewPresented, content: {
  155. NightscoutImportResultView(resolver: resolver, state: state)
  156. })
  157. .sheet(isPresented: $shouldDisplayHint) {
  158. SettingInputHintView(
  159. hintDetent: $hintDetent,
  160. shouldDisplayHint: $shouldDisplayHint,
  161. hintLabel: hintLabel ?? "",
  162. hintText: selectedVerboseHint ?? "",
  163. sheetTitle: "Help"
  164. )
  165. }
  166. .navigationBarTitle("Nightscout")
  167. .navigationBarTitleDisplayMode(.automatic)
  168. .alert(isPresented: $isImportAlertPresented) {
  169. importAlert!
  170. }
  171. .scrollContentBackground(.hidden).background(color)
  172. .onAppear(perform: configureView)
  173. }
  174. }
  175. }