BolusView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import SwiftUI
  2. struct BolusView: View {
  3. @EnvironmentObject var state: WatchStateModel
  4. @State var steps = 0.0
  5. var numberFormatter: NumberFormatter {
  6. let formatter = NumberFormatter()
  7. formatter.numberStyle = .decimal
  8. formatter.minimum = 0
  9. formatter.maximum = Double((state.maxBolus ?? 5) / (state.bolusIncrement ?? 0.1)) as NSNumber
  10. formatter.maximumFractionDigits = 2
  11. formatter.minimumFractionDigits = 2
  12. formatter.allowsFloats = true
  13. return formatter
  14. }
  15. var body: some View {
  16. GeometryReader { geo in
  17. VStack(spacing: 16) {
  18. HStack {
  19. Button {
  20. WKInterfaceDevice.current().play(.click)
  21. let newValue = steps - 1
  22. steps = max(newValue, 0)
  23. } label: { Image(systemName: "minus") }
  24. .frame(width: geo.size.width / 4)
  25. Spacer()
  26. Text(numberFormatter.string(from: (steps * Double(state.bolusIncrement ?? 0.1)) as NSNumber)! + " U")
  27. .font(.headline)
  28. .focusable(true)
  29. .digitalCrownRotation(
  30. $steps,
  31. from: 0,
  32. through: Double((state.maxBolus ?? 5) / (state.bolusIncrement ?? 0.1)),
  33. by: 1,
  34. sensitivity: .medium,
  35. isContinuous: false,
  36. isHapticFeedbackEnabled: true
  37. )
  38. Spacer()
  39. Button {
  40. WKInterfaceDevice.current().play(.click)
  41. let newValue = steps + 1
  42. steps = min(newValue, Double((state.maxBolus ?? 5) / (state.bolusIncrement ?? 0.1)))
  43. } label: { Image(systemName: "plus") }
  44. .frame(width: geo.size.width / 4)
  45. }
  46. HStack {
  47. Button {
  48. state.isBolusViewActive = false
  49. }
  50. label: {
  51. Image(systemName: "xmark.circle.fill")
  52. .resizable()
  53. .foregroundColor(.loopRed)
  54. .frame(width: 30, height: 30)
  55. }
  56. Button {
  57. WKInterfaceDevice.current().play(.click)
  58. enactBolus()
  59. }
  60. label: {
  61. Image(systemName: "checkmark.circle.fill")
  62. .resizable()
  63. .foregroundColor(.loopGreen)
  64. .frame(width: 30, height: 30)
  65. }
  66. .disabled(steps <= 0)
  67. }
  68. label: {
  69. Image(systemName: "xmark.circle.fill")
  70. .resizable()
  71. .foregroundColor(.loopRed)
  72. .frame(width: 30, height: 30)
  73. }
  74. Button {
  75. enactBolus()
  76. }
  77. label: {
  78. Image(systemName: "checkmark.circle.fill")
  79. .resizable()
  80. .foregroundColor(.loopGreen)
  81. .frame(width: 30, height: 30)
  82. }
  83. .disabled(steps <= 0)
  84. }
  85. }
  86. .navigationTitle("Enact Bolus")
  87. .onAppear {
  88. steps = Double((state.bolusRecommended ?? 0) / (state.bolusIncrement ?? 0.1))
  89. }
  90. }
  91. private func enactBolus() {
  92. let amount = steps * Double(state.bolusIncrement ?? 0.1)
  93. state.addBolus(amount: amount)
  94. }
  95. }
  96. struct BolusView_Previews: PreviewProvider {
  97. static var previews: some View {
  98. BolusView().environmentObject(WatchStateModel())
  99. }
  100. }