Skip to content

Commit

Permalink
add media upload counter next to progress bar
Browse files Browse the repository at this point in the history
add media upload counter next to progress bar
  • Loading branch information
suhailsaqan committed Mar 25, 2024
1 parent 49860d7 commit f230f5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
askForAppToLaunch = "Yes"
launchAutomaticallySubstyle = "2">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
23 changes: 21 additions & 2 deletions damus/Models/ImageUploadModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ enum MediaUpload {

class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject {
@Published var progress: Double? = nil
@Published var currentImagesUploaded: Int = 0
@Published var totalImagesToUpload: Int = 0
private var completedUploads: Int = 0

func start(media: MediaUpload, uploader: MediaUploader, keypair: Keypair? = nil) async -> ImageUploadResult {
let res = await create_upload_request(mediaToUpload: media, mediaUploader: uploader, progress: self, keypair: keypair)
DispatchQueue.main.async {
self.progress = nil
self.totalImagesToUpload += 1
}
let res = await create_upload_request(mediaToUpload: media, mediaUploader: uploader, progress: self, keypair: keypair)
return res
}

Expand All @@ -67,4 +70,20 @@ class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject {
self.progress = Double(totalBytesSent) / Double(totalBytesExpectedToSend)
}
}

func didFinishUpload() {
DispatchQueue.main.async {
self.completedUploads += 1
self.currentImagesUploaded = self.completedUploads
}
}

func resetProgress() {
DispatchQueue.main.async {
self.progress = nil
self.currentImagesUploaded = 0
self.totalImagesToUpload = 0
self.completedUploads = 0
}
}
}
11 changes: 9 additions & 2 deletions damus/Views/PostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,11 @@ struct PostView: View {
}

if let progress = image_upload.progress {
ProgressView(value: progress, total: 1.0)
.progressViewStyle(.linear)
HStack {
ProgressView(value: progress, total: 1.0)
.progressViewStyle(.linear)
Text("\(image_upload.currentImagesUploaded)/\(image_upload.totalImagesToUpload)")
}
}

Divider()
Expand Down Expand Up @@ -316,6 +319,7 @@ struct PostView: View {
let meta = blurhash.map { bh in calculate_image_metadata(url: url, img: img, blurhash: bh) }
let uploadedMedia = UploadedMedia(localURL: media.localURL, uploadedURL: url, representingImage: img, metadata: meta)
uploadedMedias.append(uploadedMedia)
image_upload.didFinishUpload()

case .failed(let error):
if let error {
Expand All @@ -325,6 +329,9 @@ struct PostView: View {
}
}

if (image_upload.currentImagesUploaded + 1) == image_upload.totalImagesToUpload {
image_upload.resetProgress()
}
}
}

Expand Down

0 comments on commit f230f5a

Please sign in to comment.