From cea09771d6515d6949f68fdaea31ed1298db0967 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 18 Mar 2021 15:09:04 -0400 Subject: [PATCH] image candidate selection refactor https://t.me/barinsta_app/18199 --- .../DirectItemMediaShareViewHolder.java | 2 +- .../DirectItemMediaViewHolder.java | 4 +- .../DirectItemProfileViewHolder.java | 3 +- .../DirectItemRavenMediaViewHolder.java | 4 +- .../DirectItemReelShareViewHolder.java | 4 +- .../DirectItemStoryShareViewHolder.java | 4 +- .../directmessages/DirectItemViewHolder.java | 6 +-- .../instagrabber/utils/ResponseBodyUtils.java | 41 ++++++++++++------- 8 files changed, 35 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java index d4e63b11..8874e818 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaShareViewHolder.java @@ -89,7 +89,7 @@ public class DirectItemMediaShareViewHolder extends DirectItemViewHolder { private void setupPreview(@NonNull final Media media, final MessageDirection messageDirection) { - final String url = ResponseBodyUtils.getThumbUrl(media.getImageVersions2()); + final String url = ResponseBodyUtils.getThumbUrl(media); if (Objects.equals(url, binding.mediaPreview.getTag())) { return; } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaViewHolder.java index cb32f0f1..24a9e62e 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemMediaViewHolder.java @@ -66,9 +66,7 @@ public class DirectItemMediaViewHolder extends DirectItemViewHolder { binding.mediaPreview.requestLayout(); binding.bgTime.getLayoutParams().width = width; binding.bgTime.requestLayout(); - final ImageVersions2 imageVersions2 = media.getImageVersions2(); - if (imageVersions2 == null) return; - final String thumbUrl = ResponseBodyUtils.getThumbUrl(imageVersions2); + final String thumbUrl = ResponseBodyUtils.getThumbUrl(media); binding.mediaPreview.setImageURI(thumbUrl); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemProfileViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemProfileViewHolder.java index 34727b95..4b55c59f 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemProfileViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemProfileViewHolder.java @@ -86,8 +86,7 @@ public class DirectItemProfileViewHolder extends DirectItemViewHolder { for (int i = 0; i < previewMedias.size(); i++) { final Media previewMedia = previewMedias.get(i); if (previewMedia == null) continue; - final ImageVersions2 imageVersions2 = previewMedia.getImageVersions2(); - final String url = ResponseBodyUtils.getThumbUrl(imageVersions2); + final String url = ResponseBodyUtils.getThumbUrl(previewMedia); if (url == null) continue; previewViews.get(i).setImageURI(url); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemRavenMediaViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemRavenMediaViewHolder.java index 1b68efa6..a8fb8dda 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemRavenMediaViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemRavenMediaViewHolder.java @@ -180,9 +180,7 @@ public class DirectItemRavenMediaViewHolder extends DirectItemViewHolder { layoutParams.width = widthHeight.first != null ? widthHeight.first : 0; layoutParams.height = widthHeight.second != null ? widthHeight.second : 0; binding.preview.requestLayout(); - final ImageVersions2 imageVersions2 = media.getImageVersions2(); - if (imageVersions2 == null) return; - final String thumbUrl = ResponseBodyUtils.getThumbUrl(imageVersions2); + final String thumbUrl = ResponseBodyUtils.getThumbUrl(media); binding.preview.setImageURI(thumbUrl); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java index 7ae6b5d8..bb139332 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemReelShareViewHolder.java @@ -162,9 +162,7 @@ public class DirectItemReelShareViewHolder extends DirectItemViewHolder { binding.preview.setHierarchy(new GenericDraweeHierarchyBuilder(itemView.getResources()) .setRoundingParams(roundingParams) .build()); - final ImageVersions2 imageVersions2 = media.getImageVersions2(); - if (imageVersions2 == null) return; - final String thumbUrl = ResponseBodyUtils.getThumbUrl(imageVersions2); + final String thumbUrl = ResponseBodyUtils.getThumbUrl(media); binding.preview.setImageURI(thumbUrl); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemStoryShareViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemStoryShareViewHolder.java index c5a81993..b6b7bf68 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemStoryShareViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectItemStoryShareViewHolder.java @@ -86,9 +86,7 @@ public class DirectItemStoryShareViewHolder extends DirectItemViewHolder { layoutParams.width = widthHeight.first != null ? widthHeight.first : 0; layoutParams.height = widthHeight.second != null ? widthHeight.second : 0; binding.ivMediaPreview.requestLayout(); - final ImageVersions2 imageVersions2 = storyShareMedia.getImageVersions2(); - if (imageVersions2 == null) return; - final String thumbUrl = ResponseBodyUtils.getThumbUrl(imageVersions2); + final String thumbUrl = ResponseBodyUtils.getThumbUrl(storyShareMedia); binding.ivMediaPreview.setImageURI(thumbUrl); } 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 55b4904a..9580abd8 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 @@ -242,10 +242,10 @@ public abstract class DirectItemViewHolder extends RecyclerView.ViewHolder imple text = replied.getPlaceholder().getMessage(); break; case MEDIA: - url = ResponseBodyUtils.getThumbUrl(replied.getMedia().getImageVersions2()); + url = ResponseBodyUtils.getThumbUrl(replied.getMedia()); break; case RAVEN_MEDIA: - url = ResponseBodyUtils.getThumbUrl(replied.getVisualMedia().getMedia().getImageVersions2()); + url = ResponseBodyUtils.getThumbUrl(replied.getVisualMedia().getMedia()); break; case VOICE_MEDIA: text = resources.getString(R.string.voice_message); @@ -255,7 +255,7 @@ public abstract class DirectItemViewHolder extends RecyclerView.ViewHolder imple if (mediaShare.getMediaType() == MediaItemType.MEDIA_TYPE_SLIDER) { mediaShare = mediaShare.getCarouselMedia().get(0); } - url = ResponseBodyUtils.getThumbUrl(mediaShare.getImageVersions2()); + url = ResponseBodyUtils.getThumbUrl(mediaShare); break; case REEL_SHARE: text = replied.getReelShare().getText(); diff --git a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java index 9a22a9c9..7d25495d 100644 --- a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java @@ -13,6 +13,7 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import awais.instagrabber.BuildConfig; import awais.instagrabber.models.StoryModel; @@ -1094,29 +1095,24 @@ public final class ResponseBodyUtils { } public static String getThumbUrl(final Media media) { - if (media == null) { - return null; - } - final ImageVersions2 imageVersions2 = media.getImageVersions2(); - return getThumbUrl(imageVersions2); - } - - public static String getThumbUrl(final ImageVersions2 imageVersions2) { - if (imageVersions2 == null) return null; - final List candidates = imageVersions2.getCandidates(); - if (candidates == null || candidates.isEmpty()) return null; - final MediaCandidate mediaCandidate = candidates.get(candidates.size() - 1); - if (mediaCandidate == null) return null; - return mediaCandidate.getUrl(); + return getImageCandidate(media, CandidateType.THUMBNAIL); } public static String getImageUrl(final Media media) { + return getImageCandidate(media, CandidateType.DOWNLOAD); + } + + private static String getImageCandidate(final Media media, final CandidateType type) { if (media == null) return null; final ImageVersions2 imageVersions2 = media.getImageVersions2(); if (imageVersions2 == null) return null; final List candidates = imageVersions2.getCandidates(); if (candidates == null || candidates.isEmpty()) return null; - final MediaCandidate candidate = candidates.get(0); + final List sortedCandidates = candidates.stream() + .sorted((c1, c2) -> Integer.compare(c2.getWidth(), c1.getWidth())) + .filter(c -> c.getWidth() < type.getValue()) + .collect(Collectors.toList()); + final MediaCandidate candidate = sortedCandidates.get(0); if (candidate == null) return null; return candidate.getUrl(); } @@ -1133,4 +1129,19 @@ public final class ResponseBodyUtils { model.setVideoUrl(data.getString("dash_playback_url")); return model; } + + private enum CandidateType { + THUMBNAIL(1000), + DOWNLOAD(10000); + + private final int value; + + CandidateType(final int value) { + this.value = value; + } + + public int getValue() { + return value; + } + } }