GlucoseChartView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Charts
  2. import Foundation
  3. import SwiftUI
  4. struct GlucoseChartView: ChartContent {
  5. let glucoseData: [GlucoseStored]
  6. let units: GlucoseUnits
  7. let highGlucose: Decimal
  8. let lowGlucose: Decimal
  9. let currentGlucoseTarget: Decimal
  10. let isSmoothingEnabled: Bool
  11. let glucoseColorScheme: GlucoseColorScheme
  12. var body: some ChartContent {
  13. drawGlucoseChart()
  14. }
  15. private func drawGlucoseChart() -> some ChartContent {
  16. ForEach(glucoseData) { item in
  17. let glucoseToDisplay = units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL
  18. // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
  19. let hardCodedLow = Decimal(55)
  20. let hardCodedHigh = Decimal(220)
  21. let isDynamicColorScheme = glucoseColorScheme == .dynamicColor
  22. let pointMarkColor: Color = Trio.getDynamicGlucoseColor(
  23. glucoseValue: Decimal(item.glucose),
  24. highGlucoseColorValue: isDynamicColorScheme ? hardCodedHigh : highGlucose,
  25. lowGlucoseColorValue: isDynamicColorScheme ? hardCodedLow : lowGlucose,
  26. targetGlucose: currentGlucoseTarget,
  27. glucoseColorScheme: glucoseColorScheme
  28. )
  29. if !isSmoothingEnabled {
  30. PointMark(
  31. x: .value("Time", item.date ?? Date(), unit: .second),
  32. y: .value("Value", glucoseToDisplay)
  33. )
  34. .foregroundStyle(pointMarkColor)
  35. .symbolSize(20)
  36. .symbol {
  37. if item.isManual {
  38. Image(systemName: "drop.fill")
  39. .font(.caption2)
  40. .symbolRenderingMode(.monochrome)
  41. .bold()
  42. .foregroundStyle(.red)
  43. } else {
  44. Image(systemName: "circle.fill")
  45. .font(.system(size: 5))
  46. .bold()
  47. .foregroundStyle(pointMarkColor)
  48. }
  49. }
  50. } else {
  51. PointMark(
  52. x: .value("Time", item.date ?? Date(), unit: .second),
  53. y: .value("Value", glucoseToDisplay)
  54. )
  55. .symbol {
  56. if item.isManual {
  57. Image(systemName: "drop.fill")
  58. .font(.caption2)
  59. .symbolRenderingMode(.monochrome)
  60. .bold()
  61. .foregroundStyle(.red)
  62. } else {
  63. Image(systemName: "record.circle.fill")
  64. .font(.system(size: 8))
  65. .bold()
  66. .foregroundStyle(pointMarkColor)
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. #Preview {
  74. struct PreviewWrapper: View {
  75. @State private var previewStack: CoreDataStack? = nil
  76. @State private var glucoseData: [GlucoseStored] = []
  77. @State private var isLoading = true
  78. var body: some View {
  79. NavigationView {
  80. Group {
  81. if isLoading {
  82. ProgressView("Loading data...")
  83. } else {
  84. VStack {
  85. Chart {
  86. GlucoseChartView(
  87. glucoseData: glucoseData,
  88. units: .mgdL,
  89. highGlucose: 180,
  90. lowGlucose: 70,
  91. currentGlucoseTarget: 100,
  92. isSmoothingEnabled: false,
  93. glucoseColorScheme: .dynamicColor
  94. )
  95. }
  96. .frame(height: 200)
  97. .padding()
  98. }
  99. }
  100. }
  101. .navigationTitle("Glucose Chart")
  102. .task {
  103. // Use the preview stack that's initialized asynchronously in CoreDataStack
  104. previewStack = try? await CoreDataStack.preview()
  105. // Now you can safely create preview data
  106. if let stack = previewStack {
  107. glucoseData = GlucoseStored.makePreviewGlucose(count: 24, provider: stack)
  108. isLoading = false
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return PreviewWrapper()
  115. }