BolusView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. VStack(spacing: 16) {
  17. HStack {
  18. Button {
  19. let newValue = steps - 1
  20. steps = max(newValue, 0)
  21. } label: { Image(systemName: "minus") }
  22. .frame(width: 50)
  23. Spacer()
  24. Text(numberFormatter.string(from: (steps * Double(state.bolusIncrement ?? 0.1)) as NSNumber)! + " U")
  25. .font(.headline)
  26. .focusable(true)
  27. .digitalCrownRotation(
  28. $steps,
  29. from: 0,
  30. through: Double((state.maxBolus ?? 5) / (state.bolusIncrement ?? 0.1)),
  31. by: 1,
  32. sensitivity: .medium,
  33. isContinuous: false,
  34. isHapticFeedbackEnabled: true
  35. )
  36. Spacer()
  37. Button {
  38. let newValue = steps + 1
  39. steps = min(newValue, Double((state.maxBolus ?? 5) / (state.bolusIncrement ?? 0.1)))
  40. } label: { Image(systemName: "plus") }
  41. .frame(width: 50)
  42. }
  43. HStack {
  44. Button {
  45. state.isBolusViewActive = false
  46. }
  47. label: {
  48. Image(systemName: "xmark.circle.fill")
  49. .resizable()
  50. .foregroundColor(.loopRed)
  51. .frame(width: 30, height: 30)
  52. }
  53. Button {
  54. enactBolus()
  55. }
  56. label: {
  57. Image(systemName: "checkmark.circle.fill")
  58. .resizable()
  59. .foregroundColor(.loopGreen)
  60. .frame(width: 30, height: 30)
  61. }
  62. .disabled(steps <= 0)
  63. }
  64. }
  65. .navigationTitle("Enact Bolus")
  66. .onAppear {
  67. steps = Double((state.bolusRecommended ?? 0) / (state.bolusIncrement ?? 0.1))
  68. }
  69. }
  70. private func enactBolus() {
  71. let amount = steps * Double(state.bolusIncrement ?? 0.1)
  72. state.enactBolus(amount: amount)
  73. }
  74. }
  75. struct BolusView_Previews: PreviewProvider {
  76. static var previews: some View {
  77. BolusView().environmentObject(WatchStateModel())
  78. }
  79. }