CarbsInputView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Foundation
  2. import SwiftUI
  3. // MARK: - Carbs Input View
  4. struct CarbsInputView: View {
  5. @Binding var navigationPath: NavigationPath
  6. @State private var carbsAmount: Double = 0.0 // Needs to be Double due to .digitalCrownRotation() stride
  7. @FocusState private var isCrownFocused: Bool // Manage crown focus
  8. let state: WatchState
  9. let continueToBolus: Bool
  10. private var effectiveCarbsLimit: Double {
  11. Double(truncating: state.maxCarbs as NSNumber)
  12. }
  13. var trioBackgroundColor = LinearGradient(
  14. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  15. startPoint: .top,
  16. endPoint: .bottom
  17. )
  18. var body: some View {
  19. let buttonLabel = continueToBolus ? String(localized: "Proceed", comment: "Button Label to Proceed to Bolus on Watch") :
  20. String(localized: "Log Carbs", comment: "Button Label to Log Carbs on Watch")
  21. // TODO: introduce meal setting fpu enablement to conditional handle FPU
  22. VStack {
  23. Spacer()
  24. HStack {
  25. // "-" Button
  26. Button(action: {
  27. if carbsAmount > 0 {
  28. carbsAmount < 5 ? carbsAmount = 0 : (carbsAmount -= 5)
  29. }
  30. }) {
  31. Image(systemName: "minus.circle.fill")
  32. .font(.title3)
  33. .tint(.orange)
  34. }
  35. .buttonStyle(.borderless)
  36. .disabled(carbsAmount <= 0)
  37. Spacer()
  38. // Display the current carb amount
  39. Text(String(format: "%.0f \(String(localized: "g", comment: "gram of carbs"))", carbsAmount))
  40. .fontWeight(.bold)
  41. .font(.system(.title2, design: .rounded))
  42. .foregroundColor(carbsAmount > 0.0 && carbsAmount >= effectiveCarbsLimit ? .loopRed : .primary)
  43. .focusable(true)
  44. .focused($isCrownFocused)
  45. .digitalCrownRotation(
  46. $carbsAmount,
  47. from: 0,
  48. through: effectiveCarbsLimit,
  49. by: 1,
  50. sensitivity: .medium,
  51. isContinuous: false,
  52. isHapticFeedbackEnabled: true
  53. )
  54. Spacer()
  55. // "+" Button
  56. Button(action: {
  57. carbsAmount = min(effectiveCarbsLimit, carbsAmount + 5)
  58. }) {
  59. Image(systemName: "plus.circle.fill")
  60. .font(.title3)
  61. .tint(.orange)
  62. }
  63. .buttonStyle(.borderless)
  64. .disabled(carbsAmount >= effectiveCarbsLimit)
  65. }.padding(.horizontal)
  66. Text("Carbohydrates")
  67. .font(.subheadline)
  68. .foregroundColor(.secondary)
  69. .padding(.bottom)
  70. Spacer()
  71. if carbsAmount > 0.0 && carbsAmount >= effectiveCarbsLimit {
  72. Text("Carbs Limit Reached!")
  73. .font(.footnote)
  74. .foregroundColor(.loopRed)
  75. }
  76. Button(buttonLabel) {
  77. if continueToBolus {
  78. state.carbsAmount = Int(min(carbsAmount, effectiveCarbsLimit))
  79. navigationPath.append(NavigationDestinations.bolusInput)
  80. } else {
  81. state.sendCarbsRequest(Int(min(carbsAmount, effectiveCarbsLimit)))
  82. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  83. }
  84. }
  85. .buttonStyle(.bordered)
  86. .tint(.orange)
  87. .disabled(!(carbsAmount > 0.0) || carbsAmount > effectiveCarbsLimit)
  88. }
  89. .background(trioBackgroundColor)
  90. .toolbar {
  91. ToolbarItem(placement: .topBarTrailing) {
  92. Image(systemName: "fork.knife")
  93. .resizable()
  94. .aspectRatio(contentMode: .fit)
  95. .frame(width: 14, height: 14)
  96. .padding()
  97. .background(Color.orange)
  98. .foregroundStyle(.white)
  99. .clipShape(Circle())
  100. }
  101. }
  102. }
  103. }