NightscoutConfigRootView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. @FetchRequest(
  13. entity: ImportError.entity(),
  14. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)], predicate: NSPredicate(
  15. format: "date > %@", Date().addingTimeInterval(-1.minutes.timeInterval) as NSDate
  16. )
  17. ) var fetchedErrors: FetchedResults<ImportError>
  18. private var portFormater: NumberFormatter {
  19. let formatter = NumberFormatter()
  20. formatter.allowsFloats = false
  21. return formatter
  22. }
  23. var body: some View {
  24. Form {
  25. Section {
  26. TextField("URL", text: $state.url)
  27. .disableAutocorrection(true)
  28. .textContentType(.URL)
  29. .autocapitalization(.none)
  30. .keyboardType(.URL)
  31. SecureField("API secret", text: $state.secret)
  32. .disableAutocorrection(true)
  33. .autocapitalization(.none)
  34. .textContentType(.password)
  35. .keyboardType(.asciiCapable)
  36. if !state.message.isEmpty {
  37. Text(state.message)
  38. }
  39. if state.connecting {
  40. HStack {
  41. Text("Connecting...")
  42. Spacer()
  43. ProgressView()
  44. }
  45. }
  46. }
  47. Section {
  48. Button("Connect") { state.connect() }
  49. .disabled(state.url.isEmpty || state.connecting)
  50. Button("Delete") { state.delete() }.foregroundColor(.red).disabled(state.connecting)
  51. }
  52. Section {
  53. Button("Open Nighstcout") {
  54. UIApplication.shared.open(URL(string: state.url)!, options: [:], completionHandler: nil)
  55. }
  56. .disabled(state.url.isEmpty || state.connecting)
  57. }
  58. Section {
  59. Toggle("Upload", isOn: $state.isUploadEnabled)
  60. if state.isUploadEnabled {
  61. Toggle("Statistics", isOn: $state.uploadStats)
  62. Toggle("Glucose", isOn: $state.uploadGlucose).disabled(!state.changeUploadGlucose)
  63. }
  64. } header: {
  65. Text("Allow Uploads")
  66. }
  67. Section {
  68. Button("Import settings from Nightscout") {
  69. importAlert = Alert(
  70. title: Text("Import settings?"),
  71. message: Text(
  72. "\n" +
  73. NSLocalizedString(
  74. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  75. comment: "Profile Import Alert"
  76. ) +
  77. "\n"
  78. ),
  79. primaryButton: .destructive(
  80. Text("Yes, Import"),
  81. action: {
  82. state.importSettings()
  83. importedHasRun = true
  84. }
  85. ),
  86. secondaryButton: .cancel()
  87. )
  88. isImportAlertPresented.toggle()
  89. }.disabled(state.url.isEmpty || state.connecting)
  90. } header: { Text("Import from Nightscout") }
  91. .alert(isPresented: $importedHasRun) {
  92. Alert(
  93. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  94. message: Text(
  95. (fetchedErrors.first?.error ?? "").count < 4 ?
  96. NSLocalizedString(
  97. "\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 Open-iAPS Settings > Configuration.\n\nBad or invalid profile settings could have disatrous effects.",
  98. comment: "Imported Profiles Alert"
  99. ) :
  100. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  101. ),
  102. primaryButton: .destructive(
  103. Text("OK")
  104. ),
  105. secondaryButton: .cancel()
  106. )
  107. }
  108. Section {
  109. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  110. HStack {
  111. Text("Port")
  112. DecimalTextField("", value: $state.localPort, formatter: portFormater)
  113. }
  114. } header: { Text("Local glucose source") }
  115. Section {
  116. Button("Backfill glucose") { state.backfillGlucose() }
  117. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  118. }
  119. Section {
  120. Toggle("Remote control", isOn: $state.allowAnnouncements)
  121. } header: { Text("Allow Remote control of Open-iAPS") }
  122. }
  123. .onAppear(perform: configureView)
  124. .navigationBarTitle("Nightscout Config")
  125. .navigationBarTitleDisplayMode(.automatic)
  126. .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
  127. .alert(isPresented: $isImportAlertPresented) {
  128. importAlert!
  129. }
  130. }
  131. }
  132. }