Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Files integration fix #2949

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 10 additions & 37 deletions File Provider Extension/FileProviderEnumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@
import NextcloudKit

class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {

var enumeratedItemIdentifier: NSFileProviderItemIdentifier
var serverUrl: String?
let fpUtility = fileProviderUtility()

init(enumeratedItemIdentifier: NSFileProviderItemIdentifier) {

self.enumeratedItemIdentifier = enumeratedItemIdentifier

// Select ServerUrl
if enumeratedItemIdentifier == .rootContainer {
serverUrl = fileProviderData.shared.homeServerUrl
} else {

let metadata = fpUtility.getTableMetadataFromItemIdentifier(enumeratedItemIdentifier)
if metadata != nil {
if let directorySource = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata!.account, metadata!.serverUrl)) {
Expand All @@ -56,12 +53,10 @@
}

func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {

var items: [NSFileProviderItemProtocol] = []

/*** WorkingSet ***/
if enumeratedItemIdentifier == .workingSet {

var itemIdentifierMetadata: [NSFileProviderItemIdentifier: tableMetadata] = [:]

// ***** Tags *****
Expand Down Expand Up @@ -103,21 +98,17 @@
}

if page == NSFileProviderPage.initialPageSortedByDate as NSFileProviderPage || page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {

self.readFileOrFolder(serverUrl: serverUrl) { metadatas in
self.completeObserver(observer, numPage: 1, metadatas: metadatas)
}

} else {

let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!

Check warning on line 105 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
completeObserver(observer, numPage: numPage, metadatas: nil)
}
}
}

func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {

var itemsDelete: [NSFileProviderItemIdentifier] = []
var itemsUpdate: [FileProviderItem] = []

Expand Down Expand Up @@ -152,12 +143,12 @@
observer.didDeleteItems(withIdentifiers: itemsDelete)
observer.didUpdate(itemsUpdate)

let data = "\(fileProviderData.shared.currentAnchor)".data(using: .utf8)

Check warning on line 146 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
observer.finishEnumeratingChanges(upTo: NSFileProviderSyncAnchor(data!), moreComing: false)
}

func currentSyncAnchor(completionHandler: @escaping (NSFileProviderSyncAnchor?) -> Void) {
let data = "\(fileProviderData.shared.currentAnchor)".data(using: .utf8)

Check warning on line 151 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
completionHandler(NSFileProviderSyncAnchor(data!))
}

Expand All @@ -166,18 +157,13 @@
// --------------------------------------------------------------------------------------------

func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, metadatas: [tableMetadata]?) {

var numPage = numPage
var items: [NSFileProviderItemProtocol] = []

if metadatas != nil {

for metadata in metadatas! {

if metadata.e2eEncrypted || (!metadata.session.isEmpty && metadata.session != NCNetworking.shared.sessionUploadBackgroundExtension) { continue }

fpUtility.createocIdentifierOnFileSystem(metadata: metadata)

let parentItemIdentifier = fpUtility.getParentItemIdentifier(metadata: metadata)
if parentItemIdentifier != nil {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
Expand All @@ -189,7 +175,7 @@

if items.count == fileProviderData.shared.itemForPage {
numPage += 1
let providerPage = NSFileProviderPage("\(numPage)".data(using: .utf8)!)

Check warning on line 178 in File Provider Extension/FileProviderEnumerator.swift

View workflow job for this annotation

GitHub Actions / Lint

Non-Optional String <-> Data Conversion Violation: Prefer using UTF-8 encoded strings when converting between `String` and `Data` (non_optional_string_data_conversion)
observer.finishEnumerating(upTo: providerPage)
} else {
observer.finishEnumerating(upTo: nil)
Expand All @@ -197,29 +183,16 @@
}

func readFileOrFolder(serverUrl: String, completionHandler: @escaping (_ metadatas: [tableMetadata]?) -> Void) {

var directoryEtag: String?

if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl)) {
directoryEtag = tableDirectory.etag
}

NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in

if directoryEtag != files.first?.etag {

NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in

if error == .success {
DispatchQueue.global().async {
NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: true) { _, metadatas in
let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal)
NCManageDatabase.shared.updateMetadatas(metadatas, predicate: predicate)
let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl), sorted: "fileName", ascending: true)
completionHandler(metadatas)
}
}
} else {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: NCKeychain().showHiddenFiles) { account, files, _, error in
if error == .success {
DispatchQueue.global().async {
NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: true) { metadataFolder, metadatas in
/// FOLDER
NCManageDatabase.shared.addMetadata(metadataFolder)
NCManageDatabase.shared.addDirectory(e2eEncrypted: metadataFolder.e2eEncrypted, favorite: metadataFolder.favorite, ocId: metadataFolder.ocId, fileId: metadataFolder.fileId, etag: metadataFolder.etag, permissions: metadataFolder.permissions, richWorkspace: metadataFolder.richWorkspace, serverUrl: serverUrl, account: metadataFolder.account)
/// FILES
let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal)
NCManageDatabase.shared.updateMetadatas(metadatas, predicate: predicate)
let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", fileProviderData.shared.account, serverUrl), sorted: "fileName", ascending: true)
completionHandler(metadatas)
}
Expand Down
8 changes: 4 additions & 4 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5264,7 +5264,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 0;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand All @@ -5290,7 +5290,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 5.3.1;
MARKETING_VERSION = 5.3.2;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "-v";
OTHER_LDFLAGS = "";
Expand Down Expand Up @@ -5330,7 +5330,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 0;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand All @@ -5353,7 +5353,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 5.3.1;
MARKETING_VERSION = 5.3.2;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "-v";
OTHER_LDFLAGS = "";
Expand Down
Loading