BolusView.swift 3.9 KB

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