-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathG7CGMManager+UI.swift
More file actions
140 lines (117 loc) · 5.36 KB
/
G7CGMManager+UI.swift
File metadata and controls
140 lines (117 loc) · 5.36 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
//
// G7CGMManager+UI.swift
// CGMBLEKitUI
//
// Created by Pete Schwamb on 9/24/22.
// Copyright © 2022 LoopKit Authors. All rights reserved.
//
import Foundation
import G7SensorKit
import LoopKitUI
import LoopKit
public struct G7DeviceStatusHighlight: DeviceStatusHighlight, Equatable {
public let localizedMessage: String
public let imageName: String
public let state: DeviceStatusHighlightState
init(localizedMessage: String, imageName: String, state: DeviceStatusHighlightState) {
self.localizedMessage = localizedMessage
self.imageName = imageName
self.state = state
}
}
extension G7CGMManager: CGMManagerUI {
public static var onboardingImage: UIImage? {
return nil
}
public static func setupViewController(bluetoothProvider: LoopKit.BluetoothProvider, displayGlucosePreference: DisplayGlucosePreference, colorPalette: LoopKitUI.LoopUIColorPalette, allowDebugFeatures: Bool, prefersToSkipUserInteraction: Bool) -> LoopKitUI.SetupUIResult<LoopKitUI.CGMManagerViewController, LoopKitUI.CGMManagerUI>
{
let vc = G7UICoordinator(colorPalette: colorPalette, displayGlucosePreference: displayGlucosePreference, allowDebugFeatures: allowDebugFeatures)
return .userInteractionRequired(vc)
}
public func settingsViewController(bluetoothProvider: BluetoothProvider, displayGlucosePreference: DisplayGlucosePreference, colorPalette: LoopUIColorPalette, allowDebugFeatures: Bool) ->CGMManagerViewController {
return G7UICoordinator(cgmManager: self, colorPalette: colorPalette, displayGlucosePreference: displayGlucosePreference, allowDebugFeatures: allowDebugFeatures)
}
public var smallImage: UIImage? {
UIImage(named: "g7", in: Bundle(for: G7SettingsViewModel.self), compatibleWith: nil)!
}
// TODO Placeholder.
public var cgmStatusHighlight: DeviceStatusHighlight? {
if lifecycleState == .searching {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Searching for\nSensor", comment: "G7 Status highlight text for searching for sensor"),
imageName: "dot.radiowaves.left.and.right",
state: .normalCGM)
}
if lifecycleState == .expired {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Sensor\nExpired", comment: "G7 Status highlight text for sensor expired"),
imageName: "clock",
state: .normalCGM)
}
if lifecycleState == .failed {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Sensor\nFailed", comment: "G7 Status highlight text for sensor failed"),
imageName: "exclamationmark.circle.fill",
state: .critical)
}
if let latestReadingReceivedAt = state.latestReadingTimestamp, latestReadingReceivedAt.timeIntervalSinceNow < -.minutes(15) {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Signal\nLoss", comment: "G7 Status highlight text for signal loss"),
imageName: "exclamationmark.circle.fill",
state: .warning)
}
if let latestReading = latestReading, latestReading.algorithmState.hasTemporaryError {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Sensor\nIssue", comment: "G7 Status highlight text for sensor error"),
imageName: "exclamationmark.circle.fill",
state: .warning)
}
if lifecycleState == .warmup {
return G7DeviceStatusHighlight(
localizedMessage: LocalizedString("Sensor\nWarmup", comment: "G7 Status highlight text for sensor warmup"),
imageName: "clock",
state: .normalCGM)
}
return nil
}
// TODO Placeholder.
public var cgmStatusBadge: DeviceStatusBadge? {
if lifecycleState == .gracePeriod {
return G7DeviceStatusBadge(image: UIImage(systemName: "clock"), state: .critical)
}
return nil
}
// TODO Placeholder.
public var cgmLifecycleProgress: DeviceLifecycleProgress? {
switch lifecycleState {
case .ok:
// show remaining lifetime, if < 24 hours
guard let expiration = sensorExpiresAt else {
return nil
}
let remaining = max(0, expiration.timeIntervalSinceNow)
if remaining < .hours(24) {
return G7LifecycleProgress(percentComplete: 1-(remaining/state.sensorType.lifetime), progressState: .warning)
}
return nil
case .gracePeriod:
guard let endTime = sensorEndsAt else {
return nil
}
let remaining = max(0, endTime.timeIntervalSinceNow)
return G7LifecycleProgress(percentComplete: 1-(remaining/state.sensorType.gracePeriod), progressState: .critical)
case .expired:
return G7LifecycleProgress(percentComplete: 1, progressState: .critical)
default:
return nil
}
}
}
struct G7DeviceStatusBadge: DeviceStatusBadge {
var image: UIImage?
var state: LoopKitUI.DeviceStatusBadgeState
}
struct G7LifecycleProgress: DeviceLifecycleProgress {
var percentComplete: Double
var progressState: LoopKit.DeviceLifecycleProgressState
}