Skip to content

Commit

Permalink
Add global retry button
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 24, 2024
1 parent 747bda3 commit 79a5aba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 43 deletions.
43 changes: 16 additions & 27 deletions WordPress/Classes/ViewRelated/Post/PostMediaUploadStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ struct PostMediaUploadStatusView: View {

var body: some View {
contents
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(Strings.close, action: onCloseTapped)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(Strings.close, action: onCloseTapped)
}
ToolbarItem(placement: .topBarTrailing) {
Menu {
Button(action: viewModel.buttonRetryTapped) {
Label(Strings.retryUploads, systemImage: "arrow.clockwise")
}.disabled(viewModel.isButtonRetryDisabled)
} label: {
Image(systemName: "ellipsis")
}
}
}
}
.navigationTitle(Strings.title)
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(Strings.title)
.navigationBarTitleDisplayMode(.inline)
}

@ViewBuilder
Expand Down Expand Up @@ -59,11 +68,9 @@ private struct MediaUploadStatusView: View {
switch viewModel.state {
case .uploading:
MediaUploadProgressView(progress: viewModel.fractionCompleted)
makeMenu()
case .failed:
Image(systemName: "exclamationmark.circle.fill")
.foregroundStyle(.red)
makeMenu()
case .uploaded:
Image(systemName: "checkmark.circle.fill")
.foregroundStyle(.secondary.opacity(0.33))
Expand All @@ -73,23 +80,6 @@ private struct MediaUploadStatusView: View {
await viewModel.loadThumbnail()
}
}

@ViewBuilder
private func makeMenu() -> some View {
Menu {
Button(action: viewModel.buttonRetryTapped) {
Label(Strings.retry, systemImage: "arrow.clockwise")
}
Button(role: .destructive, action: viewModel.buttonRemoveTapped) {
Label(Strings.remove, systemImage: "trash")
}
} label: {
Image(systemName: "ellipsis")
.font(.footnote)
.foregroundStyle(.secondary.opacity(0.75))
}
.buttonStyle(.plain)
}
}

struct MediaUploadProgressView: View {
Expand Down Expand Up @@ -133,6 +123,5 @@ private enum Strings {
static let title = NSLocalizedString("postMediaUploadStatusView.title", value: "Media Uploads", comment: "Title for post media upload status view")
static let empty = NSLocalizedString("postMediaUploadStatusView.noPendingUploads", value: "No pending uploads", comment: "Placeholder text in postMediaUploadStatusView when no uploads remain")
static let close = NSLocalizedString("postMediaUploadStatusView.close", value: "Close", comment: "Close button in postMediaUploadStatusView")
static let retry = NSLocalizedString("postMediaUploadStatusView.retry", value: "Retry", comment: "Retry upload button in postMediaUploadStatusView")
static let remove = NSLocalizedString("postMediaUploadStatusView.remove", value: "Remove", comment: "Remove media button in postMediaUploadStatusView")
static let retryUploads = NSLocalizedString("postMediaUploadStatusView.retryUploads", value: "Retry Uploads", comment: "Retry upload button in postMediaUploadStatusView")
}
25 changes: 12 additions & 13 deletions WordPress/Classes/ViewRelated/Post/PostMediaUploadViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ final class PostMediaUploadViewModel: ObservableObject {
private weak var timer: Timer?
private var cancellables: [AnyCancellable] = []

var isButtonRetryDisabled: Bool {
!(uploads.contains(where: { $0.error != nil }))
}

deinit {
timer?.invalidate()
}
Expand Down Expand Up @@ -59,6 +63,12 @@ final class PostMediaUploadViewModel: ObservableObject {
fractionCompleted = uploads.map(\.fractionCompleted).reduce(0, +) / Double(uploads.count)
completedUploadsCount = uploads.filter(\.isCompleted).count
}

func buttonRetryTapped() {
for upload in uploads {
upload.retry()
}
}
}

/// Manages individual media upload.
Expand Down Expand Up @@ -133,7 +143,7 @@ final class MediaUploadViewModel: ObservableObject, Identifiable {
self.state = media.isUploadNeeded ? .uploading : .uploaded
self.fileSize = media.filesize?.int64Value ?? 0 // Should never be `0`

if let error = media.error, MediaCoordinator.isTerminalError(error) {
if media.remoteStatus == .failed, let error = media.error, MediaCoordinator.isTerminalError(error) {
self.details = error.localizedDescription
self.state = .failed(error)
return // No retry
Expand All @@ -157,7 +167,7 @@ final class MediaUploadViewModel: ObservableObject, Identifiable {
}
}

private func retry() {
fileprivate func retry() {
retryTimer = nil
coordinator.retryMedia(media)
}
Expand All @@ -184,17 +194,6 @@ final class MediaUploadViewModel: ObservableObject, Identifiable {
// Continue showing placeholder
}
}

// MARK: - Menu

func buttonRetryTapped() {
retry()
}

func buttonRemoveTapped() {
coordinator.cancelUploadAndDeleteMedia(media)
// TODO: notify Gutenberg or update the post in a different way
}
}

private extension Media {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource
guard !viewModel.isCompleted else {
return nil
}
if let error = viewModel.uploads.lazy.map(\.error).first {
return .failed(title: Strings.mediaUploadFailedTitle, details: error?.localizedDescription) { [weak self] in
if let error = viewModel.uploads.lazy.compactMap(\.error).first {
return .failed(title: Strings.mediaUploadFailedTitle, details: error.localizedDescription) { [weak self] in
self?.buttonShowUploadInfoTapped()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct PublishButton: View {
}

private enum Constants {
static let accessoryViewWidth: CGFloat = 22
static let accessoryViewWidth: CGFloat = 20
}

final class PublishButtonViewModel: ObservableObject {
Expand Down

0 comments on commit 79a5aba

Please sign in to comment.