Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline Mode: Integrate "Media Uploads" with a sync engine for drafts #23103

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ class PostCoordinator: NSObject {
retryDelay = min(32, retryDelay * 1.5)
return retryDelay
}
func setLongerDelay() {
retryDelay = max(retryDelay, 20)
}
var retryDelay: TimeInterval
weak var retryTimer: Timer?

Expand Down Expand Up @@ -509,10 +512,11 @@ class PostCoordinator: NSObject {
}

private func startSync(for post: AbstractPost) {
guard let revision = post.getLatestRevisionNeedingSync() else {
let worker = getWorker(for: post)
if let worker = workers[post.objectID], worker.error != nil {
worker.error = nil
postDidUpdateNotification(for: post)
}
guard let revision = post.getLatestRevisionNeedingSync() else {
return DDLogInfo("sync: \(post.objectID.shortDescription) is already up to date")
}
startSync(for: post, revision: revision)
Expand Down Expand Up @@ -610,14 +614,16 @@ class PostCoordinator: NSObject {
worker.error = error
postDidUpdateNotification(for: operation.post)

if !PostCoordinator.isTerminalError(error) {
let delay = worker.nextRetryDelay
worker.retryTimer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { [weak self, weak worker] _ in
guard let self, let worker else { return }
self.didRetryTimerFire(for: worker)
}
worker.log("scheduled retry with delay: \(delay)s.")
if PostCoordinator.isTerminalError(error) {
worker.setLongerDelay()
}

let delay = worker.nextRetryDelay
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safer to retry (but with a longer delay) than to never retry again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also helps if you have a draft post where sync is failing due to the media uploads and you go to "Context Menu / Publish / Media Uploads" and remove the upload for there – it doesn't trigger re-sync (now that I wrote it maybe it also should?)

worker.retryTimer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { [weak self, weak worker] _ in
guard let self, let worker else { return }
self.didRetryTimerFire(for: worker)
}
worker.log("scheduled retry with delay: \(delay)s.")

if let error = error as? PostRepository.PostSaveError, case .deleted = error {
operation.log("post was permanently deleted")
Expand Down
38 changes: 38 additions & 0 deletions WordPress/Classes/ViewRelated/Post/PostEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extension PostEditor where Self: UIViewController {
showPostTrashedOverlay()
} else {
showAutosaveAvailableAlertIfNeeded()
showTerminalUploadErrorAlertIfNeeded()
}

var cancellables: [AnyCancellable] = []
Expand Down Expand Up @@ -273,6 +274,39 @@ extension PostEditor where Self: UIViewController {
self.post = post // Even if it's the same instance, it's how you currently refresh the editor
self.createRevisionOfPost()
}

// MARK: - Failed Media Uploads

private func showTerminalUploadErrorAlertIfNeeded() {
let hasTerminalError = post.media.contains {
guard let error = $0.error else { return false }
return MediaCoordinator.isTerminalError(error)
}
if hasTerminalError {
let notice = Notice(title: Strings.failingMediaUploadsMessage, feedbackType: .error, actionTitle: Strings.failingMediaUploadsViewAction, actionHandler: { [weak self] _ in
self?.showMediaUploadDetails()
})
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(700)) {
ActionDispatcherFacade().dispatch(NoticeAction.post(notice))
} // Delay to let the editor show first
}
}

private func showMediaUploadDetails() {
let viewController = PostMediaUploadsViewController(post: post)
let nav = UINavigationController(rootViewController: viewController)
nav.navigationBar.isTranslucent = true // Reset to default
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: UIAction { [weak self] _ in
self?.dismiss(animated: true)
})
if let sheetController = nav.sheetPresentationController {
sheetController.detents = [.medium(), .large()]
sheetController.prefersGrabberVisible = true
sheetController.preferredCornerRadius = 16
nav.additionalSafeAreaInsets = UIEdgeInsets(top: 8, left: 0, bottom: 0, right: 0)
}
self.present(nav, animated: true)
}
}

private var cancellablesKey: UInt8 = 0
Expand Down Expand Up @@ -305,4 +339,8 @@ private enum Strings {
static let trashedPostSheetCancel = NSLocalizedString("postEditor.recoverTrashedPostAlert.cancel", value: "Cancel", comment: "Editor, alert for recovering a trashed post")
static let trashedPostSheetRecover = NSLocalizedString("postEditor.recoverTrashedPostAlert.restore", value: "Restore", comment: "Editor, alert for recovering a trashed post")
static let trashedPostRestored = NSLocalizedString("postEditor.recoverTrashedPost.postRecoveredNoticeTitle", value: "Post restored as a draft", comment: "Editor, notice for successful recovery a trashed post")

static let failingMediaUploadsMessage = NSLocalizedString("postEditor.postHasFailingMediaUploadsSnackbar.message", value: "Some media items failed to upload", comment: "A message for a snackbar informing the user that some media files requires their attention")

static let failingMediaUploadsViewAction = NSLocalizedString("postEditor.postHasFailingMediaUploadsSnackbar.actionView", value: "View", comment: "A 'View' action for a snackbar informing the user that some media files requires their attention")
}
13 changes: 13 additions & 0 deletions WordPress/Classes/ViewRelated/Post/PostMediaUploadsView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import Foundation
import SwiftUI

final class PostMediaUploadsViewController: UIHostingController<PostMediaUploadsView> {
private let viewModel: PostMediaUploadsViewModel

init(post: AbstractPost) {
self.viewModel = PostMediaUploadsViewModel(post: post) // Manange lifecycle
super.init(rootView: PostMediaUploadsView(viewModel: viewModel))
}

required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

/// Displays upload progress for the media for the given post.
struct PostMediaUploadsView: View {
@ObservedObject var viewModel: PostMediaUploadsViewModel
Expand Down
Loading