-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
224 lines (201 loc) · 7.98 KB
/
AppDelegate.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
import CareKit
import CareKitStore
import UIKit
import os.log
import ResearchKit
import SwiftUI
import Foundation
import WatchConnectivity
import ActivityKit
import AVFoundation
import AVKit
import CoreMotion
@main
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
let storeManager = OCKSynchronizedStoreManager(
wrapping: OCKStore(
name: "com.apple.wwdc.carekitstore",
type: .inMemory))
@AppStorage("WatchConnectivity") var WatchConnectivity : Bool = true
@AppStorage("AppleWatch.BatteryStatus") var WatchBatteryStatus : Double = 0
var Message : String = ""
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
WatchConnectivity = false
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
if WCSession.default.isReachable {
print("Watch Connection Reachable")
} else {
print("Watch Connection Unreachable")
}
}
// func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
// print("received message from Watch: \(message)")
// DispatchQueue.main.async {
// if let value = message["watch"] as? String {
// self.Message = String(stringLiteral: value)
// }
// }
// }
// func sessionReachabilityDidChange(_ session: WCSession) {
// print(session.isReachable)
//
// }
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
//MARK: START UP FUNCTIONS
seedTasks()
if WCSession.isSupported() {
print("Watch Connection Supported")
WatchConnectivity = false
let session = WCSession.default
session.delegate = self
session.activate()
print("WCSession Activated: \(session.activationState.rawValue)")
print(session.receivedApplicationContext)
} else
{
print("Watch Connection Not Supported")
}
print("didFinishLaunchingWithOptions = Start Up Functions")
return true
//MARK: START UP FUNCTIONS
}
// MARK: UISceneSession Life Cycle
func seedTasks() {
print("View Controller loading finished.")
let thisMorning = Calendar.current.startOfDay(for: Date())
let nextMonth = Calendar.current.date(
byAdding: .month,
value: 1,
to: thisMorning
)
let dailyElement = OCKScheduleElement(
start: thisMorning,
end: nextMonth,
interval: DateComponents(day: 1),
text: "Please complete the questionnaire by the end of the day!",
targetValues: [],
duration: .allDay
)
let weeklyElement = OCKScheduleElement(
start: thisMorning,
end: nextMonth,
interval: DateComponents(weekOfYear: 2),
text: "Its time to complete the required questionnaire!",
targetValues: [],
duration: .allDay
)
let onboardSchedule = OCKSchedule.dailyAtTime(
hour: 0, minutes: 0,
start: Date(), end: Date(),
text: "Letter of Information / Consent Form",
duration: .allDay
)
var onboardTask = OCKTask(
id: TaskIDs.onboarding,
title: "Onboarding Phase",
carePlanUUID: publicDS.careplan.uuid,
schedule: onboardSchedule
)
onboardTask.instructions = "You'll need to review to some terms and conditions before proceeding with the study."
onboardTask.impactsAdherence = true
let demoschedule = OCKSchedule.dailyAtTime(hour: 0, minutes: 0, start: Date(), end: Date(), text: "Please complete the demographics questionnaire before proceeding with the study!",duration: .allDay)
var demotask = OCKTask(id: TaskIDs.demographics, title: "Please complete the Demographics Quetionnaire", carePlanUUID: publicDS.careplan.uuid, schedule: demoschedule)
demotask.impactsAdherence = true
let PSQISchedule = OCKSchedule(composing: [weeklyElement])
var PSQITask = OCKTask(id: TaskIDs.PSQI, title: "Sleep Quality Questionnaire", carePlanUUID: publicDS.careplan.uuid, schedule: PSQISchedule)
PSQITask.impactsAdherence = true
let GodinSchedule = OCKSchedule(composing: [weeklyElement])
var GodinTask = OCKTask(id: TaskIDs.GLPAQ, title: "Physical Activity Questionnaire!", carePlanUUID: publicDS.careplan.uuid, schedule: GodinSchedule)
GodinTask.impactsAdherence = true
let RecSchedule = OCKSchedule(composing: [dailyElement])
var RecTask = OCKTask(id: TaskIDs.RecreationalScreenTime, title: "Recreational Screen Time Report", carePlanUUID: publicDS.careplan.uuid, schedule: RecSchedule)
RecTask.impactsAdherence = true
let HAMDSchedule = OCKSchedule(composing: [weeklyElement])
var HAMDTask = OCKTask(
id: TaskIDs.HAMD6,
title: "Mental Health Questionnaire",
carePlanUUID: publicDS.careplan.uuid,
schedule: HAMDSchedule
)
HAMDTask.impactsAdherence = true
storeManager.store.addAnyTasks(
[onboardTask, demotask, HAMDTask, GodinTask, RecTask,PSQITask],
callbackQueue: .main) { result in
switch result {
case let .success(tasks):
Logger.store.info("Seeded \(tasks.count) tasks")
case let .failure(error):
Logger.store.warning("Failed to seed tasks: \(error as NSError)")
}
}
}
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
UISceneConfiguration(
name: "Default Configuration",
sessionRole: connectingSceneSession.role)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.banner, .badge, .sound])
}
}
func addNotification(time: Double, title: String, subtitle: String, body: String) {
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications {
UNNotification in
center.setBadgeCount(UNNotification.count)
}
let addRequest = {
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = body
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}
center.getNotificationSettings { settings in
if settings.authorizationStatus == .authorized {
addRequest()
} else {
center.requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
addRequest()
} else {
print("Authorization declined")
}
}
}
}
}
extension String {
subscript (bounds: CountableClosedRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start...end])
}
subscript (bounds: CountableRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start..<end])
}
}