NightscoutConnectView.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import SwiftUI
  2. struct NightscoutConnectView: View {
  3. @ObservedObject var state: NightscoutConfig.StateModel
  4. @State private var portFormatter: NumberFormatter
  5. @Environment(\.colorScheme) var colorScheme
  6. var color: LinearGradient {
  7. colorScheme == .dark ? LinearGradient(
  8. gradient: Gradient(colors: [
  9. Color.bgDarkBlue,
  10. Color.bgDarkerDarkBlue
  11. ]),
  12. startPoint: .top,
  13. endPoint: .bottom
  14. )
  15. :
  16. LinearGradient(
  17. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  18. startPoint: .top,
  19. endPoint: .bottom
  20. )
  21. }
  22. init(state: NightscoutConfig.StateModel) {
  23. self.state = state
  24. portFormatter = NumberFormatter()
  25. portFormatter.allowsFloats = false
  26. portFormatter.usesGroupingSeparator = false
  27. }
  28. var body: some View {
  29. Form {
  30. Section {
  31. TextField("URL", text: $state.url)
  32. .disableAutocorrection(true)
  33. .textContentType(.URL)
  34. .autocapitalization(.none)
  35. .keyboardType(.URL)
  36. SecureField("API secret", text: $state.secret)
  37. .disableAutocorrection(true)
  38. .autocapitalization(.none)
  39. .textContentType(.password)
  40. .keyboardType(.asciiCapable)
  41. if !state.message.isEmpty {
  42. Text(state.message)
  43. }
  44. if state.connecting {
  45. HStack {
  46. Text("Connecting...")
  47. Spacer()
  48. ProgressView()
  49. }
  50. }
  51. }.listRowBackground(Color.chart)
  52. Section {
  53. Button("Connect to Nightscout") { state.connect() }
  54. .disabled(state.url.isEmpty || state.connecting)
  55. Button("Delete") { state.delete() }.foregroundColor(.red).disabled(state.connecting)
  56. }.listRowBackground(Color.chart)
  57. Section {
  58. Button("Open Nightscout") {
  59. UIApplication.shared.open(URL(string: state.url)!, options: [:], completionHandler: nil)
  60. }
  61. .disabled(state.url.isEmpty || state.connecting)
  62. }.listRowBackground(Color.chart)
  63. Section {
  64. Toggle("Use local glucose server", isOn: $state.useLocalSource)
  65. HStack {
  66. Text("Port")
  67. TextFieldWithToolBar(
  68. text: $state.localPort,
  69. placeholder: "",
  70. keyboardType: .numberPad,
  71. numberFormatter: portFormatter,
  72. allowDecimalSeparator: false
  73. )
  74. }
  75. } header: { Text("Local glucose source") }.listRowBackground(Color.chart)
  76. }
  77. .navigationTitle("Connect")
  78. .scrollContentBackground(.hidden).background(color)
  79. }
  80. }