-
Notifications
You must be signed in to change notification settings - Fork 1
/
InsightsViewController.swift
284 lines (213 loc) · 10.9 KB
/
InsightsViewController.swift
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright © 2021 Apple Inc. All rights reserved.
Apple permits redistribution and use in source and binary forms, with or without
modification, providing that you adhere to the following conditions:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, and the following disclaimer in the documentation and
other distributed materials.
3. You may not use the name of the copyright holders nor the names of any contributors
to endorse or promote products that derive from this software without specific prior
written permission. Apple does not grant license to the trademarks of the copyright
holders even if this software includes such marks.
THE COPYRIGHT HOLDERS AND CONTRIBUTORS PROVIDE THIS SOFTWARE "AS IS”, AND DISCLAIM ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) WHATEVER THE CAUSE AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF YOU
ADVISE THEM OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
import CareKit
import CareKitUI
import ResearchKit
import CareKitStore
import CareKitFHIR
import UserNotifications
class MyChartDelegate: NSObject, OCKChartViewControllerDelegate {
func chartViewController<C, VS>(_ viewController: CareKit.OCKChartViewController<C, VS>, didEncounterError error: Error) where C : CareKit.OCKChartController, VS : CareKit.OCKChartViewSynchronizerProtocol {
print("Chart error: \(error)")
}
func chartViewController(_ chartViewController: OCKCartesianChartViewController, didFailWithError error: Error) {
print("Chart error: \(error)")
}
}
final class InsightsViewController:
OCKListViewController,
ORKTaskViewControllerDelegate {
let storeManager: OCKSynchronizedStoreManager
init(storeManager: OCKSynchronizedStoreManager) {
self.storeManager = storeManager
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
let saveButton = UIButton(type: .system)
saveButton.setTitle("Save as PDF", for: .normal)
saveButton.addTarget(self, action: #selector(saveButtonTapped), for: .touchUpInside)
appendView(saveButton, animated: false)
// A spacer view.
appendView(UIView(), animated: false)
//Pain and sleep and stress bar chart
let painSeries = OCKDataSeriesConfiguration(
taskID: TaskIDs.checkIn, //which task data to dispaly on the chart
legendTitle: "Pain (1-10)",
gradientStartColor: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1),
gradientEndColor: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1),
markerSize: 10, //how lage the bar will be
//it determines what Y-value should be used on the given date
eventAggregator: .custom({ events in
events
.first? //one event per day, in this case
.answer(kind: Surveys.checkInPainItemIdentifier)
?? 0
})
)
let sleepSeries = OCKDataSeriesConfiguration(
taskID: TaskIDs.checkIn,
legendTitle: "Sleep (hours)",
gradientStartColor: UIColor.systemBlue,
gradientEndColor: UIColor.systemBlue,
markerSize: 10,
eventAggregator: .custom({ events in
let ans = events
.first?
.answer(kind: Surveys.checkInSleepItemIdentifier)
?? 0
// print("events.first of sleep: \(String(describing: events.first))")
return ans
})
)
let stressSeries = OCKDataSeriesConfiguration(
taskID: TaskIDs.checkIn, //which task data to dispaly on the chart
legendTitle: "Stress (1-10)",
gradientStartColor: #colorLiteral(red: 0.4392156899, green: 0.01176470611, blue: 0.1921568662, alpha: 1),
gradientEndColor: #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1),
markerSize: 10, //how lage the bar will be
//it determines what Y-value should be used on the given date
eventAggregator: .custom({ events in
events
.first? //one event per day, in this case
.answer(kind: Surveys.checkInStressItemIdentifier)
?? 0
})
)
let barChart = OCKCartesianChartViewController(
plotType: .bar,
selectedDate: Date(),
configurations: [painSeries, sleepSeries, stressSeries],
storeManager: storeManager //to keep the content uptodate
)
barChart.title = "Pain, Sleep and Stress Correlation"
appendViewController(barChart, animated: false)
//Range of motion Scatter plot
let rangeSeriesL = OCKDataSeriesConfiguration(
taskID: TaskIDs.rangeOfMotionCheck,
legendTitle: "RoM Left (degrees)",
gradientStartColor: #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1),
gradientEndColor: #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1),
markerSize: 3,
eventAggregator: .custom({ events in
let event = events.first?.answer(kind: #keyPath(ORKRangeOfMotionResult.range))
print(event ?? 0.0)
// .answer(kind: Surveys.rangeOfMotionLeft)
return event ?? 0
})
)
//Range of motion Scatter plot
let rangeSeriesR = OCKDataSeriesConfiguration(
taskID: TaskIDs.rangeOfMotionCheckOther,
legendTitle: "RoM Right (degrees)",
gradientStartColor: #colorLiteral(red: 0.233736366, green: 0.6245343685, blue: 0.3227830231, alpha: 1),
gradientEndColor: #colorLiteral(red: 0.233736366, green: 0.6245343685, blue: 0.3227830231, alpha: 1),
markerSize: 3,
eventAggregator: .custom({ events in
let event = events.first?.answer(kind: #keyPath(ORKRangeOfMotionResult.range))
print(event ?? 0.0)
// .answer(kind: Surveys.rangeOfMotionLeft)
return event ?? 0
})
)
// view controller, with one data series
let scatterChart = OCKCartesianChartViewController(
plotType: .scatter,
selectedDate: Date(),
configurations: [rangeSeriesL, rangeSeriesR],
storeManager: storeManager
)
scatterChart.title = "Right and Left knees Range of Motion task results"
scatterChart.delegate = MyChartDelegate()
appendViewController(scatterChart, animated: false)
// A spacer view.
appendView(UIView(), animated: false)
}
func taskViewController(
_ taskViewController: ORKTaskViewController,
didFinishWith reason: ORKTaskViewControllerFinishReason,
error: Error?) {
taskViewController.dismiss(animated: true, completion: nil)
}
@IBAction func saveButtonTapped(_ sender: UIButton) {
saveAsPDF(filename: "example.pdf")
}
}
extension UIViewController {
func saveAsPDF(filename: String) {
let pdfData = NSMutableData()
let pdfBounds = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4 size in points
UIGraphicsBeginPDFContextToData(pdfData, pdfBounds, nil)
UIGraphicsBeginPDFPage()
let pdfContext = UIGraphicsGetCurrentContext()!
// Render view to PDF
view.layer.render(in: pdfContext)
// Draw text
let stringToDraw = "Weekly Charts:\n"
let string1 = "1. Correlation between Pain, Sleep and Stress Levels\n"
let string2 = "2. Range of Motion of both knees\n"
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 20)]
let stringRect = CGRect(x: 50, y: 10, width: 400, height: 25)
let stringRect1 = CGRect(x: 50, y: 35, width: 400, height: 25)
let stringRect2 = CGRect(x: 50, y: 60, width: 400, height: 25)
stringToDraw.draw(in: stringRect, withAttributes: attributes)
string1.draw(in: stringRect1, withAttributes: attributes)
string2.draw(in: stringRect2, withAttributes: attributes)
UIGraphicsEndPDFContext()
// Save PDF to temporary file and present document picker
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename + ".pdf")
do {
try pdfData.write(to: tempURL)
} catch {
print("Error writing PDF file: \(error.localizedDescription)")
return
}
let documentPicker = UIDocumentPickerViewController(forExporting: [tempURL], asCopy: true)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
present(documentPicker, animated: true, completion: nil)
}
}
extension UIViewController: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let selectedUrl = urls.first else {
print("No URL selected")
return
}
do {
let pdfData = try NSData(contentsOf: selectedUrl) as Data
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let documentURL = documentDirectory.appendingPathComponent(selectedUrl.lastPathComponent)
try pdfData.write(to: documentURL)
print("Saved PDF file to \(documentURL.absoluteString)")
} catch {
print("Error writing PDF file: \(error.localizedDescription)")
}
}
}