From 80b4869c6a17bb305b5a5c843e53a8f71e14b7d4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 31 Jul 2020 17:20:54 +0800 Subject: Only save post feed scrolled position for front page. --- .../infinityforreddit/AppModule.java | 2 +- .../infinityforreddit/Fragment/PostFragment.java | 24 ++++------------------ .../infinityforreddit/Post/PostDataSource.java | 24 ++++++---------------- .../Settings/AdvancedPreferenceFragment.java | 2 +- .../Settings/MainPreferenceFragment.java | 2 +- .../Utils/SharedPreferencesUtils.java | 13 +++++------- app/src/main/res/values/strings.xml | 6 +++--- app/src/main/res/xml/advanced_preferences.xml | 4 ++-- app/src/main/res/xml/main_preferences.xml | 6 +++--- 9 files changed, 26 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 9b930855..d5e7dfad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -202,7 +202,7 @@ class AppModule { @Provides @Named("post_feed_scrolled_position_cache") SharedPreferences providePostFeedScrolledPositionSharedPreferences() { - return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + return mApplication.getSharedPreferences(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } @Provides 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 6a574860..9e795f5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -369,7 +369,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); - savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_POST_FEED_SCROLLED_POSITION, true); + savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, true); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { @@ -717,28 +717,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } private void saveCache() { - if (savePostFeedScrolledPosition && isShown && mAdapter != null) { - String key; + if (savePostFeedScrolledPosition && postType == PostDataSource.TYPE_FRONT_PAGE && mAdapter != null) { Post currentPost = mAdapter.getItemByPosition(maxPosition); if (currentPost != null) { - String accountNameForCache = accountName == null ? SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_ANONYMOUS : accountName; + String accountNameForCache = accountName == null ? SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS : accountName; + String key = accountNameForCache + SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_FRONT_PAGE_BASE; String value = currentPost.getFullName(); - switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: - key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE; - break; - case PostDataSource.TYPE_SUBREDDIT: - key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE + subredditName; - break; - case PostDataSource.TYPE_USER: - key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_USER_BASE + username; - break; - case PostDataSource.TYPE_MULTI_REDDIT: - key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE + multiRedditPath; - break; - default: - return; - } postFeedScrolledPositionSharedPreferences.edit().putString(key, value).apply(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java index 5ef80d0c..2b18ef2a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -175,39 +175,27 @@ public class PostDataSource extends PageKeyedDataSource { public void loadInitial(@NonNull LoadInitialParams params, @NonNull final LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); - boolean savePostFeedScrolledPosition = sharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_POST_FEED_SCROLLED_POSITION, true); - String accountNameForCache = accountName == null ? SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_ANONYMOUS : accountName; + boolean savePostFeedScrolledPosition = sharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, true); + String accountNameForCache = accountName == null ? SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS : accountName; switch (postType) { case TYPE_FRONT_PAGE: if (savePostFeedScrolledPosition) { - loadBestPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE, null)); + loadBestPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_FRONT_PAGE_BASE, null)); } else { loadBestPostsInitial(callback, null); } break; case TYPE_SUBREDDIT: - if (savePostFeedScrolledPosition) { - loadSubredditPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE + subredditOrUserName, null)); - } else { - loadSubredditPostsInitial(callback, null); - } + loadSubredditPostsInitial(callback, null); break; case TYPE_USER: - if (savePostFeedScrolledPosition) { - loadUserPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_USER_BASE + subredditOrUserName, null)); - } else { - loadUserPostsInitial(callback, null); - } + loadUserPostsInitial(callback, null); break; case TYPE_SEARCH: loadSearchPostsInitial(callback, null); break; case TYPE_MULTI_REDDIT: - if (savePostFeedScrolledPosition) { - loadMultiRedditPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE + multiRedditPath, null)); - } else { - loadMultiRedditPostsInitial(callback, null); - } + loadMultiRedditPostsInitial(callback, null); break; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java index 43779da2..4548a344 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java @@ -71,7 +71,7 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { Preference deleteSortTypePreference = findPreference(SharedPreferencesUtils.DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE); Preference deletePostLaoutPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE); Preference deleteAllThemesPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_THEMES_IN_DATABASE); - Preference deletePostFeedScrolledPositionsPreference = findPreference(SharedPreferencesUtils.DELETE_POST_FEED_SCROLLED_POSITIONS_IN_DATABASE); + Preference deletePostFeedScrolledPositionsPreference = findPreference(SharedPreferencesUtils.DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE); Preference resetAllSettingsPreference = findPreference(SharedPreferencesUtils.RESET_ALL_SETTINGS); if (deleteSubredditsPreference != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java index 65384fbd..3a11855a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -39,7 +39,7 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.main_preferences, rootKey); ((Infinity) activity.getApplication()).getAppComponent().inject(this); - SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_POST_FEED_SCROLLED_POSITION); + SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION); SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); 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 50294522..b3ce6be5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -73,12 +73,9 @@ public class SharedPreferencesUtils { public static final int POST_LAYOUT_CARD = 0; public static final int POST_LAYOUT_COMPACT = 1; - public static final String POST_FEED_SCROLLED_POSITION_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.post_feed_scrolled_position"; - public static final String POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE = "_front_page"; - public static final String POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE = "_subreddit_"; - public static final String POST_FEED_SCROLLED_POSITION_USER_BASE = "_user_"; - public static final String POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE = "_multireddit_"; - public static final String POST_FEED_SCROLLED_POSITION_ANONYMOUS = ".anonymous"; + public static final String FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.front_page_scrolled_position"; + public static final String FRONT_PAGE_SCROLLED_POSITION_FRONT_PAGE_BASE = "_front_page"; + public static final String FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS = ".anonymous"; public static final String PULL_NOTIFICATION_TIME = "pull_notification_time"; public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; @@ -119,7 +116,7 @@ public class SharedPreferencesUtils { public static final String DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE = "delete_all_sort_type_data_in_database"; public static final String DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE = "delete_all_post_layout_data_in_database"; public static final String DELETE_ALL_THEMES_IN_DATABASE = "delete_all_themes_in_database"; - public static final String DELETE_POST_FEED_SCROLLED_POSITIONS_IN_DATABASE = "delete_post_feed_scrolled_positions_in_database"; + public static final String DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE = "delete_front_page_scrolled_positions_in_database"; public static final String RESET_ALL_SETTINGS = "reset_all_settings"; public static final String MAIN_PAGE_TAB_1_TITLE = "main_page_tab_1_title"; public static final String MAIN_PAGE_TAB_2_TITLE = "main_page_tab_2_title"; @@ -140,5 +137,5 @@ public class SharedPreferencesUtils { public static final String START_AUTOPLAY_VISIBLE_AREA_OFFSET_LANDSCAPE = "start_autoplay_visible_area_offset_landscape"; public static final String MUTE_NSFW_VIDEO = "mute_nsfw_video"; public static final String VIDEO_PLAYER_IGNORE_NAV_BAR = "video_player_ignore_nav_bar"; - public static final String SAVE_POST_FEED_SCROLLED_POSITION = "save_post_feed_scrolled_position"; + public static final String SAVE_FRONT_PAGE_SCROLLED_POSITION = "save_front_page_scrolled_position"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0dbcce20..244e6739 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -348,8 +348,8 @@ Amoled Dark Interface Gestures & Buttons - Save Scrolled Position in Post Feed - Browse new posts after refreshing + Save Scrolled Position in HOME + Browse new posts after refreshing in HOME (Front Page) Open Link In App Video Video Autoplay @@ -458,7 +458,7 @@ Delete All Sort Types in Database Delete All Post Layouts in Database Delete All Themes in Database - Delete All Post Feed Scrolled Positions in Database + Delete All Front Page Scrolled Positions in Database Reset All Settings Clean the database and shared preferences Restart the app to see the changes diff --git a/app/src/main/res/xml/advanced_preferences.xml b/app/src/main/res/xml/advanced_preferences.xml index 224433ba..d890159f 100644 --- a/app/src/main/res/xml/advanced_preferences.xml +++ b/app/src/main/res/xml/advanced_preferences.xml @@ -23,8 +23,8 @@ app:title="@string/settings_delete_all_themes_in_database_title" /> + app:key="delete_front_page_scrolled_positions_in_database" + app:title="@string/settings_delete_front_page_scrolled_positions_in_database_title" /> + app:key="save_front_page_scrolled_position" + app:title="@string/settings_save_front_page_scrolled_position_title" + app:summary="@string/settings_save_front_page_scrolled_position_summary" />