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] media upload progress reattach #9129

Merged
merged 12 commits into from
Jan 28, 2019
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<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to use interface Set instead of HashSet ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


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