NightscoutConnectView.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import SwiftUI
  2. struct NightscoutConnectView: View {
  3. @ObservedObject var state: NightscoutConfig.StateModel
  4. @State private var portFormatter: NumberFormatter
  5. init(state: NightscoutConfig.StateModel) {
  6. self.state = state
  7. portFormatter = NumberFormatter()
  8. portFormatter.allowsFloats = false
  9. portFormatter.usesGroupingSeparator = false
  10. }
  11. var body: some View {
  12. Form {
  13. Section {
  14. TextField("URL", text: $state.url)
  15. .disableAutocorrection(true)
  16. .textContentType(.URL)
  17. .autocapitalization(.none)
  18. .keyboardType(.URL)
  19. SecureField("API secret", text: $state.secret)
  20. .disableAutocorrection(true)
  21. .autocapitalization(.none)
  22. .textContentType(.password)
  23. .keyboardType(.asciiCapable)
  24. if !state.message.isEmpty {
  25. Text(state.message)
  26. }
  27. if state.connecting {
  28. HStack {
  29. Text("Connecting...")
  30. Spacer()
  31. ProgressView()
  32. }
  33. }
  34. }
  35. Section {
  36. Button("Connect to Nightscout") { state.connect() }
  37. .disabled(state.url.isEmpty || state.connecting)
  38. Button("Delete") { state.delete() }.foregroundColor(.red).disabled(state.connecting)
  39. }
  40. Section {
  41. Button("Open Nightscout") {
  42. UIApplication.shared.open(URL(string: state.url)!, options: [:], completionHandler: nil)
  43. }
  44. .disabled(state.url.isEmpty || state.connecting)
  45. }
  46. Section {
  47. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  48. HStack {
  49. Text("Port")
  50. TextFieldWithToolBar(
  51. text: $state.localPort,
  52. placeholder: "",
  53. keyboardType: .numberPad,
  54. numberFormatter: portFormatter,
  55. allowDecimalSeparator: false
  56. )
  57. }
  58. } header: { Text("Local glucose source") }
  59. }
  60. .navigationTitle("Connect")
  61. }
  62. }