BolusConfirmationView.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import Foundation
  2. import SwiftUI
  3. import WatchKit
  4. struct BolusConfirmationView: View {
  5. @Environment(\.dismiss) var dismiss
  6. let bolusAmount: Double
  7. @Binding var progress: Double
  8. let state: WatchState
  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(String(format: "%.1f g", state.carbsAmount))
  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: progress, total: 1.0)
  32. .tint(progress >= 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. dismiss()
  40. }
  41. .buttonStyle(.bordered)
  42. .tint(.blue)
  43. .disabled(!(bolusAmount > 0.0))
  44. }
  45. .focusable(true)
  46. .focused($isCrownFocused)
  47. .digitalCrownRotation(
  48. $progress,
  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: progress) { _, 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. dismiss()
  69. }
  70. } else if newValue > 0 {
  71. WKInterfaceDevice.current().play(.click)
  72. }
  73. }
  74. .navigationTitle("Confirm")
  75. .toolbar {
  76. ToolbarItem(placement: .topBarTrailing) {
  77. Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
  78. .symbolRenderingMode(.hierarchical)
  79. .foregroundStyle(Color.white)
  80. }
  81. }
  82. }
  83. }