-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
363 lines (333 loc) · 18.5 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
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
import UIKit
import CloudKit
import UserNotifications
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let container = CKContainer.default()
var privateDatabase: CKDatabase?
var recordZone: CKRecordZone?
var zoneID: CKRecordZoneID?
var gearID: CKRecordID?
let privateSubscriptionId = "private-changes"
private let serverChangeTokenKey = "ServerChangeToken"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
DispatchQueue.main.async {
print("Received notification, but not taking action")
}
/*
privateDatabase = container.privateCloudDatabase
recordZone = CKRecordZone(zoneName: "GearZone")
zoneID = CKRecordZoneID(zoneName: "GearZone", ownerName: CKCurrentUserDefaultName)
self.fetchZoneChanges(database: privateDatabase!, databaseTokenKey: "private", zoneIDs: [zoneID!]) {}
*/
completionHandler(.newData)
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
privateDatabase = container.privateCloudDatabase
recordZone = CKRecordZone(zoneName: "GearZone")
zoneID = CKRecordZoneID(zoneName: "GearZone", ownerName: CKCurrentUserDefaultName)
self.fetchZoneChanges(database: privateDatabase!, databaseTokenKey: "private", zoneIDs: [zoneID!]) {}
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "GearBox")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func fetchZoneChanges(database: CKDatabase, databaseTokenKey: String, zoneIDs: [CKRecordZoneID], completion: @escaping () -> Void) {
var changeToken: CKServerChangeToken? = nil
let changeTokenData = UserDefaults.standard.data(forKey: serverChangeTokenKey)
if changeTokenData != nil {
changeToken = NSKeyedUnarchiver.unarchiveObject(with: changeTokenData!) as! CKServerChangeToken?
}
var optionsByRecordZoneID = [CKRecordZoneID: CKFetchRecordZoneChangesOptions]()
for zoneID in zoneIDs {
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = changeToken
optionsByRecordZoneID[zoneID] = options
}
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zoneIDs, optionsByRecordZoneID: optionsByRecordZoneID)
operation.recordChangedBlock = { (record) in
self.addRecordToLocalCache(record: record)
print("Record added/changed in cloud: \(String(describing: record.value(forKey: "details")))")
}
operation.recordWithIDWasDeletedBlock = { (recordID, _) in
self.deleteRecordFromLocalCache(recordID: recordID)
print("Record deleted in cloud")
}
operation.recordZoneChangeTokensUpdatedBlock = { (zoneID, changeToken, data) in
guard let changeToken = changeToken else {
print("Error updating record zone change token")
return}
let changeTokenData = NSKeyedArchiver.archivedData(withRootObject: changeToken)
UserDefaults.standard.set(changeTokenData, forKey: self.serverChangeTokenKey)
}
operation.recordZoneFetchCompletionBlock = { (zoneID, changeToken, data, more, error) in
guard error == nil else {
print("Error fetching zone, \(String(describing: error?.localizedDescription))")
return
}
guard let changeToken = changeToken else {
print("No change token found")
return
}
let changeTokenData = NSKeyedArchiver.archivedData(withRootObject: changeToken)
UserDefaults.standard.set(changeTokenData, forKey: self.serverChangeTokenKey)
}
operation.fetchRecordZoneChangesCompletionBlock = { (error) in
guard error == nil else {
print("Error fetching zone changes, \(String(describing: error?.localizedDescription))")
return
}
}
database.add(operation)
}
func addRecordToLocalCache(record: CKRecord) {
let details = record.value(forKey: "details") as? String
let category = record.value(forKey: "category") as? String
let majorCategory = record.value(forKey: "majorCategory") as? String
let isSingle = record.value(forKey: "isSingle") as? Bool
let isHalf = record.value(forKey: "isHalf") as? Bool
let isTwin = record.value(forKey: "isTwin") as? Bool
let isStatic = record.value(forKey: "isStatic") as? Bool
let selfLength = record.value(forKey: "selfLength") as? String
let selfWeight = record.value(forKey: "selfWeight") as? String
let manufLength = record.value(forKey: "manufLength") as? String
let manufWeight = record.value(forKey: "manufWeight") as? String
let calculatedDensity = record.value(forKey: "calculatedDensity") as? String
let manufDensity = record.value(forKey: "manufDensity") as? String
let size = record.value(forKey: "size") as? String
let notes = record.value(forKey: "notes") as? String
let update = record.value(forKey: "update") as? NSDate
var photoData: Data?
if let photo = record.value(forKey: "photo") as? CKAsset {
do {
let data = try Data(contentsOf: photo.fileURL)
photoData = data
} catch {
print(error.localizedDescription)
}
}
let gearID = record.value(forKey: "gearID") as? String
var gears = [NSManagedObject]()
persistentContainer.performBackgroundTask { (managedContext) in
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Gear")
guard gearID != nil else {
print("gearID is nil")
return
}
fetchRequest.predicate = NSPredicate(format: "gearID == %@", gearID!)
do {
gears = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch gear. \(error), \(error.userInfo)")
}
if !gears.isEmpty {
print("Gear already saved to cache, attempting to edit")
let gear = gears[0]
gear.setValue(details, forKeyPath: "details")
gear.setValue(category, forKeyPath: "category")
gear.setValue(majorCategory, forKeyPath: "majorCategory")
gear.setValue(isSingle, forKeyPath: "isSingle")
gear.setValue(isHalf, forKeyPath: "isHalf")
gear.setValue(isTwin, forKeyPath: "isTwin")
gear.setValue(isStatic, forKeyPath: "isStatic")
gear.setValue(selfLength, forKeyPath: "selfLength")
gear.setValue(selfWeight, forKeyPath: "selfWeight")
gear.setValue(manufLength, forKeyPath: "manufLength")
gear.setValue(manufWeight, forKeyPath: "manufWeight")
gear.setValue(calculatedDensity, forKeyPath: "calculatedDensity")
gear.setValue(manufDensity, forKeyPath: "manufDensity")
gear.setValue(size, forKeyPath: "size")
gear.setValue(notes, forKeyPath: "notes")
gear.setValue(update, forKeyPath: "update")
gear.setValue(photoData, forKeyPath: "photo")
gear.setValue(gearID, forKeyPath: "gearID")
do {
try managedContext.save()
print("Added new gear to local cache: \(String(describing: details))")
} catch {
let nserror = error as NSError
print("Error adding gear to local cache: \(nserror), \(nserror.userInfo)")
}
}
else {
let entity = NSEntityDescription.entity(forEntityName: "Gear", in: managedContext)!
let gear = NSManagedObject(entity: entity, insertInto: managedContext)
gear.setValue(details, forKeyPath: "details")
gear.setValue(category, forKeyPath: "category")
gear.setValue(majorCategory, forKeyPath: "majorCategory")
gear.setValue(isSingle, forKeyPath: "isSingle")
gear.setValue(isHalf, forKeyPath: "isHalf")
gear.setValue(isTwin, forKeyPath: "isTwin")
gear.setValue(isStatic, forKeyPath: "isStatic")
gear.setValue(selfLength, forKeyPath: "selfLength")
gear.setValue(selfWeight, forKeyPath: "selfWeight")
gear.setValue(manufLength, forKeyPath: "manufLength")
gear.setValue(manufWeight, forKeyPath: "manufWeight")
gear.setValue(calculatedDensity, forKeyPath: "calculatedDensity")
gear.setValue(manufDensity, forKeyPath: "manufDensity")
gear.setValue(size, forKeyPath: "size")
gear.setValue(notes, forKeyPath: "notes")
gear.setValue(update, forKeyPath: "update")
gear.setValue(photoData, forKeyPath: "photo")
gear.setValue(gearID, forKeyPath: "gearID")
do {
try managedContext.save()
print("Added new gear to local cache: \(String(describing: details))")
} catch {
let nserror = error as NSError
print("Error adding gear to local cache: \(nserror), \(nserror.userInfo)")
}
}
}
}
func deleteRecordFromLocalCache(recordID: CKRecordID) {
var gears = [NSManagedObject]()
var gear: NSManagedObject?
persistentContainer.performBackgroundTask { (managedContext) in
let gearID = recordID.recordName
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Gear")
fetchRequest.predicate = NSPredicate(format: "gearID == %@", gearID)
do {
gears = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch CoreData gear after deletion. \(error), \(error.userInfo)")
}
guard !gears.isEmpty else {
print("No gear found to match id deleted in cloud")
return
}
gear = gears[0]
managedContext.delete(gear!)
do {
try managedContext.save()
print("Deleted gear from local cache")
} catch {
let nserror = error as NSError
print("Error deleting gear from local cache: \(nserror), \(nserror.userInfo)")
}
}
}
func addRecordToDownloadQueue(id: String) {
let userdefaults = UserDefaults.standard
var queuedRecordsToDownload = [String]()
if userdefaults.object(forKey: "recordsToDownload") != nil {
queuedRecordsToDownload = (userdefaults.object(forKey: "recordsToDownload") as! NSArray) as! [String]
}
queuedRecordsToDownload.append(id)
print("Added id to queue to download containing \(queuedRecordsToDownload.count) ids")
userdefaults.set(queuedRecordsToDownload, forKey: "recordsToDownload")
userdefaults.synchronize()
}
func addRecordToDownloadEditQueue (id: String) {
let userdefaults = UserDefaults.standard
var queuedRecordsToDownloadEdit = [String]()
if userdefaults.object(forKey: "recordsToDownloadEdit") != nil {
queuedRecordsToDownloadEdit = (userdefaults.object(forKey: "recordsToDownloadEdit") as! NSArray) as! [String]
}
queuedRecordsToDownloadEdit.append(id)
print("Added id to queue to download edit containing \(queuedRecordsToDownloadEdit.count) ids")
userdefaults.set(queuedRecordsToDownloadEdit, forKey: "recordsToDownloadEdit")
userdefaults.synchronize()
}
func addRecordToDownloadDeleteQueue (id: String) {
let userdefaults = UserDefaults.standard
var queuedIdsToDownloadDelete = [String]()
if userdefaults.object(forKey: "recordsToDownloadDelete") != nil {
queuedIdsToDownloadDelete = (userdefaults.object(forKey: "recordsToDownloadDelete") as! NSArray) as! [String]
}
queuedIdsToDownloadDelete.append(id)
print("Added id to queue to download delete containing \(queuedIdsToDownloadDelete.count) ids")
userdefaults.set(queuedIdsToDownloadDelete, forKey: "recordsToDownloadDelete")
userdefaults.synchronize()
}
func removeRecordFromDownloadQueue(id: String) {
let userdefaults = UserDefaults.standard
var queuedRecordsToDownload = [String]()
if userdefaults.object(forKey: "recordsToDownload") != nil {
queuedRecordsToDownload = (userdefaults.object(forKey: "recordsToDownload") as! NSArray) as! [String]
}
queuedRecordsToDownload = queuedRecordsToDownload.filter{$0 != id}
print("Removed id from queue to download containing \(queuedRecordsToDownload.count) ids")
userdefaults.set(queuedRecordsToDownload, forKey: "recordsToDownload")
userdefaults.synchronize()
}
func removeRecordFromDownloadEditQueue (id: String) {
let userdefaults = UserDefaults.standard
var queuedRecordsToDownloadEdit = [String]()
if userdefaults.object(forKey: "recordsToDownloadEdit") != nil {
queuedRecordsToDownloadEdit = (userdefaults.object(forKey: "recordsToDownloadEdit") as! NSArray) as! [String]
}
queuedRecordsToDownloadEdit = queuedRecordsToDownloadEdit.filter{$0 != id}
print("Removed id from queue to download edit containing \(queuedRecordsToDownloadEdit.count) ids")
userdefaults.set(queuedRecordsToDownloadEdit, forKey: "recordsToDownloadEdit")
userdefaults.synchronize()
}
func removeRecordFromDownloadDeleteQueue (id: String) {
let userdefaults = UserDefaults.standard
var queuedIdsToDownloadDelete = [String]()
if userdefaults.object(forKey: "recordsToDownloadDelete") != nil {
queuedIdsToDownloadDelete = (userdefaults.object(forKey: "recordsToDownloadDelete") as! NSArray) as! [String]
}
queuedIdsToDownloadDelete = queuedIdsToDownloadDelete.filter{$0 != id}
print("Removed id from queue to download delete containing \(queuedIdsToDownloadDelete.count) ids")
userdefaults.set(queuedIdsToDownloadDelete, forKey: "recordsToDownloadDelete")
userdefaults.synchronize()
}
}