Skip to content

Commit

Permalink
Merge pull request #9129 from wordpress-mobile/gb/media_upload_progre…
Browse files Browse the repository at this point in the history
…ss_reattach

[Gutenberg] media upload progress reattach
  • Loading branch information
mzorz committed Jan 28, 2019
2 parents 0a98204 + 6cb4c90 commit e56e050
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnGetContentTimeout;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaLibraryButtonListener;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnReattachQueryListener;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class GutenbergEditorFragment extends EditorFragmentAbstract implements
View.OnTouchListener,
Expand All @@ -58,6 +63,9 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements

private WPAndroidGlueCode mWPAndroidGlueCode;

private ConcurrentHashMap<String, Float> mUploadingMediaProgressMax = new ConcurrentHashMap<>();
private Set<String> mFailedMediaIds = new HashSet<>();

private boolean mIsNewPost;

public GutenbergEditorFragment() {
Expand Down Expand Up @@ -112,11 +120,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public void onUploadMediaButtonClicked() {
mEditorFragmentListener.onAddPhotoClicked();
}

@Override
public void onCapturePhotoButtonClicked() {
checkAndRequestCameraAndStoragePermissions();
}
},
new OnReattachQueryListener() {
@Override
public void onQueryCurrentProgressForUploadingMedia() {
updateMediaProgress();
}
},
getActivity().getApplication(),
BuildConfig.DEBUG,
BuildConfig.BUILD_GUTENBERG_FROM_SOURCE,
Expand Down Expand Up @@ -156,6 +171,13 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}

private void updateMediaProgress() {
for (String mediaId : mUploadingMediaProgressMax.keySet()) {
mWPAndroidGlueCode.mediaFileUploadProgress(Integer.valueOf(mediaId),
mUploadingMediaProgressMax.get(mediaId));
}
}

private void checkAndRequestCameraAndStoragePermissions() {
if (PermissionUtils.checkAndRequestCameraAndStoragePermissions(this,
CAPTURE_PHOTO_PERMISSION_REQUEST_CODE)) {
Expand Down Expand Up @@ -314,6 +336,12 @@ private void toggleHtmlMode() {
mEditorFragmentListener.onTrackableEvent(TrackableEvent.HTML_BUTTON_TAPPED);
mEditorFragmentListener.onHtmlModeToggledInToolbar();

// Don't switch to HTML mode if currently uploading media
if (!mUploadingMediaProgressMax.isEmpty() || isActionInProgress()) {
ToastUtils.showToast(getActivity(), R.string.alert_action_while_uploading, ToastUtils.Duration.LONG);
return;
}

mWPAndroidGlueCode.toggleEditorMode();
}

Expand Down Expand Up @@ -412,6 +440,7 @@ public void appendMediaFile(final MediaFile mediaFile, final String mediaUrl, Im
mWPAndroidGlueCode.appendMediaFile(mediaUrl);
} else {
mWPAndroidGlueCode.appendUploadMediaFile(mediaFile.getId(), "file://" + mediaUrl);
mUploadingMediaProgressMax.put(String.valueOf(mediaFile.getId()), 0f);
}
}

Expand All @@ -430,7 +459,7 @@ public boolean isUploadingMedia() {

@Override
public boolean hasFailedMediaUploads() {
return false;
return (mFailedMediaIds.size() > 0);
}

@Override
Expand All @@ -455,28 +484,40 @@ public void setContentPlaceholder(CharSequence placeholderText) {
}

@Override
public void onMediaUploadReattached(String localId, float currentProgress) {
public void onMediaUploadReattached(String localMediaId, float currentProgress) {
mUploadingMediaProgressMax.put(localMediaId, currentProgress);
mWPAndroidGlueCode.mediaFileUploadProgress(Integer.valueOf(localMediaId), currentProgress);
}

@Override
public void onMediaUploadRetry(String localId, MediaType mediaType) {
public void onMediaUploadRetry(String localMediaId, MediaType mediaType) {
if (mFailedMediaIds.contains(localMediaId)) {
mFailedMediaIds.remove(localMediaId);
mUploadingMediaProgressMax.put(localMediaId, 0f);
}

// TODO request to start the upload again from the UploadService
}

@Override
public void onMediaUploadSucceeded(final String localMediaId, final MediaFile mediaFile) {
mUploadingMediaProgressMax.remove(localMediaId);
mWPAndroidGlueCode.mediaFileUploadSucceeded(Integer.valueOf(localMediaId), mediaFile.getFileURL(),
Integer.valueOf(mediaFile.getMediaId()));
}

@Override
public void onMediaUploadProgress(final String localMediaId, final float progress) {
mUploadingMediaProgressMax.put(localMediaId, progress);
mWPAndroidGlueCode.mediaFileUploadProgress(Integer.valueOf(localMediaId), progress);
}

@Override
public void onMediaUploadFailed(final String localMediaId, final MediaType
mediaType, final String errorMessage) {
mWPAndroidGlueCode.mediaFileUploadFailed(Integer.valueOf(localMediaId));
mFailedMediaIds.add(localMediaId);
mUploadingMediaProgressMax.remove(localMediaId);
}

@Override
Expand Down

0 comments on commit e56e050

Please sign in to comment.