Move service connection fetcher method into a utils file
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 30 Jul 2024 10:16:06 +0000 (18:16 +0800)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Thu, 12 Sep 2024 08:15:58 +0000 (10:15 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/FileProviderCommunication.swift [new file with mode: 0644]
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Sharing/ShareTableViewDataSource.swift
shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj

diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/FileProviderCommunication.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/FileProviderCommunication.swift
new file mode 100644 (file)
index 0000000..111e282
--- /dev/null
@@ -0,0 +1,31 @@
+//
+//  FileProviderCommunication.swift
+//  FileProviderUIExt
+//
+//  Created by Claudio Cambra on 30/7/24.
+//
+
+import FileProvider
+
+enum FileProviderCommunicationError: Error {
+    case serviceNotFound
+    case remoteProxyObjectInvalid
+}
+
+func serviceConnection(
+    url: URL, interruptionHandler: @escaping () -> Void
+) async throws -> FPUIExtensionService {
+    let services = try await FileManager().fileProviderServicesForItem(at: url)
+    guard let service = services[fpUiExtensionServiceName] else {
+        throw FileProviderCommunicationError.serviceNotFound
+    }
+    let connection: NSXPCConnection
+    connection = try await service.fileProviderConnection()
+    connection.remoteObjectInterface = NSXPCInterface(with: FPUIExtensionService.self)
+    connection.interruptionHandler = interruptionHandler
+    connection.resume()
+    guard let proxy = connection.remoteObjectProxy as? FPUIExtensionService else {
+        throw FileProviderCommunicationError.remoteProxyObjectInvalid
+    }
+    return proxy
+}
index 3cca1d1a9df5189b00d8e854b43091eac3f32738..f0d0baf99c85269607f260d407a7cf05097c9f5c 100644 (file)
@@ -84,7 +84,9 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
         }
 
         do {
-            let connection = try await serviceConnection(url: itemURL)
+            let connection = try await serviceConnection(url: itemURL, interruptionHandler: {
+                Logger.sharesDataSource.error("Service connection interrupted")
+            })
             guard let serverPath = await connection.itemServerPath(identifier: itemIdentifier),
                   let credentials = await connection.credentials() as? Dictionary<String, String>,
                   let convertedAccount = Account(dictionary: credentials),
@@ -118,25 +120,6 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele
         }
     }
 
-    private func serviceConnection(url: URL) async throws -> FPUIExtensionService {
-        let services = try await FileManager().fileProviderServicesForItem(at: url)
-        guard let service = services[fpUiExtensionServiceName] else {
-            Logger.sharesDataSource.error("Couldn't get service, required service not present")
-            throw NSFileProviderError(.providerNotFound)
-        }
-        let connection: NSXPCConnection
-        connection = try await service.fileProviderConnection()
-        connection.remoteObjectInterface = NSXPCInterface(with: FPUIExtensionService.self)
-        connection.interruptionHandler = {
-            Logger.sharesDataSource.error("Service connection interrupted")
-        }
-        connection.resume()
-        guard let proxy = connection.remoteObjectProxy as? FPUIExtensionService else {
-            throw NSFileProviderError(.serverUnreachable)
-        }
-        return proxy
-    }
-
     private func fetch(
         itemIdentifier: NSFileProviderItemIdentifier, itemRelativePath: String
     ) async -> [NKShare] {
index 0a6e233667c45fa70ed12ee9c29f32d4432305c5..ff6aa0624e36866e08099050bcbe990043e04856 100644 (file)
@@ -27,6 +27,7 @@
                537630982B8612F00026BFAB /* FPUIExtensionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537630962B860D920026BFAB /* FPUIExtensionService.swift */; };
                537BD67A2C58D67800446ED0 /* LockViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537BD6792C58D67800446ED0 /* LockViewController.swift */; };
                537BD67C2C58D7B700446ED0 /* LockViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 537BD67B2C58D7B700446ED0 /* LockViewController.xib */; };
+               537BD6802C58F01B00446ED0 /* FileProviderCommunication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */; };
                538E396A27F4765000FA63D5 /* UniformTypeIdentifiers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */; };
                538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */; };
                538E397627F4765000FA63D5 /* FileProviderExt.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 538E396727F4765000FA63D5 /* FileProviderExt.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
                537630962B860D920026BFAB /* FPUIExtensionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FPUIExtensionService.swift; sourceTree = "<group>"; };
                537BD6792C58D67800446ED0 /* LockViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LockViewController.swift; sourceTree = "<group>"; };
                537BD67B2C58D7B700446ED0 /* LockViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LockViewController.xib; sourceTree = "<group>"; };
+               537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderCommunication.swift; sourceTree = "<group>"; };
                538E396727F4765000FA63D5 /* FileProviderExt.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = FileProviderExt.appex; sourceTree = BUILT_PRODUCTS_DIR; };
                538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UniformTypeIdentifiers.framework; path = System/Library/Frameworks/UniformTypeIdentifiers.framework; sourceTree = SDKROOT; };
                538E396C27F4765000FA63D5 /* FileProviderExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderExtension.swift; sourceTree = "<group>"; };
                                537BD6782C58D0FC00446ED0 /* Locking */,
                                537BD6772C58D0C400446ED0 /* Sharing */,
                                53B979802B84C81F002DA742 /* DocumentActionViewController.swift */,
+                               537BD67F2C58F01B00446ED0 /* FileProviderCommunication.swift */,
                                53FE14572B8E3A7C006C4193 /* FileProviderUIExt.entitlements */,
                                53B979852B84C81F002DA742 /* Info.plist */,
                        );
                                53FE14592B8E3F6C006C4193 /* ShareTableItemView.swift in Sources */,
                                5376307D2B85E2ED0026BFAB /* Logger+Extensions.swift in Sources */,
                                53FE14502B8E0658006C4193 /* ShareTableViewDataSource.swift in Sources */,
+                               537BD6802C58F01B00446ED0 /* FileProviderCommunication.swift in Sources */,
                                537630982B8612F00026BFAB /* FPUIExtensionService.swift in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;