NightscoutConfigRootView.swift 4.5 KB

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