Repository navigation
Expand file tree
/
Copy pathCGMStatusHUDViewModel.swift
More file actions
145 lines (116 loc) · 5.6 KB
/
Copy pathCGMStatusHUDViewModel.swift
File metadata and controls
145 lines (116 loc) · 5.6 KB
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
143
144
145
//
// CGMStatusHUDViewModel.swift
// LoopUI
//
// Created by Nathaniel Hamming on 2020-06-24.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import LoopAlgorithm
import LoopKit
public class CGMStatusHUDViewModel {
static let staleGlucoseRepresentation: String = NSLocalizedString("– – –", comment: "No glucose value representation (3 dashes for mg/dL)")
var trend: GlucoseTrend?
var unitsString: String = "–"
var glucoseValueString: String = CGMStatusHUDViewModel.staleGlucoseRepresentation
var accessibilityString: String = ""
var glucoseValueTintColor: UIColor = .label
var glucoseTrendTintColor: UIColor = .glucoseTintColor
var glucoseTrendIcon: UIImage? {
guard let manualGlucoseTrendIconOverride = manualGlucoseTrendIconOverride else {
return trend?.image
}
return manualGlucoseTrendIconOverride
}
var isGlucoseValueStale: Bool = false
private var isManualGlucose: Bool = false
private var isManualGlucoseCurrent: Bool {
return isManualGlucose && !isGlucoseValueStale
}
var manualGlucoseTrendIconOverride: UIImage?
private var storedStatusHighlight: DeviceStatusHighlight?
var statusHighlight: DeviceStatusHighlight? {
get {
guard !isManualGlucoseCurrent else {
// if there is a current manual glucose, don't provide the stored status highlight
return nil
}
return storedStatusHighlight
}
set {
storedStatusHighlight = newValue
if isManualGlucoseCurrent {
// If there is a current manual glucose, it displays the current status highlight icon
setManualGlucoseTrendIconOverride()
}
if let localizedMessage = storedStatusHighlight?.localizedMessage.replacingOccurrences(of: "\n", with: " "),
let statusStateMessage = storedStatusHighlight?.state.localizedDescription
{
accessibilityString = localizedMessage + ", " + statusStateMessage
}
}
}
var isVisible: Bool = true
private lazy var timeFormatter = DateFormatter(timeStyle: .short)
func setGlucoseQuantity(_ glucoseQuantity: Double,
at glucoseStartDate: Date,
unit: LoopUnit,
glucoseDisplay: GlucoseDisplayable?,
wasUserEntered: Bool,
isDisplayOnly: Bool,
isGlucoseValueStale: Bool)
{
var accessibilityStrings = [String]()
// reset state
manualGlucoseTrendIconOverride = nil
trend = nil
let time = timeFormatter.string(from: glucoseStartDate)
glucoseValueTintColor = glucoseDisplay?.glucoseRangeCategory?.glucoseColor ?? .label
self.isGlucoseValueStale = isGlucoseValueStale
let numberFormatter = NumberFormatter.glucoseFormatter(for: unit)
if let valueString = numberFormatter.string(from: glucoseQuantity) {
if !isGlucoseValueStale {
switch glucoseDisplay?.glucoseRangeCategory {
case .some(.belowRange):
glucoseValueString = LocalizedString("LOW", comment: "String displayed instead of a glucose value below the CGM range")
case .some(.aboveRange):
glucoseValueString = LocalizedString("HIGH", comment: "String displayed instead of a glucose value above the CGM range")
default:
glucoseValueString = valueString
}
} else {
displayStaleGlucoseValue()
}
accessibilityStrings.append(String(format: LocalizedString("%1$@ at %2$@", comment: "Accessbility format value describing glucose: (1: glucose number)(2: glucose time)"), valueString, time))
}
// Only a user-entered glucose value that is *not* display-only (i.e. a calibration) is considered a manual glucose entry.
isManualGlucose = wasUserEntered && !isDisplayOnly
if isManualGlucoseCurrent {
// a manual glucose value presents any status highlight icon instead of a trend icon
setManualGlucoseTrendIconOverride()
} else if let trend = glucoseDisplay?.trendType, !isGlucoseValueStale {
self.trend = trend
glucoseTrendTintColor = glucoseDisplay?.glucoseRangeCategory?.trendColor ?? .glucoseTintColor
accessibilityStrings.append(trend.localizedDescription)
} else {
glucoseTrendTintColor = .glucoseTintColor
}
if let statusStateMessage = storedStatusHighlight?.state.localizedDescription,
let localizedMessage = storedStatusHighlight?.localizedMessage.replacingOccurrences(of: "\n", with: " ")
{
accessibilityStrings.append(localizedMessage + ", " + statusStateMessage)
}
unitsString = unit.localizedShortUnitString
accessibilityString = accessibilityStrings.joined(separator: ", ")
}
func displayStaleGlucoseValue() {
glucoseValueString = CGMStatusHUDViewModel.staleGlucoseRepresentation
glucoseValueTintColor = .label
trend = nil
glucoseTrendTintColor = .glucoseTintColor
manualGlucoseTrendIconOverride = nil
}
func setManualGlucoseTrendIconOverride() {
manualGlucoseTrendIconOverride = storedStatusHighlight?.image
glucoseTrendTintColor = storedStatusHighlight?.state.color ?? .glucoseTintColor
}
}