Parse all the previews in a post to prepare to add data saving mode. Load lower resolution preview if the current preview require 65MB memory.

This commit is contained in:
Alex Ning 2020-09-27 12:15:48 +08:00
parent 3ce0d41b5c
commit c4f8c91944
6 changed files with 511 additions and 368 deletions

View File

@ -37,6 +37,8 @@ import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -257,32 +259,38 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
contentTextView.setVisibility(View.VISIBLE); contentTextView.setVisibility(View.VISIBLE);
contentTextView.setText(post.getUrl()); contentTextView.setText(post.getUrl());
} else if (post.getPostType() == Post.GIF_TYPE || post.getPostType() == Post.GALLERY_TYPE || post.getPostType() == Post.IMAGE_TYPE) { } else if (post.getPostType() == Post.GIF_TYPE || post.getPostType() == Post.GALLERY_TYPE || post.getPostType() == Post.IMAGE_TYPE) {
frameLayout.setVisibility(View.VISIBLE); Post.Preview preview = getPreview(post);
mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget<Bitmap>() { if (preview != null) {
@Override frameLayout.setVisibility(View.VISIBLE);
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { mGlide.asBitmap().load(preview.getPreviewUrl()).into(new CustomTarget<Bitmap>() {
imageView.setImage(ImageSource.bitmap(resource)); @Override
} public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImage(ImageSource.bitmap(resource));
}
@Override @Override
public void onLoadCleared(@Nullable Drawable placeholder) { public void onLoadCleared(@Nullable Drawable placeholder) {
} }
}); });
}
} else if (post.getPostType() == Post.VIDEO_TYPE) { } else if (post.getPostType() == Post.VIDEO_TYPE) {
frameLayout.setVisibility(View.VISIBLE); Post.Preview preview = getPreview(post);
mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget<Bitmap>() { if (preview != null) {
@Override frameLayout.setVisibility(View.VISIBLE);
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { mGlide.asBitmap().load(preview.getPreviewUrl()).into(new CustomTarget<Bitmap>() {
imageView.setImage(ImageSource.bitmap(resource)); @Override
} public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImage(ImageSource.bitmap(resource));
}
@Override @Override
public void onLoadCleared(@Nullable Drawable placeholder) { public void onLoadCleared(@Nullable Drawable placeholder) {
} }
}); });
playButton.setVisibility(View.VISIBLE); playButton.setVisibility(View.VISIBLE);
}
} }
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
@ -356,6 +364,16 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
}); });
} }
@Nullable
private Post.Preview getPreview(Post post) {
ArrayList<Post.Preview> previews = post.getPreviews();
if (previews != null && !previews.isEmpty()) {
return previews.get(0);
}
return null;
}
@Override @Override
protected SharedPreferences getDefaultSharedPreferences() { protected SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences; return mSharedPreferences;

View File

@ -829,12 +829,17 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} }
if (holder instanceof PostDetailVideoAutoplayViewHolder) { if (holder instanceof PostDetailVideoAutoplayViewHolder) {
((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) mPost.getPreviewWidth() / mPost.getPreviewHeight());
((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE); ((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE);
if (mImageViewWidth > mPost.getPreviewWidth()) { Post.Preview preview = getSuitablePreview(mPost.getPreviews());
mGlide.load(mPost.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); if (preview != null) {
((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight());
if (mImageViewWidth > preview.getPreviewWidth()) {
mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView);
} else {
mGlide.load(preview.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView);
}
} else { } else {
mGlide.load(mPost.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1);
} }
((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); ((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f);
@ -867,8 +872,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} else { } else {
((PostDetailVideoAndGifPreviewHolder) holder).mTypeTextView.setText(mActivity.getString(R.string.video)); ((PostDetailVideoAndGifPreviewHolder) holder).mTypeTextView.setText(mActivity.getString(R.string.video));
} }
((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth()); Post.Preview preview = getSuitablePreview(mPost.getPreviews());
loadImage((PostDetailVideoAndGifPreviewHolder) holder); if (preview != null) {
((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / (float) preview.getPreviewWidth());
loadImage((PostDetailVideoAndGifPreviewHolder) holder, preview);
}
} else if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) { } else if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) {
if (mPost.getPostType() == Post.IMAGE_TYPE) { if (mPost.getPostType() == Post.IMAGE_TYPE) {
((PostDetailImageAndGifAutoplayViewHolder) holder).mTypeTextView.setText(R.string.image); ((PostDetailImageAndGifAutoplayViewHolder) holder).mTypeTextView.setText(R.string.image);
@ -876,18 +884,25 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailImageAndGifAutoplayViewHolder) holder).mTypeTextView.setText(R.string.gif); ((PostDetailImageAndGifAutoplayViewHolder) holder).mTypeTextView.setText(R.string.gif);
} }
if (mPost.getPreviewWidth() <= 0 || mPost.getPreviewHeight() <= 0) { Post.Preview preview = getSuitablePreview(mPost.getPreviews());
((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); if (preview != null) {
((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.getLayoutParams().height = (int) (400 * mScale); if (preview.getPreviewWidth() <= 0 || preview.getPreviewHeight() <= 0) {
} else { ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth()); ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.getLayoutParams().height = (int) (400 * mScale);
} else {
((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / (float) preview.getPreviewWidth());
}
loadImage((PostDetailImageAndGifAutoplayViewHolder) holder, preview);
} }
loadImage((PostDetailImageAndGifAutoplayViewHolder) holder);
} else if (holder instanceof PostDetailLinkViewHolder) { } else if (holder instanceof PostDetailLinkViewHolder) {
String domain = Uri.parse(mPost.getUrl()).getHost(); String domain = Uri.parse(mPost.getUrl()).getHost();
((PostDetailLinkViewHolder) holder).mLinkTextView.setText(domain); ((PostDetailLinkViewHolder) holder).mLinkTextView.setText(domain);
((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth()); Post.Preview preview = getSuitablePreview(mPost.getPreviews());
loadImage((PostDetailLinkViewHolder) holder); if (preview != null) {
((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / (float) preview.getPreviewWidth());
loadImage((PostDetailLinkViewHolder) holder, preview);
}
} else if (holder instanceof PostDetailNoPreviewLinkViewHolder) { } else if (holder instanceof PostDetailNoPreviewLinkViewHolder) {
String noPreviewLinkDomain = Uri.parse(mPost.getUrl()).getHost(); String noPreviewLinkDomain = Uri.parse(mPost.getUrl()).getHost();
((PostDetailNoPreviewLinkViewHolder) holder).mLinkTextView.setText(noPreviewLinkDomain); ((PostDetailNoPreviewLinkViewHolder) holder).mLinkTextView.setText(noPreviewLinkDomain);
@ -911,12 +926,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mMarkwonAdapter.notifyDataSetChanged(); mMarkwonAdapter.notifyDataSetChanged();
} }
} else if (holder instanceof PostDetailGalleryViewHolder) { } else if (holder instanceof PostDetailGalleryViewHolder) {
if (mPost.getPreviewUrl() != null && !mPost.getPreviewUrl().equals("")) { Post.Preview preview = getSuitablePreview(mPost.getPreviews());
if (preview != null) {
((PostDetailGalleryViewHolder) holder).mRelativeLayout.setVisibility(View.VISIBLE); ((PostDetailGalleryViewHolder) holder).mRelativeLayout.setVisibility(View.VISIBLE);
((PostDetailGalleryViewHolder) holder).mImageView ((PostDetailGalleryViewHolder) holder).mImageView
.setRatio((float) mPost.getPreviewHeight() / mPost.getPreviewWidth()); .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
loadImage((PostDetailGalleryViewHolder) holder); loadImage((PostDetailGalleryViewHolder) holder, preview);
} else { } else {
((PostDetailGalleryViewHolder) holder).mNoPreviewLinkImageView.setVisibility(View.VISIBLE); ((PostDetailGalleryViewHolder) holder).mNoPreviewLinkImageView.setVisibility(View.VISIBLE);
} }
@ -1381,9 +1397,43 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} }
} }
private void loadImage(PostDetailBaseViewHolder holder) { @Nullable
private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) {
Post.Preview preview;
if (!previews.isEmpty()) {
preview = previews.get(0);
if (preview.getPreviewWidth() * preview.getPreviewHeight() >= 65 * 1000 * 1000) {
for (int i = previews.size() - 1; i >= 1; i--) {
preview = previews.get(i);
if (mImageViewWidth >= preview.getPreviewWidth()) {
if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 75 * 1000 * 1000) {
return preview;
}
} else {
int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight();
if (mImageViewWidth * height <= 75 * 1000 * 1000) {
return preview;
}
}
}
}
int divisor = 2;
while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 75 * 1000 * 1000) {
preview.setPreviewWidth(preview.getPreviewWidth() / divisor);
preview.setPreviewHeight(preview.getPreviewHeight() / divisor);
divisor *= 2;
}
return preview;
}
return null;
}
private void loadImage(PostDetailBaseViewHolder holder, @NonNull Post.Preview preview) {
if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) { if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) {
String url = mPost.getPostType() == Post.IMAGE_TYPE ? mPost.getPreviewUrl() : mPost.getUrl(); String url = mPost.getPostType() == Post.IMAGE_TYPE ? preview.getPreviewUrl() : mPost.getUrl();
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url)
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
@ -1393,7 +1443,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> {
((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE);
((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE);
loadImage(holder); loadImage(holder, preview);
}); });
return false; return false;
} }
@ -1408,14 +1458,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if ((mPost.isNSFW() && mNeedBlurNsfw && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { if ((mPost.isNSFW() && mNeedBlurNsfw && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} else { } else {
if (mImageViewWidth > mPost.getPreviewWidth()) { if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} }
} }
} else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1424,7 +1474,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> {
((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE);
((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE);
loadImage(holder); loadImage(holder, preview);
}); });
return false; return false;
} }
@ -1440,14 +1490,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); .into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} else { } else {
if (mImageViewWidth > mPost.getPreviewWidth()) { if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} }
} }
} else if (holder instanceof PostDetailLinkViewHolder) { } else if (holder instanceof PostDetailLinkViewHolder) {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1456,7 +1506,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { ((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> {
((PostDetailLinkViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailLinkViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE);
((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); ((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE);
loadImage(holder); loadImage(holder, preview);
}); });
return false; return false;
} }
@ -1472,14 +1522,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostDetailLinkViewHolder) holder).mImageView); .into(((PostDetailLinkViewHolder) holder).mImageView);
} else { } else {
if (mImageViewWidth > mPost.getPreviewWidth()) { if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView); imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView);
} }
} }
} else if (holder instanceof PostDetailGalleryViewHolder) { } else if (holder instanceof PostDetailGalleryViewHolder) {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1488,7 +1538,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailGalleryViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { ((PostDetailGalleryViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> {
((PostDetailGalleryViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailGalleryViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE);
((PostDetailGalleryViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); ((PostDetailGalleryViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE);
loadImage(holder); loadImage(holder, preview);
}); });
return false; return false;
} }
@ -1503,7 +1553,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if ((mPost.isNSFW() && mNeedBlurNsfw && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { if ((mPost.isNSFW() && mNeedBlurNsfw && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailGalleryViewHolder) holder).mImageView); imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailGalleryViewHolder) holder).mImageView);
} else { } else {
if (mImageViewWidth > mPost.getPreviewWidth()) { if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailGalleryViewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailGalleryViewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailGalleryViewHolder) holder).mImageView); imageRequestBuilder.into(((PostDetailGalleryViewHolder) holder).mImageView);

