-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCareViewController.swift
More file actions
401 lines (350 loc) · 13.8 KB
/
CareViewController.swift
File metadata and controls
401 lines (350 loc) · 13.8 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright (c) 2019, Apple Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
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/or
other materials provided with the distribution.
3. Neither the name of the copyright holder(s) nor the names of any contributors
may be used to endorse or promote products derived from this software without
specific prior written permission. No license is granted to the trademarks of
the copyright holders even if such marks are included in this software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. 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) HOWEVER
CAUSED 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 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import CareKit
import CareKitEssentials
import CareKitStore
import CareKitUI
import os.log
import SwiftUI
import UIKit
// swiftlint:disable type_body_length
@MainActor
class CareViewController: OCKDailyPageViewController {
private var isSyncing = false
private var isLoading = false
private var style: Styler {
CustomStylerKey.defaultValue
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: self,
action: #selector(synchronizeWithRemote)
)
NotificationCenter.default.addObserver(
self,
selector: #selector(synchronizeWithRemote),
name: Notification.Name(
rawValue: Constants.requestSync
),
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(updateSynchronizationProgress(_:)),
name: Notification.Name(rawValue: Constants.progressUpdate),
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(reloadView(_:)),
name: Notification.Name(rawValue: Constants.finishedAskingForPermission),
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(reloadView(_:)),
name: Notification.Name(rawValue: Constants.shouldRefreshView),
object: nil
)
}
@objc private func updateSynchronizationProgress(
_ notification: Notification
) {
guard let receivedInfo = notification.userInfo as? [String: Any],
let progress = receivedInfo[Constants.progressUpdate] as? Int else {
return
}
DispatchQueue.main.async {
switch progress {
case 0, 100:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "\(progress)",
style: .plain, target: self,
action: #selector(self.synchronizeWithRemote))
if progress == 100 {
// Give sometime for the user to see 100
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh,
target: self,
// swiftlint:disable:next line_length
action: #selector(self.synchronizeWithRemote))
// swiftlint:disable:next line_length
self.navigationItem.rightBarButtonItem?.tintColor = self.navigationItem.leftBarButtonItem?.tintColor
}
}
default:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "\(progress)",
style: .plain, target: self,
action: #selector(self.synchronizeWithRemote))
self.navigationItem.rightBarButtonItem?.tintColor = self.view.tintColor
}
}
}
@objc private func synchronizeWithRemote() {
guard !isSyncing else {
return
}
isSyncing = true
AppDelegateKey.defaultValue?.store.synchronize { error in
let errorString = error?.localizedDescription ?? "Successful sync with remote!"
Logger.feed.info("\(errorString)")
DispatchQueue.main.async {
if error != nil {
self.navigationItem.rightBarButtonItem?.tintColor = .red
} else {
self.navigationItem.rightBarButtonItem?.tintColor = self.navigationItem.leftBarButtonItem?.tintColor
}
self.isSyncing = false
}
}
}
@objc private func reloadView(_ notification: Notification? = nil) {
guard !isLoading else {
return
}
self.reload()
}
/*
This will be called each time the selected date changes.
Use this as an opportunity to rebuild the content shown to the user.
*/
override func dailyPageViewController(
_ dailyPageViewController: OCKDailyPageViewController,
prepare listViewController: OCKListViewController,
for date: Date
) {
self.isLoading = true
// Always call this method to ensure dates for
// queries are correct.
let date = modifyDateIfNeeded(date)
let isCurrentDay = isSameDay(as: date)
#if os(iOS)
// Only show the tip view on the current date
if isCurrentDay {
if Calendar.current.isDate(date, inSameDayAs: Date()) {
// Add a non-CareKit view into the list
let tipTitle = "Benefits of exercising"
let tipText = "Learn how activity can promote a healthy pregnancy."
let tipView = TipView()
tipView.headerView.titleLabel.text = tipTitle
tipView.headerView.detailLabel.text = tipText
tipView.imageView.image = UIImage(named: "exercise.jpg")
tipView.customStyle = CustomStylerKey.defaultValue
listViewController.appendView(tipView, animated: false)
}
}
#endif
fetchAndDisplayTasks(on: listViewController, for: date)
}
private func isSameDay(as date: Date) -> Bool {
Calendar.current.isDate(
date,
inSameDayAs: Date()
)
}
private func modifyDateIfNeeded(_ date: Date) -> Date {
guard date < .now else {
return date
}
guard !isSameDay(as: date) else {
return .now
}
return date.endOfDay
}
private func fetchAndDisplayTasks(
on listViewController: OCKListViewController,
for date: Date
) {
Task {
let tasks = await self.fetchTasks(on: date)
appendTasks(tasks, to: listViewController, date: date)
}
}
private func fetchTasks(on date: Date) async -> [OCKAnyTask] {
var query = OCKTaskQuery(for: date)
query.excludesTasksWithNoEvents = true
do {
let tasks = try await store.fetchAnyTasks(query: query)
let orderedTasks = TaskID.ordered.compactMap { orderedTaskID in
tasks.first(where: { $0.id == orderedTaskID })
}
return orderedTasks
} catch {
Logger.feed.error("Could not fetch tasks: \(error, privacy: .public)")
return []
}
}
private func taskViewControllers(
_ task: OCKAnyTask,
on date: Date
) -> [UIViewController]? {
var query = OCKEventQuery(for: date)
query.taskIDs = [task.id]
switch task.id {
case TaskID.steps:
let card = EventQueryView<NumericProgressTaskView>(
query: query
)
.formattedHostingController()
return [card]
case TaskID.ovulationTestResult:
let card = EventQueryView<LabeledValueTaskView>(
query: query
)
.formattedHostingController()
return [card]
case TaskID.stretch:
let card = EventQueryView<InstructionsTaskView>(
query: query
)
.formattedHostingController()
return [card]
case TaskID.kegels:
/*
Since the kegel task is only scheduled every other day, there will be cases
where it is not contained in the tasks array returned from the query.
*/
let card = EventQueryView<SimpleTaskView>(
query: query
)
.formattedHostingController()
return [card]
#if os(iOS)
// Create a card for the doxylamine task if there are events for it on this day.
case TaskID.doxylamine:
// This is a UIKit based card.
let card = OCKChecklistTaskViewController(
query: query,
store: self.store
)
return [card]
#endif
case TaskID.nausea:
let title = String(localized: "NAUSEA_DOXYLAMINE_INTAKE")
let subtitle = String(localized: "THIS_WEEK")
let duration = Calendar
.current
.dateIntervalOfWeek(for: Date())
// dynamic gradient colors
let nauseaGradientStart = Color(TintColorFlipKey.defaultValue)
let nauseaGradientEnd = Color.accentColor
let nauseaDataSeries = CKEDataSeriesConfiguration(
taskID: task.id,
mark: .bar,
legendTitle: String(localized: "NAUSEA"),
color: nauseaGradientEnd,
gradientStartColor: nauseaGradientStart,
stackingMethod: .unstacked
) { event in
event.computeProgress(by: .summingOutcomeValues)
}
let doxylamineDataSeries = CKEDataSeriesConfiguration(
taskID: TaskID.doxylamine,
mark: .bar,
legendTitle: String(localized: "DOXYLAMINE"),
color: Color(UIColor.systemGray2),
gradientStartColor: .gray,
stackingMethod: .unstacked
) { event in
event.computeProgress(by: .summingOutcomeValues)
}
let configurations = [
nauseaDataSeries,
doxylamineDataSeries
]
// This is a SwiftUI View Chart.
let chart = CareEssentialChartView(
title: title,
subtitle: subtitle,
dateInterval: duration,
period: .day,
configurations: configurations
).formattedHostingController()
#if os(iOS)
/*
Also create a card (UIKit view) that displays a single event.
The event query passed into the initializer specifies that only
today's log entries should be displayed by this log task view controller.
*/
let nauseaCard = OCKButtonLogTaskViewController(
query: query,
store: self.store
)
let cards: [UIViewController] = [
chart,
nauseaCard
]
return cards
#else
let cards: [UIViewController] = [
chart
]
return cards
#endif
default:
return nil
}
}
private func appendTasks(
_ tasks: [OCKAnyTask],
to listViewController: OCKListViewController,
date: Date
) {
let isCurrentDay = isSameDay(as: date)
tasks.compactMap {
let cards = self.taskViewControllers(
$0,
on: date
)
cards?.forEach {
if let carekitView = $0.view as? OCKView {
carekitView.customStyle = style
}
$0.view.isUserInteractionEnabled = isCurrentDay
$0.view.alpha = !isCurrentDay ? 0.4 : 1.0
}
return cards
}.forEach { (cards: [UIViewController]) in
cards.forEach {
let card = $0
DispatchQueue.main.async {
listViewController.appendViewController(card, animated: true)
}
}
}
DispatchQueue.main.async {
self.isLoading = false
}
}
}
private extension View {
/// Convert SwiftUI view to UIKit view.
func formattedHostingController() -> UIHostingController<Self> {
let viewController = UIHostingController(rootView: self)
viewController.view.backgroundColor = .clear
return viewController
}
}