CurrentGlucoseView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import CoreData
  2. import SwiftUI
  3. struct CurrentGlucoseView: View {
  4. // @Binding var recentGlucose: BloodGlucose?
  5. @Binding var timerDate: Date
  6. @Binding var delta: Int?
  7. @Binding var units: GlucoseUnits
  8. @Binding var alarm: GlucoseAlarm?
  9. @Binding var lowGlucose: Decimal
  10. @Binding var highGlucose: Decimal
  11. @State private var rotationDegrees: Double = 0.0
  12. @State private var angularGradient = AngularGradient(colors: [
  13. // 184, 87, 255
  14. // 159, 108, 250
  15. // 124, 139, 243
  16. // 87, 170, 236
  17. // 67, 187, 233
  18. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  19. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  20. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  21. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  22. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  23. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  24. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  25. @Environment(\.colorScheme) var colorScheme
  26. @FetchRequest(
  27. entity: GlucoseStored.entity(),
  28. sortDescriptors: [NSSortDescriptor(keyPath: \GlucoseStored.date, ascending: false)],
  29. predicate: NSPredicate.predicateFor30MinAgo,
  30. animation: Animation.bouncy
  31. ) var glucoseFromPersistence: FetchedResults<GlucoseStored>
  32. private var glucoseFormatter: NumberFormatter {
  33. let formatter = NumberFormatter()
  34. formatter.numberStyle = .decimal
  35. formatter.maximumFractionDigits = 0
  36. if units == .mmolL {
  37. formatter.minimumFractionDigits = 1
  38. formatter.maximumFractionDigits = 1
  39. }
  40. formatter.roundingMode = .halfUp
  41. return formatter
  42. }
  43. private var deltaFormatter: NumberFormatter {
  44. let formatter = NumberFormatter()
  45. formatter.numberStyle = .decimal
  46. formatter.maximumFractionDigits = 1
  47. formatter.positivePrefix = " +"
  48. formatter.negativePrefix = " -"
  49. return formatter
  50. }
  51. private var timaAgoFormatter: NumberFormatter {
  52. let formatter = NumberFormatter()
  53. formatter.numberStyle = .decimal
  54. formatter.maximumFractionDigits = 0
  55. formatter.negativePrefix = ""
  56. return formatter
  57. }
  58. private var dateFormatter: DateFormatter {
  59. let formatter = DateFormatter()
  60. formatter.timeStyle = .short
  61. return formatter
  62. }
  63. var body: some View {
  64. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  65. ZStack {
  66. TrendShape(gradient: angularGradient, color: triangleColor)
  67. .rotationEffect(.degrees(rotationDegrees))
  68. VStack(alignment: .center) {
  69. HStack {
  70. let glucoseValue = glucoseFromPersistence.first?.glucose ?? 100
  71. let displayGlucose = convertGlucose(glucoseValue, to: units)
  72. Text(
  73. glucoseValue == 400 ? "HIGH" :
  74. glucoseFormatter.string(from: NSNumber(value: displayGlucose)) ?? "--"
  75. )
  76. .font(.system(size: 40, weight: .bold, design: .rounded))
  77. .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
  78. }
  79. HStack {
  80. let minutesAgo = -1 * (glucoseFromPersistence.first?.date?.timeIntervalSinceNow ?? 0) / 60
  81. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  82. Text(
  83. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  84. text + " " +
  85. NSLocalizedString("min", comment: "Short form for minutes") + " "
  86. )
  87. )
  88. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  89. Text(
  90. delta
  91. .map {
  92. deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  93. } ?? "--"
  94. )
  95. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  96. }.frame(alignment: .top)
  97. }
  98. }
  99. .onChange(of: glucoseFromPersistence.first?.direction) { newDirection in
  100. withAnimation {
  101. switch newDirection {
  102. case "DoubleUp",
  103. "SingleUp",
  104. "TripleUp":
  105. rotationDegrees = -90
  106. case "FortyFiveUp":
  107. rotationDegrees = -45
  108. case "Flat":
  109. rotationDegrees = 0
  110. case "FortyFiveDown":
  111. rotationDegrees = 45
  112. case "DoubleDown",
  113. "SingleDown",
  114. "TripleDown":
  115. rotationDegrees = 90
  116. case "NONE",
  117. "NOT COMPUTABLE",
  118. "RATE OUT OF RANGE":
  119. rotationDegrees = 0
  120. default:
  121. rotationDegrees = 0
  122. }
  123. }
  124. }
  125. }
  126. private func convertGlucose(_ value: Int16, to units: GlucoseUnits) -> Double {
  127. switch units {
  128. case .mmolL:
  129. return Double(value) / 18.0
  130. case .mgdL:
  131. return Double(value)
  132. }
  133. }
  134. var colourGlucoseText: Color {
  135. // Fetch the first glucose reading and convert it to Int for comparison
  136. let whichGlucose = Int(glucoseFromPersistence.first?.glucose ?? 0)
  137. // Define default color based on the color scheme
  138. let defaultColor: Color = colorScheme == .dark ? .white : .black
  139. // Ensure the thresholds are logical
  140. guard lowGlucose < highGlucose else { return .primary }
  141. // Perform range checks using Int converted values
  142. switch whichGlucose {
  143. case 0 ..< Int(lowGlucose):
  144. return .loopRed
  145. case Int(lowGlucose) ..< Int(highGlucose):
  146. return defaultColor
  147. case Int(highGlucose)...:
  148. return .loopYellow
  149. default:
  150. return defaultColor
  151. }
  152. }
  153. }
  154. struct Triangle: Shape {
  155. func path(in rect: CGRect) -> Path {
  156. var path = Path()
  157. path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
  158. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
  159. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
  160. path.closeSubpath()
  161. return path
  162. }
  163. }
  164. struct TrendShape: View {
  165. @Environment(\.colorScheme) var colorScheme
  166. let gradient: AngularGradient
  167. let color: Color
  168. var body: some View {
  169. HStack(alignment: .center) {
  170. ZStack {
  171. Group {
  172. CircleShape(gradient: gradient)
  173. TriangleShape(color: color)
  174. }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
  175. CircleShape(gradient: gradient)
  176. }
  177. }
  178. }
  179. }
  180. struct CircleShape: View {
  181. @Environment(\.colorScheme) var colorScheme
  182. let gradient: AngularGradient
  183. var body: some View {
  184. Circle()
  185. .stroke(gradient, lineWidth: 6)
  186. .background(Circle().fill(Color.chart))
  187. .frame(width: 130, height: 130)
  188. }
  189. }
  190. struct TriangleShape: View {
  191. let color: Color
  192. var body: some View {
  193. Triangle()
  194. .fill(color)
  195. .frame(width: 35, height: 35)
  196. .rotationEffect(.degrees(90))
  197. .offset(x: 85)
  198. }
  199. }