From a4b95aee241b1d4f79b709ce06b95b0818a130af Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 7 Nov 2020 11:24:43 +0800 Subject: [PATCH] New option: Disable Image Preview in Data Saving Mode. --- .../CommentAndPostRecyclerViewAdapter.java | 106 +++++++++- .../Adapter/PostRecyclerViewAdapter.java | 192 ++++++++++-------- .../Event/ChangeDisableImagePreviewEvent.java | 9 + .../Fragment/PostFragment.java | 9 + .../DataSavingModePreferenceFragment.java | 10 + .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/drawable/ic_gallery_24dp.xml | 5 + app/src/main/res/values/strings.xml | 1 + .../res/xml/data_saving_mode_preferences.xml | 13 +- 9 files changed, 246 insertions(+), 100 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableImagePreviewEvent.java create mode 100644 app/src/main/res/drawable/ic_gallery_24dp.xml 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 0e6fabdb..df789268 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -178,6 +178,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(mActivity, LinkResolverActivity.class); - Uri uri = Uri.parse(mPost.getUrl()); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl())); - } else { - intent.setData(uri); + if (mPost != null) { + if (mPost.getPostType() == Post.VIDEO_TYPE) { + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + if (mPost.isGfycat()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + } else if (mPost.isRedgifs()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + } else { + intent.setData(Uri.parse(mPost.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + } + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, mPost.getTitle()); + intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, mPost.isNSFW()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.IMAGE_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mPost.getUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() + + "-" + mPost.getId() + ".jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.GIF_TYPE){ + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() + + "-" + mPost.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.LINK_TYPE || mPost.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(mPost.getUrl()); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl())); + } else { + intent.setData(uri); + } + intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.GALLERY_TYPE) { + Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); + intent.putParcelableArrayListExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, mPost.getGallery()); + intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); + mActivity.startActivity(intent); + } } - intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); - mActivity.startActivity(intent); }); } } 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 73d7bb8d..b2ebe254 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -187,6 +187,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (post.getPostType() == Post.GALLERY_TYPE) { - Intent intent = new Intent(mActivity, ViewRedditGalleryActivity.class); - intent.putExtra(ViewRedditGalleryActivity.EXTRA_REDDIT_GALLERY, post.getGallery()); - intent.putExtra(ViewRedditGalleryActivity.EXTRA_SUBREDDIT_NAME, post.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); + intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, post.isNSFW()); mActivity.startActivity(intent); - } else { + } else if (post.getPostType() == Post.IMAGE_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, post.getUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, post.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); + mActivity.startActivity(intent); + } else if (post.getPostType() == Post.GIF_TYPE){ + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, post.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); + mActivity.startActivity(intent); + } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(post.getUrl()); if (uri.getScheme() == null && uri.getHost() == null) { @@ -2556,10 +2567,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + imageView.performClick(); + }); + upvoteButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableImagePreviewEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableImagePreviewEvent.java new file mode 100644 index 00000000..85c7122d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableImagePreviewEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeDisableImagePreviewEvent { + public boolean disableImagePreview; + + public ChangeDisableImagePreviewEvent(boolean disableImagePreview) { + this.disableImagePreview = disableImagePreview; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 49da464a..e431341a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -74,6 +74,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeAutoplayNsfwVideosEvent; import ml.docilealligator.infinityforreddit.Event.ChangeCompactLayoutToolbarHiddenByDefaultEvent; import ml.docilealligator.infinityforreddit.Event.ChangeDataSavingModeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeDefaultPostLayoutEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeDisableImagePreviewEvent; import ml.docilealligator.infinityforreddit.Event.ChangeEnableSwipeActionSwitchEvent; import ml.docilealligator.infinityforreddit.Event.ChangeLongPressToHideToolbarInCompactLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeMuteAutoplayingVideosEvent; @@ -1306,6 +1307,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Subscribe + public void onChangeDisableImagePreviewEvent(ChangeDisableImagePreviewEvent changeDisableImagePreviewEvent) { + if (mAdapter != null) { + mAdapter.setDisableImagePreview(changeDisableImagePreviewEvent.disableImagePreview); + refreshAdapter(); + } + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java index b057ad3a..caded180 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java @@ -4,10 +4,12 @@ import android.os.Bundle; import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.Event.ChangeDataSavingModeEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeDisableImagePreviewEvent; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -18,6 +20,7 @@ public class DataSavingModePreferenceFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.data_saving_mode_preferences, rootKey); ListPreference dataSavingModeListPreference = findPreference(SharedPreferencesUtils.DATA_SAVING_MODE); + SwitchPreference disableImagePreviewPreference = findPreference(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW); if (dataSavingModeListPreference != null) { dataSavingModeListPreference.setOnPreferenceChangeListener((preference, newValue) -> { @@ -25,5 +28,12 @@ public class DataSavingModePreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (disableImagePreviewPreference != null) { + disableImagePreviewPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeDisableImagePreviewEvent((Boolean) newValue)); + 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 5a85e10c..b4d36d1b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -139,6 +139,7 @@ public class SharedPreferencesUtils { public static final String SUBREDDIT_FILTER_POPULAR_AND_ALL = "subreddit_filter_popular_and_all"; public static final String UFO_CAPTURING_ANIMATION = "ufo_capturing_animation"; public static final String HIDE_SUBREDDIT_DESCRIPTION = "hide_subreddit_description"; + public static final String DISABLE_IMAGE_PREVIEW = "disable_image_preview"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; diff --git a/app/src/main/res/drawable/ic_gallery_24dp.xml b/app/src/main/res/drawable/ic_gallery_24dp.xml new file mode 100644 index 00000000..d54dba10 --- /dev/null +++ b/app/src/main/res/drawable/ic_gallery_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9041a5d1..c41f082e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -503,6 +503,7 @@ In r/popular and r/all UFO Capturing Animation Hide Subreddit Description + Disable Image Preview in Data Saving Mode Cannot get the link diff --git a/app/src/main/res/xml/data_saving_mode_preferences.xml b/app/src/main/res/xml/data_saving_mode_preferences.xml index f120afe5..e635f2db 100644 --- a/app/src/main/res/xml/data_saving_mode_preferences.xml +++ b/app/src/main/res/xml/data_saving_mode_preferences.xml @@ -2,6 +2,11 @@ + + - + \ No newline at end of file