BolusInputView.swift 3.5 KB

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