NightscoutImportStepView.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import SwiftUI
  2. struct NightscoutImportStepView: View {
  3. @Bindable var state: Onboarding.StateModel
  4. @State var importAlert: Alert?
  5. @State var isImportAlertPresented: Bool = false
  6. var body: some View {
  7. ZStack {
  8. if state.nightscoutImportStatus == .running {
  9. VStack(alignment: .center) {
  10. Spacer(minLength: 150)
  11. CustomProgressView(
  12. text: String(
  13. localized: "Importing Settings...",
  14. comment: "Progress text when importing settings via Nightscout"
  15. )
  16. )
  17. }
  18. .frame(maxWidth: .infinity, maxHeight: .infinity)
  19. .background(Color.clear)
  20. } else {
  21. VStack(alignment: .leading, spacing: 20) {
  22. Text(
  23. "Please choose if you want to import existing therapy settings from Nightscout or start from scratch."
  24. )
  25. .font(.headline)
  26. .padding(.horizontal)
  27. .multilineTextAlignment(.leading)
  28. ForEach([NightscoutImportOption.useImport, NightscoutImportOption.skipImport], id: \.self) { option in
  29. Button(action: {
  30. state.nightscoutImportOption = option
  31. }) {
  32. HStack {
  33. Image(systemName: state.nightscoutImportOption == option ? "largecircle.fill.circle" : "circle")
  34. .foregroundColor(state.nightscoutImportOption == option ? .accentColor : .secondary)
  35. .imageScale(.large)
  36. Text(option.displayName)
  37. .foregroundColor(.primary)
  38. Spacer()
  39. }
  40. .padding()
  41. .background(Color.chart.opacity(0.65))
  42. .cornerRadius(10)
  43. }
  44. .buttonStyle(.plain)
  45. }
  46. VStack(alignment: .leading, spacing: 10) {
  47. Text("Trio will import the following therapy settings from your Nightscout instance:")
  48. .multilineTextAlignment(.leading)
  49. VStack(alignment: .leading) {
  50. Text("• Glucose Targets")
  51. Text("• Basal Rates")
  52. Text("• Carb Ratios")
  53. Text("• Insulin Sensitivities")
  54. }
  55. }
  56. .padding(.horizontal)
  57. .font(.footnote)
  58. .foregroundStyle(Color.secondary)
  59. .multilineTextAlignment(.leading)
  60. }
  61. }
  62. }
  63. .frame(maxWidth: .infinity, maxHeight: .infinity)
  64. .alert(isPresented: $isImportAlertPresented) {
  65. if state.nightscoutImportStatus == .failed, state.nightscoutImportErrors.isNotEmpty,
  66. let errorMessage = state.nightscoutImportErrors.first
  67. {
  68. DispatchQueue.main.async {
  69. importAlert = Alert(
  70. title: Text("Import Failed"),
  71. message: Text(errorMessage.description),
  72. dismissButton: .default(Text("OK"))
  73. )
  74. isImportAlertPresented = true
  75. }
  76. }
  77. return importAlert ?? Alert(title: Text("Unknown Error"))
  78. }
  79. }
  80. }