InsulinView.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Charts
  2. import Foundation
  3. import SwiftUI
  4. struct InsulinView: ChartContent {
  5. let glucoseData: [GlucoseStored]
  6. let insulinData: [PumpEventStored]
  7. let units: GlucoseUnits
  8. let bolusDisplayThreshold: BolusDisplayThreshold
  9. var body: some ChartContent {
  10. drawBoluses()
  11. }
  12. private func drawBoluses() -> some ChartContent {
  13. ForEach(insulinData) { insulin in
  14. let amount = insulin.bolus?.amount ?? 0 as NSDecimalNumber
  15. let bolusDate = insulin.timestamp ?? Date()
  16. if amount != 0, let glucose = MainChartHelper.timeToNearestGlucose(
  17. glucoseValues: glucoseData,
  18. time: bolusDate.timeIntervalSince1970
  19. )?.glucose {
  20. let yPosition = (units == .mgdL ? Decimal(glucose) : Decimal(glucose).asMmolL) + MainChartHelper
  21. .bolusOffset(units: units)
  22. let size = (MainChartHelper.Config.bolusSize + CGFloat(truncating: amount) * MainChartHelper.Config.bolusScale)
  23. PointMark(
  24. x: .value("Time", bolusDate, unit: .second),
  25. y: .value("Value", yPosition)
  26. )
  27. .symbol {
  28. Image(systemName: "arrowtriangle.down.fill").font(.system(size: size)).foregroundStyle(Color.insulin)
  29. }
  30. .annotation(position: .top) {
  31. if amount as Decimal >= bolusDisplayThreshold.rawValue {
  32. Text(Formatter.bolusFormatter.string(from: amount) ?? "")
  33. .font(.caption2)
  34. .foregroundStyle(Color.primary)
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }