forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterViewController.swift
More file actions
263 lines (222 loc) · 10.5 KB
/
MasterViewController.swift
File metadata and controls
263 lines (222 loc) · 10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// MasterViewController.swift
// LoopKit Example
//
// Created by Nathan Racklyeft on 2/24/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
import CarbKit
import LoopKit
import InsulinKit
class MasterViewController: UITableViewController, DailyValueScheduleTableViewControllerDelegate {
private var dataManager: DeviceDataManager {
get {
return DeviceDataManager.shared
}
}
// MARK: - Data Source
private enum Section: Int {
case data
case configuration
static let count = 2
}
private enum DataRow: Int {
case carbs = 0
case reservoir
case diagnostic
static let count = 3
}
private enum ConfigurationRow: Int {
case basalRate
case glucoseTargetRange
case pumpID
static let count = 3
}
// MARK: UITableViewDataSource
override func numberOfSections(in tableView: UITableView) -> Int {
return Section.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section(rawValue: section)! {
case .configuration:
return ConfigurationRow.count
case .data:
return DataRow.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
switch Section(rawValue: indexPath.section)! {
case .configuration:
switch ConfigurationRow(rawValue: indexPath.row)! {
case .basalRate:
cell.textLabel?.text = NSLocalizedString("Basal Rates", comment: "The title text for the basal rate schedule")
case .glucoseTargetRange:
cell.textLabel?.text = NSLocalizedString("Glucose Target Range", comment: "The title text for the glucose target range schedule")
case .pumpID:
cell.textLabel?.text = NSLocalizedString("Pump ID", comment: "The title text for the pump ID")
}
case .data:
switch DataRow(rawValue: indexPath.row)! {
case .carbs:
cell.textLabel?.text = NSLocalizedString("Carbs", comment: "The title for the cell navigating to the carbs screen")
case .reservoir:
cell.textLabel?.text = NSLocalizedString("Reservoir", comment: "The title for the cell navigating to the reservoir screen")
case .diagnostic:
cell.textLabel?.text = NSLocalizedString("Diagnostic", comment: "The title for the cell displaying diagnostic data")
}
}
return cell
}
// MARK: - UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch Section(rawValue: indexPath.section)! {
case .configuration:
let sender = tableView.cellForRow(at: indexPath)
let row = ConfigurationRow(rawValue: indexPath.row)!
switch row {
case .basalRate:
let scheduleVC = SingleValueScheduleTableViewController()
if let profile = dataManager.basalRateSchedule {
scheduleVC.timeZone = profile.timeZone
scheduleVC.scheduleItems = profile.items
}
scheduleVC.delegate = self
scheduleVC.title = sender?.textLabel?.text
show(scheduleVC, sender: sender)
case .glucoseTargetRange:
let scheduleVC = GlucoseRangeScheduleTableViewController()
scheduleVC.delegate = self
scheduleVC.title = sender?.textLabel?.text
if let schedule = dataManager.glucoseTargetRangeSchedule {
scheduleVC.timeZone = schedule.timeZone
scheduleVC.scheduleItems = schedule.items
scheduleVC.unit = schedule.unit
scheduleVC.workoutRange = schedule.workoutRange
show(scheduleVC, sender: sender)
} else if let glucoseStore = dataManager.glucoseStore {
glucoseStore.preferredUnit({ (unit, error) -> Void in
DispatchQueue.main.async {
if let error = error {
self.presentAlertController(with: error)
} else if let unit = unit {
scheduleVC.unit = unit
self.show(scheduleVC, sender: sender)
}
}
})
} else {
show(scheduleVC, sender: sender)
}
case .pumpID:
let textFieldVC = TextFieldTableViewController()
// textFieldVC.delegate = self
textFieldVC.title = sender?.textLabel?.text
textFieldVC.placeholder = NSLocalizedString("Enter the 6-digit pump ID", comment: "The placeholder text instructing users how to enter a pump ID")
textFieldVC.value = dataManager.pumpID
textFieldVC.keyboardType = .numberPad
textFieldVC.contextHelp = NSLocalizedString("The pump ID can be found printed on the back, or near the bottom of the STATUS/Esc screen. It is the strictly numerical portion of the serial number (shown as SN or S/N).", comment: "Instructions on where to find the pump ID on a Minimed pump")
show(textFieldVC, sender: sender)
}
case .data:
switch DataRow(rawValue: indexPath.row)! {
case .carbs:
performSegue(withIdentifier: CarbEntryTableViewController.className, sender: indexPath)
case .reservoir:
performSegue(withIdentifier: InsulinDeliveryTableViewController.className, sender: indexPath)
case .diagnostic:
let vc = CommandResponseViewController(command: { (completionHandler) -> String in
let group = DispatchGroup()
var doseStoreResponse = ""
group.enter()
self.dataManager.doseStore.generateDiagnosticReport { (report) in
doseStoreResponse = report
group.leave()
}
var carbStoreResponse = ""
if let carbStore = self.dataManager.carbStore {
group.enter()
carbStore.generateDiagnosticReport { (report) in
carbStoreResponse = report
group.leave()
}
}
var glucoseStoreResponse = ""
if let glucoseStore = self.dataManager.glucoseStore {
group.enter()
glucoseStore.generateDiagnosticReport { (report) in
glucoseStoreResponse = report
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
completionHandler([
doseStoreResponse,
carbStoreResponse,
glucoseStoreResponse
].joined(separator: "\n\n"))
}
return "..."
})
vc.title = "Diagnostic"
show(vc, sender: indexPath)
}
}
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
var targetViewController = segue.destination
if let navVC = targetViewController as? UINavigationController, let topViewController = navVC.topViewController {
targetViewController = topViewController
}
switch targetViewController {
case let vc as CarbEntryTableViewController:
vc.carbStore = dataManager.carbStore
case let vc as CarbEntryEditViewController:
if let carbStore = dataManager.carbStore {
vc.defaultAbsorptionTimes = carbStore.defaultAbsorptionTimes
vc.preferredUnit = carbStore.preferredUnit
}
case let vc as InsulinDeliveryTableViewController:
vc.doseStore = dataManager.doseStore
default:
break
}
}
// MARK: - DailyValueScheduleTableViewControllerDelegate
func dailyValueScheduleTableViewControllerWillFinishUpdating(_ controller: DailyValueScheduleTableViewController) {
if let indexPath = tableView.indexPathForSelectedRow {
switch Section(rawValue: indexPath.section)! {
case .configuration:
switch ConfigurationRow(rawValue: indexPath.row)! {
case .basalRate:
if let controller = controller as? SingleValueScheduleTableViewController {
dataManager.basalRateSchedule = BasalRateSchedule(dailyItems: controller.scheduleItems, timeZone: controller.timeZone)
}
case .glucoseTargetRange:
if let controller = controller as? GlucoseRangeScheduleTableViewController {
dataManager.glucoseTargetRangeSchedule = GlucoseRangeSchedule(unit: controller.unit, dailyItems: controller.scheduleItems, workoutRange: controller.workoutRange, timeZone: controller.timeZone)
}
/*case let row:
if let controller = controller as? DailyQuantityScheduleTableViewController {
switch row {
case .CarbRatio:
dataManager.carbRatioSchedule = CarbRatioSchedule(unit: controller.unit, dailyItems: controller.scheduleItems, timeZone: controller.timeZone)
case .InsulinSensitivity:
dataManager.insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: controller.unit, dailyItems: controller.scheduleItems, timeZone: controller.timeZone)
default:
break
}
}*/
default:
break
}
tableView.reloadRows(at: [indexPath], with: .none)
default:
break
}
}
}
}