From 9d1e698d9b94659b40a79552a2f8f76612b14c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Sun, 19 Oct 2025 14:20:44 +0200 Subject: [PATCH] Refactoring --- .../Connection/NearbyConnectionManager.swift | 46 +++++++++++++------ QuickDrop/ViewModel/ReceiveModel.swift | 7 ++- QuickDrop/ViewModel/SendModel.swift | 7 ++- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/NearbyShare/Connection/NearbyConnectionManager.swift b/NearbyShare/Connection/NearbyConnectionManager.swift index fe59347..7eec2ca 100644 --- a/NearbyShare/Connection/NearbyConnectionManager.swift +++ b/NearbyShare/Connection/NearbyConnectionManager.swift @@ -27,7 +27,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb private var mdnsServices: [NetService] = [] private var incomingConnections: [String: InboundNearbyConnection] = [:] private var foundServices: [String: FoundServiceInfo] = [:] - private var shareExtensionDelegates: [OutboundAppDelegate] = [] + private var outboundAppDelegates: [OutboundAppDelegate] = [] + private var inboundAppDelegates: [InboundAppDelegate] = [] private var outgoingTransfers: [String: OutgoingTransferInfo] = [:] private var startedDeviceDiscovery = false private var startedAdvertising = false @@ -41,7 +42,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb public let deviceInfo: EndpointInfo public let endpointID: [UInt8] = generateEndpointID() - public var mainAppDelegate: (any InboundAppDelegate)? + public static let shared = NearbyConnectionManager() @Published var attachments: AttachmentDetails? = nil @@ -191,7 +192,9 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection, acceptAutomatically: Bool) { - mainAppDelegate?.obtainUserConsent(transfer: transfer, device: device, acceptAutomatically: acceptAutomatically) + inboundAppDelegates.forEach { delegate in + delegate.obtainUserConsent(transfer: transfer, device: device, acceptAutomatically: acceptAutomatically) + } } @@ -199,13 +202,17 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb incomingConnections.removeValue(forKey: connection.id) if !connection.wasUserRejected { - mainAppDelegate?.connectionWasTerminated(from: connection.remoteDeviceInfo ?? RemoteDeviceInfo(name: "UnknownDevice".localized(), type: .unknown), wasPlainTextTransfer: connection.isPlainTextTransfer, error: error) + inboundAppDelegates.forEach { delegate in + delegate.connectionWasTerminated(from: connection.remoteDeviceInfo ?? RemoteDeviceInfo(name: "UnknownDevice".localized(), type: .unknown), wasPlainTextTransfer: connection.isPlainTextTransfer, error: error) + } } } func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) { - mainAppDelegate?.transferProgress(connectionID: connection.id, progress: progress) + inboundAppDelegates.forEach { delegate in + delegate.transferProgress(connectionID: connection.id, progress: progress) + } } @@ -283,7 +290,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb startedDeviceDiscovery = false - for delegate in shareExtensionDelegates { + for delegate in outboundAppDelegates { foundServices.values.forEach { service in guard let device = service.device, let id = device.id else { return } @@ -294,8 +301,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - public func addShareExtensionDelegate(_ delegate: OutboundAppDelegate) { - shareExtensionDelegates.append(delegate) + public func addOutboundAppDelegate(_ delegate: OutboundAppDelegate) { + outboundAppDelegates.append(delegate) for service in foundServices.values { guard let device = service.device else { continue } delegate.addDevice(device: device) @@ -303,8 +310,17 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - public func removeShareExtensionDelegate(_ delegate: OutboundAppDelegate) { - shareExtensionDelegates.removeAll(where: { $0 === delegate }) + public func addInboundAppDelegate(_ delegate: InboundAppDelegate) { + inboundAppDelegates.append(delegate) + } + + + public func removeOutboundAppDelegate(_ delegate: OutboundAppDelegate) { + outboundAppDelegates.removeAll(where: { $0 === delegate }) + } + + public func removeInboundAppDelegate(_ delegate: InboundAppDelegate) { + inboundAppDelegates.removeAll(where: { $0 === delegate }) } @@ -358,7 +374,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb if qrData == qrCodeAdvertisingToken { if let deviceInfo = deviceInfo { - for delegate in shareExtensionDelegates { + for delegate in outboundAppDelegates { delegate.startTransferWithQrCode(device: deviceInfo) } } @@ -370,7 +386,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb guard let name = String.init(data: decryptedName, encoding: .utf8) else { return } endpointInfo.name = name let deviceInfo = addFoundDevice(foundService: &foundService, endpointInfo: endpointInfo, endpointID: endpointID) - for delegate in shareExtensionDelegates { + for delegate in outboundAppDelegates { delegate.startTransferWithQrCode(device: deviceInfo) } } catch { @@ -386,7 +402,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb foundService.device = deviceInfo foundServices[endpointID] = foundService - for delegate in shareExtensionDelegates { + for delegate in outboundAppDelegates { delegate.addDevice(device: deviceInfo) } @@ -397,7 +413,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb private func removeFoundDevice(service: NWBrowser.Result) { guard let endpointID = endpointID(for: service) else { return } guard let _ = foundServices.removeValue(forKey: endpointID) else { return } - for delegate in shareExtensionDelegates { + for delegate in outboundAppDelegates { delegate.removeDevice(id: endpointID) } } @@ -463,7 +479,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb conn.start() } catch { log("[NearbyConnectionManager] Error zipping URLs: \(error)") - shareExtensionDelegates.forEach { delegate in + outboundAppDelegates.forEach { delegate in delegate.connectionFailed(error: error) } } diff --git a/QuickDrop/ViewModel/ReceiveModel.swift b/QuickDrop/ViewModel/ReceiveModel.swift index 20c75a8..803a40c 100644 --- a/QuickDrop/ViewModel/ReceiveModel.swift +++ b/QuickDrop/ViewModel/ReceiveModel.swift @@ -27,11 +27,16 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { init(controlPlusScreen: @escaping (Bool) -> Void = { _ in }) { self.controlPlusScreen = controlPlusScreen - NearbyConnectionManager.shared.mainAppDelegate = self + NearbyConnectionManager.shared.addInboundAppDelegate(self) NearbyConnectionManager.shared.becomeVisible() } + deinit { + NearbyConnectionManager.shared.removeInboundAppDelegate(self) + } + + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, acceptAutomatically: Bool) { #if os(macOS) diff --git a/QuickDrop/ViewModel/SendModel.swift b/QuickDrop/ViewModel/SendModel.swift index 67d4e6f..2c022ad 100644 --- a/QuickDrop/ViewModel/SendModel.swift +++ b/QuickDrop/ViewModel/SendModel.swift @@ -29,7 +29,12 @@ class SendModel: ObservableObject, OutboundAppDelegate { init() { NearbyConnectionManager.shared.startDeviceDiscovery() - NearbyConnectionManager.shared.addShareExtensionDelegate(self) + NearbyConnectionManager.shared.addOutboundAppDelegate(self) + } + + + deinit { + NearbyConnectionManager.shared.removeOutboundAppDelegate(self) }