-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Profile.swift
249 lines (194 loc) · 9.49 KB
/
Profile.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
//
// Profile.swift
// OCKSample
//
// Created by Corey Baker on 11/25/20.
// Copyright © 2020 Network Reconnaissance Lab. All rights reserved.
//
import Foundation
import CareKit
import CareKitStore
import SwiftUI
import ParseCareKit
import UIKit
class Profile: ObservableObject {
@Published var patient: OCKPatient? = nil
private let appDelegate = UIApplication.shared.delegate as! AppDelegate //Importing UIKit gives us access here to get the OCKStore and ParseRemote
init() {
//Find this patient
findCurrentProfile { foundPatient in
self.patient = foundPatient
}
}
private func findCurrentProfile(completion: @escaping (OCKPatient?)-> Void) {
guard let uuid = getRemoteClockUUIDAfterLoginFromLocalStorage() else {
completion(nil)
return
}
//Build query to search for OCKPatient
var queryForCurrentPatient = OCKPatientQuery(for: Date()) //This makes the query for the current version of Patient
queryForCurrentPatient.ids = [uuid.uuidString] //Search for the current logged in user
self.appDelegate.synchronizedStoreManager?.store.fetchAnyPatients(query: queryForCurrentPatient, callbackQueue: .main) { result in
switch result {
case .success(let foundPatient):
guard let currentPatient = foundPatient.first as? OCKPatient else {
completion(nil)
return
}
completion(currentPatient)
case .failure(let error):
print("Error: Couldn't find patient with id \"\(uuid)\". It's possible they have never been saved. Query error: \(error)")
completion(nil)
}
}
}
//Mark: User intentions
func saveProfile(_ first: String, last: String, birth: Date) {
if var patientToUpdate = patient {
//If there is a currentPatient that was fetched, check to see if any of the fields changed
var patientHasBeenUpdated = false
if patient?.name.givenName != first {
patientHasBeenUpdated = true
patientToUpdate.name.givenName = first
}
if patient?.name.familyName != last {
patientHasBeenUpdated = true
patientToUpdate.name.familyName = last
}
if patient?.birthday != birth {
patientHasBeenUpdated = true
patientToUpdate.birthday = birth
}
if patientHasBeenUpdated {
appDelegate.synchronizedStoreManager?.store.updateAnyPatient(patientToUpdate, callbackQueue: .main) { result in
switch result {
case .success(let updated):
print("Successfully updated patient")
guard let updatedPatient = updated as? OCKPatient else {
return
}
self.patient = updatedPatient
case .failure(let error):
print("Error updating patient: \(error)")
}
}
}
} else {
guard let remoteUUID = UserDefaults.standard.object(forKey: Constants.parseRemoteClockIDKey) as? String else {
print("Error: The user currently isn't logged in")
return
}
var newPatient = OCKPatient(id: remoteUUID, givenName: first, familyName: last)
newPatient.birthday = birth
//This is new patient that has never been saved before
appDelegate.synchronizedStoreManager?.store.addAnyPatient(newPatient, callbackQueue: .main) { result in
switch result {
case .success(let new):
print("Succesffully saved new patient")
guard let newPatient = new as? OCKPatient else {
return
}
self.patient = newPatient
case .failure(let error):
print("Error: \(error)")
}
}
}
}
func savePatientAfterSignUp(_ first: String, last: String, completion: @escaping (Result<OCKPatient,Error>) -> Void) {
let remoteUUID = UUID()
//Because of the app delegate access above, we can place the initial data in the database
self.appDelegate.setupRemotes(uuid: remoteUUID)
self.appDelegate.coreDataStore.populateSampleData()
self.appDelegate.healthKitStore.populateSampleData()
self.appDelegate.parse.automaticallySynchronizes = true
self.appDelegate.firstLogin = true
//Post notification to sync
NotificationCenter.default.post(.init(name: Notification.Name(rawValue: Constants.requestSync)))
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.appDelegate.healthKitStore.requestHealthKitPermissionsForAllTasksInStore { error in
if error != nil {
print(error!.localizedDescription)
}
}
}
//Save remote ID to local
UserDefaults.standard.setValue(remoteUUID.uuidString, forKey: Constants.parseRemoteClockIDKey)
UserDefaults.standard.synchronize()
var newPatient = OCKPatient(id: remoteUUID.uuidString, givenName: first, familyName: last)
newPatient.userInfo = [Constants.parseRemoteClockIDKey: remoteUUID.uuidString] //Save the remoteId String
appDelegate.synchronizedStoreManager?.store.addAnyPatient(newPatient, callbackQueue: .main) { result in
switch result {
case .success(let savedPatient):
guard let patient = savedPatient as? OCKPatient else {
completion(.failure(AppError.couldntCast))
return
}
self.patient = patient
print("Successfully added a new Patient")
completion(.success(patient))
case .failure(let error):
print("Error added patient: \(error)")
completion(.failure(error))
}
}
}
func getRemoteClockUUIDAfterLoginFromLocalStorage() -> UUID? {
guard let uuid = UserDefaults.standard.object(forKey: Constants.parseRemoteClockIDKey) as? String else {
return nil
}
return UUID(uuidString: uuid)
}
func getRemoteClockUUIDAfterLoginFromCloud(completion: @escaping (Result<UUID,Error>) -> Void) {
let query = Patient.query()
query.first(callbackQueue: .main) { result in
switch result {
case .success(let patient):
guard let uuid = patient.userInfo?[Constants.parseRemoteClockIDKey],
let remoteClockId = UUID(uuidString: uuid) else {
completion(.failure(AppError.valueNotFoundInUserInfo))
return
}
completion(.success(remoteClockId))
case .failure(let error):
completion(.failure(error))
}
}
}
func setupRemoteAfterLoginButtonTapped(completion: @escaping (Result<UUID,Error>) -> Void) {
getRemoteClockUUIDAfterLoginFromCloud { result in
switch result {
case .success(let uuid):
DispatchQueue.main.async {
self.appDelegate.setupRemotes(uuid: uuid)
self.appDelegate.parse.automaticallySynchronizes = true
self.appDelegate.firstLogin = true
//Save remote ID to local
UserDefaults.standard.setValue(uuid.uuidString, forKey: Constants.parseRemoteClockIDKey)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(.init(name: Notification.Name(rawValue: Constants.requestSync)))
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.appDelegate.healthKitStore.requestHealthKitPermissionsForAllTasksInStore { error in
if error != nil {
print(error!.localizedDescription)
}
}
}
completion(.success(uuid))
}
case .failure(let error):
completion(.failure(error))
}
}
}
//You may not have seen "throws" before, but it's simple, this throws an error if one occurs, if not it behaves as normal
//Normally, you've seen do {} catch{} which catches the error, same concept...
func logout() throws {
try User.logout()
UserDefaults.standard.removeObject(forKey: Constants.parseRemoteClockIDKey)
UserDefaults.standard.synchronize()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
try appDelegate.healthKitStore.reset()
try appDelegate.coreDataStore.delete() //Delete data in local OCKStore database
}
}