CurrentGlucoseView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import CoreData
  2. import SwiftUI
  3. struct CurrentGlucoseView: View {
  4. <<<<<<< HEAD
  5. @Binding var timerDate: Date
  6. =======
  7. @Binding var recentGlucose: BloodGlucose?
  8. @Binding var timerDate: Date
  9. @Binding var delta: Int?
  10. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  11. @Binding var units: GlucoseUnits
  12. @Binding var alarm: GlucoseAlarm?
  13. @Binding var lowGlucose: Decimal
  14. @Binding var highGlucose: Decimal
  15. <<<<<<< HEAD
  16. var glucose: [GlucoseStored]
  17. var manualGlucose: [GlucoseStored]
  18. @State private var rotationDegrees: Double = 0.0
  19. @State private var angularGradient = AngularGradient(colors: [
  20. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  21. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  22. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  23. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  24. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  25. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  26. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  27. @Environment(\.colorScheme) var colorScheme
  28. private var combinedGlucoseValues: [GlucoseStored] {
  29. // Combine and sort the glucose values
  30. let combined = (glucose + manualGlucose).sorted { $0.date ?? Date() > $1.date ?? Date() }
  31. return combined
  32. }
  33. =======
  34. @Binding var cgmAvailable: Bool
  35. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  36. private var glucoseFormatter: NumberFormatter {
  37. let formatter = NumberFormatter()
  38. formatter.numberStyle = .decimal
  39. if units == .mmolL {
  40. formatter.maximumFractionDigits = 1
  41. formatter.minimumFractionDigits = 1
  42. formatter.roundingMode = .halfUp
  43. } else {
  44. formatter.maximumFractionDigits = 0
  45. }
  46. return formatter
  47. }
  48. private var deltaFormatter: NumberFormatter {
  49. let formatter = NumberFormatter()
  50. formatter.numberStyle = .decimal
  51. if units == .mmolL {
  52. formatter.maximumFractionDigits = 1
  53. formatter.minimumFractionDigits = 1
  54. formatter.roundingMode = .halfUp
  55. } else {
  56. formatter.maximumFractionDigits = 0
  57. }
  58. formatter.positivePrefix = " +"
  59. formatter.negativePrefix = " -"
  60. return formatter
  61. }
  62. private var timaAgoFormatter: NumberFormatter {
  63. let formatter = NumberFormatter()
  64. formatter.numberStyle = .decimal
  65. formatter.maximumFractionDigits = 0
  66. formatter.negativePrefix = ""
  67. return formatter
  68. }
  69. private var dateFormatter: DateFormatter {
  70. let formatter = DateFormatter()
  71. formatter.timeStyle = .short
  72. return formatter
  73. }
  74. var body: some View {
  75. <<<<<<< HEAD
  76. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  77. ZStack {
  78. TrendShape(gradient: angularGradient, color: triangleColor)
  79. .rotationEffect(.degrees(rotationDegrees))
  80. VStack(alignment: .center) {
  81. HStack {
  82. if let glucoseValue = combinedGlucoseValues.first?.glucose {
  83. let displayGlucose = convertGlucose(glucoseValue, to: units)
  84. Text(
  85. glucoseValue == 400 ? "HIGH" :
  86. glucoseFormatter.string(from: NSNumber(value: displayGlucose)) ?? "--"
  87. )
  88. .font(.system(size: 40, weight: .bold, design: .rounded))
  89. .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
  90. } else {
  91. Text("--")
  92. .font(.system(size: 40, weight: .bold, design: .rounded))
  93. .foregroundColor(.secondary)
  94. }
  95. }
  96. HStack {
  97. let minutesAgo = -1 * (combinedGlucoseValues.first?.date?.timeIntervalSinceNow ?? 0) / 60
  98. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  99. Text(
  100. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  101. text + " " +
  102. NSLocalizedString("min", comment: "Short form for minutes") + " "
  103. )
  104. )
  105. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  106. Text(
  107. delta
  108. )
  109. .font(.caption2).foregroundColor(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
  110. }.frame(alignment: .top)
  111. }
  112. }
  113. .onChange(of: combinedGlucoseValues.first?.directionEnum) { newDirection in
  114. withAnimation {
  115. switch newDirection {
  116. case .doubleUp,
  117. .singleUp,
  118. .tripleUp:
  119. rotationDegrees = -90
  120. case .fortyFiveUp:
  121. rotationDegrees = -45
  122. case .flat:
  123. rotationDegrees = 0
  124. case .fortyFiveDown:
  125. rotationDegrees = 45
  126. case .doubleDown,
  127. .singleDown,
  128. .tripleDown:
  129. rotationDegrees = 90
  130. case nil,
  131. .notComputable,
  132. .rateOutOfRange:
  133. rotationDegrees = 0
  134. default:
  135. rotationDegrees = 0
  136. }
  137. }
  138. =======
  139. if cgmAvailable {
  140. VStack(alignment: .center) {
  141. HStack {
  142. Text(
  143. (recentGlucose?.glucose ?? 100) == 400 ? "HIGH" : recentGlucose?.glucose
  144. .map {
  145. glucoseFormatter
  146. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  147. ?? "--"
  148. )
  149. .font(.title).fontWeight(.bold)
  150. .foregroundColor(alarm == nil ? colorOfGlucose : .loopRed)
  151. image
  152. }
  153. HStack {
  154. let minutesAgo = -1 * (recentGlucose?.dateString.timeIntervalSinceNow ?? 0) / 60
  155. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  156. Text(
  157. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  158. text + " " +
  159. NSLocalizedString("min", comment: "Short form for minutes") + " "
  160. )
  161. )
  162. .font(.caption2).foregroundColor(.secondary)
  163. Text(
  164. delta
  165. .map {
  166. deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  167. } ?? "--"
  168. )
  169. .font(.caption2).foregroundColor(.secondary)
  170. }.frame(alignment: .top)
  171. }
  172. } else {
  173. VStack(alignment: .center, spacing: 12) {
  174. HStack
  175. {
  176. // no cgm defined so display a generic CGM
  177. Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
  178. }
  179. HStack {
  180. Text("Add CGM").font(.caption).bold()
  181. }
  182. }.frame(alignment: .top)
  183. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  184. }
  185. }
  186. private func convertGlucose(_ value: Int16, to units: GlucoseUnits) -> Double {
  187. switch units {
  188. case .mmolL:
  189. return Double(value) / 18.0
  190. case .mgdL:
  191. return Double(value)
  192. }
  193. }
  194. private var delta: String {
  195. guard combinedGlucoseValues.count >= 2 else {
  196. return "--"
  197. }
  198. let lastGlucose = combinedGlucoseValues.first?.glucose ?? 0
  199. let secondLastGlucose = combinedGlucoseValues.dropFirst().first?.glucose ?? 0
  200. let delta = lastGlucose - secondLastGlucose
  201. let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
  202. return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  203. }
  204. var colourGlucoseText: Color {
  205. // Fetch the first glucose reading and convert it to Int for comparison
  206. let whichGlucose = Int(combinedGlucoseValues.first?.glucose ?? 0)
  207. // Define default color based on the color scheme
  208. let defaultColor: Color = colorScheme == .dark ? .white : .black
  209. // Ensure the thresholds are logical
  210. guard lowGlucose < highGlucose else { return .primary }
  211. // Perform range checks using Int converted values
  212. switch whichGlucose {
  213. case 0 ..< Int(lowGlucose):
  214. return .loopRed
  215. case Int(lowGlucose) ..< Int(highGlucose):
  216. return defaultColor
  217. case Int(highGlucose)...:
  218. return .loopYellow
  219. default:
  220. return defaultColor
  221. }
  222. }
  223. }
  224. struct Triangle: Shape {
  225. func path(in rect: CGRect) -> Path {
  226. var path = Path()
  227. path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
  228. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
  229. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
  230. path.closeSubpath()
  231. return path
  232. }
  233. }
  234. struct TrendShape: View {
  235. @Environment(\.colorScheme) var colorScheme
  236. let gradient: AngularGradient
  237. let color: Color
  238. var body: some View {
  239. HStack(alignment: .center) {
  240. ZStack {
  241. Group {
  242. CircleShape(gradient: gradient)
  243. TriangleShape(color: color)
  244. }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
  245. CircleShape(gradient: gradient)
  246. }
  247. }
  248. }
  249. }
  250. struct CircleShape: View {
  251. @Environment(\.colorScheme) var colorScheme
  252. let gradient: AngularGradient
  253. var body: some View {
  254. Circle()
  255. .stroke(gradient, lineWidth: 6)
  256. .background(Circle().fill(Color.chart))
  257. .frame(width: 130, height: 130)
  258. }
  259. }
  260. struct TriangleShape: View {
  261. let color: Color
  262. var body: some View {
  263. Triangle()
  264. .fill(color)
  265. .frame(width: 35, height: 35)
  266. .rotationEffect(.degrees(90))
  267. .offset(x: 85)
  268. }
  269. }