NightscoutConnectView.swift 2.1 KB

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