NightscoutConfigRootView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. HStack(alignment: .top) {
  56. Image(systemName: "pencil.circle.fill")
  57. VStack {
  58. Text(
  59. "This enables uploading of statistics.json to Nightscout, which can be used by the Community Statistics and Demographics Project.\n\nParticipation in Community Statistics is opt-in, and requires separate registration at:\n"
  60. )
  61. .font(.caption)
  62. Text(
  63. "https://iaps-stats.hub.org"
  64. )
  65. .font(.caption)
  66. .multilineTextAlignment(.center)
  67. }
  68. }
  69. .foregroundColor(Color.secondary)
  70. Toggle("Glucose", isOn: $state.uploadGlucose)
  71. }
  72. } header: {
  73. Text("Allow Uploads")
  74. }
  75. Section {
  76. Button("Import settings from Nightscout") {
  77. importAlert = Alert(
  78. title: Text("Import settings?"),
  79. message: Text(
  80. "\n" +
  81. NSLocalizedString(
  82. "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
  83. comment: "Profile Import Alert"
  84. ) +
  85. "\n"
  86. ),
  87. primaryButton: .destructive(
  88. Text("Yes, Import"),
  89. action: {
  90. state.importSettings()
  91. importedHasRun = true
  92. }
  93. ),
  94. secondaryButton: .cancel()
  95. )
  96. isImportAlertPresented.toggle()
  97. }.disabled(state.url.isEmpty || state.connecting)
  98. } header: { Text("Import from Nightscout") }
  99. .alert(isPresented: $importedHasRun) {
  100. Alert(
  101. title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
  102. message: Text(
  103. (fetchedErrors.first?.error ?? "").count < 4 ?
  104. NSLocalizedString(
  105. "\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.",
  106. comment: "Imported Profiles Alert"
  107. ) :
  108. NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
  109. ),
  110. primaryButton: .destructive(
  111. Text("OK")
  112. ),
  113. secondaryButton: .cancel()
  114. )
  115. }
  116. Section {
  117. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  118. HStack {
  119. Text("Port")
  120. DecimalTextField("", value: $state.localPort, formatter: portFormater)
  121. }
  122. } header: { Text("Local glucose source") }
  123. Section {
  124. Button("Backfill glucose") { state.backfillGlucose() }
  125. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  126. }
  127. Section {
  128. Toggle("Remote control", isOn: $state.allowAnnouncements)
  129. } header: { Text("Allow Remote control of iAPS") }
  130. }
  131. .onAppear(perform: configureView)
  132. .navigationBarTitle("Nightscout Config")
  133. .navigationBarTitleDisplayMode(.automatic)
  134. .alert(isPresented: $isImportAlertPresented) {
  135. importAlert!
  136. }
  137. }
  138. }
  139. }