Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add estimated download time #2263

Merged
merged 15 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DuckDuckGo/Common/Localizables/UserText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ struct UserText {
static let downloadCanceled = NSLocalizedString("downloads.error.canceled", value: "Canceled", comment: "Short error description when downloaded file download was canceled")
static let downloadFailedToMoveFileToDownloads = NSLocalizedString("downloads.error.move.failed", value: "Could not move file to Downloads", comment: "Short error description when could not move downloaded file to the Downloads folder")
static let downloadFailed = NSLocalizedString("downloads.error.other", value: "Error", comment: "Short error description when Download failed")
static let downloadBytesLoadedFormat = NSLocalizedString("downloads.bytes.format", value: "%@ of %@", comment: "Number of bytes out of total bytes downloaded (1Mb of 2Mb)")
static let downloadSpeedFormat = NSLocalizedString("downloads.speed.format", value: "%@/s", comment: "Download speed format (1Mb/sec)")

static let cancelDownloadToolTip = NSLocalizedString("downloads.tooltip.cancel", value: "Cancel Download", comment: "Mouse-over tooltip for Cancel Download button")
static let restartDownloadToolTip = NSLocalizedString("downloads.tooltip.restart", value: "Restart Download", comment: "Mouse-over tooltip for Restart Download button")
Expand Down
43 changes: 36 additions & 7 deletions DuckDuckGo/FileDownload/Model/WebKitDownloadTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protocol WebKitDownloadTaskDelegate: AnyObject {
final class WebKitDownloadTask: NSObject, ProgressReporting, @unchecked Sendable {

static let downloadExtension = "duckload"
private enum Constants {
static let remainingDownloadTimeEstimationDelay: TimeInterval = 1
static let downloadSpeedSmoothingFactor = 0.1
}

let progress: Progress
let shouldPromptForLocation: Bool
Expand Down Expand Up @@ -72,7 +76,7 @@ final class WebKitDownloadTask: NSObject, ProgressReporting, @unchecked Sendable
private weak var delegate: WebKitDownloadTaskDelegate?

private let download: WebKitDownload
private var cancellables = Set<AnyCancellable>()
private var progressCancellable: AnyCancellable?

private var decideDestinationCompletionHandler: ((URL?) -> Void)?

Expand Down Expand Up @@ -114,12 +118,37 @@ final class WebKitDownloadTask: NSObject, ProgressReporting, @unchecked Sendable
private func start() {
self.progress.fileDownloadingSourceURL = download.originalRequest?.url
if let progress = (self.download as? ProgressReporting)?.progress {
progress.publisher(for: \.totalUnitCount)
.assign(to: \.totalUnitCount, onWeaklyHeld: self.progress)
.store(in: &self.cancellables)
progress.publisher(for: \.completedUnitCount)
.assign(to: \.completedUnitCount, onWeaklyHeld: self.progress)
.store(in: &self.cancellables)

var startTime: Date?
progressCancellable = progress.publisher(for: \.totalUnitCount)
.combineLatest(progress.publisher(for: \.completedUnitCount))
.sink { [weak progress=self.progress] total, completed in
guard let progress else { return }
if progress.totalUnitCount != total {
progress.totalUnitCount = total
}
progress.completedUnitCount = completed

if total > 0, completed > 0 {
guard let startTime else {
startTime = Date()
return
}
let elapsedTime = Date().timeIntervalSince(startTime)
// delay before we start calculating the estimated time - because initially it‘s not reliable
guard elapsedTime > Constants.remainingDownloadTimeEstimationDelay else { return }

// calculate instantaneous download speed
var throughput = Double(completed) / elapsedTime

// calculate the moving average of download speed
if let oldThroughput = progress.throughput.map(Double.init) {
throughput = Constants.downloadSpeedSmoothingFactor * throughput + (1 - Constants.downloadSpeedSmoothingFactor) * oldThroughput
}
progress.throughput = Int(throughput)
progress.estimatedTimeRemaining = Double(total - completed) / throughput
}
}
}
}

Expand Down
Loading
Loading