NightscoutConfigRootView.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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("Allow uploads", isOn: $state.isUploadEnabled)
  43. }
  44. Section(header: Text("Local glucose source")) {
  45. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  46. HStack {
  47. Text("Port")
  48. DecimalTextField("", value: $state.localPort, formatter: portFormater)
  49. }
  50. }
  51. Section {
  52. Button("Backfill glucose") { state.backfillGlucose() }
  53. .disabled(state.url.isEmpty || state.connecting || state.backfilling)
  54. }
  55. }
  56. .onAppear(perform: configureView)
  57. .navigationBarTitle("Nightscout Config")
  58. .navigationBarTitleDisplayMode(.automatic)
  59. }
  60. }
  61. }