From 1a104d9033075b3b466c22b3577b636ff25f8990 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:41:59 -0500 Subject: Continue implementing showing images from preview.redd.it in posts. --- .../activities/CommentActivity.java | 2 +- .../activities/FullMarkdownActivity.java | 2 +- .../infinityforreddit/activities/WikiActivity.java | 2 +- .../adapters/PostDetailRecyclerViewAdapter.java | 128 +++++++++++---------- .../adapters/RulesRecyclerViewAdapter.java | 2 +- .../fragments/SidebarFragment.java | 3 +- .../markdown/ImageAndGifEntry.java | 38 +++++- 7 files changed, 104 insertions(+), 73 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index 495a6df4..3ad2c699 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -213,7 +213,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); Markwon postBodyMarkwon = MarkdownUtils.createFullRedditMarkwon(this, miscPlugin, imageAndGifPlugin, parentTextColor, parentSpoilerBackgroundColor, null); - MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mSharedPreferences, mGlide, new ImageAndGifEntry.OnItemClickListener() { + MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mGlide, new ImageAndGifEntry.OnItemClickListener() { @Override public void onItemClick(Post.MediaMetadata mediaMetadata) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java index 0825a8fa..e6084948 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -147,7 +147,7 @@ public class FullMarkdownActivity extends BaseActivity { miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, null); MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, - mSharedPreferences, Glide.with(this), new ImageAndGifEntry.OnItemClickListener() { + Glide.with(this), new ImageAndGifEntry.OnItemClickListener() { @Override public void onItemClick(Post.MediaMetadata mediaMetadata) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java index 2df36e7b..714dc5c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java @@ -183,7 +183,7 @@ public class WikiActivity extends BaseActivity { markwon = MarkdownUtils.createFullRedditMarkwon(this, miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); - markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mSharedPreferences, + markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mGlide, new ImageAndGifEntry.OnItemClickListener() { @Override public void onItemClick(Post.MediaMetadata mediaMetadata) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index e3746a7e..11b04252 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -146,6 +146,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { - if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) { - CopyTextBottomSheetFragment.show( - mActivity.getSupportFragmentManager(), - mPost.getSelfTextPlain(), mPost.getSelfText() - ); - } - return true; - }); - } - - @Override - public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { - builder.linkResolver((view, link) -> { - Intent intent = new Intent(mActivity, LinkResolverActivity.class); - Uri uri = Uri.parse(link); - intent.setData(uri); - intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); - mActivity.startActivity(intent); - }); - } - - @Override - public void configureTheme(@NonNull MarkwonTheme.Builder builder) { - builder.linkColor(linkColor); - } - }; - BetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener = (textView, url) -> { - if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) { - UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment(); - Bundle bundle = new Bundle(); - bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url); - urlMenuBottomSheetFragment.setArguments(bundle); - urlMenuBottomSheetFragment.show(activity.getSupportFragmentManager(), urlMenuBottomSheetFragment.getTag()); - } - return true; - }; - mImageAndGifPlugin = new ImageAndGifPlugin(); - mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, - miscPlugin, mImageAndGifPlugin, markdownColor, postSpoilerBackgroundColor, onLinkLongClickListener); - mMarkwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(activity, sharedPreferences, - mGlide, new ImageAndGifEntry.OnItemClickListener() { - @Override - public void onItemClick(Post.MediaMetadata mediaMetadata) { - Intent intent = new Intent(activity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); - if (canStartActivity) { - canStartActivity = false; - activity.startActivity(intent); - } - } - })); mSeparatePostAndComments = separatePostAndComments; mLegacyAutoplayVideoControllerUI = sharedPreferences.getBoolean(SharedPreferencesUtils.LEGACY_AUTOPLAY_VIDEO_CONTROLLER_UI, false); @@ -399,6 +337,70 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { + if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) { + CopyTextBottomSheetFragment.show( + mActivity.getSupportFragmentManager(), + mPost.getSelfTextPlain(), mPost.getSelfText() + ); + } + return true; + }); + } + + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + intent.setData(uri); + intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); + mActivity.startActivity(intent); + }); + } + + @Override + public void configureTheme(@NonNull MarkwonTheme.Builder builder) { + builder.linkColor(linkColor); + } + }; + BetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener = (textView, url) -> { + if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) { + UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url); + urlMenuBottomSheetFragment.setArguments(bundle); + urlMenuBottomSheetFragment.show(activity.getSupportFragmentManager(), urlMenuBottomSheetFragment.getTag()); + } + return true; + }; + mImageAndGifPlugin = new ImageAndGifPlugin(); + mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, + miscPlugin, mImageAndGifPlugin, markdownColor, postSpoilerBackgroundColor, onLinkLongClickListener); + mImageAndGifEntry = new ImageAndGifEntry(activity, + mGlide, mDataSavingMode, + (post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit())) || (mPost.isSpoiler() && mNeedBlurSpoiler), + mediaMetadata -> { + Intent intent = new Intent(activity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + if (canStartActivity) { + canStartActivity = false; + activity.startActivity(intent); + } + }); + mMarkwonAdapter = MarkdownUtils.createTablesAdapter(mImageAndGifEntry); } public void setCanStartActivity(boolean canStartActivity) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java index 1f0a8451..a2dac6d9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java @@ -144,7 +144,7 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter { private BaseActivity baseActivity; private RequestManager glide; private SaveMemoryCenterInisdeDownsampleStrategy saveMemoryCenterInsideDownsampleStrategy; private OnItemClickListener onItemClickListener; + private boolean dataSavingMode; + private boolean blurImage; - public ImageAndGifEntry(BaseActivity baseActivity, SharedPreferences sharedPreferences, - RequestManager glide, OnItemClickListener onItemClickListener) { + public ImageAndGifEntry(BaseActivity baseActivity, RequestManager glide, + OnItemClickListener onItemClickListener) { this.baseActivity = baseActivity; this.glide = glide; + SharedPreferences sharedPreferences = baseActivity.getDefaultSharedPreferences(); this.saveMemoryCenterInsideDownsampleStrategy = new SaveMemoryCenterInisdeDownsampleStrategy( Integer.parseInt(sharedPreferences.getString(SharedPreferencesUtils.POST_FEED_MAX_RESOLUTION, "5000000"))); this.onItemClickListener = onItemClickListener; + + String dataSavingModeString = sharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); + if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) { + dataSavingMode = true; + } else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + dataSavingMode = Utils.getConnectedNetwork(baseActivity) == Utils.NETWORK_TYPE_CELLULAR; + } + } + + public ImageAndGifEntry(BaseActivity baseActivity, RequestManager glide, boolean dataSavingMode, boolean blurImage, + OnItemClickListener onItemClickListener) { + this.baseActivity = baseActivity; + this.glide = glide; + SharedPreferences sharedPreferences = baseActivity.getDefaultSharedPreferences(); + this.saveMemoryCenterInsideDownsampleStrategy = new SaveMemoryCenterInisdeDownsampleStrategy( + Integer.parseInt(sharedPreferences.getString(SharedPreferencesUtils.POST_FEED_MAX_RESOLUTION, "5000000"))); + this.onItemClickListener = onItemClickListener; + this.dataSavingMode = dataSavingMode; + this.blurImage = blurImage; } @NonNull @@ -53,8 +76,15 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry imageRequestBuilder = glide.load(node.mediaMetadata.original.url).listener(holder.requestListener); - boolean blurImage = false; + RequestBuilder imageRequestBuilder; + if (dataSavingMode) { + imageRequestBuilder = glide.load(node.mediaMetadata.downscaled.url).listener(holder.requestListener); + holder.binding.imageViewMarkdownImageAndGifBlock.setRatio((float) node.mediaMetadata.downscaled.y / node.mediaMetadata.downscaled.x); + } else { + imageRequestBuilder = glide.load(node.mediaMetadata.original.url).listener(holder.requestListener); + holder.binding.imageViewMarkdownImageAndGifBlock.setRatio((float) node.mediaMetadata.original.y / node.mediaMetadata.original.x); + } + if (blurImage) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(holder.binding.imageViewMarkdownImageAndGifBlock); -- cgit v1.2.3