Improve progress updating, fix warnings

This commit is contained in:
Christoph Hagen
2025-01-31 14:52:20 +01:00
parent 3dc5674a3a
commit 92e1eacf57
9 changed files with 154 additions and 144 deletions

View File

@ -0,0 +1,19 @@
struct ClassifierProgress {
let bytesLoaded: Double
let total: Double
var percentage: Double {
guard total > 0 else {
return 0.0
}
return bytesLoaded * 100 / total
}
init(bytesLoaded: Double = 0, total: Double = 0) {
self.bytesLoaded = bytesLoaded
self.total = total
}
}

View File

@ -0,0 +1,5 @@
protocol ClassifierProgressDelegate: AnyObject {
func classifierProgress(_ progress: ClassifierProgress)
}

View File

@ -0,0 +1,20 @@
import Foundation
final class ClassifierProgressObserver: NSObject {
weak var delegate: ClassifierProgressDelegate?
}
extension ClassifierProgressObserver: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
delegate?.classifierProgress(.init(
bytesLoaded: Double(totalBytesWritten),
total: Double(totalBytesExpectedToWrite)))
}
}