-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathLoopCompletionHUDView.swift
More file actions
168 lines (139 loc) · 4.79 KB
/
LoopCompletionHUDView.swift
File metadata and controls
168 lines (139 loc) · 4.79 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
//
// LoopCompletionHUDView.swift
// Naterade
//
// Created by Nathan Racklyeft on 5/1/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
public final class LoopCompletionHUDView: BaseHUDView {
@IBOutlet private weak var loopStateView: LoopStateView!
enum Freshness {
case fresh
case aging
case stale
case unknown
}
private(set) var freshness = Freshness.unknown {
didSet {
updateTintColor()
}
}
override public func awakeFromNib() {
super.awakeFromNib()
updateDisplay(nil)
}
public var dosingEnabled = false {
didSet {
loopStateView.open = !dosingEnabled
}
}
public var lastLoopCompleted: Date? {
didSet {
updateTimer = nil
loopInProgress = false
assertTimer()
}
}
public var loopInProgress = false {
didSet {
loopStateView.animated = loopInProgress
}
}
public func assertTimer(_ active: Bool = true) {
if active && window != nil, let date = lastLoopCompleted {
initTimer(date)
} else {
updateTimer = nil
}
}
public var stateColors: StateColorPalette? {
didSet {
updateTintColor()
}
}
private func updateTintColor() {
let tintColor: UIColor?
switch freshness {
case .fresh:
tintColor = stateColors?.normal
case .aging:
tintColor = stateColors?.warning
case .stale:
tintColor = stateColors?.error
case .unknown:
tintColor = stateColors?.unknown
}
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: RunLoopMode.defaultRunLoopMode)
}
private var updateTimer: Timer? {
willSet {
if let timer = updateTimer {
timer.invalidate()
}
}
}
private lazy var formatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute]
formatter.maximumUnitCount = 1
formatter.unitsStyle = .short
return formatter
}()
@objc private func updateDisplay(_: Timer?) {
if let date = lastLoopCompleted {
let ago = abs(min(0, date.timeIntervalSinceNow))
switch ago {
case let t where t <= .minutes(6):
freshness = .fresh
case let t where t <= .minutes(16):
freshness = .aging
case let t where t <= .hours(12):
freshness = .stale
default:
freshness = .unknown
}
if let timeString = formatter.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: NSLocalizedString("%@ 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: NSLocalizedString("Loop ran %@ ago", comment: "Accessbility format label describing the time interval since the last completion date. (1: The localized date components)"), timeString)
} else {
caption.text = "—"
accessibilityLabel = nil
}
} else {
caption.text = "—"
accessibilityLabel = NSLocalizedString("Waiting for first run", comment: "Acessibility label describing completion HUD waiting for first run")
}
if dosingEnabled {
accessibilityHint = NSLocalizedString("Closed loop", comment: "Accessibility hint describing completion HUD for a closed loop")
} else {
accessibilityHint = NSLocalizedString("Open loop", comment: "Accessbility hint describing completion HUD for an open loop")
}
}
override public func didMoveToWindow() {
super.didMoveToWindow()
assertTimer()
}
}