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 // only for logging
self.bytesTransferred += Int64(frame.payloadChunk.body.count) 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 { } catch {
log("[InboundNearbyConnection \(self.id)] Error occurred during writing file: \(error.localizedDescription)") log("[InboundNearbyConnection \(self.id)] Error occurred during writing file: \(error.localizedDescription)")
@@ -174,7 +174,7 @@ class InboundNearbyConnection: NearbyConnection {
if filesToBeReceived.isEmpty { if filesToBeReceived.isEmpty {
log("[InboundNearbyConnection \(self.id)] All files received, sending disconnection frame and disconnecting.") 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() try sendDisconnectionAndDisconnect()
} }
} }
@@ -221,7 +221,7 @@ class InboundNearbyConnection: NearbyConnection {
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL) SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
log("[InboundNearbyConnection \(self.id)] Received file payload. Disconnecting...") log("[InboundNearbyConnection \(self.id)] Received file payload. Disconnecting...")
NearbyConnectionManager.shared.inboundConnection(connection: self, transferProgress: 1) NearbyConnectionManager.shared.updatedTransferProgress(connection: self, progress: 1)
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
return true return true
} }
@@ -434,7 +434,7 @@ class InboundNearbyConnection: NearbyConnection {
} }
let metadata = TransferMetadata(files: filesToBeReceived.map { $0.value.meta }, id: id, pinCode: pinCode) let metadata = TransferMetadata(files: filesToBeReceived.map { $0.value.meta }, id: id, pinCode: pinCode)
DispatchQueue.main.async { 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 { else if let textMetadata = frame.v1.introduction.textMetadata.first {
@@ -451,7 +451,7 @@ class InboundNearbyConnection: NearbyConnection {
} }
DispatchQueue.main.async { DispatchQueue.main.async {
self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self)
} }
} }
else { else {
@@ -154,8 +154,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
} }
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) { func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
mainAppDelegate?.obtainUserConsent(for: transfer, from: device) mainAppDelegate?.obtainUserConsent(transfer: transfer, device: device)
} }
@@ -168,8 +168,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
} }
func inboundConnection(connection: InboundNearbyConnection, transferProgress: Double) { func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) {
mainAppDelegate?.transferProgress(progress: transferProgress) mainAppDelegate?.transferProgress(progress: progress)
} }
@@ -400,13 +400,13 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
} catch { } catch {
log("[NearbyConnectionManager] Error zipping URLs: \(error)") log("[NearbyConnectionManager] Error zipping URLs: \(error)")
shareExtensionDelegates.forEach { delegate in 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 } guard let transfer = outgoingTransfers[connection.id] else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
transfer.delegate.connectionWasEstablished(pinCode: connection.pinCode!) 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 } guard let transfer = outgoingTransfers[connection.id] else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
transfer.delegate.transferAccepted() 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 } guard let transfer = outgoingTransfers[connection.id] else { return }
DispatchQueue.main.async { 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 } guard let transfer = outgoingTransfers[connection.id] else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
transfer.delegate.connectionFailed(with: failedWithError) transfer.delegate.connectionFailed(error: error)
} }
outgoingTransfers.removeValue(forKey: connection.id) outgoingTransfers.removeValue(forKey: connection.id)
} }
func outboundConnectionTransferFinished(connection: OutboundNearbyConnection) { func transferFinished(connection: OutboundNearbyConnection) {
guard let transfer = outgoingTransfers[connection.id] else { return } guard let transfer = outgoingTransfers[connection.id] else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
transfer.delegate.transferFinished() transfer.delegate.transferFinished()
@@ -63,7 +63,7 @@ class OutboundNearbyConnection: NearbyConnection {
if let error = lastError { if let error = lastError {
DispatchQueue.main.async { 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) self.lastError = NearbyError.canceled(reason: .userCanceled)
log("[OutboundNearbyConnection \(self.id)] Transfer canceled") log("[OutboundNearbyConnection \(self.id)] Transfer canceled")
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
delegate?.outboundConnection(connection: self, failedWithError: self.lastError!) delegate?.failedWithError(connection: self, error: self.lastError!)
return return
} }
@@ -161,7 +161,7 @@ class OutboundNearbyConnection: NearbyConnection {
override func protocolError() { override func protocolError() {
super.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()) try sendFrameAsync(resp.serializedData())
encryptionDone = true encryptionDone = true
delegate?.outboundConnectionWasEstablished(connection: self) delegate?.connectionWasEstablished(connection: self)
} }
@@ -372,7 +372,7 @@ class OutboundNearbyConnection: NearbyConnection {
case .accept: case .accept:
currentState = .sendingFiles currentState = .sendingFiles
isTransferring = true isTransferring = true
delegate?.outboundConnectionTransferAccepted(connection: self) delegate?.transferAccepted(connection: self)
if let textToSend = textToSend { if let textToSend = textToSend {
try sendText(text: textToSend) try sendText(text: textToSend)
@@ -383,16 +383,16 @@ class OutboundNearbyConnection: NearbyConnection {
try sendNextFileChunk() try sendNextFileChunk()
} }
case .reject, .unknown: case .reject, .unknown:
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .userRejected)) delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .userRejected))
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
case .notEnoughSpace: case .notEnoughSpace:
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .notEnoughSpace)) delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .notEnoughSpace))
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
case .timedOut: case .timedOut:
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .timedOut)) delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .timedOut))
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
case .unsupportedAttachmentType: case .unsupportedAttachmentType:
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .unsupportedType)) delegate?.failedWithError(connection: self, error: NearbyError.canceled(reason: .unsupportedType))
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
} }
} }
@@ -410,7 +410,7 @@ class OutboundNearbyConnection: NearbyConnection {
private func sendText(text: String) throws { private func sendText(text: String) throws {
try sendBytesPayload(data: Data(text.utf8), id: textPayloadID) try sendBytesPayload(data: Data(text.utf8), id: textPayloadID)
delegate?.outboundConnectionTransferFinished(connection: self) delegate?.transferFinished(connection: self)
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
} }
@@ -426,7 +426,7 @@ class OutboundNearbyConnection: NearbyConnection {
if queue.isEmpty { if queue.isEmpty {
log("[OutboundNearbyConnection \(self.id)] Disconnecting because all files have been transferred") log("[OutboundNearbyConnection \(self.id)] Disconnecting because all files have been transferred")
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
delegate?.outboundConnectionTransferFinished(connection: self) delegate?.transferFinished(connection: self)
return return
} }
currentTransfer = queue.removeFirst() currentTransfer = queue.removeFirst()
@@ -468,7 +468,7 @@ class OutboundNearbyConnection: NearbyConnection {
self.bytesTransferred += Int64(fileBuffer.count) self.bytesTransferred += Int64(fileBuffer.count)
startAndResetHeartbeatTimer() startAndResetHeartbeatTimer()
delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend)) delegate?.updatedTransferProgress(connection: self, progress: Double(totalBytesSent) / Double(totalBytesToSend))
if currentTransfer!.currentOffset == currentTransfer!.totalBytes { if currentTransfer!.currentOffset == currentTransfer!.totalBytes {
// Signal end of file (yes, all this for one bit) // Signal end of file (yes, all this for one bit)
+9 -9
View File
@@ -12,23 +12,23 @@ import SwiftECC
protocol InboundNearbyConnectionDelegate { protocol InboundNearbyConnectionDelegate {
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection: InboundNearbyConnection) func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection)
func inboundConnection(connection: InboundNearbyConnection, transferProgress: Double) func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double)
func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?)
} }
protocol OutboundNearbyConnectionDelegate { protocol OutboundNearbyConnectionDelegate {
func outboundConnectionWasEstablished(connection: OutboundNearbyConnection) func connectionWasEstablished(connection: OutboundNearbyConnection)
func outboundConnection(connection: OutboundNearbyConnection, transferProgress: Double) func updatedTransferProgress(connection: OutboundNearbyConnection, progress: Double)
func outboundConnectionTransferAccepted(connection: OutboundNearbyConnection) func transferAccepted(connection: OutboundNearbyConnection)
func outboundConnection(connection: OutboundNearbyConnection, failedWithError: Error) func failedWithError(connection: OutboundNearbyConnection, error: Error)
func outboundConnectionTransferFinished(connection: OutboundNearbyConnection) func transferFinished(connection: OutboundNearbyConnection)
} }
public protocol InboundAppDelegate: AnyObject { 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 connectionWasTerminated(from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: Error?)
func transferProgress(progress: Double) func transferProgress(progress: Double)
} }
@@ -39,7 +39,7 @@ public protocol OutboundAppDelegate: AnyObject {
func removeDevice(id: String) func removeDevice(id: String)
func startTransferWithQrCode(device: RemoteDeviceInfo) func startTransferWithQrCode(device: RemoteDeviceInfo)
func connectionWasEstablished(pinCode: String) func connectionWasEstablished(pinCode: String)
func connectionFailed(with error: Error) func connectionFailed(error: Error)
func transferAccepted() func transferAccepted()
func transferProgress(progress: Double) func transferProgress(progress: Double)
func transferFinished() 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) #if os(macOS)
NSApp.activate(ignoringOtherApps: true) NSApp.activate(ignoringOtherApps: true)
+1 -1
View File
@@ -47,7 +47,7 @@ class SendModel: ObservableObject, OutboundAppDelegate {
progressValue = 0 progressValue = 0
} }
func connectionFailed(with error: any Error) { func connectionFailed(error: any Error) {
#if os(iOS) #if os(iOS)
errorVibration() 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?.isIndeterminate = false
progressProgressBar?.maxValue = 1000 progressProgressBar?.maxValue = 1000
progressProgressBar?.doubleValue = 0 progressProgressBar?.doubleValue = 0