-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathG7SettingsView.swift
More file actions
223 lines (198 loc) · 8.3 KB
/
G7SettingsView.swift
File metadata and controls
223 lines (198 loc) · 8.3 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
//
// G7SettingsView.swift
// CGMBLEKitUI
//
// Created by Pete Schwamb on 9/25/22.
// Copyright © 2022 LoopKit Authors. All rights reserved.
//
import Foundation
import SwiftUI
import G7SensorKit
import LoopKitUI
struct G7SettingsView: View {
private var sessionLengthFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute]
formatter.unitsStyle = .full
formatter.maximumUnitCount = 2
return formatter
}()
@Environment(\.guidanceColors) private var guidanceColors
@Environment(\.glucoseTintColor) private var glucoseTintColor
var didFinish: (() -> Void)
var deleteCGM: (() -> Void)
@ObservedObject var viewModel: G7SettingsViewModel
@State private var showingDeletionSheet = false
init(didFinish: @escaping () -> Void, deleteCGM: @escaping () -> Void, viewModel: G7SettingsViewModel) {
self.didFinish = didFinish
self.deleteCGM = deleteCGM
self.viewModel = viewModel
}
private var timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
formatter.locale = Locale.current
formatter.setLocalizedDateFormatFromTemplate("E, MMM d, hh:mm")
return formatter
}()
var body: some View {
List {
Section() {
VStack {
headerImage
progressBar
}
}
if let activatedAt = viewModel.activatedAt {
HStack {
Text(LocalizedString("Sensor Start", comment: "title for g7 settings row showing sensor start time"))
Spacer()
Text(timeFormatter.string(from: activatedAt))
.foregroundColor(.secondary)
}
HStack {
Text(LocalizedString("Sensor Expiration", comment: "title for g7 settings row showing sensor expiration time"))
Spacer()
Text(timeFormatter.string(from: activatedAt.addingTimeInterval(viewModel.sensorType.lifetime)))
.foregroundColor(.secondary)
}
HStack {
Text(LocalizedString("Grace Period End", comment: "title for g7 settings row showing sensor grace period end time"))
Spacer()
Text(timeFormatter.string(from: activatedAt.addingTimeInterval(viewModel.sensorType.lifetime + viewModel.sensorType.gracePeriod)))
.foregroundColor(.secondary)
}
}
Section("Last Reading") {
LabeledValueView(label: LocalizedString("Glucose", comment: "Field label"),
value: viewModel.lastGlucoseString)
LabeledDateView(label: LocalizedString("Time", comment: "Field label"),
date: viewModel.latestReadingTimestamp,
dateFormatter: viewModel.dateFormatter)
LabeledValueView(label: LocalizedString("Trend", comment: "Field label"),
value: viewModel.lastGlucoseTrendString)
}
Section () {
Button(LocalizedString("Open Dexcom App", comment:"Opens the dexcom app to allow users to manage active sensors"), action: {
if let appURL = URL(string: viewModel.sensorType.dexcomAppURL) {
UIApplication.shared.open(appURL)
}
})
}
Section("Bluetooth") {
if let name = viewModel.sensorName {
HStack {
Text(LocalizedString("Name", comment: "title for g7 settings row showing BLE Name"))
Spacer()
Text(name)
.foregroundColor(.secondary)
}
}
if viewModel.scanning {
HStack {
Text(LocalizedString("Scanning", comment: "title for g7 settings connection status when scanning"))
Spacer()
SwiftUI.ProgressView()
}
} else {
if viewModel.connected {
Text(LocalizedString("Connected", comment: "title for g7 settings connection status when connected"))
} else {
HStack {
Text(LocalizedString("Connecting", comment: "title for g7 settings connection status when connecting"))
Spacer()
SwiftUI.ProgressView()
}
}
}
if let lastConnect = viewModel.lastConnect {
LabeledValueView(label: LocalizedString("Last Connect", comment: "title for g7 settings row showing sensor last connect time"),
value: timeFormatter.string(from: lastConnect))
}
}
Section("Configuration") {
HStack {
Toggle(LocalizedString("Upload Readings", comment: "title for g7 config settings to upload readings"), isOn: $viewModel.uploadReadings)
}
HStack {
Toggle(LocalizedString("15 Day Sensor", comment: "title for g7 config settings to enable 15 day sensor option"), isOn: $viewModel.isFifteenDaySensor)
}
}
Section () {
if !self.viewModel.scanning {
Button("Scan for new sensor", action: {
self.viewModel.scanForNewSensor()
})
}
deleteCGMButton
}
}
.insetGroupedListStyle()
.navigationBarItems(trailing: doneButton)
.navigationBarTitle(viewModel.sensorTypeDisplayName)
}
private var deleteCGMButton: some View {
Button(action: {
showingDeletionSheet = true
}, label: {
Text(LocalizedString("Delete CGM", comment: "Button label for removing CGM"))
.foregroundColor(.red)
}).actionSheet(isPresented: $showingDeletionSheet) {
ActionSheet(
title: Text("Are you sure you want to delete this CGM?"),
buttons: [
.destructive(Text("Delete CGM")) {
self.deleteCGM()
},
.cancel(),
]
)
}
}
private var headerImage: some View {
VStack(alignment: .center) {
Image(frameworkImage: "g7")
.resizable()
.aspectRatio(contentMode: ContentMode.fit)
.frame(height: 150)
.padding(.horizontal)
}.frame(maxWidth: .infinity)
}
@ViewBuilder
private var progressBar: some View {
VStack(alignment: .leading, spacing: 4) {
HStack(alignment: .firstTextBaseline) {
Text(viewModel.progressBarState.label)
.font(.system(size: 17))
.foregroundColor(color(for: viewModel.progressBarState.labelColor))
Spacer()
if let referenceDate = viewModel.progressReferenceDate {
Text(sessionLengthFormatter.string(from: referenceDate.timeIntervalSince(Date())) ?? "")
.foregroundColor(.secondary)
}
}
ProgressView(value: viewModel.progressBarProgress)
.accentColor(color(for: viewModel.progressBarColorStyle))
}
}
private func color(for colorStyle: ColorStyle) -> Color {
switch colorStyle {
case .glucose:
return glucoseTintColor
case .warning:
return guidanceColors.warning
case .critical:
return guidanceColors.critical
case .normal:
return .primary
case .dimmed:
return .secondary
}
}
private var doneButton: some View {
Button("Done", action: {
self.didFinish()
})
}
}