CurrentGlucoseView.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import CoreData
  2. import SwiftUI
  3. struct CurrentGlucoseView: View {
  4. @Binding var timerDate: Date
  5. @Binding var units: GlucoseUnits
  6. @Binding var alarm: GlucoseAlarm?
  7. @Binding var lowGlucose: Decimal
  8. @Binding var highGlucose: Decimal
  9. @Binding var cgmAvailable: Bool
  10. @Binding var currentGlucoseTarget: Decimal
  11. @Binding var glucoseColorScheme: GlucoseColorScheme
  12. var 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. // low glucose, high glucose and target is parsed in state to mmol/L; parse it back to mg/dl here for comparison
  73. let lowGlucose = units == .mgdL ? lowGlucose : lowGlucose.asMgdL
  74. let highGlucose = units == .mgdL ? highGlucose : highGlucose.asMgdL
  75. let targetGlucose = units == .mgdL ? currentGlucoseTarget : currentGlucoseTarget.asMgdL
  76. var glucoseDisplayColor = Color.primary
  77. // 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
  78. let hardCodedLow = Decimal(55)
  79. let hardCodedHigh = Decimal(220)
  80. if Decimal(glucoseValue) <= lowGlucose || Decimal(glucoseValue) >= highGlucose {
  81. glucoseDisplayColor = FreeAPS.getDynamicGlucoseColor(
  82. glucoseValue: Decimal(glucoseValue),
  83. highGlucoseColorValue: hardCodedHigh,
  84. lowGlucoseColorValue: hardCodedLow,
  85. targetGlucose: targetGlucose,
  86. glucoseColorScheme: glucoseColorScheme
  87. )
  88. }
  89. return Text(
  90. glucoseValue == 400 ? "HIGH" : displayGlucose
  91. )
  92. .font(.system(size: 40, weight: .bold, design: .rounded))
  93. .foregroundStyle(glucoseDisplayColor)
  94. } else {
  95. return Text("--")
  96. .font(.system(size: 40, weight: .bold, design: .rounded))
  97. .foregroundStyle(.secondary)
  98. }
  99. }
  100. HStack {
  101. let minutesAgo = -1 * (glucose.last?.date?.timeIntervalSinceNow ?? 0) / 60
  102. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  103. Text(
  104. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  105. text + " " +
  106. NSLocalizedString("min", comment: "Short form for minutes") + " "
  107. )
  108. )
  109. .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  110. Text(
  111. delta
  112. )
  113. .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  114. }.frame(alignment: .top)
  115. }
  116. }
  117. .onChange(of: glucose.last?.directionEnum) {
  118. withAnimation {
  119. switch glucose.last?.directionEnum {
  120. case .doubleUp,
  121. .singleUp,
  122. .tripleUp:
  123. rotationDegrees = -90
  124. case .fortyFiveUp:
  125. rotationDegrees = -45
  126. case .flat:
  127. rotationDegrees = 0
  128. case .fortyFiveDown:
  129. rotationDegrees = 45
  130. case .doubleDown,
  131. .singleDown,
  132. .tripleDown:
  133. rotationDegrees = 90
  134. case nil,
  135. .notComputable,
  136. .rateOutOfRange:
  137. rotationDegrees = 0
  138. default:
  139. rotationDegrees = 0
  140. }
  141. }
  142. }
  143. } else {
  144. VStack(alignment: .center, spacing: 12) {
  145. HStack
  146. {
  147. // no cgm defined so display a generic CGM
  148. Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
  149. }
  150. HStack {
  151. Text("Add CGM").font(.caption).bold()
  152. }
  153. }.frame(alignment: .top)
  154. }
  155. }
  156. private var delta: String {
  157. guard glucose.count >= 2 else {
  158. return "--"
  159. }
  160. let lastGlucose = glucose.last?.glucose ?? 0
  161. let secondLastGlucose = glucose.first?.glucose ?? 0
  162. let delta = lastGlucose - secondLastGlucose
  163. let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
  164. return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  165. }
  166. }
  167. struct Triangle: Shape {
  168. func path(in rect: CGRect) -> Path {
  169. var path = Path()
  170. path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
  171. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
  172. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
  173. path.closeSubpath()
  174. return path
  175. }
  176. }
  177. struct TrendShape: View {
  178. @Environment(\.colorScheme) var colorScheme
  179. let gradient: AngularGradient
  180. let color: Color
  181. var body: some View {
  182. HStack(alignment: .center) {
  183. ZStack {
  184. Group {
  185. CircleShape(gradient: gradient)
  186. TriangleShape(color: color)
  187. }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
  188. CircleShape(gradient: gradient)
  189. }
  190. }
  191. }
  192. }
  193. struct CircleShape: View {
  194. @Environment(\.colorScheme) var colorScheme
  195. let gradient: AngularGradient
  196. var body: some View {
  197. Circle()
  198. .stroke(gradient, lineWidth: 6)
  199. .background(Circle().fill(Color.chart))
  200. .frame(width: 130, height: 130)
  201. }
  202. }
  203. struct TriangleShape: View {
  204. let color: Color
  205. var body: some View {
  206. Triangle()
  207. .fill(color)
  208. .frame(width: 35, height: 35)
  209. .rotationEffect(.degrees(90))
  210. .offset(x: 85)
  211. }
  212. }