-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnumerator+Trash.swift
More file actions
108 lines (97 loc) · 3.81 KB
/
Enumerator+Trash.swift
File metadata and controls
108 lines (97 loc) · 3.81 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
//
// Enumerator+Trash.swift
// NextcloudFileProviderKit
//
// Created by Claudio Cambra on 2024-12-02.
//
import FileProvider
import NextcloudKit
extension Enumerator {
static func completeEnumerationObserver(
_ observer: NSFileProviderEnumerationObserver,
account: Account,
remoteInterface: RemoteInterface,
dbManager: FilesDatabaseManager,
numPage: Int,
trashItems: [NKTrash]
) {
var metadatas = [SendableItemMetadata]()
for trashItem in trashItems {
let metadata = trashItem.toItemMetadata(account: account)
dbManager.addItemMetadata(metadata)
metadatas.append(metadata)
}
Task { [metadatas] in
do {
let items = try await metadatas.toFileProviderItems(
account: account, remoteInterface: remoteInterface, dbManager: dbManager
)
Task { @MainActor in
observer.didEnumerate(items)
Self.logger.info("Did enumerate \(items.count) trash items")
observer.finishEnumerating(upTo: fileProviderPageforNumPage(numPage))
}
} catch let error {
Self.logger.info("Unexpected error enumerating trash items, observing error.")
Task { @MainActor in observer.finishEnumeratingWithError(error) }
}
}
}
static func completeChangesObserver(
_ observer: NSFileProviderChangeObserver,
anchor: NSFileProviderSyncAnchor,
account: Account,
remoteInterface: RemoteInterface,
dbManager: FilesDatabaseManager,
trashItems: [NKTrash]
) async {
var newTrashedItems = [NSFileProviderItem]()
// NKTrash items do not have an etag ; we assume they cannot be modified while they are in
// the trash, so we will just check by ocId
var existingTrashedItems = dbManager.trashedItemMetadatas(account: account)
for trashItem in trashItems {
if let existingTrashItemIndex = existingTrashedItems.firstIndex(
where: { $0.ocId == trashItem.ocId }
) {
existingTrashedItems.remove(at: existingTrashItemIndex)
continue
}
let metadata = trashItem.toItemMetadata(account: account)
dbManager.addItemMetadata(metadata)
let item = Item(
metadata: metadata,
parentItemIdentifier: .trashContainer,
account: account,
remoteInterface: remoteInterface,
dbManager: dbManager,
remoteSupportsTrash: await remoteInterface.supportsTrash(account: account)
)
newTrashedItems.append(item)
Self.logger.debug(
"""
Will enumerate changed trashed item with ocId: \(metadata.ocId, privacy: .public)
and name: \(metadata.fileName, privacy: .public)
"""
)
}
let deletedTrashedItemsIdentifiers = existingTrashedItems.map {
NSFileProviderItemIdentifier($0.ocId)
}
if !deletedTrashedItemsIdentifiers.isEmpty {
for itemIdentifier in deletedTrashedItemsIdentifiers {
dbManager.deleteItemMetadata(ocId: itemIdentifier.rawValue)
}
Self.logger.debug(
"""
Will enumerate deleted trashed items:
\(deletedTrashedItemsIdentifiers, privacy: .public)
"""
)
observer.didDeleteItems(withIdentifiers: deletedTrashedItemsIdentifiers)
}
if !newTrashedItems.isEmpty {
observer.didUpdate(newTrashedItems)
}
observer.finishEnumeratingChanges(upTo: anchor, moreComing: false)
}
}