NightscoutConfigRootView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Text(" • ") + Text("DIA (Pump settings)")
  29. Text(" • ") + Text("Basal Profile")
  30. Text(" • ") + Text("Insulin Sensitivities")
  31. Text(" • ") + Text("Carb Ratios")
  32. Text(" • ") + Text("Target Glucose")
  33. }
  34. ) {
  35. Button("Import settings") {
  36. importAlert = Alert(
  37. title: Text("Import settings?"),
  38. message: Text(
  39. "\n" +
  40. NSLocalizedString(
  41. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  42. comment: "Profile Import Alert"
  43. ) +
  44. "\n"
  45. ),
  46. primaryButton: .destructive(
  47. Text("Yes, Import"),
  48. action: {
  49. state.importSettings()
  50. importedHasRun = true
  51. }
  52. ),
  53. secondaryButton: .cancel()
  54. )
  55. isImportAlertPresented.toggle()
  56. }.disabled(state.url.isEmpty || state.connecting)
  57. .alert(isPresented: $importedHasRun) {
  58. Alert(
  59. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  60. message: Text(
  61. (fetchedErrors.first?.error ?? "").count < 4 ?
  62. NSLocalizedString(
  63. "\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.",
  64. comment: "Imported Profiles Alert"
  65. ) :
  66. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  67. ),
  68. primaryButton: .destructive(
  69. Text("OK")
  70. ),
  71. secondaryButton: .cancel()
  72. )
  73. }
  74. }
  75. Section {
  76. Button("Backfill glucose") { state.backfillGlucose() }
  77. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  78. } header: { Text("Backfill glucose from Nightscout")
  79. }
  80. }
  81. .navigationBarTitle("Nightscout Config")
  82. .navigationBarTitleDisplayMode(.automatic)
  83. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  84. .alert(isPresented: $isImportAlertPresented) {
  85. importAlert!
  86. }
  87. .onAppear(perform: configureView)
  88. }
  89. }
  90. }