From c4f8c91944fc1a0a4e919f8f2f0870ecbb2a09fb Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 27 Sep 2020 12:15:48 +0800 Subject: 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. --- .../Activity/SubmitCrosspostActivity.java | 60 ++-- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 110 ++++-- .../Adapter/PostRecyclerViewAdapter.java | 386 ++++++++++++--------- .../infinityforreddit/Post/FetchRemovedPost.java | 22 +- .../infinityforreddit/Post/ParsePost.java | 134 ++++--- .../infinityforreddit/Post/Post.java | 165 +++++---- 6 files changed, 510 insertions(+), 367 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java index 4718755b..37a9258b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubmitCrosspostActivity.java @@ -37,6 +37,8 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.ArrayList; + import javax.inject.Inject; import javax.inject.Named; @@ -257,32 +259,38 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom contentTextView.setVisibility(View.VISIBLE); contentTextView.setText(post.getUrl()); } else if (post.getPostType() == Post.GIF_TYPE || post.getPostType() == Post.GALLERY_TYPE || post.getPostType() == Post.IMAGE_TYPE) { - frameLayout.setVisibility(View.VISIBLE); - mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - imageView.setImage(ImageSource.bitmap(resource)); - } + Post.Preview preview = getPreview(post); + if (preview != null) { + frameLayout.setVisibility(View.VISIBLE); + mGlide.asBitmap().load(preview.getPreviewUrl()).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + imageView.setImage(ImageSource.bitmap(resource)); + } - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { - } - }); + } + }); + } } else if (post.getPostType() == Post.VIDEO_TYPE) { - frameLayout.setVisibility(View.VISIBLE); - mGlide.asBitmap().load(post.getPreviewUrl()).into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - imageView.setImage(ImageSource.bitmap(resource)); - } + Post.Preview preview = getPreview(post); + if (preview != null) { + frameLayout.setVisibility(View.VISIBLE); + mGlide.asBitmap().load(preview.getPreviewUrl()).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + imageView.setImage(ImageSource.bitmap(resource)); + } - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { - } - }); - playButton.setVisibility(View.VISIBLE); + } + }); + playButton.setVisibility(View.VISIBLE); + } } iconGifImageView.setOnClickListener(view -> { @@ -356,6 +364,16 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom }); } + @Nullable + private Post.Preview getPreview(Post post) { + ArrayList previews = post.getPreviews(); + if (previews != null && !previews.isEmpty()) { + return previews.get(0); + } + + return null; + } + @Override protected SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index bc476b67..d1c417f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -829,12 +829,17 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mPost.getPreviewWidth()) { - mGlide.load(mPost.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); + Post.Preview preview = getSuitablePreview(mPost.getPreviews()); + 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 { - mGlide.load(mPost.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); + ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } ((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); @@ -867,8 +872,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 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) { - String url = mPost.getPostType() == Post.IMAGE_TYPE ? mPost.getPreviewUrl() : mPost.getUrl(); + String url = mPost.getPostType() == Post.IMAGE_TYPE ? preview.getPreviewUrl() : mPost.getUrl(); RequestBuilder imageRequestBuilder = mGlide.load(url) .listener(new RequestListener() { @Override @@ -1393,7 +1443,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); - loadImage(holder); + loadImage(holder, preview); }); return false; } @@ -1408,14 +1458,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mPost.getPreviewWidth()) { + if (mImageViewWidth > preview.getPreviewWidth()) { imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); } else { imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); } } } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1424,7 +1474,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); - loadImage(holder); + loadImage(holder, preview); }); return false; } @@ -1440,14 +1490,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mPost.getPreviewWidth()) { + if (mImageViewWidth > preview.getPreviewWidth()) { imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } else { imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } } } else if (holder instanceof PostDetailLinkViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1456,7 +1506,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { ((PostDetailLinkViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); - loadImage(holder); + loadImage(holder, preview); }); return false; } @@ -1472,14 +1522,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mPost.getPreviewWidth()) { + if (mImageViewWidth > preview.getPreviewWidth()) { imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView); } else { imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView); } } } else if (holder instanceof PostDetailGalleryViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1488,7 +1538,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { ((PostDetailGalleryViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); ((PostDetailGalleryViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); - loadImage(holder); + loadImage(holder, preview); }); return false; } @@ -1503,7 +1553,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mPost.getPreviewWidth()) { + if (mImageViewWidth > preview.getPreviewWidth()) { imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailGalleryViewHolder) holder).mImageView); } else { imageRequestBuilder.into(((PostDetailGalleryViewHolder) holder).mImageView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 285f6441..86ce0e1c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -53,6 +53,7 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; +import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -185,6 +186,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter post.getPreviewWidth()) { - mGlide.load(post.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostVideoAutoplayViewHolder) holder).previewImageView); + Post.Preview preview = getSuitablePreview(post.getPreviews()); + 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 { - mGlide.load(post.getPreviewUrl()).into(((PostVideoAutoplayViewHolder) holder).previewImageView); + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } ((PostVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (post.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); @@ -601,13 +608,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter previews = post.getPreviews(); + if (previews != null && !previews.isEmpty()) { + loadImage(holder, post, previews.get(0)); + } } if (mPostType == PostDataSource.TYPE_SUBREDDIT && !mDisplaySubredditName && post.isStickied()) { @@ -920,157 +941,202 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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); - }); - return false; + @Nullable + private Post.Preview getSuitablePreview(ArrayList 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; + } + } } + } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + 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; + } - 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); + 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 imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 target, DataSource dataSource, boolean isFirstResource) { + ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((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 { - imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); - } - } - } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post); - }); - return false; + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + } } + } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((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 target, DataSource dataSource, boolean isFirstResource) { - ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostVideoAndGifPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostVideoAndGifPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((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); + if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostVideoAndGifPreviewViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostVideoAndGifPreviewViewHolder) holder).imageView); - } - } - } else if (holder instanceof PostLinkTypeViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post); - }); - return false; + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostVideoAndGifPreviewViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostVideoAndGifPreviewViewHolder) holder).imageView); + } } + } else if (holder instanceof PostLinkTypeViewHolder) { + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((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 target, DataSource dataSource, boolean isFirstResource) { - ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((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); + if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostLinkTypeViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView); - } - } - } else if (holder instanceof PostGalleryTypeViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post); - }); - return false; + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostLinkTypeViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView); + } } + } else if (holder instanceof PostGalleryTypeViewHolder) { + RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((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 target, DataSource dataSource, boolean isFirstResource) { - ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostGalleryTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostGalleryTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((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); + 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 { - 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) { - String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl(); - RequestBuilder imageRequestBuilder = mGlide.load(previewUrl) - .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } + } else if (holder instanceof PostCompactBaseViewHolder) { + String postCompactThumbnailPreviewUrl; + ArrayList previews = post.getPreviews(); + if (previews != null && !previews.isEmpty()) { + if (previews.size() >= 2) { + postCompactThumbnailPreviewUrl = previews.get(1).getPreviewUrl(); + } else { + postCompactThumbnailPreviewUrl = preview.getPreviewUrl(); + } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, 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); - } else { - imageRequestBuilder.into(((PostCompactBaseViewHolder) holder).imageView); + RequestBuilder imageRequestBuilder = mGlide.load(postCompactThumbnailPreviewUrl) + .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, 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); + } else { + imageRequestBuilder.into(((PostCompactBaseViewHolder) holder).imageView); + } + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java index f24e995b..dd641e6e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java @@ -9,8 +9,9 @@ import androidx.annotation.NonNull; import org.json.JSONException; import org.json.JSONObject; +import java.util.ArrayList; + import ml.docilealligator.infinityforreddit.API.PushshiftAPI; -import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Call; @@ -85,7 +86,24 @@ public class FetchRemovedPost { } if (!result.isNull("thumbnail")) { - post.setThumbnailPreviewUrl(result.getString("thumbnail")); + ArrayList 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 newPreviews = new ArrayList<>(); + newPreviews.add(preview); + post.setPreviews(newPreviews); + } } return post; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java index 11a08868..4aba1588 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -30,7 +30,7 @@ public class ParsePost { 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 fullName = data.getString(JSONUtils.NAME_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 previewUrl = ""; - String thumbnailPreviewUrl = ""; - int previewWidth = -1; - int previewHeight = -1; + ArrayList previews = new ArrayList<>(); if (data.has(JSONUtils.PREVIEW_KEY)) { 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); - int thumbnailPreviewsLength = thumbnailPreviews.length(); - if (thumbnailPreviewsLength > 0) { - if (thumbnailPreviewsLength >= 3) { - thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2).getString(JSONUtils.URL_KEY); - } else { - thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(0).getString(JSONUtils.URL_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)); } - 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)) { //Cross post 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, author, authorFlair, authorFlairHTMLBuilder.toString(), - postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, + postTime, title, previews, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, true); post.setCrosspostParentId(crosspostParent.getId()); @@ -141,7 +140,7 @@ public class ParsePost { } else { return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTMLBuilder.toString(), - postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, + postTime, title, previews, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, 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, String subredditName, String subredditNamePrefixed, String author, String authorFlair, String authorFlairHTML, - long postTimeMillis, String title, String previewUrl, - String thumbnailPreviewUrl, int previewWidth, int previewHeight, + long postTimeMillis, String title, ArrayList previews, int score, int voteType, int nComments, String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, @@ -161,7 +159,7 @@ public class ParsePost { boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); 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)) { //Text post int postType = Post.TEXT_TYPE; @@ -191,19 +189,17 @@ public class ParsePost { int postType = Post.IMAGE_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - url, thumbnailPreviewUrl, url, permalink, score, postType, voteType, + authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); } else { //No preview link post int postType = Post.NO_PREVIEW_LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTML, postTimeMillis, - title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, + title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if (data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -214,9 +210,24 @@ public class ParsePost { } } } else { - if (previewUrl.equals("")) { - previewUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) - .getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); + if (previews.isEmpty()) { + if (data.has(JSONUtils.PREVIEW_KEY)) { + 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) { @@ -227,13 +238,11 @@ public class ParsePost { String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, + authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); post.setVideoUrl(videoUrl); post.setVideoDownloadUrl(videoDownloadUrl); } 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); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, + authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); post.setVideoUrl(videoUrl); post.setVideoDownloadUrl(videoDownloadUrl); } else { @@ -261,35 +268,31 @@ public class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTML, postTimeMillis, - title, url, thumbnailPreviewUrl, url, permalink, score, postType, + title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); } else if (url.endsWith("gif")){ //Gif post int postType = Post.GIF_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTML, postTimeMillis, - title, previewUrl, thumbnailPreviewUrl, url, permalink, score, + title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); post.setVideoUrl(url); } else if (url.endsWith("mp4")) { //Video post int postType = Post.VIDEO_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, + authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); post.setVideoUrl(url); post.setVideoDownloadUrl(url); } else { @@ -303,8 +306,8 @@ public class ParsePost { awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + //Need attention + post.setPreviews(previews); if (data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); @@ -328,7 +331,7 @@ public class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTML, postTimeMillis, - title, previewUrl, thumbnailPreviewUrl, url, permalink, score, + title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if (data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -337,8 +340,7 @@ public class ParsePost { post.setSelfText(Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim())); } - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); } } } @@ -348,23 +350,19 @@ public class ParsePost { int postType = Post.IMAGE_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, + authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); } else if (url.endsWith("mp4")) { //Video post int postType = Post.VIDEO_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, + authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); - post.setPreviewWidth(previewWidth); - post.setPreviewHeight(previewHeight); + post.setPreviews(previews); post.setVideoUrl(url); post.setVideoDownloadUrl(url); } else { @@ -372,10 +370,10 @@ public class ParsePost { int postType = Post.NO_PREVIEW_LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, postTimeMillis, title, - url, thumbnailPreviewUrl, url, permalink, score, postType, voteType, + authorFlair, authorFlairHTML, postTimeMillis, title, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); + //Need attention } } } @@ -414,11 +412,9 @@ public class ParsePost { galleryItemUrl = sourceObject.getString(JSONUtils.MP4_KEY); } } - if ((previewUrl.equals("")) && mimeType.contains("jpg") || mimeType.contains("png")) { - previewUrl = galleryItemUrl; - post.setPreviewUrl(previewUrl); - post.setPreviewWidth(singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY)); - post.setPreviewHeight(singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY)); + if ((previews.isEmpty()) && mimeType.contains("jpg") || mimeType.contains("png")) { + previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY), + singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY))); } gallery.add(new Post.Gallery(mimeType, galleryItemUrl, subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1))); } @@ -520,7 +516,7 @@ public class ParsePost { try { JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY); - post = parseBasicData(data, locale); + post = parseBasicData(data); } catch (JSONException e) { e.printStackTrace(); parseFailed = true; @@ -538,7 +534,7 @@ public class ParsePost { try { if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) { JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - Post post = parseBasicData(data, locale); + Post post = parseBasicData(data); if (!(!nsfw && post.isNSFW())) { if (filter == PostFragment.EXTRA_NO_FILTER) { newPosts.add(post); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index 82c550d5..d2b8748e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -47,8 +47,6 @@ public class Post implements Parcelable { private String selfText; private String selfTextPlain; private String selfTextPlainTrimmed; - private String previewUrl; - private String thumbnailPreviewUrl; private String url; private String videoUrl; private String videoDownloadUrl; @@ -64,8 +62,6 @@ public class Post implements Parcelable { private int score; private int postType; private int voteType; - private int previewWidth; - private int previewHeight; private int nComments; private boolean hidden; private boolean spoiler; @@ -76,11 +72,12 @@ public class Post implements Parcelable { private boolean saved; private boolean isCrosspost; private String crosspostParentId; + private ArrayList previews = new ArrayList<>(); private ArrayList gallery = new ArrayList<>(); public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, 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 awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { @@ -94,8 +91,6 @@ public class Post implements Parcelable { this.authorFlairHTML = authorFlairHTML; this.postTimeMillis = postTimeMillis; this.title = title; - this.previewUrl = previewUrl; - this.thumbnailPreviewUrl = thumbnailPreviewUrl; this.permalink = APIUtils.API_BASE_URI + permalink; this.score = score; this.postType = postType; @@ -116,7 +111,7 @@ public class Post implements Parcelable { public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, 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 flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { @@ -130,8 +125,6 @@ public class Post implements Parcelable { this.authorFlairHTML = authorFlairHTML; this.postTimeMillis = postTimeMillis; this.title = title; - this.previewUrl = previewUrl; - this.thumbnailPreviewUrl = thumbnailPreviewUrl; this.url = url; this.permalink = APIUtils.API_BASE_URI + permalink; this.score = score; @@ -151,40 +144,6 @@ public class Post implements Parcelable { 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) { id = in.readString(); fullName = in.readString(); @@ -201,8 +160,6 @@ public class Post implements Parcelable { selfText = in.readString(); selfTextPlain = in.readString(); selfTextPlainTrimmed = in.readString(); - previewUrl = in.readString(); - thumbnailPreviewUrl = in.readString(); url = in.readString(); videoUrl = in.readString(); videoDownloadUrl = in.readString(); @@ -217,8 +174,6 @@ public class Post implements Parcelable { score = in.readInt(); postType = in.readInt(); voteType = in.readInt(); - previewWidth = in.readInt(); - previewHeight = in.readInt(); nComments = in.readInt(); hidden = in.readByte() != 0; spoiler = in.readByte() != 0; @@ -229,6 +184,7 @@ public class Post implements Parcelable { saved = in.readByte() != 0; isCrosspost = in.readByte() != 0; crosspostParentId = in.readString(); + in.readTypedList(previews, Preview.CREATOR); in.readTypedList(gallery, Gallery.CREATOR); } @@ -321,22 +277,6 @@ public class Post implements Parcelable { 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() { return url; } @@ -437,22 +377,6 @@ public class Post implements Parcelable { 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() { return nComments; } @@ -522,6 +446,14 @@ public class Post implements Parcelable { this.crosspostParentId = crosspostParentId; } + public ArrayList getPreviews() { + return previews; + } + + public void setPreviews(ArrayList previews) { + this.previews = previews; + } + public ArrayList getGallery() { return gallery; } @@ -547,8 +479,6 @@ public class Post implements Parcelable { parcel.writeString(selfText); parcel.writeString(selfTextPlain); parcel.writeString(selfTextPlainTrimmed); - parcel.writeString(previewUrl); - parcel.writeString(thumbnailPreviewUrl); parcel.writeString(url); parcel.writeString(videoUrl); parcel.writeString(videoDownloadUrl); @@ -563,8 +493,6 @@ public class Post implements Parcelable { parcel.writeInt(score); parcel.writeInt(postType); parcel.writeInt(voteType); - parcel.writeInt(previewWidth); - parcel.writeInt(previewHeight); parcel.writeInt(nComments); parcel.writeByte((byte) (hidden ? 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) (isCrosspost ? 1 : 0)); parcel.writeString(crosspostParentId); + parcel.writeTypedList(previews); parcel.writeTypedList(gallery); } @@ -591,7 +520,7 @@ public class Post implements Parcelable { 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_GIF = 1; public static final int TYPE_VIDEO = 2; @@ -646,4 +575,70 @@ public class Post implements Parcelable { 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 CREATOR = new Creator() { + @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); + } + } } -- cgit v1.2.3