mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-21 05:46:03 +00:00
Fix if pending file takes too long
This commit is contained in:
1 parent
c0fb5dd303
commit
bb86ebc606
7 files changed
+53
-19
No files matched your search
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Helpers.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 04.04.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public func isPlusVersion() -> Bool {
|
||||
return UserDefaults.standard.bool(forKey: UserDefaultsKeys.plusVersion.rawValue)
|
||||
}
|
||||
|
||||
public enum UserDefaultsKeys: String, CaseIterable {
|
||||
case isEligibleForIap = "isEligibleForIap"
|
||||
case appLaunchedBefore = "ShowedWelcomeScreen"
|
||||
case plusVersion = "isPlusVersion"
|
||||
case transmissionCount = "reviewRequestCountKey"
|
||||
case automaticallyAcceptFiles = "automaticallyAcceptFiles"
|
||||
case saveFolderBookmark = "saveFolderBookmark"
|
||||
}
|
||||
@@ -43,6 +43,7 @@ class InboundNearbyConnection: NearbyConnection{
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.connectionWasTerminated(connection: self, error: self.lastError)
|
||||
|
||||
SaveFilesManager.shared.movePendingFilesToTarget()
|
||||
SaveFilesManager.shared.stopAccessingSecurityScopedResource()
|
||||
}
|
||||
}
|
||||
@@ -113,6 +114,8 @@ class InboundNearbyConnection: NearbyConnection{
|
||||
try fileInfo.fileHandle?.close()
|
||||
transferredFiles[id]!.fileHandle=nil
|
||||
fileInfo.progress?.unpublish()
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
|
||||
transferredFiles.removeValue(forKey: id)
|
||||
if transferredFiles.isEmpty{
|
||||
try sendDisconnectionAndDisconnect()
|
||||
@@ -135,6 +138,7 @@ class InboundNearbyConnection: NearbyConnection{
|
||||
transferredFiles[id]!.fileHandle=nil
|
||||
fileInfo.progress?.unpublish()
|
||||
transferredFiles.removeValue(forKey: id)
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
try sendDisconnectionAndDisconnect()
|
||||
return true
|
||||
}
|
||||
@@ -442,8 +446,20 @@ public class SaveFilesManager {
|
||||
internal let tempDirectory: URL = FileManager.default.temporaryDirectory.appendingPathComponent("Pending")
|
||||
private var securityScopeUrl: URL?
|
||||
|
||||
private var filesFinishedDownloading = [URL]()
|
||||
|
||||
|
||||
public func registerFileFinishedDownloading(_ fileURL: URL) {
|
||||
filesFinishedDownloading.append(fileURL)
|
||||
}
|
||||
|
||||
|
||||
public func movePendingFilesToTarget() {
|
||||
|
||||
if !isPlusVersion() {
|
||||
return
|
||||
}
|
||||
|
||||
log("Moving pending files to target directory")
|
||||
|
||||
do {
|
||||
@@ -453,9 +469,16 @@ public class SaveFilesManager {
|
||||
let target = getSaveDirectory()
|
||||
|
||||
for file in files {
|
||||
|
||||
let fileName = file.lastPathComponent
|
||||
var destinationURL = target.appendingPathComponent(fileName)
|
||||
|
||||
if !filesFinishedDownloading.contains(destinationURL) {
|
||||
log("File \(file) not finished downloading, skipping")
|
||||
log("Pending files: \(filesFinishedDownloading)")
|
||||
continue
|
||||
}
|
||||
|
||||
var counter = 1
|
||||
let fileExtension = destinationURL.pathExtension
|
||||
let baseName = destinationURL.deletingPathExtension().lastPathComponent
|
||||
@@ -469,10 +492,22 @@ public class SaveFilesManager {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
try fileManager.removeItem(at: tempDirectory)
|
||||
|
||||
stopAccessingSecurityScopedResource()
|
||||
|
||||
log("Moved all pending files to target directory")
|
||||
|
||||
Reference in new issue
Block a user