-
Notifications
You must be signed in to change notification settings - Fork 514
/
CurrentGlucoseView.swift
142 lines (128 loc) · 4.76 KB
/
CurrentGlucoseView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import SwiftUI
struct CurrentGlucoseView: View {
@Binding var recentGlucose: BloodGlucose?
@Binding var timerDate: Date
@Binding var delta: Int?
@Binding var units: GlucoseUnits
@Binding var alarm: GlucoseAlarm?
@Binding var lowGlucose: Decimal
@Binding var highGlucose: Decimal
@Binding var cgmAvailable: Bool
private var glucoseFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
if units == .mmolL {
formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 1
}
formatter.roundingMode = .halfUp
return formatter
}
private var deltaFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1
formatter.positivePrefix = " +"
formatter.negativePrefix = " -"
return formatter
}
private var timaAgoFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
formatter.negativePrefix = ""
return formatter
}
private var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.timeStyle = .short
return formatter
}
var body: some View {
if cgmAvailable {
VStack(alignment: .center) {
HStack {
Text(
(recentGlucose?.glucose ?? 100) == 400 ? "HIGH" : recentGlucose?.glucose
.map {
glucoseFormatter
.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
?? "--"
)
.font(.title).fontWeight(.bold)
.foregroundColor(alarm == nil ? colorOfGlucose : .loopRed)
image
}
HStack {
let minutesAgo = -1 * (recentGlucose?.dateString.timeIntervalSinceNow ?? 0) / 60
let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
Text(
minutesAgo <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
text + " " +
NSLocalizedString("min", comment: "Short form for minutes") + " "
)
)
.font(.caption2).foregroundColor(.secondary)
Text(
delta
.map {
deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
} ?? "--"
)
.font(.caption2).foregroundColor(.secondary)
}.frame(alignment: .top)
}
} 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)
}
}
var image: Image {
guard let direction = recentGlucose?.direction else {
return Image(systemName: "arrow.left.and.right")
}
switch direction {
case .doubleUp,
.singleUp,
.tripleUp:
return Image(systemName: "arrow.up")
case .fortyFiveUp:
return Image(systemName: "arrow.up.right")
case .flat:
return Image(systemName: "arrow.forward")
case .fortyFiveDown:
return Image(systemName: "arrow.down.forward")
case .doubleDown,
.singleDown,
.tripleDown:
return Image(systemName: "arrow.down")
case .none,
.notComputable,
.rateOutOfRange:
return Image(systemName: "arrow.left.and.right")
}
}
var colorOfGlucose: Color {
let whichGlucose = recentGlucose?.glucose ?? 0
guard lowGlucose < highGlucose else { return .primary }
switch whichGlucose {
case 0 ..< Int(lowGlucose):
return .loopRed
case Int(lowGlucose) ..< Int(highGlucose):
return .loopGreen
case Int(highGlucose)...:
return .loopYellow
default:
return .loopYellow
}
}
}