BolusConfirmationView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Foundation
  2. import SwiftUI
  3. import WatchKit
  4. struct BolusConfirmationView: View {
  5. @Binding var navigationPath: NavigationPath
  6. let state: WatchState
  7. @Binding var bolusAmount: Double
  8. @Binding var confirmationProgress: Double
  9. @FocusState private var isCrownFocused: Bool
  10. var trioBackgroundColor = LinearGradient(
  11. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  12. startPoint: .top,
  13. endPoint: .bottom
  14. )
  15. var body: some View {
  16. VStack(spacing: 10) {
  17. Spacer()
  18. VStack {
  19. if state.carbsAmount > 0 {
  20. HStack {
  21. Text("Carbs:")
  22. Spacer()
  23. Text("\(state.carbsAmount) g")
  24. .bold()
  25. .foregroundStyle(.orange)
  26. }.padding(.horizontal)
  27. }
  28. HStack {
  29. Text("Bolus")
  30. Spacer()
  31. Text(String(format: "%.1f U", bolusAmount))
  32. .bold()
  33. .foregroundStyle(Color.insulin)
  34. }.padding(.horizontal)
  35. }
  36. ProgressView(value: confirmationProgress, total: 1.0)
  37. .tint(confirmationProgress >= 1.0 ? .loopGreen : .gray)
  38. .padding(.horizontal)
  39. Spacer()
  40. Button("Cancel") {
  41. if state.carbsAmount > 0 {
  42. state.carbsAmount = 0 // reset carbs in state
  43. }
  44. bolusAmount = 0 // reset bolus in state
  45. confirmationProgress = 0 // reset auth progress
  46. navigationPath.removeLast(navigationPath.count)
  47. }
  48. .buttonStyle(.bordered)
  49. }
  50. .focusable(true)
  51. .focused($isCrownFocused)
  52. .digitalCrownRotation(
  53. $confirmationProgress,
  54. from: 0.0,
  55. through: 1.0,
  56. by: 0.05,
  57. sensitivity: .medium,
  58. isContinuous: false,
  59. isHapticFeedbackEnabled: true
  60. )
  61. .onAppear {
  62. isCrownFocused = true
  63. }
  64. .onChange(of: confirmationProgress) { _, newValue in
  65. if newValue >= 1.0 {
  66. WKInterfaceDevice.current().play(.success)
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  68. if state.carbsAmount > 0 {
  69. state.sendCarbsRequest(state.carbsAmount, Date())
  70. state.carbsAmount = 0 // reset carbs in state
  71. }
  72. state.activeBolusAmount = bolusAmount
  73. state.sendBolusRequest(Decimal(bolusAmount))
  74. bolusAmount = 0 // reset bolus in state
  75. confirmationProgress = 0 // reset auth progress
  76. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  77. }
  78. } else if newValue > 0 {
  79. WKInterfaceDevice.current().play(.click)
  80. }
  81. }
  82. .navigationTitle("Confirm")
  83. .background(trioBackgroundColor)
  84. .toolbar {
  85. ToolbarItem(placement: .topBarTrailing) {
  86. Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
  87. .symbolRenderingMode(.hierarchical)
  88. .foregroundStyle(Color.white)
  89. }
  90. }
  91. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  92. .overlay {
  93. if state.showBolusProgressOverlay {
  94. BolusProgressOverlay(state: state) {
  95. state.shouldNavigateToRoot = false
  96. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  97. }.transition(.opacity)
  98. }
  99. }
  100. }
  101. }