Skip to content

Commit 89e2c68

Browse files
authored
Merge pull request #9158 from wordpress-mobile/gb/retry-failed-upload
Added onRetryUploadForMediaClicked interface for GB
2 parents e3eed06 + fd55945 commit 89e2c68

File tree

7 files changed

+186
-16
lines changed

7 files changed

+186
-16
lines changed

WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,27 @@ private void requestTemporaryPermissions(DragEvent dragEvent) {
32313231
requestDragAndDropPermissions(dragEvent);
32323232
}
32333233

3234+
@Override
3235+
public void onMediaRetryAllClicked(Set<String> failedMediaIds) {
3236+
UploadService.cancelFinalNotification(this, mPost);
3237+
UploadService.cancelFinalNotificationForMedia(this, mSite);
3238+
3239+
ArrayList<MediaModel> failedMediaList = new ArrayList<>();
3240+
for (String mediaId : failedMediaIds) {
3241+
failedMediaList.add(mMediaStore.getMediaWithLocalId(Integer.valueOf(mediaId)));
3242+
}
3243+
3244+
if (!failedMediaList.isEmpty()) {
3245+
for (MediaModel mediaModel : failedMediaList) {
3246+
mediaModel.setUploadState(MediaUploadState.QUEUED);
3247+
mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(mediaModel));
3248+
}
3249+
startUploadService(failedMediaList);
3250+
}
3251+
3252+
AnalyticsTracker.track(Stat.EDITOR_UPLOAD_MEDIA_RETRIED);
3253+
}
3254+
32343255
@Override
32353256
public boolean onMediaRetryClicked(final String mediaId) {
32363257
if (TextUtils.isEmpty(mediaId)) {
@@ -3300,10 +3321,25 @@ public void onMediaUploadCancelClicked(String localMediaId) {
33003321
@Override
33013322
public void onMediaDeleted(String localMediaId) {
33023323
if (!TextUtils.isEmpty(localMediaId)) {
3303-
mAztecBackspaceDeletedMediaItemIds.add(localMediaId);
3304-
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedMediaItemIds);
3305-
// passing false here as we need to keep the media item in case the user wants to undo
3306-
cancelMediaUpload(StringUtils.stringToInt(localMediaId), false);
3324+
if (mShowAztecEditor) {
3325+
mAztecBackspaceDeletedMediaItemIds.add(localMediaId);
3326+
UploadService.setDeletedMediaItemIds(mAztecBackspaceDeletedMediaItemIds);
3327+
// passing false here as we need to keep the media item in case the user wants to undo
3328+
cancelMediaUpload(StringUtils.stringToInt(localMediaId), false);
3329+
} else if (mShowGutenbergEditor) {
3330+
MediaModel mediaModel = mMediaStore.getMediaWithLocalId(StringUtils.stringToInt(localMediaId));
3331+
if (mediaModel == null) {
3332+
return;
3333+
}
3334+
3335+
// also make sure it's not being uploaded anywhere else (maybe on some other Post,
3336+
// simultaneously)
3337+
if (mediaModel.getUploadState() != null
3338+
&& MediaUtils.isLocalFile(mediaModel.getUploadState().toLowerCase(Locale.ROOT))
3339+
&& !UploadService.isPendingOrInProgressMediaUpload(mediaModel)) {
3340+
mDispatcher.dispatch(MediaActionBuilder.newRemoveMediaAction(mediaModel));
3341+
}
3342+
}
33073343
}
33083344
}
33093345

@@ -3482,6 +3518,18 @@ public void onJavaScriptAlert(String url, String message) {
34823518
}
34833519
});
34843520
}
3521+
3522+
// probably here is best for Gutenberg to start interacting with
3523+
if (mShowGutenbergEditor && mEditorFragment instanceof GutenbergEditorFragment) {
3524+
List<MediaModel> failedMedia = mMediaStore.getMediaForPostWithState(mPost, MediaUploadState.FAILED);
3525+
if (failedMedia != null && !failedMedia.isEmpty()) {
3526+
HashSet<Integer> mediaIds = new HashSet<>();
3527+
for (MediaModel media : failedMedia) {
3528+
mediaIds.add(media.getId());
3529+
}
3530+
((GutenbergEditorFragment) mEditorFragment).resetUploadingMediaToFailed(mediaIds);
3531+
}
3532+
}
34853533
}
34863534

