CurrentGlucoseView.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import CoreData
  2. import SwiftUI
  3. struct CurrentGlucoseView: View {
  4. let timerDate: Date
  5. let units: GlucoseUnits
  6. let alarm: GlucoseAlarm?
  7. let lowGlucose: Decimal
  8. let highGlucose: Decimal
  9. let cgmAvailable: Bool
  10. var currentGlucoseTarget: Decimal
  11. let glucoseColorScheme: GlucoseColorScheme
  12. let glucose: [GlucoseStored] // This contains the last two glucose values, no matter if its manual or a cgm reading
  13. @State private var rotationDegrees: Double = 0.0
  14. @State private var angularGradient = AngularGradient(colors: [
  15. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  16. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  17. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  18. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  19. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  20. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  21. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  22. @Environment(\.colorScheme) var colorScheme
  23. private var glucoseFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. if units == .mmolL {
  27. formatter.maximumFractionDigits = 1
  28. formatter.minimumFractionDigits = 1
  29. formatter.roundingMode = .halfUp
  30. } else {
  31. formatter.maximumFractionDigits = 0
  32. }
  33. return formatter
  34. }
  35. private var deltaFormatter: NumberFormatter {
  36. let formatter = NumberFormatter()
  37. formatter.numberStyle = .decimal
  38. if units == .mmolL {
  39. formatter.maximumFractionDigits = 1
  40. formatter.minimumFractionDigits = 1
  41. formatter.roundingMode = .halfUp
  42. } else {
  43. formatter.maximumFractionDigits = 0
  44. }
  45. formatter.positivePrefix = " +"
  46. formatter.negativePrefix = " -"
  47. return formatter
  48. }
  49. private var timaAgoFormatter: NumberFormatter {
  50. let formatter = NumberFormatter()
  51. formatter.numberStyle = .decimal
  52. formatter.maximumFractionDigits = 0
  53. formatter.negativePrefix = ""
  54. return formatter
  55. }
  56. private var dateFormatter: DateFormatter {
  57. let formatter = DateFormatter()
  58. formatter.timeStyle = .short
  59. return formatter
  60. }
  61. var body: some View {
  62. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  63. if cgmAvailable {
  64. ZStack {
  65. TrendShape(gradient: angularGradient, color: triangleColor)
  66. .rotationEffect(.degrees(rotationDegrees))
  67. VStack(alignment: .center) {
  68. HStack {
  69. if let glucoseValue = glucose.last?.glucose {
  70. let displayGlucose = units == .mgdL ? Decimal(glucoseValue).description : Decimal(glucoseValue)
  71. .formattedAsMmolL
  72. var glucoseDisplayColor = Color.primary
  73. // 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
  74. let hardCodedLow = Decimal(55)
  75. let hardCodedHigh = Decimal(220)
  76. let isDynamicColorScheme = glucoseColorScheme == .dynamicColor
  77. if Decimal(glucoseValue) <= lowGlucose || Decimal(glucoseValue) >= highGlucose {
  78. glucoseDisplayColor = FreeAPS.getDynamicGlucoseColor(
  79. glucoseValue: Decimal(glucoseValue),
  80. highGlucoseColorValue: isDynamicColorScheme ? hardCodedHigh : highGlucose,
  81. lowGlucoseColorValue: isDynamicColorScheme ? hardCodedLow : lowGlucose,
  82. targetGlucose: currentGlucoseTarget,
  83. glucoseColorScheme: glucoseColorScheme
  84. )
  85. }
  86. return Text(
  87. glucoseValue == 400 ? "HIGH" : displayGlucose
  88. )
  89. .font(.system(size: 40, weight: .bold, design: .rounded))
  90. .foregroundStyle(glucoseDisplayColor)
  91. } else {
  92. return Text("--")
  93. .font(.system(size: 40, weight: .bold, design: .rounded))
  94. .foregroundStyle(.secondary)
  95. }
  96. }
  97. HStack {
  98. let minutesAgo = -1 * (glucose.last?.date?.timeIntervalSinceNow ?? 0) / 60
  99. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  100. Text(
  101. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  102. text + " " +
  103. NSLocalizedString("min", comment: "Short form for minutes") + " "
  104. )
  105. )
  106. .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  107. Text(
  108. delta
  109. )
  110. .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  111. }.frame(alignment: .top)
  112. }
  113. }
  114. .onChange(of: glucose.last?.directionEnum) {
  115. withAnimation {
  116. switch glucose.last?.directionEnum {
  117. case .doubleUp,
  118. .singleUp,
  119. .tripleUp:
  120. rotationDegrees = -90
  121. case .fortyFiveUp:
  122. rotationDegrees = -45
  123. case .flat:
  124. rotationDegrees = 0
  125. case .fortyFiveDown:
  126. rotationDegrees = 45
  127. case .doubleDown,
  128. .singleDown,
  129. .tripleDown:
  130. rotationDegrees = 90
  131. case nil,
  132. .notComputable,
  133. .rateOutOfRange:
  134. rotationDegrees = 0
  135. default:
  136. rotationDegrees = 0
  137. }
  138. }
  139. }
  140. } else {
  141. VStack(alignment: .center, spacing: 12) {
  142. HStack
  143. {
  144. // no cgm defined so display a generic CGM
  145. Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
  146. }
  147. HStack {
  148. Text("Add CGM").font(.caption).bold()
  149. }
  150. }.frame(alignment: .top)
  151. }
  152. }
  153. private var delta: String {
  154. guard glucose.count >= 2 else {
  155. return "--"
  156. }
  157. let lastGlucose = glucose.last?.glucose ?? 0
  158. let secondLastGlucose = glucose.first?.glucose ?? 0
  159. let delta = lastGlucose - secondLastGlucose
  160. let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
  161. return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  162. }
  163. }
  164. struct Triangle: Shape {
  165. func path(in rect: CGRect) -> Path {
  166. var path = Path()
  167. path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
  168. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
  169. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
  170. path.closeSubpath()
  171. return path
  172. }
  173. }
  174. struct TrendShape: View {
  175. @Environment(\.colorScheme) var colorScheme
  176. let gradient: AngularGradient
  177. let color: Color
  178. var body: some View {
  179. HStack(alignment: .center) {
  180. ZStack {
  181. Group {
  182. CircleShape(gradient: gradient)
  183. TriangleShape(color: color)
  184. }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
  185. CircleShape(gradient: gradient)
  186. }
  187. }
  188. }
  189. }
  190. struct CircleShape: View {
  191. @Environment(\.colorScheme) var colorScheme
  192. let gradient: AngularGradient
  193. var body: some View {
  194. Circle()
  195. .stroke(gradient, lineWidth: 6)
  196. .background(Circle().fill(Color.chart))
  197. .frame(width: 130, height: 130)
  198. }
  199. }
  200. struct TriangleShape: View {
  201. let color: Color
  202. var body: some View {
  203. Triangle()
  204. .fill(color)
  205. .frame(width: 35, height: 35)
  206. .rotationEffect(.degrees(90))
  207. .offset(x: 85)
  208. }
  209. }