CurrentGlucoseView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import SwiftUI
  2. struct CurrentGlucoseView: View {
  3. @Binding var recentGlucose: BloodGlucose?
  4. @Binding var timerDate: Date
  5. @Binding var delta: Int?
  6. @Binding var units: GlucoseUnits
  7. @Binding var alarm: GlucoseAlarm?
  8. @Binding var lowGlucose: Decimal
  9. @Binding var highGlucose: Decimal
  10. @State private var rotationDegrees: Double = 0.0
  11. @Environment(\.colorScheme) var colorScheme
  12. private var glucoseFormatter: NumberFormatter {
  13. let formatter = NumberFormatter()
  14. formatter.numberStyle = .decimal
  15. formatter.maximumFractionDigits = 0
  16. if units == .mmolL {
  17. formatter.minimumFractionDigits = 1
  18. formatter.maximumFractionDigits = 1
  19. }
  20. formatter.roundingMode = .halfUp
  21. return formatter
  22. }
  23. private var deltaFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 1
  27. formatter.positivePrefix = " +"
  28. formatter.negativePrefix = " -"
  29. return formatter
  30. }
  31. private var timaAgoFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. formatter.maximumFractionDigits = 0
  35. formatter.negativePrefix = ""
  36. return formatter
  37. }
  38. private var dateFormatter: DateFormatter {
  39. let formatter = DateFormatter()
  40. formatter.timeStyle = .short
  41. return formatter
  42. }
  43. var body: some View {
  44. let colourGlucoseText: Color = colorScheme == .dark ? .white : .black
  45. ZStack {
  46. TrendShape(color: colorOfGlucose)
  47. .rotationEffect(.degrees(rotationDegrees))
  48. VStack(alignment: .center) {
  49. HStack {
  50. Text(
  51. (recentGlucose?.glucose ?? 100) == 400 ? "HIGH" : recentGlucose?.glucose
  52. .map {
  53. glucoseFormatter
  54. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  55. ?? "--"
  56. )
  57. .font(.system(size: 40, weight: .bold))
  58. .foregroundColor(alarm == nil ? colourGlucoseText : .loopRed)
  59. // image
  60. }
  61. HStack {
  62. let minutesAgo = -1 * (recentGlucose?.dateString.timeIntervalSinceNow ?? 0) / 60
  63. let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
  64. Text(
  65. minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  66. text + " " +
  67. NSLocalizedString("min", comment: "Short form for minutes") + " "
  68. )
  69. )
  70. .font(.caption2).foregroundColor(.secondary)
  71. Text(
  72. delta
  73. .map {
  74. deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  75. } ?? "--"
  76. )
  77. .font(.caption2).foregroundColor(.secondary)
  78. }.frame(alignment: .top)
  79. }
  80. }
  81. .onChange(of: recentGlucose?.direction) { newDirection in
  82. withAnimation {
  83. switch newDirection {
  84. case .doubleUp,
  85. .singleUp,
  86. .tripleUp:
  87. rotationDegrees = -90
  88. case .fortyFiveUp:
  89. rotationDegrees = -45
  90. case .flat:
  91. rotationDegrees = 0
  92. case .fortyFiveDown:
  93. rotationDegrees = 45
  94. case .doubleDown,
  95. .singleDown,
  96. .tripleDown:
  97. rotationDegrees = 90
  98. case .none,
  99. .notComputable,
  100. .rateOutOfRange:
  101. rotationDegrees = 0
  102. @unknown default:
  103. rotationDegrees = 0
  104. }
  105. }
  106. }
  107. }
  108. // var image: Image {
  109. // guard let direction = recentGlucose?.direction else {
  110. // return Image(systemName: "arrow.left.and.right")
  111. // }
  112. //
  113. // switch direction {
  114. // case .doubleUp,
  115. // .singleUp,
  116. // .tripleUp:
  117. // return Image(systemName: "arrow.up")
  118. // case .fortyFiveUp:
  119. // return Image(systemName: "arrow.up.right")
  120. // case .flat:
  121. // return Image(systemName: "arrow.forward")
  122. // case .fortyFiveDown:
  123. // return Image(systemName: "arrow.down.forward")
  124. // case .doubleDown,
  125. // .singleDown,
  126. // .tripleDown:
  127. // return Image(systemName: "arrow.down")
  128. //
  129. // case .none,
  130. // .notComputable,
  131. // .rateOutOfRange:
  132. // return Image(systemName: "arrow.left.and.right")
  133. // }
  134. // }
  135. var colorOfGlucose: Color {
  136. let whichGlucose = recentGlucose?.glucose ?? 0
  137. guard lowGlucose < highGlucose else { return .primary }
  138. switch whichGlucose {
  139. case 0 ..< Int(lowGlucose):
  140. return .loopRed
  141. case Int(lowGlucose) ..< Int(highGlucose):
  142. return .loopGreen
  143. case Int(highGlucose)...:
  144. return .loopYellow
  145. default:
  146. return .loopYellow
  147. }
  148. }
  149. }
  150. struct Triangle: Shape {
  151. func path(in rect: CGRect) -> Path {
  152. var path = Path()
  153. let cornerRadius: CGFloat = 8
  154. path.move(to: CGPoint(x: rect.midX, y: rect.minY))
  155. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
  156. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY - cornerRadius), control: CGPoint(x: rect.midX, y: rect.maxY))
  157. path.closeSubpath()
  158. return path
  159. }
  160. }
  161. struct TrendShape: View {
  162. let color: Color
  163. var body: some View {
  164. HStack(alignment: .center) {
  165. ZStack {
  166. CircleShape(color: color)
  167. TriangleShape(color: color)
  168. }
  169. }
  170. }
  171. }
  172. struct CircleShape: View {
  173. @Environment(\.colorScheme) var colorScheme
  174. let color: Color
  175. var body: some View {
  176. let colorBackground: Color = colorScheme == .dark ? .black : .white
  177. Circle()
  178. .stroke(color, lineWidth: 10)
  179. .shadow(radius: 3)
  180. .background(Circle().fill(colorBackground))
  181. .frame(width: 110, height: 110)
  182. }
  183. }
  184. struct TriangleShape: View {
  185. let color: Color
  186. var body: some View {
  187. Triangle()
  188. .fill(color)
  189. .frame(width: 30, height: 30)
  190. .rotationEffect(.degrees(90))
  191. .offset(x: 65)
  192. }
  193. }