NightscoutConnectView.swift 4.3 KB

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