BolusConfirmationView.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import Foundation
  2. import SwiftUI
  3. import WatchKit
  4. struct BolusConfirmationView: View {
  5. @ObservedObject var navigationState: NavigationState
  6. let state: WatchState
  7. @Binding var bolusAmount: Double
  8. @Binding var confirmationProgress: Double
  9. @FocusState private var isCrownFocused: Bool
  10. var body: some View {
  11. VStack(spacing: 10) {
  12. Spacer()
  13. VStack {
  14. if state.carbsAmount > 0 {
  15. HStack {
  16. Text("Carbs:")
  17. Spacer()
  18. Text("\(state.carbsAmount) g")
  19. .bold()
  20. .foregroundStyle(.orange)
  21. }.padding(.horizontal)
  22. }
  23. HStack {
  24. Text("Bolus")
  25. Spacer()
  26. Text(String(format: "%.1f U", bolusAmount))
  27. .bold()
  28. .foregroundStyle(.blue)
  29. }.padding(.horizontal)
  30. }
  31. ProgressView(value: confirmationProgress, total: 1.0)
  32. .tint(confirmationProgress >= 1.0 ? .green : .gray)
  33. .padding(.horizontal)
  34. Spacer()
  35. Button("Cancel") {
  36. if state.carbsAmount > 0 {
  37. state.carbsAmount = 0 // reset carbs in state
  38. }
  39. bolusAmount = 0 // reset bolus in state
  40. confirmationProgress = 0 // reset auth progress
  41. navigationState.resetToRoot()
  42. }
  43. .buttonStyle(.bordered)
  44. }
  45. .focusable(true)
  46. .focused($isCrownFocused)
  47. .digitalCrownRotation(
  48. $confirmationProgress,
  49. from: 0.0,
  50. through: 1.0,
  51. by: 0.05,
  52. sensitivity: .medium,
  53. isContinuous: false,
  54. isHapticFeedbackEnabled: true
  55. )
  56. .onAppear {
  57. isCrownFocused = true
  58. }
  59. .onChange(of: confirmationProgress) { _, newValue in
  60. if newValue >= 1.0 {
  61. WKInterfaceDevice.current().play(.success)
  62. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  63. if state.carbsAmount > 0 {
  64. state.sendCarbsRequest(state.carbsAmount, Date())
  65. state.carbsAmount = 0 // reset carbs in state
  66. }
  67. state.activeBolusAmount = bolusAmount
  68. state.sendBolusRequest(Decimal(bolusAmount))
  69. bolusAmount = 0 // reset bolus in state
  70. confirmationProgress = 0 // reset auth progress
  71. navigationState.resetToRoot()
  72. // TODO: add a fancy success animation
  73. }
  74. } else if newValue > 0 {
  75. WKInterfaceDevice.current().play(.click)
  76. }
  77. }
  78. .navigationTitle("Confirm")
  79. .toolbar {
  80. ToolbarItem(placement: .topBarTrailing) {
  81. Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
  82. .symbolRenderingMode(.hierarchical)
  83. .foregroundStyle(Color.white)
  84. }
  85. }
  86. }
  87. }