BolusConfirmationView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. let bolusIncrement = Double(truncating: state.bolusIncrement as NSNumber)
  17. let adjustedBolusAmount = floor(bolusAmount / bolusIncrement) * bolusIncrement
  18. VStack(spacing: 10) {
  19. Spacer()
  20. VStack {
  21. if state.carbsAmount > 0 {
  22. HStack {
  23. Text("Carbs:")
  24. Spacer()
  25. Text("\(state.carbsAmount) g")
  26. .bold()
  27. .foregroundStyle(.orange)
  28. }.padding(.horizontal)
  29. }
  30. HStack {
  31. Text("Bolus")
  32. Spacer()
  33. Text(String(format: "%.2f U", adjustedBolusAmount))
  34. .bold()
  35. .foregroundStyle(Color.insulin)
  36. }.padding(.horizontal)
  37. }
  38. ProgressView(value: confirmationProgress, total: 1.0)
  39. .tint(confirmationProgress >= 1.0 ? .loopGreen : .gray)
  40. .padding(.horizontal)
  41. Spacer()
  42. Button("Cancel") {
  43. if state.carbsAmount > 0 {
  44. state.carbsAmount = 0 // reset carbs in state
  45. }
  46. bolusAmount = 0 // reset bolus in state
  47. confirmationProgress = 0 // reset auth progress
  48. navigationPath.removeLast(navigationPath.count)
  49. }
  50. .buttonStyle(.bordered)
  51. }
  52. .focusable(true)
  53. .focused($isCrownFocused)
  54. .digitalCrownRotation(
  55. $confirmationProgress,
  56. from: 0.0,
  57. through: 1.0,
  58. by: state.confirmBolusFaster ? 0.5 : 0.05,
  59. sensitivity: .medium,
  60. isContinuous: false,
  61. isHapticFeedbackEnabled: true
  62. )
  63. .onAppear {
  64. isCrownFocused = true
  65. }
  66. .onChange(of: confirmationProgress) { _, newValue in
  67. if newValue >= 1.0 {
  68. WKInterfaceDevice.current().play(.success)
  69. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  70. if state.carbsAmount > 0 {
  71. state.sendCarbsRequest(state.carbsAmount, Date())
  72. state.carbsAmount = 0 // reset carbs in state
  73. }
  74. state.activeBolusAmount = bolusAmount
  75. state.sendBolusRequest(Decimal(bolusAmount))
  76. bolusAmount = 0 // reset bolus in state
  77. confirmationProgress = 0 // reset auth progress
  78. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  79. }
  80. } else if newValue > 0 {
  81. WKInterfaceDevice.current().play(.click)
  82. }
  83. }
  84. .navigationTitle("Confirm")
  85. .background(trioBackgroundColor)
  86. .toolbar {
  87. ToolbarItem(placement: .topBarTrailing) {
  88. Image(
  89. systemName: WKInterfaceDevice.current()
  90. .wristLocation == .left ? "digitalcrown.arrow.clockwise.fill" : "digitalcrown.arrow.counterclockwise.fill"
  91. )
  92. .symbolRenderingMode(.hierarchical)
  93. .foregroundStyle(Color.white)
  94. }
  95. }
  96. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  97. .overlay {
  98. if state.showBolusProgressOverlay {
  99. BolusProgressOverlay(state: state) {
  100. state.shouldNavigateToRoot = false
  101. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  102. }.transition(.opacity)
  103. }
  104. }
  105. }
  106. }