From 141abc654a66c003351091d9df9f94229d9eb9ce Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Sun, 6 Mar 2022 10:41:02 +0800 Subject: New option: Settings->Miscellaneous->Post Feed Preview Max Resolution. Fix low resolution post previews when showing them for the first time. Tweak themed icon. --- .../SaveMemoryCenterInisdeDownsampleStrategy.java | 14 +++- .../adapters/PostDetailRecyclerViewAdapter.java | 12 ++-- .../adapters/PostRecyclerViewAdapter.java | 75 ++++++++++++++++++---- .../customviews/AspectRatioGifImageView.java | 1 + .../customviews/CustomFontEditTextPreference.java | 52 +++++++++++++++ .../events/ChangePostFeedMaxResolutionEvent.java | 9 +++ .../infinityforreddit/fragments/PostFragment.java | 9 +++ .../settings/MiscellaneousPreferenceFragment.java | 21 ++++++ .../utils/SharedPreferencesUtils.java | 1 + .../drawable/ic_launcher_foreground_monochrome.xml | 51 ++++++++------- app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/miscellaneous_preferences.xml | 14 ++++ 12 files changed, 220 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CustomFontEditTextPreference.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/ChangePostFeedMaxResolutionEvent.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java index 8516c53e..acb38267 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java @@ -4,16 +4,22 @@ import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy; public class SaveMemoryCenterInisdeDownsampleStrategy extends DownsampleStrategy { + private int threshold; + + public SaveMemoryCenterInisdeDownsampleStrategy(int threshold) { + this.threshold = threshold; + } + @Override public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { int originalSourceWidth = sourceWidth; int originalSourceHeight = sourceHeight; - if (sourceWidth * sourceHeight > 5_000_000) { + if (sourceWidth * sourceHeight > threshold) { int divisor = 2; do { sourceWidth /= divisor; sourceHeight /= divisor; - } while (sourceWidth * sourceHeight > 5_000_000); + } while (sourceWidth * sourceHeight > threshold); } float widthPercentage = (float) requestedWidth / (float) sourceWidth; @@ -26,4 +32,8 @@ public class SaveMemoryCenterInisdeDownsampleStrategy extends DownsampleStrategy public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { return SampleSizeRounding.MEMORY; } + + public void setThreshold(int threshold) { + this.threshold = threshold; + } } 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 529bed8d..983c887a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -141,6 +141,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) @@ -944,7 +946,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) @@ -972,7 +974,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) @@ -999,7 +1001,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter 5_000_000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() > mMaxResolution) { for (int i = previews.size() - 1; i >= 1; i--) { preview = previews.get(i); - if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 5_000_000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() <= mMaxResolution) { return preview; } } @@ -1513,7 +1555,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { @@ -65,5 +69,22 @@ public class MiscellaneousPreferenceFragment extends CustomFontPreferenceFragmen return true; }); } + + if (postFeedMaxResolution != null) { + postFeedMaxResolution.setOnPreferenceChangeListener((preference, newValue) -> { + try { + int resolution = Integer.parseInt((String) newValue); + if (resolution <= 0) { + Toast.makeText(activity, R.string.not_a_valid_number, Toast.LENGTH_SHORT).show(); + return false; + } + EventBus.getDefault().post(new ChangePostFeedMaxResolutionEvent(resolution)); + } catch (NumberFormatException e) { + Toast.makeText(activity, R.string.not_a_valid_number, Toast.LENGTH_SHORT).show(); + return false; + } + return true; + }); + } } } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 4ade7642..9417352d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -208,6 +208,7 @@ public class SharedPreferencesUtils { public static final String SHOW_AUTHOR_AVATAR = "show_author_avatar"; public static final String ALWAYS_SHOW_CHILD_COMMENT_COUNT = "always_show_child_comment_count"; public static final String HIDE_UPVOTE_RATIO = "hide_upvote_ratio"; + public static final String POST_FEED_MAX_RESOLUTION = "post_feed_max_resolution"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; diff --git a/app/src/main/res/drawable/ic_launcher_foreground_monochrome.xml b/app/src/main/res/drawable/ic_launcher_foreground_monochrome.xml index ea998cae..40fde1ad 100644 --- a/app/src/main/res/drawable/ic_launcher_foreground_monochrome.xml +++ b/app/src/main/res/drawable/ic_launcher_foreground_monochrome.xml @@ -1,24 +1,27 @@ - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e0a00dca..1293fe57 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -637,6 +637,9 @@ Reddit User Agreement Always Show the Number of Child Comments Hide Upvote Ratio + Dangerous + Increase the value to show previews in higher resolution, but the app may crash unexpectedly. + Post Feed Preview Max Resolution (Width * Height) Cannot get the link @@ -1283,4 +1286,6 @@ Option 5 Option 6 + Not a valid number + diff --git a/app/src/main/res/xml/miscellaneous_preferences.xml b/app/src/main/res/xml/miscellaneous_preferences.xml index 132df3d8..1ae7ed8f 100644 --- a/app/src/main/res/xml/miscellaneous_preferences.xml +++ b/app/src/main/res/xml/miscellaneous_preferences.xml @@ -40,4 +40,18 @@ app:title="@string/settings_language_title" app:useSimpleSummaryProvider="true" /> + + + + + + \ No newline at end of file -- cgit v1.2.3