NightscoutConfigRootView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. NavigationLink("Connect", destination: NightscoutConnectView(state: state))
  44. NavigationLink("Upload", destination: NightscoutUploadView(state: state))
  45. NavigationLink("Fetch and Remote Control", destination: NightscoutFetchView(state: state))
  46. Section(
  47. header: Text("Import Settings from Nightscout"),
  48. footer: VStack(alignment: .leading, spacing: 2) {
  49. Text(
  50. "Importing settings from Nightscout will overwrite these settings in Trio Settings -> Configuration:"
  51. )
  52. Text(" • ") + Text("DIA (Pump settings)")
  53. Text(" • ") + Text("Basal Profile")
  54. Text(" • ") + Text("Insulin Sensitivities")
  55. Text(" • ") + Text("Carb Ratios")
  56. Text(" • ") + Text("Target Glucose")
  57. }
  58. ) {
  59. Button("Import settings") {
  60. importAlert = Alert(
  61. title: Text("Import settings?"),
  62. message: Text(
  63. "\n" +
  64. NSLocalizedString(
  65. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  66. comment: "Profile Import Alert"
  67. ) +
  68. "\n"
  69. ),
  70. primaryButton: .destructive(
  71. Text("Yes, Import"),
  72. action: {
  73. state.importSettings()
  74. importedHasRun = true
  75. }
  76. ),
  77. secondaryButton: .cancel()
  78. )
  79. isImportAlertPresented.toggle()
  80. }.disabled(state.url.isEmpty || state.connecting)
  81. .alert(isPresented: $importedHasRun) {
  82. Alert(
  83. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  84. message: Text(
  85. (fetchedErrors.first?.error ?? "").count < 4 ?
  86. NSLocalizedString(
  87. "\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.",
  88. comment: "Imported Profiles Alert"
  89. ) :
  90. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  91. ),
  92. primaryButton: .destructive(
  93. Text("OK")
  94. ),
  95. secondaryButton: .cancel()
  96. )
  97. }
  98. }
  99. Section {
  100. Button("Backfill glucose") {
  101. Task {
  102. state.backfillGlucose()
  103. }
  104. }
  105. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  106. } header: { Text("Backfill glucose from Nightscout")
  107. }
  108. }
  109. .navigationBarTitle("Nightscout Config")
  110. .navigationBarTitleDisplayMode(.automatic)
  111. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  112. .alert(isPresented: $isImportAlertPresented) {
  113. importAlert!
  114. }
  115. .onAppear(perform: configureView)
  116. }
  117. }
  118. }