View File

@ -53,6 +53,7 @@ import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -185,6 +186,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
private boolean mAutomaticallyTryRedgifs; private boolean mAutomaticallyTryRedgifs;
private boolean mLongPressToHideToolbarInCompactLayout; private boolean mLongPressToHideToolbarInCompactLayout;
private boolean mCompactLayoutToolbarHiddenByDefault; private boolean mCompactLayoutToolbarHiddenByDefault;
private boolean mDataSavingMode;
private Drawable mCommentIcon; private Drawable mCommentIcon;
private NetworkState networkState; private NetworkState networkState;
private ExoCreator mExoCreator; private ExoCreator mExoCreator;
@ -562,12 +564,17 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
if (holder instanceof PostVideoAutoplayViewHolder) { if (holder instanceof PostVideoAutoplayViewHolder) {
((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) post.getPreviewWidth() / post.getPreviewHeight());
((PostVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE); ((PostVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE);
if (mImageViewWidth > post.getPreviewWidth()) { Post.Preview preview = getSuitablePreview(post.getPreviews());
mGlide.load(post.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostVideoAutoplayViewHolder) holder).previewImageView); if (preview != null) {
((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight());
if (mImageViewWidth > preview.getPreviewWidth()) {
mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostVideoAutoplayViewHolder) holder).previewImageView);
} else {
mGlide.load(preview.getPreviewUrl()).into(((PostVideoAutoplayViewHolder) holder).previewImageView);
}
} else { } else {
mGlide.load(post.getPreviewUrl()).into(((PostVideoAutoplayViewHolder) holder).previewImageView); ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1);
} }
((PostVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (post.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); ((PostVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (post.isNSFW() && mMuteNSFWVideo) ? 0f : 1f);
@ -601,13 +608,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostVideoAndGifPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); ((PostVideoAndGifPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif));
} }
((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostVideoAndGifPreviewViewHolder) holder).imageView
.setRatio((float) post.getPreviewHeight() / post.getPreviewWidth());
loadImage(holder, post);
if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { Post.Preview preview = getSuitablePreview(post.getPreviews());
((PostVideoAndGifPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); if (preview != null) {
((PostVideoAndGifPreviewViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); ((PostVideoAndGifPreviewViewHolder) holder).imageView
.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
if (preview.getPreviewWidth() <= 0 || preview.getPreviewHeight() <= 0) {
((PostVideoAndGifPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
((PostVideoAndGifPreviewViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale);
}
loadImage(holder, post, preview);
} }
} else if (holder instanceof PostImageAndGifAutoplayViewHolder) { } else if (holder instanceof PostImageAndGifAutoplayViewHolder) {
if (post.getPostType() == Post.IMAGE_TYPE) { if (post.getPostType() == Post.IMAGE_TYPE) {
@ -616,32 +626,40 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostImageAndGifAutoplayViewHolder) holder).typeTextView.setText(R.string.gif); ((PostImageAndGifAutoplayViewHolder) holder).typeTextView.setText(R.string.gif);
} }
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { Post.Preview preview = getSuitablePreview(post.getPreviews());
((PostImageAndGifAutoplayViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); if (preview != null) {
((PostImageAndGifAutoplayViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); if (preview.getPreviewWidth() <= 0 || preview.getPreviewHeight() <= 0) {
} else { ((PostImageAndGifAutoplayViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
((PostImageAndGifAutoplayViewHolder) holder).imageView ((PostImageAndGifAutoplayViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale);
.setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); } else {
((PostImageAndGifAutoplayViewHolder) holder).imageView
.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
}
loadImage(holder, post, preview);
} }
loadImage(holder, post);
} else if (holder instanceof PostLinkTypeViewHolder) { } else if (holder instanceof PostLinkTypeViewHolder) {
((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostLinkTypeViewHolder) holder).imageView
.setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); Post.Preview preview = getSuitablePreview(post.getPreviews());
loadImage(holder, post); if (preview != null) {
((PostLinkTypeViewHolder) holder).imageView
.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
loadImage(holder, post, preview);
}
String domain = Uri.parse(post.getUrl()).getHost(); String domain = Uri.parse(post.getUrl()).getHost();
((PostLinkTypeViewHolder) holder).linkTextView.setText(domain); ((PostLinkTypeViewHolder) holder).linkTextView.setText(domain);
} else if (holder instanceof PostNoPreviewLinkTypeViewHolder) { } else if (holder instanceof PostNoPreviewLinkTypeViewHolder) {
((PostNoPreviewLinkTypeViewHolder) holder).linkTextView.setText(Uri.parse(post.getUrl()).getHost()); ((PostNoPreviewLinkTypeViewHolder) holder).linkTextView.setText(Uri.parse(post.getUrl()).getHost());
} else if (holder instanceof PostGalleryTypeViewHolder) { } else if (holder instanceof PostGalleryTypeViewHolder) {
if (post.getPreviewUrl() != null && !post.getPreviewUrl().equals("")) { Post.Preview preview = getSuitablePreview(post.getPreviews());
if (preview != null) {
((PostGalleryTypeViewHolder) holder).imageWrapperRelativeLayout.setVisibility(View.VISIBLE); ((PostGalleryTypeViewHolder) holder).imageWrapperRelativeLayout.setVisibility(View.VISIBLE);
((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostGalleryTypeViewHolder) holder).imageView ((PostGalleryTypeViewHolder) holder).imageView
.setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
loadImage(holder, post); loadImage(holder, post, preview);
} else { } else {
((PostGalleryTypeViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE); ((PostGalleryTypeViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE);
} }
@ -783,11 +801,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
if (mCompactLayoutToolbarHiddenByDefault) { if (mCompactLayoutToolbarHiddenByDefault) {
ViewGroup.LayoutParams params = (LinearLayout.LayoutParams) ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.getLayoutParams(); ViewGroup.LayoutParams params = ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.getLayoutParams();
params.height = 0; params.height = 0;
((PostCompactBaseViewHolder) holder).bottomConstraintLayout.setLayoutParams(params); ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.setLayoutParams(params);
} else { } else {
ViewGroup.LayoutParams params = (LinearLayout.LayoutParams) ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.getLayoutParams(); ViewGroup.LayoutParams params = ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.getLayoutParams();
params.height = LinearLayout.LayoutParams.WRAP_CONTENT; params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
((PostCompactBaseViewHolder) holder).bottomConstraintLayout.setLayoutParams(params); ((PostCompactBaseViewHolder) holder).bottomConstraintLayout.setLayoutParams(params);
} }
@ -845,12 +863,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post.getPostType() != Post.GIF_TYPE && post.getPostType() != Post.VIDEO_TYPE) { if (post.getPostType() != Post.GIF_TYPE && post.getPostType() != Post.VIDEO_TYPE) {
((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
} }
if (post.getPostType() == Post.GALLERY_TYPE && post.getPreviewUrl() == null || post.getPreviewUrl().equals("")) { if (post.getPostType() == Post.GALLERY_TYPE && post.getPreviews().isEmpty()) {
((PostCompactBaseViewHolder) holder).noPreviewLinkImageFrameLayout.setVisibility(View.VISIBLE); ((PostCompactBaseViewHolder) holder).noPreviewLinkImageFrameLayout.setVisibility(View.VISIBLE);
} else { } else {
((PostCompactBaseViewHolder) holder).imageView.setVisibility(View.VISIBLE); ((PostCompactBaseViewHolder) holder).imageView.setVisibility(View.VISIBLE);
} }
loadImage(holder, post); ArrayList<Post.Preview> previews = post.getPreviews();
if (previews != null && !previews.isEmpty()) {
loadImage(holder, post, previews.get(0));
}
} }
if (mPostType == PostDataSource.TYPE_SUBREDDIT && !mDisplaySubredditName && post.isStickied()) { if (mPostType == PostDataSource.TYPE_SUBREDDIT && !mDisplaySubredditName && post.isStickied()) {
@ -920,157 +941,202 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
} }
private void loadImage(final RecyclerView.ViewHolder holder, final Post post) { @Nullable
if (holder instanceof PostImageAndGifAutoplayViewHolder) { private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) {
String url = post.getPostType() == Post.IMAGE_TYPE ? post.getPreviewUrl() : post.getUrl(); Post.Preview preview;
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url).listener(new RequestListener<Drawable>() { if (!previews.isEmpty()) {
@Override preview = previews.get(0);
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { if (preview.getPreviewWidth() * preview.getPreviewHeight() >= 65 * 1000 * 1000) {
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); for (int i = previews.size() - 1; i >= 1; i--) {
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); preview = previews.get(i);
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { if (mImageViewWidth >= preview.getPreviewWidth()) {
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 75 * 1000 * 1000) {
return preview;
}
} else {
int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight();
if (mImageViewWidth * height <= 75 * 1000 * 1000) {
return preview;
}
}
}
}
int divisor = 2;
while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 75 * 1000 * 1000) {
preview.setPreviewWidth(preview.getPreviewWidth() / divisor);
preview.setPreviewHeight(preview.getPreviewHeight() / divisor);
divisor *= 2;
}
return preview;
}
return null;
}
private void loadImage(final RecyclerView.ViewHolder holder, final Post post, @NonNull Post.Preview preview) {
if (preview != null) {
if (holder instanceof PostImageAndGifAutoplayViewHolder) {
String url = post.getPostType() == Post.IMAGE_TYPE ? preview.getPreviewUrl() : post.getUrl();
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE);
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE);
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> {
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post, preview);
});
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post); ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE);
}); return false;
return false; }
} });
@Override if ((post.isNSFW() && mNeedBlurNSFW && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler) {
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); .into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
} else {
if (mImageViewWidth > post.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
}
} }
} } else if (holder instanceof PostVideoAndGifPreviewViewHolder) {
} else if (holder instanceof PostVideoAndGifPreviewViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener<Drawable>() {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { @Override
@Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE);
((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE);
((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> {
((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post, preview);
});
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post); ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE);
}); return false;
return false; }
} });
@Override if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); .into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
} else {
if (mImageViewWidth > post.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostVideoAndGifPreviewViewHolder) holder).imageView); if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
}
} }
} } else if (holder instanceof PostLinkTypeViewHolder) {
} else if (holder instanceof PostLinkTypeViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener<Drawable>() {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { @Override
@Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE);
((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> {
((PostLinkTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post, preview);
});
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post); ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
}); return false;
return false; }
} });
@Override if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); .into(((PostLinkTypeViewHolder) holder).imageView);
((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostLinkTypeViewHolder) holder).imageView);
} else {
if (mImageViewWidth > post.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostLinkTypeViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView); if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostLinkTypeViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView);
}
} }
} } else if (holder instanceof PostGalleryTypeViewHolder) {
} else if (holder instanceof PostGalleryTypeViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener<Drawable>() {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { @Override
@Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE); ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE);
((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> {
((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post, preview);
});
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
loadImage(holder, post); ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
}); return false;
return false; }
} });
@Override if ((post.isNSFW() && mNeedBlurNSFW && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler) {
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); .into(((PostGalleryTypeViewHolder) holder).imageView);
((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostGalleryTypeViewHolder) holder).imageView);
} else {
if (mImageViewWidth > post.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostGalleryTypeViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostGalleryTypeViewHolder) holder).imageView); if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostGalleryTypeViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostGalleryTypeViewHolder) holder).imageView);
}
} }
} } else if (holder instanceof PostCompactBaseViewHolder) {
} else if (holder instanceof PostCompactBaseViewHolder) { String postCompactThumbnailPreviewUrl;
String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl(); ArrayList<Post.Preview> previews = post.getPreviews();
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(previewUrl) if (previews != null && !previews.isEmpty()) {
.error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener<Drawable>() { if (previews.size() >= 2) {
@Override postCompactThumbnailPreviewUrl = previews.get(1).getPreviewUrl();
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { } else {
((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); postCompactThumbnailPreviewUrl = preview.getPreviewUrl();
return false; }
}
@Override RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(postCompactThumbnailPreviewUrl)
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener<Drawable>() {
((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); @Override
return false; public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
} ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE);
}); return false;
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { }
imageRequestBuilder
.transform(new BlurTransformation(50, 2)).into(((PostCompactBaseViewHolder) holder).imageView); @Override
} else { public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
imageRequestBuilder.into(((PostCompactBaseViewHolder) holder).imageView); ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder
.transform(new BlurTransformation(50, 2)).into(((PostCompactBaseViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostCompactBaseViewHolder) holder).imageView);
}
}
} }
} }
} }

