diff --git a/NearbyShare/Connection/NearbyConnectionManager.swift b/NearbyShare/Connection/NearbyConnectionManager.swift index 7eec2ca..8239c5b 100644 --- a/NearbyShare/Connection/NearbyConnectionManager.swift +++ b/NearbyShare/Connection/NearbyConnectionManager.swift @@ -203,7 +203,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb if !connection.wasUserRejected { inboundAppDelegates.forEach { delegate in - delegate.connectionWasTerminated(from: connection.remoteDeviceInfo ?? RemoteDeviceInfo(name: "UnknownDevice".localized(), type: .unknown), wasPlainTextTransfer: connection.isPlainTextTransfer, error: error) + delegate.connectionWasTerminated(connectionID: connection.id, from: connection.remoteDeviceInfo ?? RemoteDeviceInfo(name: "UnknownDevice".localized(), type: .unknown), wasPlainTextTransfer: connection.isPlainTextTransfer, error: error) } } } diff --git a/NearbyShare/Model/DataModel.swift b/NearbyShare/Model/DataModel.swift index 8942d94..7200f78 100644 --- a/NearbyShare/Model/DataModel.swift +++ b/NearbyShare/Model/DataModel.swift @@ -29,7 +29,7 @@ protocol OutboundNearbyConnectionDelegate { public protocol InboundAppDelegate: AnyObject { func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, acceptAutomatically: Bool) - func connectionWasTerminated(from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: Error?) + func connectionWasTerminated(connectionID: String, from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: Error?) func transferProgress(connectionID: String, progress: Double) } diff --git a/QuickDrop/ViewModel/ReceiveModel.swift b/QuickDrop/ViewModel/ReceiveModel.swift index 803a40c..6ba52ef 100644 --- a/QuickDrop/ViewModel/ReceiveModel.swift +++ b/QuickDrop/ViewModel/ReceiveModel.swift @@ -17,6 +17,9 @@ import LUI class ReceiveModel: ObservableObject, InboundAppDelegate { + /// For each connection ID, store the last reported progress value + private var processes: [String: Double] = [:] + #if os(macOS) @Published var progress: Double? = nil private var toastWindow: NSWindow? @@ -156,28 +159,42 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { func transferProgress(connectionID: String, progress: Double) { + // Update process dictionary + processes[connectionID] = progress + + // Calcualate average value for all ongoing transfers + let totalProgress = processes.values.reduce(0, +) + let averageProgress = totalProgress / Double(processes.count) + #if os(iOS) - ProgressAlert.shared.updateProgress(progress) + ProgressAlert.shared.updateProgress(averageProgress) #else DispatchQueue.main.async { - self.progress = progress + self.progress = averageProgress self.showQuickDropToast(for: connectionID) } #endif } - func connectionWasTerminated(from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: (any Error)?) { + func connectionWasTerminated(connectionID: String, from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: (any Error)?) { + + processes.removeValue(forKey: connectionID) #if os(macOS) DispatchQueue.main.async { self.hideQuickDropToast() - self.progress = nil + + if self.processes.isEmpty { + self.progress = nil + } } finish() #else - ProgressAlert.shared.updateProgress(nil) { - finish() + if self.processes.isEmpty { + ProgressAlert.shared.updateProgress(nil) { + finish() + } } #endif