Refactoring

This commit is contained in:
Leon Böttger committed 2025-08-20 13:40:25 +02:00
1 parent 149c375230
commit c7b47eba6f
7 files changed
+41 -41

No files matched your search

@@ -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 {
@@ -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()
@@ -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)
+9 -9
View File
@@ -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()
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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()
+1 -1
View File
@@ -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