View File

@ -9,8 +9,9 @@ import androidx.annotation.NonNull;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import ml.docilealligator.infinityforreddit.API.PushshiftAPI; import ml.docilealligator.infinityforreddit.API.PushshiftAPI;
import ml.docilealligator.infinityforreddit.Post.Post;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.Utils.Utils;
import retrofit2.Call; import retrofit2.Call;
@ -85,7 +86,24 @@ public class FetchRemovedPost {
} }
if (!result.isNull("thumbnail")) { if (!result.isNull("thumbnail")) {
post.setThumbnailPreviewUrl(result.getString("thumbnail")); ArrayList<Post.Preview> previews = post.getPreviews();
if (previews != null && !previews.isEmpty()) {
if (previews.size() >= 2) {
Post.Preview preview = previews.get(1);
preview.setPreviewUrl(result.getString("thumbnail"));
previews.set(1, preview);
} else {
Post.Preview preview = previews.get(0);
preview.setPreviewUrl(result.getString("thumbnail"));
previews.set(0, preview);
}
} else {
Post.Preview preview = new Post.Preview(result.getString("thumbnail"), 1, 1);
preview.setPreviewUrl(result.getString("thumbnail"));
ArrayList<Post.Preview> newPreviews = new ArrayList<>();
newPreviews.add(preview);
post.setPreviews(newPreviews);
}
} }
return post; return post;

