Show image upload progress

This commit is contained in:
William Casarin
2023-03-16 09:13:03 -06:00
parent 7b6d3ef9df
commit 7b1f4b7701
4 changed files with 137 additions and 59 deletions

View File

@@ -0,0 +1,28 @@
//
// ImageUploadModel.swift
// damus
//
// Created by William Casarin on 2023-03-16.
//
import Foundation
import UIKit
class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject {
@Published var progress: Double? = nil
func start(img: UIImage, uploader: ImageUploader) async -> ImageUploadResult {
return await create_image_upload_request(imageToUpload: img, imageUploader: uploader, progress: self)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
DispatchQueue.main.async {
self.progress = Double(totalBytesSent) / Double(totalBytesExpectedToSend)
if self.progress! >= 1.0 {
self.progress = nil
}
}
}
}