From d01e1d7c3431123dd7e96a6407f7612758203815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Thu, 6 Nov 2025 20:57:46 +0100 Subject: [PATCH] Refactoring --- .../Connection/InboundNearbyConnection.swift | 21 +- .../Connection/NearbyConnectionManager.swift | 24 ++- NearbyShare/Model/DataModel.swift | 8 +- QuickDrop/ViewModel/ReceiveModel.swift | 188 +++++++++--------- QuickDropiOS/View/UIAlert.swift | 48 ++--- 5 files changed, 163 insertions(+), 126 deletions(-) diff --git a/NearbyShare/Connection/InboundNearbyConnection.swift b/NearbyShare/Connection/InboundNearbyConnection.swift index 2cd00d9..d923fef 100644 --- a/NearbyShare/Connection/InboundNearbyConnection.swift +++ b/NearbyShare/Connection/InboundNearbyConnection.swift @@ -542,7 +542,13 @@ class InboundNearbyConnection: NearbyConnection { let acceptAutomatically = isAuthenticated || Settings.shared.automaticallyAcceptFiles DispatchQueue.main.async { - self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self, acceptAutomatically: acceptAutomatically) + if acceptAutomatically { + self.delegate?.obtainedUserConsentAutomatically(transfer: metadata, device: self.remoteDeviceInfo!, connection: self) + self.submitUserConsent(accepted: true, trustDevice: false) + } + else { + self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self) + } } } } @@ -557,14 +563,14 @@ class InboundNearbyConnection: NearbyConnection { } - func submitUserConsent(accepted: Bool, trustDevice: Bool, storeInTemp: Bool = false) { + func submitUserConsent(accepted: Bool, trustDevice: Bool) { if trustDevice, let peerCertificate = peerCertificate, let remoteDeviceInfo = remoteDeviceInfo { TrustStore.shared.addTrusted(certificate: peerCertificate, device: remoteDeviceInfo) } DispatchQueue.global(qos: .utility).async { if accepted { - self.acceptTransfer(storeInTemp: storeInTemp) + self.acceptTransfer() } else { self.rejectTransfer() } @@ -572,11 +578,18 @@ class InboundNearbyConnection: NearbyConnection { } - private func acceptTransfer(storeInTemp: Bool) { + private func acceptTransfer() { if currentState == .disconnected { log("[InboundNearbyConnection \(self.id)] Detected timeout, not accepting transfer") return } + + let storeInTemp = isFileTransferRestricted() + + if storeInTemp { + log("[InboundNearbyConnection \(self.id)] File transfer restrictions detected, storing files in temporary directory.") + delegate?.showPlusScreen() + } do { if storeInTemp { diff --git a/NearbyShare/Connection/NearbyConnectionManager.swift b/NearbyShare/Connection/NearbyConnectionManager.swift index 8239c5b..dbf0e34 100644 --- a/NearbyShare/Connection/NearbyConnectionManager.swift +++ b/NearbyShare/Connection/NearbyConnectionManager.swift @@ -191,9 +191,16 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection, acceptAutomatically: Bool) { + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection) { inboundAppDelegates.forEach { delegate in - delegate.obtainUserConsent(transfer: transfer, device: device, acceptAutomatically: acceptAutomatically) + delegate.obtainUserConsent(transfer: transfer, device: device) + } + } + + + func obtainedUserConsentAutomatically(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection) { + inboundAppDelegates.forEach { delegate in + delegate.obtainedUserConsentAutomatically(transfer: transfer, device: device) } } @@ -209,6 +216,13 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } + func showPlusScreen() { + inboundAppDelegates.forEach { delegate in + delegate.showPlusScreen() + } + } + + func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) { inboundAppDelegates.forEach { delegate in delegate.transferProgress(connectionID: connection.id, progress: progress) @@ -216,11 +230,11 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb } - public func submitUserConsent(transferID: String, accept: Bool, trustDevice: Bool, storeInTemp: Bool = false) { + public func submitUserConsent(transferID: String, accept: Bool, trustDevice: Bool) { guard let conn = incomingConnections[transferID] else { return } - log("[NearbyConnectionManager] Submitting user consent for transfer \(transferID), accepted: \(accept), store in temp: \(storeInTemp)") - conn.submitUserConsent(accepted: accept, trustDevice: trustDevice, storeInTemp: storeInTemp) + log("[NearbyConnectionManager] Submitting user consent for transfer \(transferID), accepted: \(accept)") + conn.submitUserConsent(accepted: accept, trustDevice: trustDevice) } diff --git a/NearbyShare/Model/DataModel.swift b/NearbyShare/Model/DataModel.swift index ff0dc61..9210d9e 100644 --- a/NearbyShare/Model/DataModel.swift +++ b/NearbyShare/Model/DataModel.swift @@ -12,9 +12,11 @@ import SwiftECC protocol InboundNearbyConnectionDelegate { - func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection, acceptAutomatically: Bool) + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection) + func obtainedUserConsentAutomatically(transfer: TransferMetadata, device: RemoteDeviceInfo, connection: InboundNearbyConnection) func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double) func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) + func showPlusScreen() } @@ -28,9 +30,11 @@ protocol OutboundNearbyConnectionDelegate { public protocol InboundAppDelegate: AnyObject { - func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, acceptAutomatically: Bool) + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo) + func obtainedUserConsentAutomatically(transfer: TransferMetadata, device: RemoteDeviceInfo) func connectionWasTerminated(connectionID: String, from device: RemoteDeviceInfo, wasPlainTextTransfer: Bool, error: Error?) func transferProgress(connectionID: String, progress: Double) + func showPlusScreen() } diff --git a/QuickDrop/ViewModel/ReceiveModel.swift b/QuickDrop/ViewModel/ReceiveModel.swift index d179b55..6639743 100644 --- a/QuickDrop/ViewModel/ReceiveModel.swift +++ b/QuickDrop/ViewModel/ReceiveModel.swift @@ -17,6 +17,7 @@ import LUI class ReceiveModel: ObservableObject, InboundAppDelegate { + /// For each connection ID, store the last reported progress value private var processes: [String: Double] = [:] @@ -40,95 +41,117 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { } - func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, acceptAutomatically: Bool) { + func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo) { #if os(macOS) NSApp.activate(ignoringOtherApps: true) AudioManager.playIncomingFileSound() #endif - let mainMessage = transfer.getDescription(deviceName: device.name ?? "Android", alreadyAccepted: acceptAutomatically) + let mainMessage = transfer.getDescription(deviceName: device.name ?? "Android", alreadyAccepted: false) let pinCodeMessage = transfer.getPinCodeMessage() let transferID = transfer.id - #if os(macOS) - let isMac = true - #else - let isMac = false - #endif + let title = "QuickDrop - \(pinCodeMessage)" + let primaryButtonTitle = "Accept".localized() + let primaryButtonAction = { (trustDevice: Bool) in + NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: true, trustDevice: trustDevice) + + #if os(macOS) + if transfer.type == .text { + DispatchQueue.main.async { + BezelNotification.show(messageText: "InsertedIntoClipboard".localized(), icon: .clipboard) + } + } + #endif + } + let secondaryButtonTitle = "Decline".localized() + let secondaryButtonAction = { NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: false, trustDevice: false) } + + #if os(macOS) + let alert = NSAlert() + alert.alertStyle = .informational - if acceptAutomatically && isMac { - pressAcceptButton(transferID: transfer.id, trustDevice: false) + alert.messageText = title + alert.informativeText = mainMessage + alert.addButton(withTitle: primaryButtonTitle) + alert.addButton(withTitle: secondaryButtonTitle) + + if transfer.allowsToBeAddedAsTrustedDevice { + alert.showsSuppressionButton = true + alert.suppressionButton?.title = "AutoAcceptFromThisDevice".localized() + } - if transfer.type != .file { - UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in - if granted { - log("[ReceiveModel] User granted notification permissions") - - let notificationContent = UNMutableNotificationContent() - notificationContent.title = "QuickDrop" - notificationContent.body = mainMessage - notificationContent.sound = nil - let notificationId = UUID().uuidString - - let notificationReq = UNNotificationRequest(identifier: notificationId, content: notificationContent, trigger: nil) - UNUserNotificationCenter.current().add(notificationReq) - - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { - UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notificationId]) - } + let result = alert.runModal() + + if result == .alertFirstButtonReturn { + primaryButtonAction(alert.suppressionButton?.state == .on) + } else if result == .alertSecondButtonReturn { + secondaryButtonAction() + } + #else + // iOS + let alwaysAcceptLabel = transfer.allowsToBeAddedAsTrustedDevice ? "AlwaysAccept".localized() : nil + ProgressAlert.shared.askForUserPermission(title: title, message: mainMessage, acceptLabel: primaryButtonTitle, acceptAlwaysLabel: alwaysAcceptLabel, rejectLabel: secondaryButtonTitle) { accepted in + + switch accepted { + case .Accept: + primaryButtonAction(false) + case .AcceptAlways: + primaryButtonAction(true) + case .Decline: + secondaryButtonAction() + } + } onCancel: { + NearbyConnectionManager.shared.cancelTransfer(id: transferID) + } + #endif + } + + + func obtainedUserConsentAutomatically(transfer: TransferMetadata, device: RemoteDeviceInfo) { + + let mainMessage = transfer.getDescription(deviceName: device.name ?? "Android", alreadyAccepted: true) + + #if os(macOS) + + NSApp.activate(ignoringOtherApps: true) + AudioManager.playIncomingFileSound() + + // If file -> no notification, as there is the QuickDrop progress toast + if transfer.type != .file { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in + if granted { + log("[ReceiveModel] User granted notification permissions") + + let notificationContent = UNMutableNotificationContent() + notificationContent.title = "QuickDrop" + notificationContent.body = mainMessage + notificationContent.sound = nil + let notificationId = UUID().uuidString + + let notificationReq = UNNotificationRequest(identifier: notificationId, content: notificationContent, trigger: nil) + UNUserNotificationCenter.current().add(notificationReq) + + DispatchQueue.main.asyncAfter(deadline: .now() + 5) { + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notificationId]) } } } } - else { - - let title = "QuickDrop - \(pinCodeMessage)" - let primaryButtonTitle = "Accept".localized() - let primaryButtonAction = { (trustDevice: Bool) in self.pressAcceptButton(transferID: transferID, trustDevice: trustDevice) } - let secondaryButtonTitle = "Decline".localized() - let secondaryButtonAction = { NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: false, trustDevice: false, storeInTemp: false) } - - #if os(macOS) - let alert = NSAlert() - alert.alertStyle = .informational - - alert.messageText = title - alert.informativeText = mainMessage - alert.addButton(withTitle: primaryButtonTitle) - alert.addButton(withTitle: secondaryButtonTitle) - - if transfer.allowsToBeAddedAsTrustedDevice { - alert.showsSuppressionButton = true - alert.suppressionButton?.title = "AutoAcceptFromThisDevice".localized() - } - - let result = alert.runModal() - - if result == .alertFirstButtonReturn { - primaryButtonAction(alert.suppressionButton?.state == .on) - } else if result == .alertSecondButtonReturn { - secondaryButtonAction() - } - #else - // iOS - let alwaysAcceptLabel = transfer.allowsToBeAddedAsTrustedDevice ? "AlwaysAccept".localized() : nil - ProgressAlert.shared.askForUserPermission(title: title, message: mainMessage, acceptLabel: primaryButtonTitle, acceptAlwaysLabel: alwaysAcceptLabel, rejectLabel: secondaryButtonTitle, acceptAutomatically: acceptAutomatically) { accepted in - - switch accepted { - case .Accept: - primaryButtonAction(false) - case .AcceptAlways: - primaryButtonAction(true) - case .Decline: - secondaryButtonAction() - } - } onCancel: { - NearbyConnectionManager.shared.cancelTransfer(id: transferID) - } - #endif + #else + // If text is received, nothing is shown, as it is directly inserted into clipboard automatically + // Therefore, give a feedback dialog in this case + if transfer.type == .text { + showAlert(title: "QuickDrop", message: mainMessage) } + else { + ProgressAlert.shared.showProgressAlert(onCancel: { + NearbyConnectionManager.shared.cancelTransfer(id: transfer.id) + }) + } + #endif } @@ -188,10 +211,6 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { doubleVibration() #endif - if wasPlainTextTransfer { - showCopiedToClipboardAlert() - } - let currentCount = Settings.shared.incomingTransmissionCount #if os(macOS) @@ -209,22 +228,9 @@ class ReceiveModel: ObservableObject, InboundAppDelegate { } - private func showCopiedToClipboardAlert() { - #if os(macOS) + func showPlusScreen() { DispatchQueue.main.async { - BezelNotification.show(messageText: "InsertedIntoClipboard".localized(), icon: .clipboard) - } - #endif - } - - - private func pressAcceptButton(transferID: String, trustDevice: Bool) { - if isFileTransferRestricted() { - log("[ReceiveModel] Showing plus screen...") - NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: true, trustDevice: trustDevice, storeInTemp: true) - controlPlusScreen(true) - } else { - NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: true, trustDevice: trustDevice, storeInTemp: false) + self.controlPlusScreen(true) } } diff --git a/QuickDropiOS/View/UIAlert.swift b/QuickDropiOS/View/UIAlert.swift index 8fb36c0..24dcfa9 100644 --- a/QuickDropiOS/View/UIAlert.swift +++ b/QuickDropiOS/View/UIAlert.swift @@ -18,38 +18,38 @@ final class ProgressAlert { private weak var progressView: UIProgressView? // MARK: - Initial Accept / Decline - func askForUserPermission(title: String, message: String, acceptLabel: String, acceptAlwaysLabel: String?, rejectLabel: String, acceptAutomatically: Bool, onAccept: @escaping (AcceptAction) -> Void, onCancel: @escaping () -> Void) { + func askForUserPermission(title: String, message: String, acceptLabel: String, acceptAlwaysLabel: String?, rejectLabel: String, onAccept: @escaping (AcceptAction) -> Void, onCancel: @escaping () -> Void) { guard let vc = topMostViewController() else { return } - if acceptAutomatically { + let alert = UIAlertController(title: title, + message: message, + preferredStyle: .alert) + + alert.addAction(UIAlertAction(title: rejectLabel, style: .cancel, handler: { _ in + onAccept(.Decline) + })) + + alert.addAction(UIAlertAction(title: acceptLabel, style: .default, handler: { _ in onAccept(.Accept) self.showProgressAlert(on: vc, onCancel: onCancel) - } - else { - - let alert = UIAlertController(title: title, - message: message, - preferredStyle: .alert) - - alert.addAction(UIAlertAction(title: rejectLabel, style: .cancel, handler: { _ in - onAccept(.Decline) - })) - - alert.addAction(UIAlertAction(title: acceptLabel, style: .default, handler: { _ in - onAccept(.Accept) + })) + + if let acceptAlways = acceptAlwaysLabel { + alert.addAction(UIAlertAction(title: acceptAlways, style: .default, handler: { _ in + onAccept(.AcceptAlways) self.showProgressAlert(on: vc, onCancel: onCancel) })) - - if let acceptAlways = acceptAlwaysLabel { - alert.addAction(UIAlertAction(title: acceptAlways, style: .default, handler: { _ in - onAccept(.AcceptAlways) - self.showProgressAlert(on: vc, onCancel: onCancel) - })) - } - - vc.present(alert, animated: true) } + + vc.present(alert, animated: true) } + + + func showProgressAlert(onCancel: @escaping () -> Void) { + guard let vc = topMostViewController() else { return } + self.showProgressAlert(on: vc, onCancel: onCancel) + } + // MARK: - Progress Alert private func showProgressAlert(on vc: UIViewController, onCancel: @escaping () -> Void) {