Skip to content

Commit

Permalink
add media upload counter next to progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
suhailsaqan committed Mar 25, 2024
1 parent 50a4190 commit 6db601f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"originHash" : "0c5ba17ae4238ee8c0614e8f0957f83778e450906dcaf8ccca5c1aa44c24a252",
"pins" : [
{
"identity" : "gsplayer",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wxxsw/GSPlayer",
"state" : {
"revision" : "aa6dad7943d52f5207f7fcc2ad3e4274583443b8",
"version" : "0.2.26"
}
},
{
"identity" : "kingfisher",
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Kingfisher",
"state" : {
"revision" : "415b1d97fb38bda1e5a6b2dde63354720832110b",
"version" : "7.6.1"
}
},
{
"identity" : "secp256k1.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jb55/secp256k1.swift",
"state" : {
"revision" : "40b4b38b3b1c83f7088c76189a742870e0ca06a9"
}
},
{
"identity" : "swift-markdown-ui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/damus-io/swift-markdown-ui",
"state" : {
"revision" : "76bb7971da7fbf429de1c84f1244adf657242fee"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "5b356adceabff6ca027f6574aac79e9fee145d26",
"version" : "1.14.1"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
}
}
],
"version" : 3
}
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 6db601f

Please sign in to comment.