From 22a927e9e7a98e2f0938bac9390e34868720ab6b Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 16 Mar 2021 16:51:50 -0400 Subject: [PATCH] open on giphy & download voice; close #792 --- .../directmessages/DirectItemViewHolder.java | 9 +++ .../DirectMessageThreadFragment.java | 67 +++++++++++-------- .../instagrabber/utils/DownloadUtils.java | 42 +++++++++--- app/src/main/res/values/ids.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 82 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java index 8271c38c..55b4904a 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemViewHolder.java @@ -543,6 +543,15 @@ public abstract class DirectItemViewHolder extends RecyclerView.ViewHolder imple if (thread.getInputMode() != 1 && messageDirection == MessageDirection.OUTGOING) { builder.add(new DirectItemContextMenu.MenuItem(R.id.unsend, R.string.dms_inbox_unsend)); } + final DirectItemType itemType = item.getItemType(); + switch (itemType) { + case ANIMATED_MEDIA: + builder.add(new DirectItemContextMenu.MenuItem(R.id.detail, R.string.dms_inbox_giphy)); + break; + case VOICE_MEDIA: + builder.add(new DirectItemContextMenu.MenuItem(R.id.detail, R.string.action_download)); + break; + } final boolean showReactions = thread.getInputMode() != 1 && allowReaction(); final ImmutableList menuItems = builder.build(); if (!showReactions && menuItems.isEmpty()) return; diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 4ce2ef40..387f5eb8 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -30,6 +30,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.LiveData; import androidx.lifecycle.MediatorLiveData; @@ -57,6 +58,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import awais.instagrabber.ProfileNavGraphDirections; import awais.instagrabber.R; @@ -86,6 +88,7 @@ import awais.instagrabber.fragments.PostViewV2Fragment; import awais.instagrabber.fragments.UserSearchFragment; import awais.instagrabber.fragments.UserSearchFragmentDirections; import awais.instagrabber.models.Resource; +import awais.instagrabber.models.enums.DirectItemType; import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.repositories.requests.StoryViewerOptions; import awais.instagrabber.repositories.responses.Media; @@ -97,6 +100,7 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItemVisual import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.RankedRecipient; import awais.instagrabber.utils.AppExecutors; +import awais.instagrabber.utils.DownloadUtils; import awais.instagrabber.utils.PermissionUtils; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.TextUtils; @@ -121,6 +125,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact private ConstraintLayout root; private boolean shouldRefresh = true; private List itemOrHeaders; + private List users; private FragmentDirectMessagesThreadBinding binding; private Tooltip tooltip; private float initialSendX; @@ -258,6 +263,19 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact final NavController navController = NavHostFragment.findNavController(DirectMessageThreadFragment.this); navController.navigate(actionGlobalUserSearch); } + if (itemId == R.id.detail) { + final Context context = getContext(); + if (context == null) return; + final DirectItemType itemType = item.getItemType(); + switch (itemType) { + case ANIMATED_MEDIA: + Utils.openURL(context, "https://giphy.com/gifs/" + item.getAnimatedMedia().getId()); + break; + case VOICE_MEDIA: + downloadItem(item.getVoiceMedia() == null ? null : item.getVoiceMedia().getMedia(), context); + break; + } + } } }; private final DirectItemLongClickListener directItemLongClickListener = position -> { @@ -906,6 +924,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact itemsAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY); binding.chats.setAdapter(itemsAdapter); registerDataObserver(); + users = thread.getUsers(); final List items = viewModel.getItems().getValue(); if (items != null && itemsAdapter.getItems() != items) { submitItemsToAdapter(items); @@ -1230,25 +1249,19 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact appExecutors.mainThread().execute(prevTitleRunnable, 1000); } - // private void downloadItem(final Context context) { - // final DirectUser user = getUser(directItemModel.getUserId()); - // final DirectItemMediaModel selectedItem = directItemModel.getItemType() == DirectItemType.MEDIA - // ? (DirectItemMediaModel) directItemModel.getMediaModel() - // : ((DirectItemRavenMediaModel) directItemModel.getMediaModel()).getMedia(); - // final String url = selectedItem.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO - // ? selectedItem.getVideoUrl() - // : selectedItem.getThumbUrl(); - // if (url == null) { - // Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - // } else { - // if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { - // DownloadUtils.dmDownload(context, user != null ? user.getUsername() : "", selectedItem.getId(), url); - // } else { - // requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); - // } - // Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show(); - // } - // } + // currently ONLY for voice + private void downloadItem(final Media media, final Context context) { + if (media == null) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + } else { + if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) { + DownloadUtils.download(context, media); + } else { + requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); + } + Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show(); + } + } // private void sendText(final String text, final String itemId, final boolean delete) { // DirectThreadBroadcaster.TextBroadcastOptions textOptions = null; @@ -1339,14 +1352,14 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact // broadcaster.execute(broadcastOptions); // } - // @NonNull - // private DirectUser getUser(final long userId) { - // for (final DirectUser user : users) { - // if (userId != user.getPk()) continue; - // return user; - // } - // return null; - // } + @NonNull + private User getUser(final long userId) { + for (final User user : users) { + if (userId != user.getPk()) continue; + return user; + } + return null; + } // private void searchUsername(final String text) { // final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java index 89f3bd42..dbd14027 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java @@ -39,6 +39,7 @@ import java.util.regex.Pattern; import awais.instagrabber.R; import awais.instagrabber.models.StoryModel; import awais.instagrabber.models.enums.MediaItemType; +import awais.instagrabber.repositories.responses.Audio; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.VideoVersion; @@ -89,17 +90,17 @@ public final class DownloadUtils { return dir; } - public static void dmDownload(@NonNull final Context context, - @Nullable final String username, - final String modelId, - final String url) { - if (url == null) return; - if (ContextCompat.checkSelfPermission(context, PERMS[0]) == PackageManager.PERMISSION_GRANTED) { - dmDownloadImpl(context, username, modelId, url); - } else if (context instanceof Activity) { - ActivityCompat.requestPermissions((Activity) context, PERMS, 8020); - } - } +// public static void dmDownload(@NonNull final Context context, +// @Nullable final String username, +// final String modelId, +// final String url) { +// if (url == null) return; +// if (ContextCompat.checkSelfPermission(context, PERMS[0]) == PackageManager.PERMISSION_GRANTED) { +// dmDownloadImpl(context, username, modelId, url); +// } else if (context instanceof Activity) { +// ActivityCompat.requestPermissions((Activity) context, PERMS, 8020); +// } +// } private static void dmDownloadImpl(@NonNull final Context context, @Nullable final String username, @@ -303,6 +304,17 @@ public final class DownloadUtils { map.put(url, file.getAbsolutePath()); break; } + case MEDIA_TYPE_VOICE: { + final String url = getUrlOfType(media); + String fileName = media.getId(); + final User user = media.getUser(); + if (user != null) { + fileName = user.getUsername() + "_" + fileName; + } + final File file = getDownloadSaveFile(downloadDir, fileName, url); + map.put(url, file.getAbsolutePath()); + break; + } case MEDIA_TYPE_SLIDER: final List sliderItems = media.getCarouselMedia(); for (int i = 0; i < sliderItems.size(); i++) { @@ -338,6 +350,14 @@ public final class DownloadUtils { } return url; } + case MEDIA_TYPE_VOICE: { + final Audio audio = media.getAudio(); + String url = null; + if (audio != null) { + url = audio.getAudioSrc(); + } + return url; + } } return null; } diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 26a474c0..ecb381cf 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index da8ae5ea..296b1324 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -180,6 +180,7 @@ Unlike message Unsend message View author profile + View on GIPHY %s shared a post by @%s %s shared an image %s shared a video