BolusInputView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Foundation
  2. import SwiftUI
  3. import WatchKit
  4. // MARK: - Bolus Input View
  5. struct BolusInputView: View {
  6. @State private var bolusAmount = 0.0
  7. @State private var navigateToConfirmation = false
  8. @State private var confirmationProgress = 0.0
  9. let state: WatchState
  10. @FocusState private var isCrownFocused: Bool
  11. var body: some View {
  12. VStack {
  13. if state.carbsAmount > 0 {
  14. HStack {
  15. Text("Carbs:").bold().font(.subheadline).padding(.leading)
  16. Text(String(format: "%.0f g", state.carbsAmount)).font(.subheadline).foregroundStyle(Color.orange)
  17. Spacer()
  18. }
  19. }
  20. Spacer()
  21. HStack {
  22. // "-" Button
  23. Button(action: {
  24. if bolusAmount > 0 { bolusAmount -= 1 }
  25. }) {
  26. Image(systemName: "minus.circle.fill")
  27. .font(.title3)
  28. .foregroundColor(.blue)
  29. }
  30. .buttonStyle(.borderless)
  31. .disabled(bolusAmount < 1)
  32. Spacer()
  33. // Display the current carb amount
  34. Text(String(format: "%.2f U", bolusAmount))
  35. .fontWeight(.bold)
  36. .font(.system(.title2, design: .rounded))
  37. .foregroundColor(.primary)
  38. .focusable(true)
  39. .focused($isCrownFocused)
  40. .digitalCrownRotation(
  41. $bolusAmount,
  42. from: 0,
  43. through: 150.0, // TODO: use maxBolus here
  44. by: 1, // TODO: use pump increment here
  45. sensitivity: .medium,
  46. isContinuous: false,
  47. isHapticFeedbackEnabled: true
  48. )
  49. Spacer()
  50. // TODO: introduce maxBolus here, disable button if bolusAmount > maxBolus
  51. // "+" Button
  52. Button(action: {
  53. bolusAmount += 1
  54. }) {
  55. Image(systemName: "plus.circle.fill")
  56. .font(.title3)
  57. .foregroundColor(.blue)
  58. }
  59. .buttonStyle(.borderless)
  60. }.padding(.horizontal)
  61. Text("Insulin")
  62. .font(.subheadline)
  63. .foregroundColor(.secondary)
  64. .padding(.bottom)
  65. Spacer()
  66. Button("Log Bolus") {
  67. navigateToConfirmation = true
  68. }
  69. .buttonStyle(.bordered)
  70. .tint(.blue)
  71. .disabled(!(bolusAmount > 0.0))
  72. }
  73. .toolbar {
  74. ToolbarItem(placement: .topBarTrailing) {
  75. Image(systemName: "syringe.fill")
  76. .resizable()
  77. .aspectRatio(contentMode: .fit)
  78. .frame(width: 14, height: 14)
  79. .padding()
  80. .background(Color.blue)
  81. .foregroundStyle(.white)
  82. .clipShape(Circle())
  83. }
  84. }
  85. .navigationDestination(isPresented: $navigateToConfirmation) {
  86. BolusConfirmationView(
  87. bolusAmount: bolusAmount,
  88. progress: $confirmationProgress,
  89. state: state
  90. )
  91. }
  92. }
  93. }
  94. #Preview {
  95. BolusInputView(state: WatchState())
  96. }