From 62d285364f4e8318980132ce8ffc3a09975106f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Sat, 17 May 2025 15:01:22 +0200 Subject: [PATCH] Automatically open folder if file transfer finished --- NearDrop/AppDelegate.swift | 6 +- NearbyShare/Helpers.swift | 8 ++ NearbyShare/InboundNearbyConnection.swift | 132 ++++++++++++++-------- 3 files changed, 96 insertions(+), 50 deletions(-) diff --git a/NearDrop/AppDelegate.swift b/NearDrop/AppDelegate.swift index 62b1e2f..80599fc 100644 --- a/NearDrop/AppDelegate.swift +++ b/NearDrop/AppDelegate.swift @@ -229,10 +229,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele } } - func transmissionCount() -> Int { - return UserDefaults.standard.integer(forKey: UserDefaultsKeys.transmissionCount.rawValue) - } - // Action for "Recommended Apps" menu item @objc func openRecommendedApps() { if let url = URL(string: "https://apps.apple.com/de/developer/leon-boettger/id1537384790") { @@ -346,7 +342,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele } private func pressAcceptButton(transferID: String) { - if (!isPlusVersion() && transmissionCount() > 1) { + if isFileTransferRestricted() { continueTransmission(accept: true, transferID: transferID, storeInTemp: true) log("Showing plus screen...") diff --git a/NearbyShare/Helpers.swift b/NearbyShare/Helpers.swift index 9c24fbb..c39fd7e 100644 --- a/NearbyShare/Helpers.swift +++ b/NearbyShare/Helpers.swift @@ -11,6 +11,14 @@ public func isPlusVersion() -> Bool { return UserDefaults.standard.bool(forKey: UserDefaultsKeys.plusVersion.rawValue) } +public func isFileTransferRestricted() -> Bool { + (!isPlusVersion() && transmissionCount() > 1) +} + +public func transmissionCount() -> Int { + return UserDefaults.standard.integer(forKey: UserDefaultsKeys.transmissionCount.rawValue) +} + public enum UserDefaultsKeys: String, CaseIterable { case isEligibleForIap = "isEligibleForIap" case appLaunchedBefore = "ShowedWelcomeScreen" diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index 06f3e41..dbe2dc8 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -479,7 +479,30 @@ protocol InboundNearbyConnectionDelegate { } public class SaveFilesManager { - private init() {} + + private init() { + // remove old temp directory + + let tempPath = FileManager.default.temporaryDirectory + let fileManager = FileManager.default + + do { + let contents = try fileManager.contentsOfDirectory(at: tempPath, includingPropertiesForKeys: nil) + + var didSomething = false + + for item in contents { + didSomething = true + try fileManager.removeItem(at: item) + } + + if didSomething { + log("Temporary directory cleared.") + } + } catch { + log("Failed to list contents of temp directory: \(error)") + } + } public static let shared = SaveFilesManager() @@ -487,58 +510,77 @@ public class SaveFilesManager { private var securityScopeUrl: URL? private var filesFinishedDownloading = [URL]() - + private var filesFinishedDownloadingSinceLastRun = [URL]() + public func registerFileFinishedDownloading(_ fileURL: URL) { filesFinishedDownloading.append(fileURL) + filesFinishedDownloadingSinceLastRun.append(fileURL) } - + public func movePendingFilesToTarget() { - if !isPlusVersion() { - return - } - log("Moving pending files to target directory") - - do { - let fileManager = FileManager.default - let files = try fileManager.contentsOfDirectory(at: tempDirectory, includingPropertiesForKeys: nil) - - let target = getSaveDirectory() - - for file in files { - let fileName = file.lastPathComponent - let destinationURL = target.appendingPathComponent(fileName) - - if !filesFinishedDownloading.contains(destinationURL) { - log("File \(file) not finished downloading, skipping") - continue + if isPlusVersion() { + do { + let fileManager = FileManager.default + let files = try fileManager.contentsOfDirectory(at: tempDirectory, includingPropertiesForKeys: nil) + + let target = getSaveDirectory() + var loggedExecution = false + + for file in files { + + if !loggedExecution { + log("Moving pending files to target directory") + loggedExecution = true + } + + let fileName = file.lastPathComponent + let destinationURL = target.appendingPathComponent(fileName) + + if !filesFinishedDownloading.contains(destinationURL) { + log("File \(file) not finished downloading, skipping") + continue + } + + log("Moving file: \(file.lastPathComponent) to \(destinationURL.lastPathComponent)") + + do { + try fileManager.copyItem(at: file, to: destinationURL) + + let progress = Progress() + progress.fileURL = destinationURL + progress.totalUnitCount = 10 + progress.kind = .file + progress.isPausable = false + progress.publish() + + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + progress.completedUnitCount = 10 + progress.unpublish() + } + + try fileManager.removeItem(at: file) + } + catch { + log("Error moving file: \(error)") + } } - - log("Moving file: \(file.lastPathComponent) to \(destinationURL.lastPathComponent)") - try fileManager.copyItem(at: file, to: destinationURL) - - let progress = Progress() - progress.fileURL = destinationURL - progress.totalUnitCount = 10 - progress.kind = .file - progress.isPausable = false - progress.publish() - - DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - progress.completedUnitCount = 10 - progress.unpublish() - } - - try fileManager.removeItem(at: file) + log("Moved all pending files to target directory") + } + catch { + // Pending directory doesn't exist or is empty } - - stopAccessingSecurityScopedResource() - - log("Moved all pending files to target directory") - - } catch { - log("Error moving pending files: \(error)") } + + if !filesFinishedDownloadingSinceLastRun.isEmpty && !isFileTransferRestricted() { + log("Opening \(filesFinishedDownloadingSinceLastRun.count) file(s) in Finder.") + NSWorkspace.shared.activateFileViewerSelecting(filesFinishedDownloadingSinceLastRun) + + // Clear the list of finished files + filesFinishedDownloadingSinceLastRun.removeAll() + } + + stopAccessingSecurityScopedResource() } public func stopAccessingSecurityScopedResource() {