Move all share fetching logic to FileProviderUIExt

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2024-02-27 23:56:55 +08:00
parent 331a76195b
commit ff9e344810
6 changed files with 69 additions and 81 deletions

View file

@ -13,7 +13,6 @@ let fpUiExtensionServiceName = NSFileProviderServiceName(
)
@objc protocol FPUIExtensionService {
func shares(forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier) async -> [NKShare]?
func credentials() async -> NSDictionary
func itemServerPath(identifier: NSFileProviderItemIdentifier) async -> NSString?
}

View file

@ -39,15 +39,6 @@ class FPUIExtensionServiceSource: NSObject, NSFileProviderServiceSource, NSXPCLi
//MARK: - FPUIExtensionService protocol methods
func shares(
forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier
) async -> [NKShare]? {
let controller = ItemSharesController(
itemIdentifier: itemIdentifier, parentExtension: fpExtension
)
return await controller.fetch()
}
func credentials() async -> NSDictionary {
return (fpExtension.ncAccount?.dictionary() ?? [:]) as NSDictionary
}

View file

@ -1,52 +0,0 @@
//
// ItemSharesController.swift
// FileProviderExt
//
// Created by Claudio Cambra on 27/2/24.
//
import FileProvider
import Foundation
import NextcloudKit
import OSLog
class ItemSharesController {
let itemIdentifier: NSFileProviderItemIdentifier
let parentExtension: FileProviderExtension
init(itemIdentifier: NSFileProviderItemIdentifier, parentExtension: FileProviderExtension) {
self.itemIdentifier = itemIdentifier
self.parentExtension = parentExtension
}
func fetch() async -> [NKShare]? {
let rawIdentifier = itemIdentifier.rawValue
Logger.shares.info("Fetching shares for item \(rawIdentifier, privacy: .public)")
guard let baseUrl = parentExtension.ncAccount?.davFilesUrl else {
Logger.shares.error("Could not fetch shares as ncAccount on parent extension is nil")
return nil
}
let dbManager = NextcloudFilesDatabaseManager.shared
guard let item = dbManager.itemMetadataFromFileProviderItemIdentifier(itemIdentifier) else {
Logger.shares.error("No item \(rawIdentifier, privacy: .public) in db, no shares.")
return nil
}
let completePath = item.serverUrl + "/" + item.fileName
let relativePath = completePath.replacingOccurrences(of: baseUrl, with: "")
let parameter = NKShareParameter(path: relativePath)
return await withCheckedContinuation { continuation in
let kit = parentExtension.ncKit
kit.readShares(parameters: parameter) { account, shares, data, error in
defer { continuation.resume(returning: shares) }
guard error == .success else {
Logger.shares.error("Error fetching shares for \(rawIdentifier): \(error)")
return
}
}
}
}
}

View file

