forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopCompletionHUDView.swift
More file actions
226 lines (187 loc) · 8.96 KB
/
LoopCompletionHUDView.swift
File metadata and controls
226 lines (187 loc) · 8.96 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// LoopCompletionHUDView.swift
// Naterade
//
// Created by Nathan Racklyeft on 5/1/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
import LoopKitUI
import LoopCore
public final class LoopCompletionHUDView: BaseHUDView {
@IBOutlet private weak var loopStateView: LoopStateView!
override public var orderPriority: HUDViewOrderPriority {
return 2
}
private(set) var freshness = LoopCompletionFreshness.stale {
didSet {
updateTintColor()
}
}
override public func awakeFromNib() {
super.awakeFromNib()
updateDisplay(nil)
}
public var loopIconClosed = false {
didSet {
loopStateView.open = !loopIconClosed
}
}
public var lastLoopCompleted: Date? {
didSet {
if lastLoopCompleted != oldValue {
loopInProgress = false
}
}
}
public var loopInProgress = false {
didSet {
loopStateView.animated = loopInProgress
if !loopInProgress {
updateTimer = nil
assertTimer()
}
}
}
public var closedLoopDisallowedLocalizedDescription: String?
public func assertTimer(_ active: Bool = true) {
if active && window != nil, let date = lastLoopCompleted {
initTimer(date)
} else {
updateTimer = nil
}
}
override public func stateColorsDidUpdate() {
super.stateColorsDidUpdate()
updateTintColor()
}
private func updateTintColor() {
let tintColor: UIColor?
switch freshness {
case .fresh:
tintColor = stateColors?.normal
case .aging:
tintColor = stateColors?.warning
case .stale:
tintColor = stateColors?.error
}
self.tintColor = tintColor
}
private func initTimer(_ startDate: Date) {
let updateInterval = TimeInterval(minutes: 1)
let timer = Timer(
fireAt: startDate.addingTimeInterval(2),
interval: updateInterval,
target: self,
selector: #selector(updateDisplay(_:)),
userInfo: nil,
repeats: true
)
updateTimer = timer
RunLoop.main.add(timer, forMode: .default)
}
private var updateTimer: Timer? {
willSet {
if let timer = updateTimer {
timer.invalidate()
}
}
}
private lazy var formatterFull: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute]
formatter.maximumUnitCount = 1
formatter.unitsStyle = .full
return formatter
}()
private var lastLoopMessage: String = ""
private lazy var timeAgoFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute]
formatter.maximumUnitCount = 1
formatter.unitsStyle = .short
return formatter
}()
private lazy var timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}()
private lazy var timeDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
formatter.locale = Locale.current
return formatter
}()
@objc private func updateDisplay(_: Timer?) {
lastLoopMessage = ""
let timeAgoToIncludeTimeStamp: TimeInterval = .minutes(20)
let timeAgoToIncludeDate: TimeInterval = .hours(4)
if let date = lastLoopCompleted {
let ago = abs(min(0, date.timeIntervalSinceNow))
freshness = LoopCompletionFreshness(age: ago)
if let timeString = timeAgoFormatter.string(from: ago) {
switch traitCollection.preferredContentSizeCategory {
case UIContentSizeCategory.extraSmall,
UIContentSizeCategory.small,
UIContentSizeCategory.medium,
UIContentSizeCategory.large:
// Use a longer form only for smaller text sizes
caption?.text = String(format: LocalizedString("%@ ago", comment: "Format string describing the time interval since the last completion date. (1: The localized date components"), timeString)
default:
caption?.text = timeString
}
accessibilityLabel = String(format: LocalizedString("Loop ran %@ ago", comment: "Accessbility format label describing the time interval since the last completion date. (1: The localized date components)"), timeString)
var fullTimeStr: String = ""
if ago >= timeAgoToIncludeDate {
fullTimeStr = String(format: LocalizedString("was at %1$@", comment: "Format string describing last completion. (1: the date"), timeDateFormatter.string(from: date))
} else if ago >= timeAgoToIncludeTimeStamp {
fullTimeStr = String(format: LocalizedString("%1$@ ago at %2$@", comment: "Format string describing last completion. (1: time ago, (2: the date"), timeAgoFormatter.string(from: ago)!, timeFormatter.string(from: date))
} else if ago < .minutes(1) {
fullTimeStr = String(format: LocalizedString("<1 min ago", comment: "Format string describing last completion"))
} else {
fullTimeStr = String(format: LocalizedString("%1$@ ago", comment: "Format string describing last completion. (1: time ago"), timeAgoFormatter.string(from: ago)!)
}
lastLoopMessage = String(format: LocalizedString("Last completed loop %1$@.", comment: "Last loop time completed message (1: last loop time string)"), fullTimeStr)
} else {
caption?.text = "–"
accessibilityLabel = nil
}
} else {
caption?.text = "–"
accessibilityLabel = LocalizedString("Waiting for first run", comment: "Accessibility label describing completion HUD waiting for first run")
}
if loopIconClosed {
accessibilityHint = LocalizedString("Closed loop", comment: "Accessibility hint describing completion HUD for a closed loop")
} else {
accessibilityHint = LocalizedString("Open loop", comment: "Accessbility hint describing completion HUD for an open loop")
}
}
override public func didMoveToWindow() {
super.didMoveToWindow()
assertTimer()
}
}
extension LoopCompletionHUDView {
public var loopCompletionMessage: (title: String, message: String) {
switch freshness {
case .fresh:
if loopStateView.open {
let reason = closedLoopDisallowedLocalizedDescription ?? LocalizedString("Tap Settings to toggle Closed Loop ON if you wish for the app to automate your insulin.", comment: "Instructions for user to close loop if it is allowed.")
return (title: LocalizedString("Closed Loop OFF", comment: "Title of green open loop OFF message"),
message: String(format: LocalizedString("\n%1$@ is operating with Closed Loop in the OFF position. Your pump and CGM will continue operating, but the app will not adjust dosing automatically.\n\n%2$@", comment: "Green closed loop OFF message (1: app name)(2: reason for open loop)"), Bundle.main.bundleDisplayName, reason))
} else {
return (title: LocalizedString("Closed Loop ON", comment: "Title of green closed loop ON message"),
message: String(format: LocalizedString("\n%1$@\n\n%2$@ is operating with Closed Loop in the ON position.", comment: "Green closed loop ON message (1: last loop string) (2: app name)"), lastLoopMessage, Bundle.main.bundleDisplayName))
}
case .aging:
return (title: LocalizedString("Loop Warning", comment: "Title of yellow loop message"),
message: String(format: LocalizedString("\n%1$@\n\nTap your CGM and insulin pump status icons for more information. %2$@ will continue trying to complete a loop, but watch for potential communication issues with your pump and CGM.", comment: "Yellow loop message (1: last loop string) (2: app name)"), lastLoopMessage, Bundle.main.bundleDisplayName))
case .stale:
return (title: LocalizedString("Loop Failure", comment: "Title of red loop message"),
message: String(format: LocalizedString("\n%1$@\n\nTap your CGM and insulin pump status icons for more information. %2$@ will continue trying to complete a loop, but check for potential communication issues with your pump and CGM.", comment: "Red loop message (1: last loop string) (2: app name)"), lastLoopMessage, Bundle.main.bundleDisplayName))
}
}
}