From c7b47eba6fe9966d5406fdd51e4b4b78a8f25ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Wed, 20 Aug 2025 13:40:25 +0200 Subject: [PATCH] Refactoring --- .../Connection/InboundNearbyConnection.swift | 10 ++++---- .../Connection/NearbyConnectionManager.swift | 24 +++++++++---------- .../Connection/OutboundNearbyConnection.swift | 24 +++++++++---------- NearbyShare/Model/DataModel.swift | 18 +++++++------- QuickDropiOS/ReceiveModel.swift | 2 +- QuickDropiOS/SendModel.swift | 2 +- ShareExtension/ShareViewController.swift | 2 +- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/NearbyShare/Connection/InboundNearbyConnection.swift b/NearbyShare/Connection/InboundNearbyConnection.swift index effa10f..37a382f 100644 --- a/NearbyShare/Connection/InboundNearbyConnection.swift +++ b/NearbyShare/Connection/InboundNearbyConnection.swift @@ -155,7 +155,7 @@ class InboundNearbyConnection: NearbyConnection { // only for logging self.bytesTransferred += Int64(frame.payloadChunk.body.count) - NearbyConnectionManager.shared.inboundConnection(connection: self, transferProgress: Double(self.bytesTransferred) / Double(self.bytesToBeTransferred)) + NearbyConnectionManager.shared.updatedTransferProgress(connection: self, progress: Double(self.bytesTransferred) / Double(self.bytesToBeTransferred)) } catch { log("[InboundNearbyConnection \(self.id)] Error occurred during writing file: \(error.localizedDescription)") @@ -174,7 +174,7 @@ class InboundNearbyConnection: NearbyConnection { if filesToBeReceived.isEmpty { log("[InboundNearbyConnection \(self.id)] All files received, sending disconnection frame and disconnecting.") - NearbyConnectionManager.shared.inboundConnection(connection: self, transferProgress: 1) + NearbyConnectionManager.shared.updatedTransferProgress(connection: self, progress: 1) try sendDisconnectionAndDisconnect() } } @@ -221,7 +221,7 @@ class InboundNearbyConnection: NearbyConnection { SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL) log("[InboundNearbyConnection \(self.id)] Received file payload. Disconnecting...") - NearbyConnectionManager.shared.inboundConnection(connection: self, transferProgress: 1) + NearbyConnectionManager.shared.updatedTransferProgress(connection: self, progress: 1) try sendDisconnectionAndDisconnect() return true } @@ -434,7 +434,7 @@ class InboundNearbyConnection: NearbyConnection { } let metadata = TransferMetadata(files: filesToBeReceived.map { $0.value.meta }, id: id, pinCode: pinCode) DispatchQueue.main.async { - self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) + self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self) } } else if let textMetadata = frame.v1.introduction.textMetadata.first { @@ -451,7 +451,7 @@ class InboundNearbyConnection: NearbyConnection { } DispatchQueue.main.async { - self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) + self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self) } } else { diff --git a/NearbyShare/Connection/NearbyConnectionManager.swift b/NearbyShare/Connection/NearbyConnectionManager.swift index b200100..077fa15 100644 --- a/NearbyShare/Connection/NearbyConnectionManager.swift +++ b/NearbyShare/Connection/NearbyConnectionManager.swift @@ -154,8 +154,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) { - mainAppDelegate?.obtainUserConsent(for: transfer, from: device) + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection) { + mainAppDelegate?.obtainUserConsent(transfer: transfer, device: device) } @@ -168,8 +168,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - func inboundConnection(connection: InboundNearbyConnection, transferProgress: Double) { - mainAppDelegate?.transferProgress(progress: transferProgress) + func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) { + mainAppDelegate?.transferProgress(progress: progress) } @@ -400,13 +400,13 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } catch { log("[NearbyConnectionManager] Error zipping URLs: \(error)") shareExtensionDelegates.forEach { delegate in - delegate.connectionFailed(with: error) + delegate.connectionFailed(error: error) } } } - func outboundConnectionWasEstablished(connection: OutboundNearbyConnection) { + func connectionWasEstablished(connection: OutboundNearbyConnection) { guard let transfer = outgoingTransfers[connection.id] else { return } DispatchQueue.main.async { transfer.delegate.connectionWasEstablished(pinCode: connection.pinCode!) @@ -414,7 +414,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - func outboundConnectionTransferAccepted(connection: OutboundNearbyConnection) { + func transferAccepted(connection: OutboundNearbyConnection) { guard let transfer = outgoingTransfers[connection.id] else { return } DispatchQueue.main.async { transfer.delegate.transferAccepted() @@ -422,24 +422,24 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - func outboundConnection(connection: OutboundNearbyConnection, transferProgress: Double) { + func updatedTransferProgress(connection: OutboundNearbyConnection, progress: Double) { guard let transfer = outgoingTransfers[connection.id] else { return } DispatchQueue.main.async { - transfer.delegate.transferProgress(progress: transferProgress) + transfer.delegate.transferProgress(progress: progress) } } - func outboundConnection(connection: OutboundNearbyConnection, failedWithError: Error) { + func failedWithError(connection: OutboundNearbyConnection, error: Error) { guard let transfer = outgoingTransfers[connection.id] else { return } DispatchQueue.main.async { - transfer.delegate.connectionFailed(with: failedWithError) + transfer.delegate.connectionFailed(error: error) } outgoingTransfers.removeValue(forKey: connection.id) } - func outboundConnectionTransferFinished(connection: OutboundNearbyConnection) { + func transferFinished(connection: OutboundNearbyConnection) { guard let transfer = outgoingTransfers[connection.id] else { return } DispatchQueue.main.async { transfer.delegate.transferFinished() diff --git a/NearbyShare/Connection/OutboundNearbyConnection.swift b/NearbyShare/Connection/OutboundNearbyConnection.swift index 54fdf89..7c1c0db 100644 --- a/NearbyShare/Connection/OutboundNearbyConnection.swift +++ b/NearbyShare/Connection/OutboundNearbyConnection.swift @@ -63,7 +63,7 @@ class OutboundNearbyConnection: NearbyConnection { if let error = lastError { DispatchQueue.main.async { - self.delegate?.outboundConnection(connection: self, failedWithError: error) + self.delegate?.failedWithError(connection: self, error: error) } } } @@ -140,7 +140,7 @@ class OutboundNearbyConnection: NearbyConnection { self.lastError = NearbyError.canceled(reason: .userCanceled) log("[OutboundNearbyConnection \(self.id)] Transfer canceled") try sendDisconnectionAndDisconnect() - delegate?.outboundConnection(connection: self, failedWithError: self.lastError!) + delegate?.failedWithError(connection: self, error: self.lastError!) return } @@ -161,7 +161,7 @@ class OutboundNearbyConnection: NearbyConnection { override func protocolError() { super.protocolError() - delegate?.outboundConnection(connection: self, failedWithError: lastError!) + delegate?.failedWithError(connection: self, error: lastError!) } @@ -261,7 +261,7 @@ class OutboundNearbyConnection: NearbyConnection { try sendFrameAsync(resp.serializedData()) encryptionDone = true - delegate?.outboundConnectionWasEstablished(connection: self) + delegate?.connectionWasEstablished(connection: self) } @@ -372,7 +372,7 @@ class OutboundNearbyConnection: NearbyConnection { case .accept: currentState = .sendingFiles isTransferring = true - delegate?.outboundConnectionTransferAccepted(connection: self) + delegate?.transferAccepted(connection: self) if let textToSend = textToSend { try sendText(text: textToSend) @@ -383,16 +383,16 @@ class OutboundNearbyConnection: NearbyConnection { try sendNextFileChunk() } case .reject, .unknown: - delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .userRejected)) + delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .userRejected)) try sendDisconnectionAndDisconnect() case .notEnoughSpace: - delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .notEnoughSpace)) + delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .notEnoughSpace)) try sendDisconnectionAndDisconnect() case .timedOut: - delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .timedOut)) + delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .timedOut)) try sendDisconnectionAndDisconnect() case .unsupportedAttachmentType: - delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .unsupportedType)) + delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .unsupportedType)) try sendDisconnectionAndDisconnect() } } @@ -410,7 +410,7 @@ class OutboundNearbyConnection: NearbyConnection { private func sendText(text: String) throws { try sendBytesPayload(data: Data(text.utf8), id: textPayloadID) - delegate?.outboundConnectionTransferFinished(connection: self) + delegate?.transferFinished(connection: self) try sendDisconnectionAndDisconnect() } @@ -426,7 +426,7 @@ class OutboundNearbyConnection: NearbyConnection { if queue.isEmpty { log("[OutboundNearbyConnection \(self.id)] Disconnecting because all files have been transferred") try sendDisconnectionAndDisconnect() - delegate?.outboundConnectionTransferFinished(connection: self) + delegate?.transferFinished(connection: self) return } currentTransfer = queue.removeFirst() @@ -468,7 +468,7 @@ class OutboundNearbyConnection: NearbyConnection { self.bytesTransferred += Int64(fileBuffer.count) startAndResetHeartbeatTimer() - delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend)) + delegate?.updatedTransferProgress(connection: self, progress: Double(totalBytesSent) / Double(totalBytesToSend)) if currentTransfer!.currentOffset == currentTransfer!.totalBytes { // Signal end of file (yes, all this for one bit) diff --git a/NearbyShare/Model/DataModel.swift b/NearbyShare/Model/DataModel.swift index a887d35..c3b7c6b 100644 --- a/NearbyShare/Model/DataModel.swift +++ b/NearbyShare/Model/DataModel.swift @@ -12,23 +12,23 @@ import SwiftECC protocol InboundNearbyConnectionDelegate { - func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection: InboundNearbyConnection) - func inboundConnection(connection: InboundNearbyConnection, transferProgress: Double) + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection) + func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) } protocol OutboundNearbyConnectionDelegate { - func outboundConnectionWasEstablished(connection: OutboundNearbyConnection) - func outboundConnection(connection: OutboundNearbyConnection, transferProgress: Double) - func outboundConnectionTransferAccepted(connection: OutboundNearbyConnection) - func outboundConnection(connection: OutboundNearbyConnection, failedWithError: Error) - func outboundConnectionTransferFinished(connection: OutboundNearbyConnection) + func connectionWasEstablished(connection: OutboundNearbyConnection) + func updatedTransferProgress(connection: OutboundNearbyConnection, progress: Double) + func transferAccepted(connection: OutboundNearbyConnection) + func failedWithError(connection: OutboundNearbyConnection, error: Error) + func transferFinished(connection: OutboundNearbyConnection) } public protocol InboundAppDelegate: AnyObject { - func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo) + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo) func connectionWasTerminated(from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: Error?) func transferProgress(progress: Double) } @@ -39,7 +39,7 @@ public protocol OutboundAppDelegate: AnyObject { func removeDevice(id: String) func startTransferWithQrCode(device: RemoteDeviceInfo) func connectionWasEstablished(pinCode: String) - func connectionFailed(with error: Error) + func connectionFailed(error: Error) func transferAccepted() func transferProgress(progress: Double) func transferFinished() diff --git a/QuickDropiOS/ReceiveModel.swift b/QuickDropiOS/ReceiveModel.swift index e3bfccc..439d138 100644 --- a/QuickDropiOS/ReceiveModel.swift +++ b/QuickDropiOS/ReceiveModel.swift @@ -26,7 +26,7 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { } - func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo) { + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo) { #if os(macOS) NSApp.activate(ignoringOtherApps: true) diff --git a/QuickDropiOS/SendModel.swift b/QuickDropiOS/SendModel.swift index 731f3ce..3313cf4 100644 --- a/QuickDropiOS/SendModel.swift +++ b/QuickDropiOS/SendModel.swift @@ -47,7 +47,7 @@ class SendModel: ObservableObject, OutboundAppDelegate { progressValue = 0 } - func connectionFailed(with error: any Error) { + func connectionFailed(error: any Error) { #if os(iOS) errorVibration() diff --git a/ShareExtension/ShareViewController.swift b/ShareExtension/ShareViewController.swift index 8e7050f..305dc79 100644 --- a/ShareExtension/ShareViewController.swift +++ b/ShareExtension/ShareViewController.swift @@ -234,7 +234,7 @@ class ShareViewController: NSViewController, OutboundAppDelegate { } - func connectionFailed(with error: Error) { + func connectionFailed(error: Error) { progressProgressBar?.isIndeterminate = false progressProgressBar?.maxValue = 1000 progressProgressBar?.doubleValue = 0