Skip to content

Commit

Permalink
Add media progress reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Feb 20, 2024
1 parent df7a67e commit 730c835
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
2 changes: 2 additions & 0 deletions WordPress/Classes/Services/MediaCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MediaCoordinator: NSObject {
///
/// - Returns: `true` if all media in the post is uploading or was uploaded, `false` otherwise.
///
@discardableResult
func uploadMedia(for post: AbstractPost, automatedRetry: Bool = false) -> Bool {
let failedMedia: [Media] = post.media.filter({ $0.remoteStatus == .failed })
let mediasToUpload: [Media]
Expand Down Expand Up @@ -230,6 +231,7 @@ class MediaCoordinator: NSObject {
trackUploadOf(media, analyticsInfo: analyticsInfo)

let uploadProgress = uploadMedia(media)
totalProgress.setUserInfoObject(uploadProgress, forKey: .uploadProgress)
totalProgress.addChild(uploadProgress, withPendingUnitCount: MediaExportProgressUnits.uploadDone)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension ProgressUserInfoKey {
static let mediaID = ProgressUserInfoKey("mediaID")
static let mediaError = ProgressUserInfoKey("mediaError")
static let mediaObject = ProgressUserInfoKey("mediaObject")
static let uploadProgress = ProgressUserInfoKey("uploadProgress")
}

/// Media Progress Coordinator allow the tracking of progress on multiple media objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource
private var cancellables = Set<AnyCancellable>()
@Published private var keyboardShown: Bool = false

private weak var mediaPollingTimer: Timer?

init(post: Post,
identifiers: [PrepublishingIdentifier],
completion: @escaping (CompletionResult) -> (),
Expand Down Expand Up @@ -176,6 +178,13 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource

updatePublishButtonLabel()

if FeatureFlag.offlineMode.enabled {
updatePublishButtonState()
mediaPollingTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in
self?.updatePublishButtonState()
}
}

return footerView
}

Expand Down Expand Up @@ -420,6 +429,18 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource

// MARK: - Publish Button

private func updatePublishButtonState() {
if let state = PublishButtonState.uploadingState(for: post) {
publishButtonViewModel.state = state
} else {
if case .loading = publishButtonViewModel.state {
// Do nothing
} else {
publishButtonViewModel.state = .default
}
}
}

private func updatePublishButtonLabel() {
publishButtonViewModel.title = post.isScheduled() ? Strings.schedule : Strings.publish
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,40 @@ enum PublishButtonState {
case failed(title: String, details: String? = nil, onRetryTapped: (() -> Void)? = nil)

struct Progress {
let completed: Int64
let total: Int64
var completed: Int64
var total: Int64
}

/// Returns the state of the button based on the current upload progress
/// for the given post.
static func uploadingState(for post: AbstractPost, coordinator: MediaCoordinator = .shared) -> PublishButtonState? {
if post.hasFailedMedia {
return .failed(title: Strings.mediaUploadFailed, onRetryTapped: {
coordinator.uploadMedia(for: post)
})
}
if coordinator.isUploadingMedia(for: post) {
var totalUploadProgress = Progress(completed: 0, total: 0)
var completedUploadCount = 0
var totalUploadCount = 0

for media in post.media {
if let progress = coordinator.progress(for: media) {
if let uploadProgress = progress.userInfo[.uploadProgress] as? Foundation.Progress,
let filesize = media.filesize?.int64Value {
totalUploadProgress.completed += Int64(Double(filesize) * uploadProgress.fractionCompleted)
totalUploadProgress.total += filesize
}

if progress.fractionCompleted >= 1.0 {
completedUploadCount += 1
}
totalUploadCount += 1
}
}
return .uploading(title: Strings.uploadingMedia + ": \(completedUploadCount) / \(totalUploadCount)", progress: totalUploadProgress)
}
return nil
}
}

Expand All @@ -111,6 +143,8 @@ private enum Strings {
}

static let retry = NSLocalizedString("publishButton.retry", value: "Retry", comment: "Retry button title")
static let mediaUploadFailed = NSLocalizedString("prepublishing.mediaUploadFailed", value: "Failed to upload media", comment: "Title for an error messaage in the pre-publishing sheet")
static let uploadingMedia = NSLocalizedString("prepublishing.uploadingMedia", value: "Uploading media", comment: "Title for a publish button state in the pre-publishing sheet")
}

#Preview {
Expand Down

0 comments on commit 730c835

Please sign in to comment.