34873535
@Override

WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadService.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,16 @@ private void unpackPostIntent(@NonNull Intent intent) {
244244
}
245245

246246
if (intent.getBooleanExtra(KEY_SHOULD_RETRY, false)) {
247-
if (AppPrefs.isAztecEditorEnabled()) {
247+
if (AppPrefs.isAztecEditorEnabled() || AppPrefs.isGutenbergEditorEnabled()) {
248248
if (!NetworkUtils.isNetworkAvailable(this)) {
249249
rebuildNotificationError(post, getString(R.string.no_network_message));
250250
return;
251251
}
252-
aztecRetryUpload(post);
252+
boolean postHasGutenbergBlocks = PostUtils.contentContainsGutenbergBlocks(post.getContent());
253+
boolean processWithAztec =
254+
AppPrefs.isAztecEditorEnabled() && !AppPrefs.isGutenbergEditorEnabled()
255+
&& !postHasGutenbergBlocks;
256+
retryUpload(post, processWithAztec);
253257
} else {
254258
ToastUtils.showToast(this, R.string.retry_needs_aztec);
255259
}
@@ -706,7 +710,7 @@ private void rebuildNotificationError(PostModel post, String errorMessage) {
706710
errorMessage, 0);
707711
}
708712

709-
private void registerFailedMediaForThisPost(PostModel post) {
713+
private void aztecRegisterFailedMediaForThisPost(PostModel post) {
710714
// there could be failed media in the post, that has not been registered in the UploadStore because
711715
// the media was being uploaded separately (i.e. the user included media, started uploading within
712716
// the editor, and such media failed _before_ exiting the eidtor, thus the registration never happened.
@@ -740,10 +744,12 @@ private void registerFailedMediaForThisPost(PostModel post) {
740744
}
741745
}
742746

743-
private void aztecRetryUpload(PostModel post) {
747+
private void retryUpload(PostModel post, boolean processWithAztec) {
744748
AnalyticsTracker.track(AnalyticsTracker.Stat.NOTIFICATION_UPLOAD_POST_ERROR_RETRY);
745749

746-
registerFailedMediaForThisPost(post);
750+
if (processWithAztec) {
751+
aztecRegisterFailedMediaForThisPost(post);
752+
}
747753

748754
Set<MediaModel> failedMedia = mUploadStore.getFailedMediaForPost(post);
749755
ArrayList<MediaModel> mediaToRetry = new ArrayList<>(failedMedia);
@@ -755,11 +761,13 @@ private void aztecRetryUpload(PostModel post) {
755761
mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(media));
756762
}
757763

758-
// do the same within the Post content itself
759-
String postContentWithRestartedUploads =
760-
AztecEditorFragment.restartFailedMediaToUploading(this, post.getContent());
761-
post.setContent(postContentWithRestartedUploads);
762-
mDispatcher.dispatch(PostActionBuilder.newUpdatePostAction(post));
764+
if (processWithAztec) {
765+
// do the same within the Post content itself
766+
String postContentWithRestartedUploads =
767+
AztecEditorFragment.restartFailedMediaToUploading(this, post.getContent());
768+
post.setContent(postContentWithRestartedUploads);
769+
mDispatcher.dispatch(PostActionBuilder.newUpdatePostAction(post));
770+
}
763771

764772
// no retry uploading the media items
765773
for (MediaModel media : mediaToRetry) {

libs/editor/WordPressEditor/src/androidTest/java/org.wordpress.android.editor/MockEditorActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.wordpress.android.util.helpers.MediaFile;
1515

1616
import java.util.ArrayList;
17+
import java.util.Set;
1718

1819
public class MockEditorActivity extends AppCompatActivity implements EditorFragmentListener,
1920
EditorDragAndDropListener {
@@ -57,6 +58,10 @@ public boolean onMediaRetryClicked(String mediaId) {
5758
return true;
5859
}
5960

61+
@Override
62+
public void onMediaRetryAllClicked(Set<String> mediaIdSet) {
63+
}
64+
6065
@Override
6166
public void onMediaUploadCancelClicked(String mediaId) {
6267
}

libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragmentAbstract.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.HashMap;
20+
import java.util.Set;
2021

2122
public abstract class EditorFragmentAbstract extends Fragment {
2223
public abstract void setTitle(CharSequence text);
@@ -182,6 +183,7 @@ public interface EditorFragmentListener {
182183
void onAddPhotoClicked();
183184
void onCapturePhotoClicked();
184185
boolean onMediaRetryClicked(String mediaId);
186+
void onMediaRetryAllClicked(Set<String> mediaIdSet);
185187
void onMediaUploadCancelClicked(String mediaId);
186188
void onMediaDeleted(String mediaId);
187189
void onUndoMediaCheck(String undoedContent);

libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import android.app.Activity;
44
import android.arch.lifecycle.LiveData;
55
import android.content.Context;
6+
import android.content.DialogInterface;
7+
import android.content.DialogInterface.OnClickListener;
68
import android.content.res.Configuration;
79
import android.os.Bundle;
810
import android.os.Handler;
911
import android.support.annotation.NonNull;
1012
import android.support.v7.app.ActionBar;
13+
import android.support.v7.app.AlertDialog;
1114
import android.support.v7.app.AppCompatActivity;
1215
import android.text.Editable;
1316
import android.text.Spanned;
17+
import android.view.ContextThemeWrapper;
1418
import android.view.LayoutInflater;
1519
import android.view.Menu;
1620
import android.view.MenuInflater;
@@ -124,10 +128,19 @@ public void onUploadMediaButtonClicked() {
124128
public void onCapturePhotoButtonClicked() {
125129
checkAndRequestCameraAndStoragePermissions();
126130
}
131+
132+
@Override public void onRetryUploadForMediaClicked(int mediaId) {
133+
showRetryMediaUploadDialog(mediaId);
134+
}
135+
136+
@Override public void onCancelUploadForMediaClicked(int mediaId) {
137+
showCancelMediaUploadDialog(mediaId);
138+
}
127139
},
128140
new OnReattachQueryListener() {
129141
@Override
130142
public void onQueryCurrentProgressForUploadingMedia() {
143+
updateFailedMediaState();
131144
updateMediaProgress();
132145
}
133146
},
@@ -170,20 +183,108 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
170183
}
171184
}
172185

186+
public void resetUploadingMediaToFailed(Set<Integer> failedMediaIds) {
187+
// get all media failed for this post, and represent it on tje UI
188+
if (failedMediaIds != null && !failedMediaIds.isEmpty()) {
189+
for (Integer mediaId : failedMediaIds) {
190+
// and keep track of failed ids around
191+
mFailedMediaIds.add(String.valueOf(mediaId));
192+
}
193+
}
194+
}
195+
196+
private void updateFailedMediaState() {
197+
for (String mediaId : mFailedMediaIds) {
198+
mWPAndroidGlueCode.mediaFileUploadFailed(Integer.valueOf(mediaId));
199+
}
200+
}
201+
173202
private void updateMediaProgress() {
174203
for (String mediaId : mUploadingMediaProgressMax.keySet()) {
175204
mWPAndroidGlueCode.mediaFileUploadProgress(Integer.valueOf(mediaId),
176205
mUploadingMediaProgressMax.get(mediaId));
177206
}
178207
}
179-
208+
180209
private void checkAndRequestCameraAndStoragePermissions() {
181210
if (PermissionUtils.checkAndRequestCameraAndStoragePermissions(this,
182211
CAPTURE_PHOTO_PERMISSION_REQUEST_CODE)) {
183212
mEditorFragmentListener.onCapturePhotoClicked();
184213
}
185214
}
186215

216+
private void showCancelMediaUploadDialog(final int localMediaId) {
217+
// Display 'cancel upload' dialog
218+
AlertDialog.Builder builder = new AlertDialog.Builder(
219+
new ContextThemeWrapper(getActivity(), R.style.Calypso_Dialog_Alert));
220+
builder.setTitle(getString(R.string.stop_upload_dialog_title));
221+
builder.setPositiveButton(R.string.stop_upload_dialog_button_yes,
222+
new DialogInterface.OnClickListener() {
223+
public void onClick(DialogInterface dialog, int id) {
224+
if (mUploadingMediaProgressMax.containsKey(String.valueOf(localMediaId))) {
225+
mEditorFragmentListener.onMediaUploadCancelClicked(String.valueOf(localMediaId));
226+
// remove from editor
227+
mEditorFragmentListener.onMediaDeleted(String.valueOf(localMediaId));
228+
mWPAndroidGlueCode.clearMediaFileURL(localMediaId);
229+
mUploadingMediaProgressMax.remove(localMediaId);
230+
} else {
231+
ToastUtils.showToast(getActivity(), R.string.upload_finished_toast).show();
232+
}
233+
dialog.dismiss();
234+
}
235+
});
236+
237+
builder.setNegativeButton(R.string.stop_upload_dialog_button_no, new DialogInterface.OnClickListener() {
238+
public void onClick(DialogInterface dialog, int id) {
239+
dialog.dismiss();
240+
}
241+
});
242+
243+
AlertDialog dialog = builder.create();
244+
dialog.show();
245+
}
246+
247+
private void showRetryMediaUploadDialog(final int mediaId) {
248+
// Display 'retry upload' dialog
249+
AlertDialog.Builder builder = new AlertDialog.Builder(
250+
new ContextThemeWrapper(getActivity(), R.style.Calypso_Dialog_Alert));
251+
builder.setTitle(getString(R.string.retry_failed_upload_title));
252+
builder.setPositiveButton(R.string.retry_failed_upload_yes,
253+
new DialogInterface.OnClickListener() {
254+
public void onClick(DialogInterface dialog, int id) {
255+
dialog.dismiss();
256+
boolean successfullyRetried = true;
257+
if (mFailedMediaIds.contains(String.valueOf(mediaId))) {
258+
successfullyRetried = mEditorFragmentListener.onMediaRetryClicked(String.valueOf(mediaId));
259+
}
260+
if (successfullyRetried) {
261+
mFailedMediaIds.remove(String.valueOf(mediaId));
262+
mUploadingMediaProgressMax.put(String.valueOf(mediaId), 0f);
263+
mWPAndroidGlueCode.mediaFileUploadProgress(mediaId,
264+
mUploadingMediaProgressMax.get(String.valueOf(mediaId)));
265+
}
266+
}
267+
});
268+
269+
builder.setNeutralButton(R.string.retry_failed_upload_retry_all, new OnClickListener() {
270+
@Override public void onClick(DialogInterface dialog, int which) {
271+
dialog.dismiss();
272+
mEditorFragmentListener.onMediaRetryAllClicked(mFailedMediaIds);
273+
}
274+
});
275+
276+
builder.setNegativeButton(R.string.retry_failed_upload_remove, new DialogInterface.OnClickListener() {
277+
public void onClick(DialogInterface dialog, int id) {
278+
dialog.dismiss();
279+
mEditorFragmentListener.onMediaDeleted(String.valueOf(mediaId));
280+
mWPAndroidGlueCode.clearMediaFileURL(mediaId);
281+
}
282+
});
283+
284+
AlertDialog dialog = builder.create();
285+
dialog.show();
286+
}
287+
187288
@Override
188289
public void onPause() {
189290
super.onPause();

libs/editor/WordPressEditor/src/main/res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,10 @@
128128
<string name="stop_upload_dialog_button_yes">Yes</string>
129129
<string name="stop_upload_dialog_button_no">No</string>
130130

131+
<string name="retry_failed_upload_title">Retry uploading?</string>
132+
<string name="retry_failed_upload_yes">Retry</string>
133+
<string name="retry_failed_upload_remove">Remove</string>
134+
<string name="retry_failed_upload_retry_all">Retry all</string>
135+
136+
131137
</resources>

0 commit comments

Comments
 (0)