image candidate selection refactor

https://t.me/barinsta_app/18199
This commit is contained in:
Austin Huang 2021-03-18 15:09:04 -04:00
parent 85953352b9
commit cea09771d6
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
8 changed files with 35 additions and 33 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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<MediaCandidate> 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<MediaCandidate> candidates = imageVersions2.getCandidates();
if (candidates == null || candidates.isEmpty()) return null;
final MediaCandidate candidate = candidates.get(0);
final List<MediaCandidate> 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;
}
}
}