BolusConfirmationView.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.sendBolusRequest(Decimal(bolusAmount))
  68. bolusAmount = 0 // reset bolus in state
  69. confirmationProgress = 0 // reset auth progress
  70. navigationState.resetToRoot()
  71. // TODO: add a fancy success animation
  72. }
  73. } else if newValue > 0 {
  74. WKInterfaceDevice.current().play(.click)
  75. }
  76. }
  77. .navigationTitle("Confirm")
  78. .toolbar {
  79. ToolbarItem(placement: .topBarTrailing) {
  80. Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
  81. .symbolRenderingMode(.hierarchical)
  82. .foregroundStyle(Color.white)
  83. }
  84. }
  85. }
  86. }