| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- import CoreData
- import LoopKit
- import SwiftUI
- struct CurrentGlucoseView: View {
- let timerDate: Date
- let units: GlucoseUnits
- let alarm: GlucoseAlarm?
- let lowGlucose: Decimal
- let highGlucose: Decimal
- let cgmAvailable: Bool
- var currentGlucoseTarget: Decimal
- let glucoseColorScheme: GlucoseColorScheme
- let glucose: [GlucoseStored] // This contains the last two glucose values, no matter if its manual or a cgm reading
- /// Drives the outer ring.
- var cgmProgress: DeviceLifecycleProgress?
- /// CGM status highlight, rendered verbatim.
- var cgmStatus: CgmDisplayState?
- /// Sensor expiration — fallback tag when `cgmStatus` is nil.
- var cgmSensorExpiresAt: Date?
- @State private var rotationDegrees: Double = 0.0
- @State private var angularGradient = AngularGradient(colors: [
- Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
- Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
- Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
- Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
- Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
- Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
- ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
- @Environment(\.colorScheme) var colorScheme
- private var deltaFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- if units == .mmolL {
- formatter.maximumFractionDigits = 1
- formatter.minimumFractionDigits = 1
- formatter.roundingMode = .halfUp
- } else {
- formatter.maximumFractionDigits = 0
- }
- formatter.positivePrefix = " +"
- formatter.negativePrefix = " -"
- return formatter
- }
- var body: some View {
- let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
- if cgmAvailable {
- ZStack {
- if let progress = cgmProgress {
- SensorLifecycleArcView(
- progress: progress.percentComplete,
- progressState: progress.progressState
- )
- }
- TrendShape(gradient: angularGradient, color: triangleColor, showArrow: true)
- .rotationEffect(.degrees(rotationDegrees))
- VStack(alignment: .center) {
- bobbleContent()
- }
- }
- .overlay(alignment: .bottom) {
- // Tag floats outside the bobble's frame so it doesn't push
- // chart / stats / tab bar down. When the trend triangle
- // rotates into the lower half (`rotationDegrees >= 45`) it
- // ends up around 6 o'clock and collides with the tag —
- // hide the SensorStatusTagView to avoid collision
- if let tag = tagLabel, !trendIsDownward {
- SensorStatusTagView(text: tag.text, theme: tag.theme)
- .offset(y: 14)
- .zIndex(1)
- }
- }
- .onChange(of: glucose.last?.directionEnum) {
- withAnimation {
- switch glucose.last?.directionEnum {
- case .doubleUp,
- .singleUp,
- .tripleUp:
- rotationDegrees = -90
- case .fortyFiveUp:
- rotationDegrees = -45
- case .flat:
- rotationDegrees = 0
- case .fortyFiveDown:
- rotationDegrees = 45
- case .doubleDown,
- .singleDown,
- .tripleDown:
- rotationDegrees = 90
- case nil,
- .notComputable,
- .rateOutOfRange:
- rotationDegrees = 0
- default:
- rotationDegrees = 0
- }
- }
- }
- } else {
- VStack(alignment: .center, spacing: 12) {
- HStack
- {
- // no cgm defined so display a generic CGM
- Image(systemName: "sensor.tag.radiowaves.forward.fill").font(.body).imageScale(.large)
- }
- HStack {
- Text("Add CGM").font(.caption).bold()
- }
- }.frame(alignment: .top)
- }
- }
- private var delta: String {
- guard glucose.count >= 2 else {
- return "--"
- }
- var lastGlucose = Decimal(glucose.last?.glucose ?? 0)
- var secondLastGlucose = Decimal(glucose.first?.glucose ?? 0)
- if units == .mmolL {
- lastGlucose = lastGlucose.asMmolL
- secondLastGlucose = secondLastGlucose.asMmolL
- }
- let delta = lastGlucose - secondLastGlucose
- return deltaFormatter.string(from: delta as NSNumber) ?? "--"
- }
- @ViewBuilder private func bobbleContent() -> some View {
- HStack {
- if let glucoseValue = glucose.last?.glucose, isReadingFresh {
- let displayGlucose = units == .mgdL
- ? Decimal(glucoseValue).description
- : Decimal(glucoseValue).formattedAsMmolL
- Text(glucoseValue == 400 ? "HIGH" : displayGlucose)
- .font(.system(size: 40, weight: .bold, design: .rounded))
- .foregroundStyle(glucoseColor(for: glucoseValue))
- } else {
- Text("– –")
- .font(.system(size: 40, weight: .bold, design: .rounded))
- .foregroundStyle(.secondary)
- }
- }
- if isReadingFresh {
- HStack {
- let minutesAgoString = TimeAgoFormatter.minutesAgo(from: glucose.last?.date)
- Group {
- Text(minutesAgoString)
- Text(delta)
- }
- .font(.callout).fontWeight(.bold)
- .foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
- }
- .frame(alignment: .top)
- }
- }
- /// Matches `APSManager`'s loop-input freshness gate — readings older than
- /// 12 minutes (one missed CGM transmission on a 5-min schedule) get
- /// masked to dashes. Handles warmup + sensor failure naturally: no
- /// fresh data → no number on the bobble.
- private var isReadingFresh: Bool {
- guard let date = glucose.last?.date else { return false }
- return Date().timeIntervalSince(date) < 12 * 60
- }
- /// True when the trend arrow is rotated into the lower half of the
- /// circle — used to decide whether the bottom tag needs to dodge the
- /// triangle by sliding up onto the bobble's rim.
- private var trendIsDownward: Bool { rotationDegrees >= 45 }
- /// Status highlight wins; otherwise fall back to remaining-time.
- private var tagLabel: (text: String, theme: SensorStatusTagTheme)? {
- if let status = cgmStatus {
- return (status.localizedMessage, theme(for: status.status))
- }
- if let expiresAt = cgmSensorExpiresAt {
- let text = SensorRemainingTimeFormatter.format(until: expiresAt)
- let theme: SensorStatusTagTheme
- switch cgmProgress?.progressState {
- case .critical: theme = .red
- case .warning: theme = .orange
- default: theme = .green
- }
- return (text, theme)
- }
- return nil
- }
- private func theme(for status: CgmDisplayStatus) -> SensorStatusTagTheme {
- switch status {
- case .critical: return .red
- case .warning: return .orange
- case .normal: return .secondary
- }
- }
- private func glucoseColor(for glucoseValue: Int16) -> Color {
- // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
- let hardCodedLow = Decimal(55)
- let hardCodedHigh = Decimal(220)
- let isDynamicColorScheme = glucoseColorScheme == .dynamicColor
- guard Decimal(glucoseValue) <= lowGlucose || Decimal(glucoseValue) >= highGlucose else {
- return Color.primary
- }
- return Trio.getDynamicGlucoseColor(
- glucoseValue: Decimal(glucoseValue),
- highGlucoseColorValue: isDynamicColorScheme ? hardCodedHigh : highGlucose,
- lowGlucoseColorValue: isDynamicColorScheme ? hardCodedLow : lowGlucose,
- targetGlucose: currentGlucoseTarget,
- glucoseColorScheme: glucoseColorScheme
- )
- }
- }
- struct Triangle: Shape {
- func path(in rect: CGRect) -> Path {
- var path = Path()
- path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
- path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
- path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
- path.closeSubpath()
- return path
- }
- }
- struct TrendShape: View {
- @Environment(\.colorScheme) var colorScheme
- let gradient: AngularGradient
- let color: Color
- var showArrow: Bool = true
- var body: some View {
- HStack(alignment: .center) {
- ZStack {
- Group {
- CircleShape(gradient: gradient)
- if showArrow {
- TriangleShape(color: color)
- }
- }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
- CircleShape(gradient: gradient)
- }
- }
- }
- }
- struct CircleShape: View {
- @Environment(\.colorScheme) var colorScheme
- let gradient: AngularGradient
- var body: some View {
- Circle()
- .stroke(gradient, lineWidth: 6)
- .background(Circle().fill(Color.chart))
- .frame(width: 130, height: 130)
- }
- }
- struct TriangleShape: View {
- let color: Color
- var body: some View {
- Triangle()
- .fill(color)
- .frame(width: 35, height: 35)
- .rotationEffect(.degrees(90))
- .offset(x: 85)
- }
- }
|