NightscoutConnectView.swift 4.2 KB

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