CurrentGlucoseView.swift 8.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. var glucose: [GlucoseStored]
  10. var manualGlucose: [GlucoseStored]
  11. @State private var rotationDegrees: Double = 0.0
  12. @State private var angularGradient = AngularGradient(colors: [
  13. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  14. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  15. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  16. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  17. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  18. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  19. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  20. @Environment(\.colorScheme) var colorScheme
  21. private var combinedGlucoseValues: [GlucoseStored] {
  22. // Combine and sort the glucose values
  23. let combined = (glucose + manualGlucose).sorted { $0.date ?? Date() > $1.date ?? Date() }
  24. return combined
  25. }
  26. private var glucoseFormatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. if units == .mmolL {
  30. formatter.maximumFractionDigits = 1
  31. formatter.minimumFractionDigits = 1
  32. formatter.roundingMode = .halfUp
  33. } else {
  34. formatter.maximumFractionDigits = 0
  35. }
  36. return formatter
  37. }
  38. private var deltaFormatter: NumberFormatter {
  39. let formatter = NumberFormatter()
  40. formatter.numberStyle = .decimal
  41. if units == .mmolL {
  42. formatter.maximumFractionDigits = 1
  43. formatter.minimumFractionDigits = 1
  44. formatter.roundingMode = .halfUp
  45. } else {
  46. formatter.maximumFractionDigits = 0
  47. }
  48. formatter.positivePrefix = " +"
  49. formatter.negativePrefix = " -"
  50. return formatter
  51. }
  52. private var timaAgoFormatter: NumberFormatter {
  53. let formatter = NumberFormatter()
  54. formatter.numberStyle = .decimal
  55. formatter.maximumFractionDigits = 0
  56. formatter.negativePrefix = ""
  57. return formatter
  58. }
  59. private var dateFormatter: DateFormatter {
  60. let formatter = DateFormatter()
  61. formatter.timeStyle = .short
  62. return formatter
  63. }
  64. var body: some View {
  65. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  66. ZStack {
  67. TrendShape(gradient: angularGradient, color: triangleColor)
  68. .rotationEffect(.degrees(rotationDegrees))
  69. VStack(alignment: .center) {
  70. HStack {
  71. if let glucoseValue = combinedGlucoseValues.first?.glucose {
  72. let displayGlucose = convertGlucose(glucoseValue, to: units)
  73. Text(
  74. glucoseValue == 400 ? "HIGH" :
  75. glucoseFormatter.string(from: NSNumber(value: displayGlucose)) ?? "--"
  76. )
  77. .font(.system(size: 40, weight: .bold, design: .rounded))
  78. .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
  79. } else {
  80. Text("--")
  81. .font(.system(size: 40, weight: .bold, design: .rounded))
  82. .foregroundColor(.secondary)
  83. }
  84. }
  85. HStack {
  86. let minutesAgo = -1 * (combinedGlucoseValues.first?.date?.timeIntervalSinceNow ?? 0) / 60
  87. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  88. Text(
  89. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  90. text + " " +
  91. NSLocalizedString("min", comment: "Short form for minutes") + " "
  92. )
  93. )
  94. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  95. Text(
  96. delta
  97. )
  98. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  99. }.frame(alignment: .top)
  100. }
  101. }
  102. .onChange(of: combinedGlucoseValues.first?.directionEnum) { newDirection in
  103. withAnimation {
  104. switch newDirection {
  105. case .doubleUp,
  106. .singleUp,
  107. .tripleUp:
  108. rotationDegrees = -90
  109. case .fortyFiveUp:
  110. rotationDegrees = -45
  111. case .flat:
  112. rotationDegrees = 0
  113. case .fortyFiveDown:
  114. rotationDegrees = 45
  115. case .doubleDown,
  116. .singleDown,
  117. .tripleDown:
  118. rotationDegrees = 90
  119. case nil,
  120. .notComputable,
  121. .rateOutOfRange:
  122. rotationDegrees = 0
  123. default:
  124. rotationDegrees = 0
  125. }
  126. }
  127. }
  128. }
  129. private func convertGlucose(_ value: Int16, to units: GlucoseUnits) -> Double {
  130. switch units {
  131. case .mmolL:
  132. return Double(value) / 18.0
  133. case .mgdL:
  134. return Double(value)
  135. }
  136. }
  137. private var delta: String {
  138. guard combinedGlucoseValues.count >= 2 else {
  139. return "--"
  140. }
  141. let lastGlucose = combinedGlucoseValues.first?.glucose ?? 0
  142. let secondLastGlucose = combinedGlucoseValues.dropFirst().first?.glucose ?? 0
  143. let delta = lastGlucose - secondLastGlucose
  144. let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
  145. return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  146. }
  147. var colourGlucoseText: Color {
  148. // Fetch the first glucose reading and convert it to Int for comparison
  149. let whichGlucose = Int(combinedGlucoseValues.first?.glucose ?? 0)
  150. // Define default color based on the color scheme
  151. let defaultColor: Color = colorScheme == .dark ? .white : .black
  152. // Ensure the thresholds are logical
  153. guard lowGlucose < highGlucose else { return .primary }
  154. // Perform range checks using Int converted values
  155. switch whichGlucose {
  156. case 0 ..< Int(lowGlucose):
  157. return .loopRed
  158. case Int(lowGlucose) ..< Int(highGlucose):
  159. return defaultColor
  160. case Int(highGlucose)...:
  161. return .loopYellow
  162. default:
  163. return defaultColor
  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. }