Improve progress updating, fix warnings
This commit is contained in:
19
Caps/Download/ClassifierProgress.swift
Normal file
19
Caps/Download/ClassifierProgress.swift
Normal 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
|
||||
}
|
||||
}
|
5
Caps/Download/ClassifierProgressDelegate.swift
Normal file
5
Caps/Download/ClassifierProgressDelegate.swift
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
protocol ClassifierProgressDelegate: AnyObject {
|
||||
|
||||
func classifierProgress(_ progress: ClassifierProgress)
|
||||
}
|
20
Caps/Download/ClassifierProgressObserver.swift
Normal file
20
Caps/Download/ClassifierProgressObserver.swift
Normal 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)))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user