NightscoutConfigRootView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import SwiftUI
  2. import Swinject
  3. extension NightscoutConfig {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. private var portFormater: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.allowsFloats = false
  10. return formatter
  11. }
  12. var body: some View {
  13. Form {
  14. Section {
  15. TextField("URL", text: $state.url)
  16. .disableAutocorrection(true)
  17. .textContentType(.URL)
  18. .autocapitalization(.none)
  19. .keyboardType(.URL)
  20. SecureField("API secret", text: $state.secret)
  21. .disableAutocorrection(true)
  22. .autocapitalization(.none)
  23. .textContentType(.password)
  24. .keyboardType(.asciiCapable)
  25. if !state.message.isEmpty {
  26. Text(state.message)
  27. }
  28. if state.connecting {
  29. HStack {
  30. Text("Connecting...")
  31. Spacer()
  32. ProgressView()
  33. }
  34. }
  35. }
  36. Section {
  37. Button("Connect") { state.connect() }
  38. .disabled(state.url.isEmpty || state.connecting)
  39. Button("Delete") { state.delete() }.foregroundColor(.red).disabled(state.connecting)
  40. }
  41. Section {
  42. Toggle("Upload", isOn: $state.isUploadEnabled)
  43. if state.isUploadEnabled {
  44. Toggle("Statistics", isOn: $state.uploadStats)
  45. Toggle("Glucose", isOn: $state.uploadGlucose)
  46. }
  47. } header: {
  48. Text("Allow Uploads")
  49. }
  50. Section(header: Text("Local glucose source")) {
  51. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  52. HStack {
  53. Text("Port")
  54. DecimalTextField("", value: $state.localPort, formatter: portFormater)
  55. }
  56. }
  57. Section {
  58. Button("Backfill glucose") { state.backfillGlucose() }
  59. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  60. }
  61. }
  62. .onAppear(perform: configureView)
  63. .navigationBarTitle("Nightscout Config")
  64. .navigationBarTitleDisplayMode(.automatic)
  65. }
  66. }
  67. }