CurrentGlucoseView.swift 8.8 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. Text(
  73. glucoseValue == 400 ? "HIGH" : displayGlucose
  74. )
  75. .font(.system(size: 40, weight: .bold, design: .rounded))
  76. .foregroundColor(alarm == nil ? glucoseDisplayColor : .loopRed)
  77. } else {
  78. Text("--")
  79. .font(.system(size: 40, weight: .bold, design: .rounded))
  80. .foregroundColor(.secondary)
  81. }
  82. }
  83. HStack {
  84. let minutesAgo = -1 * (glucose.last?.date?.timeIntervalSinceNow ?? 0) / 60
  85. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  86. Text(
  87. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  88. text + " " +
  89. NSLocalizedString("min", comment: "Short form for minutes") + " "
  90. )
  91. )
  92. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  93. Text(
  94. delta
  95. )
  96. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  97. }.frame(alignment: .top)
  98. }
  99. }
  100. .onChange(of: glucose.last?.directionEnum) { newDirection in
  101. withAnimation {
  102. switch newDirection {
  103. case .doubleUp,
  104. .singleUp,
  105. .tripleUp:
  106. rotationDegrees = -90
  107. case .fortyFiveUp:
  108. rotationDegrees = -45
  109. case .flat:
  110. rotationDegrees = 0
  111. case .fortyFiveDown:
  112. rotationDegrees = 45
  113. case .doubleDown,
  114. .singleDown,
  115. .tripleDown:
  116. rotationDegrees = 90
  117. case nil,
  118. .notComputable,
  119. .rateOutOfRange:
  120. rotationDegrees = 0
  121. default:
  122. rotationDegrees = 0
  123. }
  124. }
  125. }
  126. } else {
  127. VStack(alignment: .center, spacing: 12) {
  128. HStack
  129. {
  130. // no cgm defined so display a generic CGM
  131. Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
  132. }
  133. HStack {
  134. Text("Add CGM").font(.caption).bold()
  135. }
  136. }.frame(alignment: .top)
  137. }
  138. }
  139. private var delta: String {
  140. guard glucose.count >= 2 else {
  141. return "--"
  142. }
  143. let lastGlucose = glucose.last?.glucose ?? 0
  144. let secondLastGlucose = glucose.first?.glucose ?? 0
  145. let delta = lastGlucose - secondLastGlucose
  146. let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
  147. return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  148. }
  149. var glucoseDisplayColor: Color {
  150. guard let lastGlucose = glucose.last?.glucose else { return .primary }
  151. // low and high glucose is parsed in state to mmol/L; parse it back to mg/dl here for comparison
  152. let lowGlucose = units == .mgdL ? lowGlucose : lowGlucose.asMgdL
  153. let highGlucose = units == .mgdL ? highGlucose : highGlucose.asMgdL
  154. // Ensure the thresholds are logical
  155. guard lowGlucose < highGlucose else { return .primary }
  156. guard Decimal(lastGlucose) <= lowGlucose && Decimal(lastGlucose) >= highGlucose else { return .primary }
  157. return FreeAPS.getDynamicGlucoseColor(
  158. glucoseValue: Decimal(lastGlucose),
  159. highGlucoseColorValue: highGlucose,
  160. lowGlucoseColorValue: lowGlucose,
  161. targetGlucose: currentGlucoseTarget,
  162. glucoseColorScheme: glucoseColorScheme,
  163. offset: units == .mgdL ? 20 : 20.asMmolL
  164. )
  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. }