-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlatformService.swift
73 lines (59 loc) · 2.54 KB
/
PlatformService.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
//
// PlatformService.swift
// Pilot
//
// Created by Nick Eckert on 7/18/16.
// Copyright © 2016 Sanction. All rights reserved.
//
import Cocoa
import FileKit
class PlatformService: NSObject {
fileprivate let user: PilotUser!
init(user: PilotUser) {
self.user = user
}
func syncFacebook(_ facebookService: FacebookService) {
facebookService.refreshCachedCloudContent({ cloudFiles in
// Fetch the cachedLocalContent from facebookService
let localFiles = facebookService.fetchCachedLocalContent()
// TODO: Compare the arrays and check for a change before starting the comparison algorithm
// TODO: Optimise this comparison for larger arrays
// Check for cloudFiles not in the localFiles array. Downloaded them if this is the case
for item in cloudFiles {
if !localFiles.contains(where: {$0.name == item.name}) {
// Create the DB entry and download the file upon completion! I'm so excited about this!
DBController.sharedDBController.createFacebookFile(item, completion: { _ in
facebookService.download(item, platformType: PlatformType.facebook, failure: { failedFile in
// If the download fialed then obliterate the file >:)
DBController.sharedDBController.deleteFacebookFileByName(failedFile.name)
})
})
}
}
// Check for localFiles not in the cloudFiles array. Delete them if this is the case
for item in localFiles {
if !cloudFiles.contains(where: {$0.name == item.name}) {
guard let itemStringPath = facebookService.preferences.getRootPath(service: .facebook) else {
continue
}
// Remove the DB entry
DBController.sharedDBController.deleteFacebookFileByName(item.name)
// Remove the file from cachedLocalContent
if let removeIndex = facebookService.cachedLocalContent.index(where: {$0.name == item.name}) {
facebookService.cachedLocalContent.remove(at: removeIndex)
let itemPath = Path(itemStringPath)
let fileToDelete = File<NSDictionary>(path: itemPath)
// Delete the file
do {
try fileToDelete.delete()
} catch FileKitError.deleteFileFail(path: itemPath) {
ErrorController.sharedErrorController.displayError("Unable to delete file at path \(itemStringPath)")
} catch {
print("There was an error trying to delete file at path \(itemStringPath)")
}
}
}
}
})
}
}