NightscoutConfigRootView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. @Environment(\.colorScheme) var colorScheme
  13. var color: LinearGradient {
  14. colorScheme == .dark ? LinearGradient(
  15. gradient: Gradient(colors: [
  16. Color.bgDarkBlue,
  17. Color.bgDarkerDarkBlue
  18. ]),
  19. startPoint: .top,
  20. endPoint: .bottom
  21. )
  22. :
  23. LinearGradient(
  24. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  25. startPoint: .top,
  26. endPoint: .bottom
  27. )
  28. }
  29. @FetchRequest(
  30. entity: ImportError.entity(),
  31. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)], predicate: NSPredicate(
  32. format: "date > %@", Date().addingTimeInterval(-1.minutes.timeInterval) as NSDate
  33. )
  34. ) var fetchedErrors: FetchedResults<ImportError>
  35. private var portFormater: NumberFormatter {
  36. let formatter = NumberFormatter()
  37. formatter.allowsFloats = false
  38. formatter.usesGroupingSeparator = false
  39. return formatter
  40. }
  41. var body: some View {
  42. Form {
  43. Section {
  44. NavigationLink("Connect", destination: NightscoutConnectView(state: state))
  45. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  46. NavigationLink("Fetch and Remote Control", destination: NightscoutFetchView(state: state))
  47. }.listRowBackground(Color.chart)
  48. Section(
  49. header: Text("Import Settings from Nightscout"),
  50. footer: VStack(alignment: .leading, spacing: 2) {
  51. Text(
  52. "Importing settings from Nightscout will overwrite these settings in Trio Settings -> Configuration:"
  53. )
  54. Text(" • ") + Text("DIA (Pump settings)")
  55. Text(" • ") + Text("Basal Profile")
  56. Text(" • ") + Text("Insulin Sensitivities")
  57. Text(" • ") + Text("Carb Ratios")
  58. Text(" • ") + Text("Target Glucose")
  59. }
  60. ) {
  61. Button("Import settings") {
  62. importAlert = Alert(
  63. title: Text("Import settings?"),
  64. message: Text(
  65. "\n" +
  66. NSLocalizedString(
  67. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  68. comment: "Profile Import Alert"
  69. ) +
  70. "\n"
  71. ),
  72. primaryButton: .destructive(
  73. Text("Yes, Import"),
  74. action: {
  75. state.importSettings()
  76. importedHasRun = true
  77. }
  78. ),
  79. secondaryButton: .cancel()
  80. )
  81. isImportAlertPresented.toggle()
  82. }
  83. .listRowBackground(Color.chart)
  84. .disabled(state.url.isEmpty || state.connecting)
  85. .alert(isPresented: $importedHasRun) {
  86. Alert(
  87. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  88. message: Text(
  89. (fetchedErrors.first?.error ?? "").count < 4 ?
  90. NSLocalizedString(
  91. "\nNow please verify all of your new settings thoroughly: \n\n • DIA (Pump settings)\n • Basal Profile\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.",
  92. comment: "Imported Profiles Alert"
  93. ) :
  94. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  95. ),
  96. primaryButton: .destructive(
  97. Text("OK")
  98. ),
  99. secondaryButton: .cancel()
  100. )
  101. }
  102. }
  103. Section {
  104. Button("Backfill glucose") {
  105. Task {
  106. await state.backfillGlucose()
  107. }
  108. }
  109. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  110. }
  111. header: { Text("Backfill glucose from Nightscout")
  112. }.listRowBackground(Color.chart)
  113. }
  114. .navigationBarTitle("Nightscout Config")
  115. .navigationBarTitleDisplayMode(.automatic)
  116. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  117. .alert(isPresented: $isImportAlertPresented) {
  118. importAlert!
  119. }
  120. .scrollContentBackground(.hidden).background(color)
  121. .onAppear(perform: configureView)
  122. }
  123. }
  124. }