NightscoutConnectView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @Environment(AppState.self) var appState
  7. init(state: NightscoutConfig.StateModel) {
  8. self.state = state
  9. portFormatter = NumberFormatter()
  10. portFormatter.allowsFloats = false
  11. portFormatter.usesGroupingSeparator = false
  12. }
  13. var body: some View {
  14. List {
  15. Section(
  16. header: Text("Connect to Nightscout"),
  17. content: {
  18. TextField("URL", text: $state.url)
  19. .disableAutocorrection(true)
  20. .textContentType(.URL)
  21. .autocapitalization(.none)
  22. .keyboardType(.URL)
  23. SecureField("API secret", text: $state.secret)
  24. .disableAutocorrection(true)
  25. .autocapitalization(.none)
  26. .textContentType(.password)
  27. .keyboardType(.asciiCapable)
  28. if !state.message.isEmpty {
  29. Text(state.message)
  30. }
  31. if state.connecting {
  32. HStack {
  33. Text("Connecting...")
  34. Spacer()
  35. ProgressView()
  36. }
  37. }
  38. if !state.isConnectedToNS {
  39. Button {
  40. state.connect()
  41. } label: {
  42. Text("Connect to Nightscout")
  43. .font(.title3) }
  44. .frame(maxWidth: .infinity, alignment: .center)
  45. .buttonStyle(.bordered)
  46. .disabled(state.url.isEmpty && state.connecting)
  47. } else {
  48. Button(role: .destructive) {
  49. state.delete()
  50. } label: {
  51. Text("Disconnect and Remove")
  52. .font(.title3)
  53. }
  54. .frame(maxWidth: .infinity, alignment: .center)
  55. .buttonStyle(.bordered)
  56. .tint(Color.loopRed)
  57. }
  58. }
  59. ).listRowBackground(Color.chart)
  60. if state.isConnectedToNS {
  61. Section {
  62. Button {
  63. UIApplication.shared.open(URL(string: state.url)!, options: [:], completionHandler: nil)
  64. }
  65. label: { Label("Open Nightscout", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding() }
  66. .frame(maxWidth: .infinity, alignment: .center)
  67. .buttonStyle(.bordered)
  68. }
  69. .listRowBackground(Color.clear)
  70. }
  71. // TODO: Find out if this is still required or needed ?!
  72. // Section {
  73. // Toggle("Use local glucose server", isOn: $state.useLocalSource)
  74. // HStack {
  75. // Text("Port")
  76. // TextFieldWithToolBar(
  77. // text: $state.localPort,
  78. // placeholder: "",
  79. // keyboardType: .numberPad,
  80. // numberFormatter: portFormatter,
  81. // allowDecimalSeparator: false
  82. // )
  83. // }
  84. // } header: { Text("Local glucose source") }.listRowBackground(Color.chart)
  85. }
  86. .listSectionSpacing(sectionSpacing)
  87. .navigationTitle("Connect")
  88. .navigationBarTitleDisplayMode(.automatic)
  89. .scrollContentBackground(.hidden)
  90. .background(appState.trioBackgroundColor(for: colorScheme))
  91. }
  92. }