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

[Gutenberg] cancel upload on image block removal #9402

Merged
merged 15 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ enum RestartEditorOptions {
private boolean mMediaInsertedOnCreation;

private List<String> mPendingVideoPressInfoRequests;
private List<String> mAztecBackspaceDeletedMediaItemIds = new ArrayList<>();
private List<String> mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds = new ArrayList<>();
private List<String> mMediaMarkedUploadingOnStartIds = new ArrayList<>();
private PostEditorAnalyticsSession mPostEditorAnalyticsSession;
private boolean mIsConfigChange = false;
Expand Down Expand Up @@ -3482,9 +3482,8 @@ public void onMediaUploadCancelClicked(String localMediaId) {
@Override
public void onMediaDeleted(String localMediaId) {
if (!TextUtils.isEmpty(localMediaId)) {
if (mShowAztecEditor) {
mAztecBackspaceDeletedMediaItemIds.add(localMediaId);
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedMediaItemIds);
if (mShowAztecEditor && !mShowGutenbergEditor) {
setDeletedMediaIdOnUploadService(localMediaId);
// passing false here as we need to keep the media item in case the user wants to undo
cancelMediaUpload(StringUtils.stringToInt(localMediaId), false);
} else if (mShowGutenbergEditor) {
Expand All @@ -3493,6 +3492,8 @@ public void onMediaDeleted(String localMediaId) {
return;
}

setDeletedMediaIdOnUploadService(localMediaId);

// also make sure it's not being uploaded anywhere else (maybe on some other Post,
// simultaneously)
if (mediaModel.getUploadState() != null
Expand All @@ -3504,6 +3505,11 @@ public void onMediaDeleted(String localMediaId) {
}
}

private void setDeletedMediaIdOnUploadService(String localMediaId) {
mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds.add(localMediaId);
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds);
}

private void cancelMediaUpload(int localMediaId, boolean delete) {
MediaModel mediaModel = mMediaStore.getMediaWithLocalId(Integer.valueOf(localMediaId));
if (mediaModel != null) {
Expand All @@ -3520,7 +3526,7 @@ private void cancelMediaUpload(int localMediaId, boolean delete) {
* physically delete from the FluxC DB those items that have been deleted by the user using backspace.
* */
private void definitelyDeleteBackspaceDeletedMediaItems() {
for (String mediaId : mAztecBackspaceDeletedMediaItemIds) {
for (String mediaId : mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds) {
if (!TextUtils.isEmpty(mediaId)) {
// make sure the MediaModel exists
MediaModel mediaModel = mMediaStore.getMediaWithLocalId(StringUtils.stringToInt(mediaId));
Expand Down Expand Up @@ -3563,9 +3569,9 @@ public void onUndoMediaCheck(final String undoedContent) {

if (!found) {
if (mEditorFragment instanceof AztecEditorFragment) {
mAztecBackspaceDeletedMediaItemIds.remove(mediaId);
mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds.remove(mediaId);
// update the mediaIds list in UploadService
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedMediaItemIds);
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedOrGbBlockDeletedMediaItemIds);
((AztecEditorFragment) mEditorFragment).setMediaToFailed(mediaId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,17 @@ private boolean cancelPostUploadMatchingMedia(@NonNull MediaModel media, String
return false;
}

SiteModel site = mSiteStore.getSiteByLocalId(postToCancel.getLocalSiteId());
mPostUploadNotifier.incrementUploadedPostCountFromForegroundNotification(postToCancel);
if (PostUploadHandler.isPostUploadingOrQueued(postToCancel) && !isPostCurrentlyBeingEdited(postToCancel)) {
// post is not being edited and is currently queued, update the count on the foreground notification
mPostUploadNotifier.incrementUploadedPostCountFromForegroundNotification(postToCancel);
}

if (showError || mUploadStore.isFailedPost(postToCancel)) {
// Only show the media upload error notification if the post is NOT registered in the UploadStore
// - otherwise if it IS registered in the UploadStore and we get a `cancelled` signal it means
// the user actively cancelled it. No need to show an error then.
String message = UploadUtils.getErrorMessage(this, postToCancel, errorMessage, true);
SiteModel site = mSiteStore.getSiteByLocalId(postToCancel.getLocalSiteId());
mPostUploadNotifier.updateNotificationErrorForPost(postToCancel, site, message,
mUploadStore.getFailedMediaForPost(postToCancel).size());
}
Expand Down Expand Up @@ -920,8 +923,10 @@ private boolean doFinalProcessingOfPosts(OnPostUploaded event) {
}
// TODO Should do some extra validation here
// e.g. what if the post has local media URLs but no pending media uploads?
mPostUploadHandler.upload(updatedPost);
return true;
if (PostUtils.isPublishable(updatedPost)) {
mPostUploadHandler.upload(updatedPost);
return true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ public void onCapturePhotoButtonClicked() {
@Override public void onCancelUploadForMediaClicked(int mediaId) {
showCancelMediaUploadDialog(mediaId);
}

@Override public void onCancelUploadForMediaDueToDeletedBlock(int mediaId) {
cancelMediaUploadForDeletedBlock(mediaId);
}
},
new OnReattachQueryListener() {
@Override
Expand Down Expand Up @@ -337,6 +341,18 @@ private void checkAndRequestCameraAndStoragePermissions() {
}
}

private void cancelMediaUploadForDeletedBlock(int localMediaId) {
if (mUploadingMediaProgressMax.containsKey(String.valueOf(localMediaId))) {
// first make sure to signal deletion
mEditorFragmentListener.onMediaDeleted(String.valueOf(localMediaId));
// second also perform a media upload cancel action, through the onMediaUploadCancelClicked interface
mEditorFragmentListener.onMediaUploadCancelClicked(String.valueOf(localMediaId));
mUploadingMediaProgressMax.remove(localMediaId);
} else {
// upload has already finished by the time the user deleted the block, so no op
}
}

private void showCancelMediaUploadDialog(final int localMediaId) {
// Display 'cancel upload' dialog
AlertDialog.Builder builder = new AlertDialog.Builder(
Expand Down