Skip to content

Commit

Permalink
Merge pull request #2381 from wordpress-mobile/try/seamless-media-com…
Browse files Browse the repository at this point in the history
…pletion

[Android] Seamless media upload completion
  • Loading branch information
mkevins authored Jun 18, 2020
2 parents 88b2737 + 7eb08b8 commit a6e1740
Show file tree
Hide file tree
Showing 7 changed files with 535 additions and 114 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
------
* [**] Add support for customizing gradient type and angle in Buttons and Cover blocks.
* [*] Show content information (block, word and characters counts).
* [*] [Android] Fix handling of upload completion while re-opening the editor

1.30.0
------
Expand Down
16 changes: 8 additions & 8 deletions android/app/src/main/java/com/gutenberg/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ public void responseHtml(String title, String html, boolean changed, ReadableMap
}

@Override
public void requestMediaImport(String url, MediaUploadCallback mediaUploadCallback) {
public void requestMediaImport(String url, MediaSelectedCallback mediaSelectedCallback) {
}

@Override
public void requestMediaPickerFromDeviceCamera(MediaUploadCallback mediaUploadCallback, MediaType mediaType) {
public void requestMediaPickerFromDeviceCamera(MediaSelectedCallback mediaSelectedCallback, MediaType mediaType) {
}

@Override
public void requestMediaPickFromDeviceLibrary(MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection, MediaType mediaType) {
public void requestMediaPickFromDeviceLibrary(MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection, MediaType mediaType) {
}

@Override
public void requestMediaPickFromMediaLibrary(MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection, MediaType mediaType) {
public void requestMediaPickFromMediaLibrary(MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection, MediaType mediaType) {
List<RNMedia> rnMediaList = new ArrayList<>();
if (mediaType == MediaType.IMAGE) {
rnMediaList.add(new Media(1, "https://cldup.com/cXyG__fTLN.jpg", "image", "Mountain" ));
} else if (mediaType == MediaType.VIDEO) {
rnMediaList.add(new Media(2, "https://i.cloudup.com/YtZFJbuQCE.mov", "video", "Cloudup" ));
}
mediaUploadCallback.onUploadMediaFileSelected(rnMediaList);
mediaSelectedCallback.onMediaFileSelected(rnMediaList);
}


@Override
public void mediaUploadSync(MediaUploadCallback mediaUploadCallback) {
public void mediaUploadSync(MediaSelectedCallback mediaSelectedCallback) {
}

@Override
Expand Down Expand Up @@ -103,7 +103,7 @@ public void getOtherMediaPickerOptions(OtherMediaOptionsReceivedCallback otherMe
}

@Override
public void requestMediaPickFrom(String mediaSource, MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection) {
public void requestMediaPickFrom(String mediaSource, MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection) {

}

Expand All @@ -113,7 +113,7 @@ public void requestImageFullscreenPreview(String mediaUrl) {
}

@Override
public void requestMediaEditor(MediaUploadCallback mediaUploadCallback, String mediaUrl) {
public void requestMediaEditor(MediaSelectedCallback mediaSelectedCallback, String mediaUrl) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ interface OtherMediaOptionsReceivedCallback {
void onOtherMediaOptionsReceived(ArrayList<MediaOption> mediaList);
}

interface MediaUploadCallback {
void onUploadMediaFileSelected(List<RNMedia> mediaList);
interface MediaSelectedCallback {
void onMediaFileSelected(List<RNMedia> mediaList);
}

interface MediaUploadEventEmitter {
void onUploadMediaFileClear(int mediaId);
void onMediaFileUploadProgress(int mediaId, float progress);
void onMediaFileUploadSucceeded(int mediaId, String mediaUrl, int serverId);
Expand Down Expand Up @@ -109,15 +112,15 @@ public static GutenbergUserEvent getEnum(String eventName) {
}
}

void requestMediaPickFromMediaLibrary(MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection, MediaType mediaType);
void requestMediaPickFromMediaLibrary(MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection, MediaType mediaType);

void requestMediaPickFromDeviceLibrary(MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection, MediaType mediaType);
void requestMediaPickFromDeviceLibrary(MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection, MediaType mediaType);

void requestMediaPickerFromDeviceCamera(MediaUploadCallback mediaUploadCallback, MediaType mediaType);
void requestMediaPickerFromDeviceCamera(MediaSelectedCallback mediaSelectedCallback, MediaType mediaType);

void requestMediaImport(String url, MediaUploadCallback mediaUploadCallback);
void requestMediaImport(String url, MediaSelectedCallback mediaSelectedCallback);

void mediaUploadSync(MediaUploadCallback mediaUploadCallback);
void mediaUploadSync(MediaSelectedCallback mediaSelectedCallback);

void requestImageFailedRetryDialog(int mediaId);

Expand All @@ -131,11 +134,11 @@ public static GutenbergUserEvent getEnum(String eventName) {

void getOtherMediaPickerOptions(OtherMediaOptionsReceivedCallback otherMediaOptionsReceivedCallback, MediaType mediaType);

void requestMediaPickFrom(String mediaSource, MediaUploadCallback mediaUploadCallback, Boolean allowMultipleSelection);
void requestMediaPickFrom(String mediaSource, MediaSelectedCallback mediaSelectedCallback, Boolean allowMultipleSelection);

void requestImageFullscreenPreview(String mediaUrl);

void requestMediaEditor(MediaUploadCallback mediaUploadCallback, String mediaUrl);
void requestMediaEditor(MediaSelectedCallback mediaSelectedCallback, String mediaUrl);

void logUserEvent(GutenbergUserEvent gutenbergUserEvent, ReadableMap eventProperties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.MediaType;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.OtherMediaOptionsReceivedCallback;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.StarterPageTemplatesTooltipShownCallback;
import org.wordpress.mobile.WPAndroidGlue.DeferredEventEmitter;
import org.wordpress.mobile.WPAndroidGlue.MediaOption;

import java.io.Serializable;
Expand All @@ -30,15 +31,15 @@
import java.util.List;
import java.util.Map;

public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModule {
public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModule implements
DeferredEventEmitter.JSEventEmitter {
private final ReactApplicationContext mReactContext;
private final GutenbergBridgeJS2Parent mGutenbergBridgeJS2Parent;

private static final String EVENT_NAME_REQUEST_GET_HTML = "requestGetHtml";
private static final String EVENT_NAME_UPDATE_HTML = "updateHtml";
private static final String EVENT_NAME_UPDATE_TITLE = "setTitle";
private static final String EVENT_NAME_FOCUS_TITLE = "setFocusOnTitle";
private static final String EVENT_NAME_MEDIA_UPLOAD = "mediaUpload";
private static final String EVENT_NAME_MEDIA_APPEND = "mediaAppend";
private static final String EVENT_NAME_TOGGLE_HTML_MODE = "toggleHTMLMode";
private static final String EVENT_NAME_NOTIFY_MODAL_CLOSED = "notifyModalClosed";
Expand All @@ -48,28 +49,17 @@ public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModu

private static final String MAP_KEY_UPDATE_HTML = "html";
private static final String MAP_KEY_UPDATE_TITLE = "title";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_STATE = "state";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_ID = "mediaId";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_URL = "mediaUrl";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_TYPE = "mediaType";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_PROGRESS = "progress";
private static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_SERVER_ID = "mediaServerId";
public static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_ID = "mediaId";
public static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_URL = "mediaUrl";
public static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_TYPE = "mediaType";
private static final String MAP_KEY_THEME_UPDATE_COLORS = "colors";
private static final String MAP_KEY_THEME_UPDATE_GRADIENTS = "gradients";

private static final String MAP_KEY_IS_PREFERRED_COLOR_SCHEME_DARK = "isPreferredColorSchemeDark";

private static final int MEDIA_UPLOAD_STATE_UPLOADING = 1;
private static final int MEDIA_UPLOAD_STATE_SUCCEEDED = 2;
private static final int MEDIA_UPLOAD_STATE_FAILED = 3;
private static final int MEDIA_UPLOAD_STATE_RESET = 4;

private static final int MEDIA_SERVER_ID_UNKNOWN = 0;

private static final String MEDIA_SOURCE_MEDIA_LIBRARY = "SITE_MEDIA_LIBRARY";
private static final String MEDIA_SOURCE_DEVICE_LIBRARY = "DEVICE_MEDIA_LIBRARY";
private static final String MEDIA_SOURCE_DEVICE_CAMERA = "DEVICE_CAMERA";
private static final String MEDIA_SOURCE_MEDIA_EDITOR = "MEDIA_EDITOR";

private static final String MAP_KEY_REPLACE_BLOCK_HTML = "html";
private static final String MAP_KEY_REPLACE_BLOCK_BLOCK_ID = "clientId";
Expand All @@ -89,15 +79,15 @@ public String getName() {
return "RNReactNativeGutenbergBridge";
}


@Override
public Map<String, Object> getConstants() {
final HashMap<String, Object> constants = new HashMap<>();
constants.put("isInitialColorSchemeDark", mIsDarkMode);
return constants;
}

private void emitToJS(String eventName, @Nullable WritableMap data) {
@Override
public void emitToJS(String eventName, @Nullable WritableMap data) {
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
}

Expand Down Expand Up @@ -165,16 +155,16 @@ public void editorDidMount(ReadableArray unsupportedBlockNames) {
}

@ReactMethod
public void requestMediaPickFrom(String mediaSource, ReadableArray filter, Boolean allowMultipleSelection, final Callback onUploadMediaSelected) {
public void requestMediaPickFrom(String mediaSource, ReadableArray filter, Boolean allowMultipleSelection, final Callback onMediaSelected) {
MediaType mediaType = getMediaTypeFromFilter(filter);
if (mediaSource.equals(MEDIA_SOURCE_MEDIA_LIBRARY)) {
mGutenbergBridgeJS2Parent.requestMediaPickFromMediaLibrary(getNewUploadMediaCallback(allowMultipleSelection, onUploadMediaSelected), allowMultipleSelection, mediaType);
mGutenbergBridgeJS2Parent.requestMediaPickFromMediaLibrary(getNewMediaSelectedCallback(allowMultipleSelection, onMediaSelected), allowMultipleSelection, mediaType);
} else if (mediaSource.equals(MEDIA_SOURCE_DEVICE_LIBRARY)) {
mGutenbergBridgeJS2Parent.requestMediaPickFromDeviceLibrary(getNewUploadMediaCallback(allowMultipleSelection, onUploadMediaSelected), allowMultipleSelection, mediaType);
mGutenbergBridgeJS2Parent.requestMediaPickFromDeviceLibrary(getNewMediaSelectedCallback(allowMultipleSelection, onMediaSelected), allowMultipleSelection, mediaType);
} else if (mediaSource.equals(MEDIA_SOURCE_DEVICE_CAMERA)) {
mGutenbergBridgeJS2Parent.requestMediaPickerFromDeviceCamera(getNewUploadMediaCallback(allowMultipleSelection, onUploadMediaSelected), mediaType);
mGutenbergBridgeJS2Parent.requestMediaPickerFromDeviceCamera(getNewMediaSelectedCallback(allowMultipleSelection, onMediaSelected), mediaType);
} else {
mGutenbergBridgeJS2Parent.requestMediaPickFrom(mediaSource, getNewUploadMediaCallback(allowMultipleSelection, onUploadMediaSelected), allowMultipleSelection);
mGutenbergBridgeJS2Parent.requestMediaPickFrom(mediaSource, getNewMediaSelectedCallback(allowMultipleSelection, onMediaSelected), allowMultipleSelection);
}
}

Expand All @@ -197,12 +187,12 @@ private MediaType getMediaTypeFromFilter(ReadableArray filter) {

@ReactMethod
public void requestMediaImport(String url, final Callback onUploadMediaSelected) {
mGutenbergBridgeJS2Parent.requestMediaImport(url, getNewUploadMediaCallback(false, onUploadMediaSelected));
mGutenbergBridgeJS2Parent.requestMediaImport(url, getNewMediaSelectedCallback(false, onUploadMediaSelected));
}

@ReactMethod
public void mediaUploadSync() {
mGutenbergBridgeJS2Parent.mediaUploadSync(getNewUploadMediaCallback(false,null));
mGutenbergBridgeJS2Parent.mediaUploadSync(getNewMediaSelectedCallback(false,null));
}

@ReactMethod
Expand All @@ -227,7 +217,7 @@ public void requestImageFullscreenPreview(String mediaUrl) {

@ReactMethod
public void requestMediaEditor(String mediaUrl, final Callback onUploadMediaSelected) {
mGutenbergBridgeJS2Parent.requestMediaEditor(getNewUploadMediaCallback(false, onUploadMediaSelected), mediaUrl);
mGutenbergBridgeJS2Parent.requestMediaEditor(getNewMediaSelectedCallback(false, onUploadMediaSelected), mediaUrl);
}

@ReactMethod
Expand Down Expand Up @@ -316,10 +306,10 @@ private StarterPageTemplatesTooltipShownCallback requestStarterPageTemplatesTool
};
}

private GutenbergBridgeJS2Parent.MediaUploadCallback getNewUploadMediaCallback(final Boolean allowMultipleSelection, final Callback jsCallback) {
return new GutenbergBridgeJS2Parent.MediaUploadCallback() {
private GutenbergBridgeJS2Parent.MediaSelectedCallback getNewMediaSelectedCallback(final Boolean allowMultipleSelection, final Callback jsCallback) {
return new GutenbergBridgeJS2Parent.MediaSelectedCallback() {
@Override
public void onUploadMediaFileSelected(List<RNMedia> mediaList) {
public void onMediaFileSelected(List<RNMedia> mediaList) {
if (allowMultipleSelection) {
WritableArray writableArray = new WritableNativeArray();
for (RNMedia media : mediaList) {
Expand All @@ -335,42 +325,9 @@ public void onUploadMediaFileSelected(List<RNMedia> mediaList) {
}
}

@Override public void onUploadMediaFileClear(int mediaId) {
setMediaFileUploadDataInJS(MEDIA_UPLOAD_STATE_RESET, mediaId, null, 0);
}

@Override
public void onMediaFileUploadProgress(int mediaId, float progress) {
setMediaFileUploadDataInJS(MEDIA_UPLOAD_STATE_UPLOADING, mediaId, null, progress);
}

@Override
public void onMediaFileUploadSucceeded(int mediaId, String mediaUrl, int mediaServerId) {
setMediaFileUploadDataInJS(MEDIA_UPLOAD_STATE_SUCCEEDED, mediaId, mediaUrl, 1, mediaServerId);
}

@Override
public void onMediaFileUploadFailed(int mediaId) {
setMediaFileUploadDataInJS(MEDIA_UPLOAD_STATE_FAILED, mediaId, null, 0);
}
};
}

private void setMediaFileUploadDataInJS(int state, int mediaId, String mediaUrl, float progress) {
setMediaFileUploadDataInJS(state, mediaId, mediaUrl, progress, MEDIA_SERVER_ID_UNKNOWN);
}

private void setMediaFileUploadDataInJS(int state, int mediaId, String mediaUrl, float progress, int mediaServerId) {
WritableMap writableMap = new WritableNativeMap();
writableMap.putInt(MAP_KEY_MEDIA_FILE_UPLOAD_STATE, state);
writableMap.putInt(MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_ID, mediaId);
writableMap.putString(MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_URL, mediaUrl);
writableMap.putDouble(MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_PROGRESS, progress);
if (mediaServerId != MEDIA_SERVER_ID_UNKNOWN) {
writableMap.putInt(MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_SERVER_ID, mediaServerId);
}
emitToJS(EVENT_NAME_MEDIA_UPLOAD, writableMap);
}

public void toggleEditorMode() {
emitToJS(EVENT_NAME_TOGGLE_HTML_MODE, null);
Expand Down
Loading

0 comments on commit a6e1740

Please sign in to comment.