Skip to content

Commit

Permalink
Merge pull request #6 from shiki/issue/8627-giphy-picker-editor-import
Browse files Browse the repository at this point in the history
Import from Giphy in Post Editor
  • Loading branch information
kwonye authored Dec 13, 2018
2 parents a489172 + 174629e commit ac38d5f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public enum PhotoPickerIcon {
ANDROID_CAPTURE_PHOTO,
ANDROID_CAPTURE_VIDEO,
WP_MEDIA,
STOCK_MEDIA
STOCK_MEDIA,
GIPHY
}

/*
Expand Down Expand Up @@ -243,6 +244,10 @@ public void doIconClicked(@NonNull PhotoPickerIcon icon) {
case WP_MEDIA:
AnalyticsTracker.track(AnalyticsTracker.Stat.MEDIA_PICKER_OPEN_WP_MEDIA);
break;
case STOCK_MEDIA:
break;
case GIPHY:
break;
}

if (mListener != null) {
Expand Down Expand Up @@ -282,6 +287,14 @@ public boolean onMenuItemClick(MenuItem item) {
return true;
}
});

MenuItem itemGiphy = popup.getMenu().add(R.string.photo_picker_giphy);
itemGiphy.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override public boolean onMenuItemClick(MenuItem item) {
doIconClicked(PhotoPickerIcon.GIPHY);
return true;
}
});
}

popup.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import org.wordpress.android.ui.RequestCodes;
import org.wordpress.android.ui.Shortcut;
import org.wordpress.android.ui.accounts.HelpActivity.Origin;
import org.wordpress.android.ui.giphy.GiphyPickerActivity;
import org.wordpress.android.ui.history.HistoryListItem.Revision;
import org.wordpress.android.ui.media.MediaBrowserActivity;
import org.wordpress.android.ui.media.MediaBrowserType;
Expand Down Expand Up @@ -998,6 +999,9 @@ public void onPhotoPickerIconClicked(@NonNull PhotoPickerIcon icon) {
ActivityLauncher.showStockMediaPickerForResult(
this, mSite, RequestCodes.STOCK_MEDIA_PICKER_MULTI_SELECT);
break;
case GIPHY:
ActivityLauncher.showGiphyPickerForResult(this, mSite, RequestCodes.GIPHY_PICKER);
break;
}
}

Expand Down Expand Up @@ -2777,6 +2781,27 @@ public void done() {
savePostAsync(null);
}
break;
case RequestCodes.GIPHY_PICKER:
if (data.hasExtra(GiphyPickerActivity.KEY_SAVED_MEDIA_MODEL_LOCAL_IDS)) {
int[] localIds = data.getIntArrayExtra(GiphyPickerActivity.KEY_SAVED_MEDIA_MODEL_LOCAL_IDS);
ArrayList<MediaModel> mediaModels = new ArrayList<>();
for (int localId : localIds) {
mediaModels.add(mMediaStore.getMediaWithLocalId(localId));
}

if (isModernEditor()) {
startUploadService(mediaModels);
}

for (MediaModel mediaModel : mediaModels) {
mediaModel.setLocalPostId(mPost.getId());
mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(mediaModel));

MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(mediaModel);
mEditorFragment.appendMediaFile(mediaFile, mediaFile.getFilePath(), mImageLoader);
}
}
break;
case RequestCodes.HISTORY_DETAIL:
if (data.hasExtra(KEY_REVISION)) {
mViewPager.setCurrentItem(PAGE_CONTENT);
Expand Down Expand Up @@ -2980,19 +3005,30 @@ public void onMediaChanged(OnMediaChanged event) {
* Starts the upload service to upload selected media.
*/
private void startUploadService(MediaModel media) {
final ArrayList<MediaModel> mediaList = new ArrayList<>();
mediaList.add(media);
startUploadService(mediaList);
}

/**
* Start the {@link UploadService} to upload the given {@code mediaModels}.
*
* Only {@link MediaModel} objects that have {@code MediaUploadState.QUEUED} statuses will be uploaded. .
*/
private void startUploadService(@NonNull List<MediaModel> mediaModels) {
// make sure we only pass items with the QUEUED state to the UploadService
if (!MediaUploadState.QUEUED.toString().equals(media.getUploadState())) {
return;
final ArrayList<MediaModel> queuedMediaModels = new ArrayList<>();
for (MediaModel media : mediaModels) {
if (MediaUploadState.QUEUED.toString().equals(media.getUploadState())) {
queuedMediaModels.add(media);
}
}

final ArrayList<MediaModel> mediaList = new ArrayList<>();
mediaList.add(media);
// before starting the service, we need to update the posts' contents so we are sure the service
// can retrieve it from there on
savePostAsync(new AfterSavePostListener() {
@Override
public void onPostSave() {
UploadService.uploadMediaFromEditor(EditPostActivity.this, mediaList);
@Override public void onPostSave() {
UploadService.uploadMediaFromEditor(EditPostActivity.this, queuedMediaModels);
}
});
}
Expand Down

0 comments on commit ac38d5f

Please sign in to comment.