View File

@ -30,7 +30,7 @@ public class ParsePost {
new ParsePostDataAsyncTask(response, locale, true, parsePostListener).execute(); new ParsePostDataAsyncTask(response, locale, true, parsePostListener).execute();
} }
private static Post parseBasicData(JSONObject data, Locale locale) throws JSONException { private static Post parseBasicData(JSONObject data) throws JSONException {
String id = data.getString(JSONUtils.ID_KEY); String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.NAME_KEY); String fullName = data.getString(JSONUtils.NAME_KEY);
String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY); String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY);
@ -108,32 +108,31 @@ public class ParsePost {
String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString(); String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString();
String previewUrl = ""; ArrayList<Post.Preview> previews = new ArrayList<>();
String thumbnailPreviewUrl = "";
int previewWidth = -1;
int previewHeight = -1;
if (data.has(JSONUtils.PREVIEW_KEY)) { if (data.has(JSONUtils.PREVIEW_KEY)) {
JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); String previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
int previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
int previewHeight = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY);
previews.add(new Post.Preview(previewUrl, previewWidth, previewHeight));
JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY); JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY);
int thumbnailPreviewsLength = thumbnailPreviews.length(); for (int i = 0; i < thumbnailPreviews.length(); i++) {
if (thumbnailPreviewsLength > 0) { JSONObject thumbnailPreview = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2);
if (thumbnailPreviewsLength >= 3) { String thumbnailPreviewUrl = thumbnailPreview.getString(JSONUtils.URL_KEY);
thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2).getString(JSONUtils.URL_KEY); int thumbnailPreviewWidth = thumbnailPreview.getInt(JSONUtils.WIDTH_KEY);
} else { int thumbnailPreviewHeight = thumbnailPreview.getInt(JSONUtils.HEIGHT_KEY);
thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(0).getString(JSONUtils.URL_KEY);
} previews.add(new Post.Preview(thumbnailPreviewUrl, thumbnailPreviewWidth, thumbnailPreviewHeight));
} }
previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
previewHeight = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY);
} }
if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) {
//Cross post //Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
Post crosspostParent = parseBasicData(data, locale); Post crosspostParent = parseBasicData(data);
Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, authorFlair, authorFlairHTMLBuilder.toString(), author, authorFlair, authorFlairHTMLBuilder.toString(),
postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, postTime, title, previews,
score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden,
spoiler, nsfw, stickied, archived, locked, saved, true); spoiler, nsfw, stickied, archived, locked, saved, true);
post.setCrosspostParentId(crosspostParent.getId()); post.setCrosspostParentId(crosspostParent.getId());
@ -141,7 +140,7 @@ public class ParsePost {
} else { } else {
return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, authorFlair, authorFlairHTMLBuilder.toString(), author, authorFlair, authorFlairHTMLBuilder.toString(),
postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, postTime, title, previews,
score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden,
spoiler, nsfw, stickied, archived, locked, saved, false); spoiler, nsfw, stickied, archived, locked, saved, false);
} }
@ -150,8 +149,7 @@ public class ParsePost {
private static Post parseData(JSONObject data, String permalink, String id, String fullName, private static Post parseData(JSONObject data, String permalink, String id, String fullName,
String subredditName, String subredditNamePrefixed, String author, String subredditName, String subredditNamePrefixed, String author,
String authorFlair, String authorFlairHTML, String authorFlair, String authorFlairHTML,
long postTimeMillis, String title, String previewUrl, long postTimeMillis, String title, ArrayList<Post.Preview> previews,
String thumbnailPreviewUrl, int previewWidth, int previewHeight,
int score, int voteType, int nComments, String flair, int score, int voteType, int nComments, String flair,
String awards, int nAwards, boolean hidden, boolean spoiler, String awards, int nAwards, boolean hidden, boolean spoiler,
boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean nsfw, boolean stickied, boolean archived, boolean locked,
@ -161,7 +159,7 @@ public class ParsePost {
boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY);
String url = Html.fromHtml(data.getString(JSONUtils.URL_KEY)).toString(); String url = Html.fromHtml(data.getString(JSONUtils.URL_KEY)).toString();
if (!data.has(JSONUtils.PREVIEW_KEY) && previewUrl.equals("")) { if (!data.has(JSONUtils.PREVIEW_KEY) && previews.isEmpty()) {
if (url.contains(permalink)) { if (url.contains(permalink)) {
//Text post //Text post
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
@ -191,19 +189,17 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType,
url, thumbnailPreviewUrl, url, permalink, score, postType, voteType,
nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived,
locked, saved, isCrosspost); locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
} else { } else {
//No preview link post //No preview link post
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, authorFlair, authorFlairHTML, postTimeMillis,
title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, title, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
@ -214,9 +210,24 @@ public class ParsePost {
} }
} }
} else { } else {
if (previewUrl.equals("")) { if (previews.isEmpty()) {
previewUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) if (data.has(JSONUtils.PREVIEW_KEY)) {
.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
String previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
int previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
int previewHeight = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY);
previews.add(new Post.Preview(previewUrl, previewWidth, previewHeight));
JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY);
for (int i = 0; i < thumbnailPreviews.length(); i++) {
JSONObject thumbnailPreview = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2);
String thumbnailPreviewUrl = thumbnailPreview.getString(JSONUtils.URL_KEY);
int thumbnailPreviewWidth = thumbnailPreview.getInt(JSONUtils.WIDTH_KEY);
int thumbnailPreviewHeight = thumbnailPreview.getInt(JSONUtils.HEIGHT_KEY);
previews.add(new Post.Preview(thumbnailPreviewUrl, thumbnailPreviewWidth, thumbnailPreviewHeight));
}
}
} }
if (isVideo) { if (isVideo) {
@ -227,13 +238,11 @@ public class ParsePost {
String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY);
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType,
previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType,
nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked,
saved, isCrosspost); saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl); post.setVideoDownloadUrl(videoDownloadUrl);
} else if (data.has(JSONUtils.PREVIEW_KEY)) { } else if (data.has(JSONUtils.PREVIEW_KEY)) {
@ -246,12 +255,10 @@ public class ParsePost {
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.FALLBACK_URL_KEY); .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.FALLBACK_URL_KEY);
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType,
previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType,
nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived,
locked, saved, isCrosspost); locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl); post.setVideoDownloadUrl(videoDownloadUrl);
} else { } else {
@ -261,35 +268,31 @@ public class ParsePost {
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, authorFlair, authorFlairHTML, postTimeMillis,
title, url, thumbnailPreviewUrl, url, permalink, score, postType, title, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw,
stickied, archived, locked, saved, isCrosspost); stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
} else if (url.endsWith("gif")){ } else if (url.endsWith("gif")){
//Gif post //Gif post
int postType = Post.GIF_TYPE; int postType = Post.GIF_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, authorFlair, authorFlairHTML, postTimeMillis,
title, previewUrl, thumbnailPreviewUrl, url, permalink, score, title, url, permalink, score,
postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler,
nsfw, stickied, archived, locked, saved, isCrosspost); nsfw, stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(url); post.setVideoUrl(url);
} else if (url.endsWith("mp4")) { } else if (url.endsWith("mp4")) {
//Video post //Video post
int postType = Post.VIDEO_TYPE; int postType = Post.VIDEO_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType,
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(url); post.setVideoUrl(url);
post.setVideoDownloadUrl(url); post.setVideoDownloadUrl(url);
} else { } else {
@ -303,8 +306,8 @@ public class ParsePost {
awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked,
saved, isCrosspost); saved, isCrosspost);
post.setPreviewWidth(previewWidth); //Need attention
post.setPreviewHeight(previewHeight); post.setPreviews(previews);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText(""); post.setSelfText("");
@ -328,7 +331,7 @@ public class ParsePost {
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, authorFlair, authorFlairHTML, postTimeMillis,
title, previewUrl, thumbnailPreviewUrl, url, permalink, score, title, url, permalink, score,
postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler,
nsfw, stickied, archived, locked, saved, isCrosspost); nsfw, stickied, archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
@ -337,8 +340,7 @@ public class ParsePost {
post.setSelfText(Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim())); post.setSelfText(Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim()));
} }
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
} }
} }
} }
@ -348,23 +350,19 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType,
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
} else if (url.endsWith("mp4")) { } else if (url.endsWith("mp4")) {
//Video post //Video post
int postType = Post.VIDEO_TYPE; int postType = Post.VIDEO_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType,
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviews(previews);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(url); post.setVideoUrl(url);
post.setVideoDownloadUrl(url); post.setVideoDownloadUrl(url);
} else { } else {
@ -372,10 +370,10 @@ public class ParsePost {
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, postTimeMillis, title, authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType,
url, thumbnailPreviewUrl, url, permalink, score, postType, voteType,
nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived,
locked, saved, isCrosspost); locked, saved, isCrosspost);
//Need attention
} }
} }
} }
@ -414,11 +412,9 @@ public class ParsePost {
galleryItemUrl = sourceObject.getString(JSONUtils.MP4_KEY); galleryItemUrl = sourceObject.getString(JSONUtils.MP4_KEY);
} }
} }
if ((previewUrl.equals("")) && mimeType.contains("jpg") || mimeType.contains("png")) { if ((previews.isEmpty()) && mimeType.contains("jpg") || mimeType.contains("png")) {
previewUrl = galleryItemUrl; previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY),
post.setPreviewUrl(previewUrl); singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY)));
post.setPreviewWidth(singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY));
post.setPreviewHeight(singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY));
} }
gallery.add(new Post.Gallery(mimeType, galleryItemUrl, subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1))); gallery.add(new Post.Gallery(mimeType, galleryItemUrl, subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1)));
} }
@ -520,7 +516,7 @@ public class ParsePost {
try { try {
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY); JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
post = parseBasicData(data, locale); post = parseBasicData(data);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
parseFailed = true; parseFailed = true;
@ -538,7 +534,7 @@ public class ParsePost {
try { try {
if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) { if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) {
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
Post post = parseBasicData(data, locale); Post post = parseBasicData(data);
if (!(!nsfw && post.isNSFW())) { if (!(!nsfw && post.isNSFW())) {
if (filter == PostFragment.EXTRA_NO_FILTER) { if (filter == PostFragment.EXTRA_NO_FILTER) {
newPosts.add(post); newPosts.add(post);

View File

@ -47,8 +47,6 @@ public class Post implements Parcelable {
private String selfText; private String selfText;
private String selfTextPlain; private String selfTextPlain;
private String selfTextPlainTrimmed; private String selfTextPlainTrimmed;
private String previewUrl;
private String thumbnailPreviewUrl;
private String url; private String url;
private String videoUrl; private String videoUrl;
private String videoDownloadUrl; private String videoDownloadUrl;
@ -64,8 +62,6 @@ public class Post implements Parcelable {
private int score; private int score;
private int postType; private int postType;
private int voteType; private int voteType;
private int previewWidth;
private int previewHeight;
private int nComments; private int nComments;
private boolean hidden; private boolean hidden;
private boolean spoiler; private boolean spoiler;
@ -76,11 +72,12 @@ public class Post implements Parcelable {
private boolean saved; private boolean saved;
private boolean isCrosspost; private boolean isCrosspost;
private String crosspostParentId; private String crosspostParentId;
private ArrayList<Preview> previews = new ArrayList<>();
private ArrayList<Gallery> gallery = new ArrayList<>(); private ArrayList<Gallery> gallery = new ArrayList<>();
public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, public Post(String id, String fullName, String subredditName, String subredditNamePrefixed,
String author, String authorFlair, String authorFlairHTML, String author, String authorFlair, String authorFlairHTML,
long postTimeMillis, String title, String previewUrl, String thumbnailPreviewUrl, long postTimeMillis, String title,
String permalink, int score, int postType, int voteType, int nComments, String flair, String permalink, int score, int postType, int voteType, int nComments, String flair,
String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied,
boolean archived, boolean locked, boolean saved, boolean isCrosspost) { boolean archived, boolean locked, boolean saved, boolean isCrosspost) {
@ -94,8 +91,6 @@ public class Post implements Parcelable {
this.authorFlairHTML = authorFlairHTML; this.authorFlairHTML = authorFlairHTML;
this.postTimeMillis = postTimeMillis; this.postTimeMillis = postTimeMillis;
this.title = title; this.title = title;
this.previewUrl = previewUrl;
this.thumbnailPreviewUrl = thumbnailPreviewUrl;
this.permalink = APIUtils.API_BASE_URI + permalink; this.permalink = APIUtils.API_BASE_URI + permalink;
this.score = score; this.score = score;
this.postType = postType; this.postType = postType;
@ -116,7 +111,7 @@ public class Post implements Parcelable {
public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, public Post(String id, String fullName, String subredditName, String subredditNamePrefixed,
String author, String authorFlair, String authorFlairHTML, String author, String authorFlair, String authorFlairHTML,
long postTimeMillis, String title, String previewUrl, String thumbnailPreviewUrl, long postTimeMillis, String title,
String url, String permalink, int score, int postType, int voteType, int nComments, String url, String permalink, int score, int postType, int voteType, int nComments,
String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw,
boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) {
@ -130,8 +125,6 @@ public class Post implements Parcelable {
this.authorFlairHTML = authorFlairHTML; this.authorFlairHTML = authorFlairHTML;
this.postTimeMillis = postTimeMillis; this.postTimeMillis = postTimeMillis;
this.title = title; this.title = title;
this.previewUrl = previewUrl;
this.thumbnailPreviewUrl = thumbnailPreviewUrl;
this.url = url; this.url = url;
this.permalink = APIUtils.API_BASE_URI + permalink; this.permalink = APIUtils.API_BASE_URI + permalink;
this.score = score; this.score = score;
@ -151,40 +144,6 @@ public class Post implements Parcelable {
this.isCrosspost = isCrosspost; this.isCrosspost = isCrosspost;
} }
public Post(String id, String fullName, String subredditName, String subredditNamePrefixed,
String author, String authorFlair, String authorFlairHTML,
long postTimeMillis, String title, String permalink, int score, int postType,
int voteType, int nComments, String flair, String awards, int nAwards, boolean hidden,
boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked,
boolean saved, boolean isCrosspost) {
this.id = id;
this.fullName = fullName;
this.subredditName = subredditName;
this.subredditNamePrefixed = subredditNamePrefixed;
this.author = author;
this.authorNamePrefixed = "u/" + author;
this.authorFlair = authorFlair;
this.authorFlairHTML = authorFlairHTML;
this.postTimeMillis = postTimeMillis;
this.title = title;
this.permalink = APIUtils.API_BASE_URI + permalink;
this.score = score;
this.postType = postType;
this.voteType = voteType;
this.nComments = nComments;
this.flair = flair;
this.awards = awards;
this.nAwards = nAwards;
this.hidden = hidden;
this.spoiler = spoiler;
this.nsfw = nsfw;
this.stickied = stickied;
this.archived = archived;
this.locked = locked;
this.saved = saved;
this.isCrosspost = isCrosspost;
}
protected Post(Parcel in) { protected Post(Parcel in) {
id = in.readString(); id = in.readString();
fullName = in.readString(); fullName = in.readString();
@ -201,8 +160,6 @@ public class Post implements Parcelable {
selfText = in.readString(); selfText = in.readString();
selfTextPlain = in.readString(); selfTextPlain = in.readString();
selfTextPlainTrimmed = in.readString(); selfTextPlainTrimmed = in.readString();
previewUrl = in.readString();
thumbnailPreviewUrl = in.readString();
url = in.readString(); url = in.readString();
videoUrl = in.readString(); videoUrl = in.readString();
videoDownloadUrl = in.readString(); videoDownloadUrl = in.readString();
@ -217,8 +174,6 @@ public class Post implements Parcelable {
score = in.readInt(); score = in.readInt();
postType = in.readInt(); postType = in.readInt();
voteType = in.readInt(); voteType = in.readInt();
previewWidth = in.readInt();
previewHeight = in.readInt();
nComments = in.readInt(); nComments = in.readInt();
hidden = in.readByte() != 0; hidden = in.readByte() != 0;
spoiler = in.readByte() != 0; spoiler = in.readByte() != 0;
@ -229,6 +184,7 @@ public class Post implements Parcelable {
saved = in.readByte() != 0; saved = in.readByte() != 0;
isCrosspost = in.readByte() != 0; isCrosspost = in.readByte() != 0;
crosspostParentId = in.readString(); crosspostParentId = in.readString();
in.readTypedList(previews, Preview.CREATOR);
in.readTypedList(gallery, Gallery.CREATOR); in.readTypedList(gallery, Gallery.CREATOR);
} }
@ -321,22 +277,6 @@ public class Post implements Parcelable {
this.selfTextPlainTrimmed = selfTextPlainTrimmed; this.selfTextPlainTrimmed = selfTextPlainTrimmed;
} }
public String getPreviewUrl() {
return previewUrl;
}
public void setPreviewUrl(String previewUrl) {
this.previewUrl = previewUrl;
}
public String getThumbnailPreviewUrl() {
return thumbnailPreviewUrl;
}
public void setThumbnailPreviewUrl(String thumbnailPreviewUrl) {
this.thumbnailPreviewUrl = thumbnailPreviewUrl;
}
public String getUrl() { public String getUrl() {
return url; return url;
} }
@ -437,22 +377,6 @@ public class Post implements Parcelable {
this.voteType = voteType; this.voteType = voteType;
} }
public int getPreviewWidth() {
return previewWidth;
}
public void setPreviewWidth(int previewWidth) {
this.previewWidth = previewWidth;
}
public int getPreviewHeight() {
return previewHeight;
}
public void setPreviewHeight(int previewHeight) {
this.previewHeight = previewHeight;
}
public int getNComments() { public int getNComments() {
return nComments; return nComments;
} }
@ -522,6 +446,14 @@ public class Post implements Parcelable {
this.crosspostParentId = crosspostParentId; this.crosspostParentId = crosspostParentId;
} }
public ArrayList<Preview> getPreviews() {
return previews;
}
public void setPreviews(ArrayList<Preview> previews) {
this.previews = previews;
}
public ArrayList<Gallery> getGallery() { public ArrayList<Gallery> getGallery() {
return gallery; return gallery;
} }
@ -547,8 +479,6 @@ public class Post implements Parcelable {
parcel.writeString(selfText); parcel.writeString(selfText);
parcel.writeString(selfTextPlain); parcel.writeString(selfTextPlain);
parcel.writeString(selfTextPlainTrimmed); parcel.writeString(selfTextPlainTrimmed);
parcel.writeString(previewUrl);
parcel.writeString(thumbnailPreviewUrl);
parcel.writeString(url); parcel.writeString(url);
parcel.writeString(videoUrl); parcel.writeString(videoUrl);
parcel.writeString(videoDownloadUrl); parcel.writeString(videoDownloadUrl);
@ -563,8 +493,6 @@ public class Post implements Parcelable {
parcel.writeInt(score); parcel.writeInt(score);
parcel.writeInt(postType); parcel.writeInt(postType);
parcel.writeInt(voteType); parcel.writeInt(voteType);
parcel.writeInt(previewWidth);
parcel.writeInt(previewHeight);
parcel.writeInt(nComments); parcel.writeInt(nComments);
parcel.writeByte((byte) (hidden ? 1 : 0)); parcel.writeByte((byte) (hidden ? 1 : 0));
parcel.writeByte((byte) (spoiler ? 1 : 0)); parcel.writeByte((byte) (spoiler ? 1 : 0));
@ -575,6 +503,7 @@ public class Post implements Parcelable {
parcel.writeByte((byte) (saved ? 1 : 0)); parcel.writeByte((byte) (saved ? 1 : 0));
parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0));
parcel.writeString(crosspostParentId); parcel.writeString(crosspostParentId);
parcel.writeTypedList(previews);
parcel.writeTypedList(gallery); parcel.writeTypedList(gallery);
} }
@ -591,7 +520,7 @@ public class Post implements Parcelable {
return id.hashCode(); return id.hashCode();
} }
public static class Gallery implements Parcelable{ public static class Gallery implements Parcelable {
public static final int TYPE_IMAGE = 0; public static final int TYPE_IMAGE = 0;
public static final int TYPE_GIF = 1; public static final int TYPE_GIF = 1;
public static final int TYPE_VIDEO = 2; public static final int TYPE_VIDEO = 2;
@ -646,4 +575,70 @@ public class Post implements Parcelable {
parcel.writeInt(mediaType); parcel.writeInt(mediaType);
} }
} }
public static class Preview implements Parcelable {
private String previewUrl;
private int previewWidth;
private int previewHeight;
public Preview(String previewUrl, int previewWidth, int previewHeight) {
this.previewUrl = previewUrl;
this.previewWidth = previewWidth;
this.previewHeight = previewHeight;
}
protected Preview(Parcel in) {
previewUrl = in.readString();
previewWidth = in.readInt();
previewHeight = in.readInt();
}
public static final Creator<Preview> CREATOR = new Creator<Preview>() {
@Override
public Preview createFromParcel(Parcel in) {
return new Preview(in);
}
@Override
public Preview[] newArray(int size) {
return new Preview[size];
}
};
public String getPreviewUrl() {
return previewUrl;
}
public void setPreviewUrl(String previewUrl) {
this.previewUrl = previewUrl;
}
public int getPreviewWidth() {
return previewWidth;
}
public void setPreviewWidth(int previewWidth) {
this.previewWidth = previewWidth;
}
public int getPreviewHeight() {
return previewHeight;
}
public void setPreviewHeight(int previewHeight) {
this.previewHeight = previewHeight;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(previewUrl);
parcel.writeInt(previewWidth);
parcel.writeInt(previewHeight);
}
}
} }