NightscoutConfigRootView.swift 5.2 KB

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