NightscoutConfigRootView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension NightscoutConfig {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State var importAlert: Alert?
  9. @State var isImportAlertPresented = false
  10. @State var importedHasRun = false
  11. @FetchRequest(
  12. entity: ImportError.entity(),
  13. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)], predicate: NSPredicate(
  14. format: "date > %@", Date().addingTimeInterval(-1.minutes.timeInterval) as NSDate
  15. )
  16. ) var fetchedErrors: FetchedResults<ImportError>
  17. private var portFormater: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.allowsFloats = false
  20. return formatter
  21. }
  22. var body: some View {
  23. Form {
  24. Section {
  25. TextField("URL", text: $state.url)
  26. .disableAutocorrection(true)
  27. .textContentType(.URL)
  28. .autocapitalization(.none)
  29. .keyboardType(.URL)
  30. SecureField("API secret", text: $state.secret)
  31. .disableAutocorrection(true)
  32. .autocapitalization(.none)
  33. .textContentType(.password)
  34. .keyboardType(.asciiCapable)
  35. if !state.message.isEmpty {
  36. Text(state.message)
  37. }
  38. if state.connecting {
  39. HStack {
  40. Text("Connecting...")
  41. Spacer()
  42. ProgressView()
  43. }
  44. }
  45. }
  46. Section {
  47. Button("Connect") { state.connect() }
  48. .disabled(state.url.isEmpty || state.connecting)
  49. Button("Delete") { state.delete() }.foregroundColor(.red).disabled(state.connecting)
  50. }
  51. Section {
  52. Toggle("Upload", isOn: $state.isUploadEnabled)
  53. if state.isUploadEnabled {
  54. Toggle("Statistics", isOn: $state.uploadStats)
  55. Toggle("Glucose", isOn: $state.uploadGlucose).disabled(!state.changeUploadGlucose)
  56. }
  57. } header: {
  58. Text("Allow Uploads")
  59. }
  60. Section {
  61. Button("Import settings from Nightscout") {
  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. }.disabled(state.url.isEmpty || state.connecting)
  83. } header: { Text("Import from Nightscout") }
  84. .alert(isPresented: $importedHasRun) {
  85. Alert(
  86. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  87. message: Text(
  88. (fetchedErrors.first?.error ?? "").count < 4 ?
  89. NSLocalizedString(
  90. "\nNow please verify all of your new settings thoroughly:\n\n* Basal Settings\n * Carb Ratios\n * Glucose Targets\n * Insulin Sensitivities\n * DIA\n\n in iAPS Settings > Configuration.\n\nBad or invalid profile settings could have disatrous effects.",
  91. comment: "Imported Profiles Alert"
  92. ) :
  93. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  94. ),
  95. primaryButton: .destructive(
  96. Text("OK")
  97. ),
  98. secondaryButton: .cancel()
  99. )
  100. }
  101. Section {
  102. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  103. HStack {
  104. Text("Port")
  105. DecimalTextField("", value: $state.localPort, formatter: portFormater)
  106. }
  107. } header: { Text("Local glucose source") }
  108. Section {
  109. Button("Backfill glucose") { state.backfillGlucose() }
  110. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  111. }
  112. Section {
  113. Toggle("Remote control", isOn: $state.allowAnnouncements)
  114. } header: { Text("Allow Remote control of iAPS") }
  115. }
  116. .onAppear(perform: configureView)
  117. .navigationBarTitle("Nightscout Config")
  118. .navigationBarTitleDisplayMode(.automatic)
  119. .alert(isPresented: $isImportAlertPresented) {
  120. importAlert!
  121. }
  122. }
  123. }
  124. }