CarbsInputView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Foundation
  2. import SwiftUI
  3. // MARK: - Carbs Input View
  4. struct CarbsInputView: View {
  5. @Environment(\.dismiss) var dismiss
  6. @State private var carbsAmount: Double = 0.0 // Needs to be Double due to .digitalCrownRotation() stride
  7. @State private var navigateToBolus = false // Track navigation to BolusInputView
  8. @FocusState private var isCrownFocused: Bool // Manage crown focus
  9. let state: WatchState
  10. let continueToBolus: Bool
  11. var body: some View {
  12. let buttonLabel = continueToBolus ? "Proceed" : "Log Carbs"
  13. // TODO: introduce meal setting fpu enablement to conditional handle FPU
  14. VStack {
  15. Spacer()
  16. HStack {
  17. // "-" Button
  18. Button(action: {
  19. if carbsAmount > 0 { carbsAmount -= 1 }
  20. }) {
  21. Image(systemName: "minus.circle.fill")
  22. .font(.title3)
  23. .foregroundColor(.orange)
  24. }
  25. .buttonStyle(.borderless)
  26. .disabled(carbsAmount < 1)
  27. Spacer()
  28. // Display the current carb amount
  29. Text(String(format: "%.0f g", carbsAmount))
  30. .fontWeight(.bold)
  31. .font(.system(.title2, design: .rounded))
  32. .foregroundColor(.primary)
  33. .focusable(true)
  34. .focused($isCrownFocused)
  35. .digitalCrownRotation(
  36. $carbsAmount,
  37. from: 0,
  38. through: 150.0, // TODO: introduce maxCarbs here
  39. by: 1,
  40. sensitivity: .medium,
  41. isContinuous: false,
  42. isHapticFeedbackEnabled: true
  43. )
  44. Spacer()
  45. // TODO: introduce maxCarbs here, disable button if carbsAmount > maxCarbs
  46. // "+" Button
  47. Button(action: {
  48. carbsAmount += 1
  49. }) {
  50. Image(systemName: "plus.circle.fill")
  51. .font(.title3)
  52. .foregroundColor(.orange)
  53. }
  54. .buttonStyle(.borderless)
  55. }.padding(.horizontal)
  56. Text("Carbohydrates")
  57. .font(.subheadline)
  58. .foregroundColor(.secondary)
  59. .padding(.bottom)
  60. Spacer()
  61. Button(buttonLabel) {
  62. if continueToBolus {
  63. state.carbsAmount = Int(carbsAmount)
  64. navigateToBolus = true
  65. } else {
  66. state.sendCarbsRequest(Int(carbsAmount))
  67. dismiss()
  68. }
  69. }
  70. .buttonStyle(.bordered)
  71. .tint(.orange)
  72. .disabled(!(carbsAmount > 0.0))
  73. }
  74. .toolbar {
  75. ToolbarItem(placement: .topBarTrailing) {
  76. Image(systemName: "fork.knife")
  77. .resizable()
  78. .aspectRatio(contentMode: .fit)
  79. .frame(width: 14, height: 14)
  80. .padding()
  81. .background(Color.orange)
  82. .foregroundStyle(.white)
  83. .clipShape(Circle())
  84. }
  85. }
  86. .navigationDestination(isPresented: $navigateToBolus) {
  87. BolusInputView(state: state) // Navigate to BolusInputView
  88. }
  89. }
  90. }