@ -21,14 +21,25 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
sharesTableView?.reloadData()
}
}
private var itemIdentifier: NSFileProviderItemIdentifier?
private var kit: NextcloudKit?
private var itemURL: URL?
private var shares: [NKShare] = [] {
didSet { sharesTableView?.reloadData() }
}
private var account: NextcloudAccount? {
didSet {
guard let account = account else { return }
kit = NextcloudKit()
kit?.setup(
user: account.username,
userId: account.username,
password: account.password,
urlBase: account.serverUrl
)
}
}
func loadItem(identifier: NSFileProviderItemIdentifier, url: URL) {
itemIdentifier = identifier
func loadItem(url: URL) {
itemURL = url
Task {
await reload()
@ -36,12 +47,37 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
}
private func reload() async {
guard let itemIdentifier = itemIdentifier, let itemURL = itemURL else { return }
guard let itemURL = itemURL else { return }
guard let itemIdentifier = await withCheckedContinuation({
(continuation: CheckedContinuation<NSFileProviderItemIdentifier?, Never>) -> Void in
NSFileProviderManager.getIdentifierForUserVisibleFile(
at: itemURL
) { identifier, domainIdentifier, error in
defer { continuation.resume(returning: identifier) }
guard error == nil else {
Logger.sharesDataSource.error("No identifier: \(error, privacy: .public)")
return
}
}
}) else {
Logger.sharesDataSource.error("Could not get identifier for item, no shares.")
return
}
do {
let connection = try await serviceConnection(url: itemURL)
shares = await connection.shares(forItemIdentifier: itemIdentifier) ?? []
guard let serverPath = await connection.itemServerPath(identifier: itemIdentifier),
let credentials = await connection.credentials() as? Dictionary<String, String>,
let convertedAccount = NextcloudAccount(dictionary: credentials) else {
Logger.sharesDataSource.error("Failed to get details from FileProviderExt")
return
}
account = convertedAccount
shares = await fetch(
itemIdentifier: itemIdentifier, itemRelativePath: serverPath as String
)
} catch let error {
Logger.sharesDataSource.error("Could not reload data: \(error)")
Logger.sharesDataSource.error("Could not reload data: \(error, privacy: .public)")
}
}
@ -64,6 +100,32 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
return proxy
}
private func fetch(
itemIdentifier: NSFileProviderItemIdentifier, itemRelativePath: String
) async -> [NKShare] {
let rawIdentifier = itemIdentifier.rawValue
Logger.sharesDataSource.info("Fetching shares for item \(rawIdentifier, privacy: .public)")
guard let kit = kit else {
Logger.sharesDataSource.error("NextcloudKit instance is nil")
return []
}
let parameter = NKShareParameter(path: itemRelativePath)
return await withCheckedContinuation { continuation in
kit.readShares(parameters: parameter) { account, shares, data, error in
let shareCount = shares?.count ?? 0
Logger.sharesDataSource.info("Received \(shareCount, privacy: .public) shares")
defer { continuation.resume(returning: shares ?? []) }
guard error == .success else {
Logger.sharesDataSource.error("Error fetching shares: \(error)")
return
}
}
}
}
// MARK: - NSTableViewDataSource protocol methods
@objc func numberOfRows(in tableView: NSTableView) -> Int {

View file

@ -60,7 +60,7 @@ class ShareViewController: NSViewController {
let itemUrl = try await manager.getUserVisibleURL(for: itemIdentifier)
await updateDisplay(itemUrl: itemUrl)
shareDataSource.sharesTableView = tableView
shareDataSource.loadItem(identifier: itemIdentifier, url: itemUrl)
shareDataSource.loadItem(url: itemUrl)
} catch let error {
let errorString = "Error processing item: \(error)"
Logger.shareViewController.error("\(errorString)")

View file

@ -11,7 +11,6 @@
5307A6E82965DAD8001E0C6A /* NextcloudKit in Frameworks */ = {isa = PBXBuildFile; productRef = 5307A6E72965DAD8001E0C6A /* NextcloudKit */; };
5307A6EB2965DB8D001E0C6A /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 5307A6EA2965DB8D001E0C6A /* RealmSwift */; };
5307A6F229675346001E0C6A /* NextcloudFilesDatabaseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5307A6F129675346001E0C6A /* NextcloudFilesDatabaseManager.swift */; };
531522802B8DBBA2002E31BE /* ItemSharesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5315227F2B8DBBA2002E31BE /* ItemSharesController.swift */; };
531522822B8E01C6002E31BE /* ShareTableItemView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 531522812B8E01C6002E31BE /* ShareTableItemView.xib */; };
5318AD9129BF42FB00CBB71C /* NextcloudItemMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */; };
5318AD9529BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */; };
@ -155,7 +154,6 @@
/* Begin PBXFileReference section */
5307A6F129675346001E0C6A /* NextcloudFilesDatabaseManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudFilesDatabaseManager.swift; sourceTree = "<group>"; };
5315227F2B8DBBA2002E31BE /* ItemSharesController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemSharesController.swift; sourceTree = "<group>"; };
531522812B8E01C6002E31BE /* ShareTableItemView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ShareTableItemView.xib; sourceTree = "<group>"; };
5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudItemMetadataTable.swift; sourceTree = "<group>"; };
5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudLocalFileMetadataTable.swift; sourceTree = "<group>"; };
@ -264,14 +262,6 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
5315227E2B8DBB7E002E31BE /* Shares */ = {
isa = PBXGroup;
children = (
5315227F2B8DBBA2002E31BE /* ItemSharesController.swift */,
);
path = Shares;
sourceTree = "<group>";
};
5318AD8F29BF406500CBB71C /* Database */ = {
isa = PBXGroup;
children = (
@ -328,7 +318,6 @@
5318AD8F29BF406500CBB71C /* Database */,
5352E85929B7BFB4002CE85C /* Extensions */,
5350E4C72B0C368B00F276CB /* Services */,
5315227E2B8DBB7E002E31BE /* Shares */,
53D666602B70C9A70042C03D /* FileProviderConfig.swift */,
538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */,
53ED471F29C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift */,
@ -712,7 +701,6 @@
535AE30E29C0A2CC0042A9BA /* Logger+Extensions.swift in Sources */,
5307A6F229675346001E0C6A /* NextcloudFilesDatabaseManager.swift in Sources */,
537630952B860D560026BFAB /* FPUIExtensionServiceSource.swift in Sources */,
531522802B8DBBA2002E31BE /* ItemSharesController.swift in Sources */,
53D056312970594F00988392 /* LocalFilesUtils.swift in Sources */,
538E396F27F4765000FA63D5 /* FileProviderItem.swift in Sources */,
5352B36829DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift in Sources */,