diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-07-30 15:12:34 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-07-30 15:12:34 +0000 |
commit | 61d3302a309c888d4a0718ded3b83518f10d3630 (patch) | |
tree | 0c78fccaae250bd8fc3ac614c0ff0acf4f1d67f8 | |
parent | bb598531b5b575ca03d7efc4e79ab02a4b882bd8 (diff) | |
download | infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar.gz infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar.bz2 infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar.lz infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar.xz infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.tar.zst infinity-for-reddit-61d3302a309c888d4a0718ded3b83518f10d3630.zip |
Use SharedPreferences to store post feed scrolled position instead of QNCache. Add an option to save the scrolled position.
12 files changed, 208 insertions, 126 deletions
diff --git a/app/build.gradle b/app/build.gradle index 2c2a225e..9bed2fff 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -95,7 +95,6 @@ dependencies { implementation 'me.zhanghai.android.fastscroll:library:1.1.2' implementation "com.thefuntasty.hauler:core:3.1.0" implementation 'com.github.Piasy:BigImageViewer:1.6.5' - implementation 'com.fewlaps.quitnowcache:quitnow-cache:3.4.0' def toroVersion = '3.7.0.2010003' implementation "im.ene.toro3:toro:$toroVersion" diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index fbbba343..9b930855 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -6,7 +6,6 @@ import android.content.SharedPreferences; import androidx.preference.PreferenceManager; -import com.fewlaps.quitnowcache.QNCache; import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; import com.google.android.exoplayer2.upstream.cache.SimpleCache; @@ -24,8 +23,8 @@ import im.ene.toro.exoplayer.ExoCreator; import im.ene.toro.exoplayer.MediaSourceBuilder; import im.ene.toro.exoplayer.ToroExo; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; @@ -201,6 +200,12 @@ class AppModule { } @Provides + @Named("post_feed_scrolled_position_cache") + SharedPreferences providePostFeedScrolledPositionSharedPreferences() { + return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + } + + @Provides @Singleton CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, @Named("dark_theme") SharedPreferences darkThemeSharedPreferences, @@ -217,10 +222,4 @@ class AppModule { .build(); return ToroExo.with(mApplication).getCreator(config); } - - @Provides - @Singleton - QNCache<String> provideQNCache() { - return new QNCache.Builder().build(); - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSavePostFeedScrolledPositionEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSavePostFeedScrolledPositionEvent.java new file mode 100644 index 00000000..5dd363eb --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSavePostFeedScrolledPositionEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeSavePostFeedScrolledPositionEvent { + public boolean savePostFeedScrolledPosition; + + public ChangeSavePostFeedScrolledPositionEvent(boolean savePostFeedScrolledPosition) { + this.savePostFeedScrolledPosition = savePostFeedScrolledPosition; + } +} 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 d825c619..6a574860 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -38,7 +38,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; -import com.fewlaps.quitnowcache.QNCache; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -67,6 +66,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeMuteAutoplayingVideosEve import ml.docilealligator.infinityforreddit.Event.ChangeMuteNSFWVideoEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; @@ -87,7 +87,6 @@ import ml.docilealligator.infinityforreddit.Post.PostViewModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.Utils.CacheUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Retrofit; @@ -147,7 +146,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Inject ExoCreator exoCreator; @Inject - QNCache<String> cache; + @Named("post_feed_scrolled_position_cache") + SharedPreferences postFeedScrolledPositionSharedPreferences; private RequestManager mGlide; private AppCompatActivity activity; private LinearLayoutManager mLinearLayoutManager; @@ -157,6 +157,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean isLazyModePaused = false; private boolean hasPost = false; private boolean isShown = false; + private boolean savePostFeedScrolledPosition; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; private Window window; @@ -368,6 +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); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { @@ -407,12 +409,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (accessToken == null) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, - accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType, - sortType, filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType, - sortType, filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_SUBREDDIT) { subredditName = getArguments().getString(EXTRA_NAME); @@ -475,12 +477,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (accessToken == null) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); } } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { multiRedditPath = getArguments().getString(EXTRA_NAME); @@ -529,12 +531,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (accessToken == null) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, - accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_USER) { username = getArguments().getString(EXTRA_USER_NAME); @@ -582,12 +584,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (accessToken == null) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where, - filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); } } else { String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name()); @@ -625,7 +627,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, cache, postType, sortType, filter, nsfw)).get(PostViewModel.class); + accountName, getResources().getConfiguration().locale, mSharedPreferences, postFeedScrolledPositionSharedPreferences, + postType, sortType, filter, nsfw)).get(PostViewModel.class); } if (activity instanceof ActivityToolbarInterface) { @@ -714,29 +717,29 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } private void saveCache() { - if (isShown && mAdapter != null) { + if (savePostFeedScrolledPosition && isShown && mAdapter != null) { String key; Post currentPost = mAdapter.getItemByPosition(maxPosition); if (currentPost != null) { - String accountNameForCache = accountName == null ? CacheUtils.ANONYMOUS : accountName; + String accountNameForCache = accountName == null ? SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_ANONYMOUS : accountName; String value = currentPost.getFullName(); switch (postType) { case PostDataSource.TYPE_FRONT_PAGE: - key = accountNameForCache + CacheUtils.FRONT_PAGE_BASE; + key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE; break; case PostDataSource.TYPE_SUBREDDIT: - key = accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditName; + key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE + subredditName; break; case PostDataSource.TYPE_USER: - key = accountNameForCache + CacheUtils.USER_BASE + username; + key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_USER_BASE + username; break; case PostDataSource.TYPE_MULTI_REDDIT: - key = accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath; + key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE + multiRedditPath; break; default: return; } - cache.set(key, value); + postFeedScrolledPositionSharedPreferences.edit().putString(key, value).apply(); } } } @@ -1042,6 +1045,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Subscribe + public void onChangeSavePostFeedScrolledPositionEvent(ChangeSavePostFeedScrolledPositionEvent changeSavePostFeedScrolledPositionEvent) { + savePostFeedScrolledPosition = changeSavePostFeedScrolledPositionEvent.savePostFeedScrolledPosition; + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { 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 e886d731..5ef80d0c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -1,11 +1,11 @@ package ml.docilealligator.infinityforreddit.Post; +import android.content.SharedPreferences; + import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; -import com.fewlaps.quitnowcache.QNCache; - import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; @@ -15,7 +15,7 @@ import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.Utils.APIUtils; -import ml.docilealligator.infinityforreddit.Utils.CacheUtils; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -39,7 +39,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private String accessToken; private String accountName; private Locale locale; - private QNCache<String> cache; + private SharedPreferences sharedPreferences; + private SharedPreferences postFeedScrolledPositionSharedPreferences; private String subredditOrUserName; private String query; private int postType; @@ -57,13 +58,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private LoadParams<String> params; private LoadCallback<String, Post> callback; - PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - int postType, SortType sortType, int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, + SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); @@ -74,13 +77,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String path, int postType, SortType sortType, int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String path, int postType, + SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; if (postType == TYPE_SUBREDDIT) { this.subredditOrUserName = path; } else { @@ -112,14 +117,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditOrUserName, int postType, SortType sortType, String where, int filter, - boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, + int postType, SortType sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditOrUserName = subredditOrUserName; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); @@ -132,14 +138,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditOrUserName, String query, int postType, SortType sortType, int filter, - boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, + String query, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditOrUserName = subredditOrUserName; this.query = query; paginationNetworkStateLiveData = new MutableLiveData<>(); @@ -168,22 +175,39 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); - String accountNameForCache = accountName == null ? CacheUtils.ANONYMOUS : accountName; + boolean savePostFeedScrolledPosition = sharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_POST_FEED_SCROLLED_POSITION, true); + String accountNameForCache = accountName == null ? SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_ANONYMOUS : accountName; switch (postType) { case TYPE_FRONT_PAGE: - loadBestPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.FRONT_PAGE_BASE)); + if (savePostFeedScrolledPosition) { + loadBestPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE, null)); + } else { + loadBestPostsInitial(callback, null); + } break; case TYPE_SUBREDDIT: - loadSubredditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditOrUserName)); + if (savePostFeedScrolledPosition) { + loadSubredditPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE + subredditOrUserName, null)); + } else { + loadSubredditPostsInitial(callback, null); + } break; case TYPE_USER: - loadUserPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.USER_BASE + subredditOrUserName)); + if (savePostFeedScrolledPosition) { + loadUserPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_USER_BASE + subredditOrUserName, null)); + } else { + loadUserPostsInitial(callback, null); + } break; case TYPE_SEARCH: loadSearchPostsInitial(callback, null); break; case TYPE_MULTI_REDDIT: - loadMultiRedditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath)); + if (savePostFeedScrolledPosition) { + loadMultiRedditPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE + multiRedditPath, null)); + } else { + loadMultiRedditPostsInitial(callback, null); + } break; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java index 6ed8a51c..39c4d6b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java @@ -1,11 +1,11 @@ package ml.docilealligator.infinityforreddit.Post; +import android.content.SharedPreferences; + import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; -import com.fewlaps.quitnowcache.QNCache; - import java.util.Locale; import ml.docilealligator.infinityforreddit.SortType; @@ -16,7 +16,8 @@ class PostDataSourceFactory extends DataSource.Factory { private String accessToken; private String accountName; private Locale locale; - private QNCache<String> cache; + private SharedPreferences sharedPreferences; + private SharedPreferences postFeedScrolledPositionSharedPreferences; private String subredditName; private String query; private int postType; @@ -28,13 +29,15 @@ class PostDataSourceFactory extends DataSource.Factory { private PostDataSource postDataSource; private MutableLiveData<PostDataSource> postDataSourceLiveData; - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; this.sortType = sortType; @@ -42,13 +45,15 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; @@ -57,14 +62,16 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, int postType, SortType sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; @@ -74,14 +81,16 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, String query, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; this.query = query; postDataSourceLiveData = new MutableLiveData<>(); @@ -95,17 +104,17 @@ class PostDataSourceFactory extends DataSource.Factory { @Override public DataSource<String, Post> create() { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, - postType, sortType, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, + sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw); } else if (postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, - subredditName, query, postType, sortType, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, - subredditName, postType, sortType, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw); } else { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, - subredditName, postType, sortType, userWhere, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw); } postDataSourceLiveData.postValue(postDataSource); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java index 3da11f53..c141aa8a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit.Post; +import android.content.SharedPreferences; + import androidx.annotation.NonNull; import androidx.core.util.Pair; import androidx.lifecycle.LiveData; @@ -11,8 +13,6 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; -import com.fewlaps.quitnowcache.QNCache; - import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; @@ -29,10 +29,11 @@ public class PostViewModel extends ViewModel { private MutableLiveData<SortType> sortTypeLiveData; private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData; - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - int postType, SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, - postType, sortType, filter, nsfw); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences cache, int postType, + SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, + sharedPreferences, cache, postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -60,10 +61,11 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, - subredditName, postType, sortType, filter, nsfw); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, + int postType, SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, + sharedPreferences, cache, subredditName, postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -91,11 +93,12 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, int postType, SortType sortType, String where, int filter, + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, + int postType, SortType sortType, String where, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, - subredditName, postType, sortType, where, filter, nsfw); + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, + sharedPreferences, cache, subredditName, postType, sortType, where, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -123,11 +126,12 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, String query, int postType, SortType sortType, int filter, + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, + String query, int postType, SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, - subredditName, query, postType, sortType, filter, nsfw); + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, + sharedPreferences, cache, subredditName, query, postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -192,7 +196,8 @@ public class PostViewModel extends ViewModel { private String accessToken; private String accountName; private Locale locale; - private QNCache<String> cache; + private SharedPreferences sharedPreferences; + private SharedPreferences postFeedScrolledPositionSharedPreferences; private String subredditName; private String query; private int postType; @@ -201,26 +206,30 @@ public class PostViewModel extends ViewModel { private int filter; private boolean nsfw; - public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.postType = postType; this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; } - public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; this.postType = postType; this.sortType = sortType; @@ -229,14 +238,16 @@ public class PostViewModel extends ViewModel { } //User posts - public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, int postType, SortType sortType, String where, int filter, + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + int postType, SortType sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; this.postType = postType; this.sortType = sortType; @@ -245,14 +256,15 @@ public class PostViewModel extends ViewModel { this.nsfw = nsfw; } - public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, - String subredditName, String query, int postType, SortType sortType, int filter, - boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + String query, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.locale = locale; - this.cache = cache; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; this.subredditName = subredditName; this.query = query; this.postType = postType; @@ -265,17 +277,17 @@ public class PostViewModel extends ViewModel { @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, postType, - sortType, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, + postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw); } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, - query, postType, sortType, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, - postType, sortType, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw); } else { - return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, - postType, sortType, userWhere, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw); } } } 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 aa1b77d0..65384fbd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -17,6 +17,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; @@ -28,6 +29,9 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { @Inject @Named("default") SharedPreferences sharedPreferences; + @Inject + @Named("post_feed_scrolled_position_cache") + SharedPreferences cache; private Activity activity; @Override @@ -35,11 +39,22 @@ 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 confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY); + if (savePostFeedScrolledPositionSwitch != null) { + savePostFeedScrolledPositionSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + if (!(Boolean) newValue) { + cache.edit().clear().apply(); + } + EventBus.getDefault().post(new ChangeSavePostFeedScrolledPositionEvent((Boolean) newValue)); + return true; + }); + } + if (confirmToExitSwitch != null) { confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new RecreateActivityEvent()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java deleted file mode 100644 index e0280204..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java +++ /dev/null @@ -1,9 +0,0 @@ -package ml.docilealligator.infinityforreddit.Utils; - -public class CacheUtils { - public static final String FRONT_PAGE_BASE = "_front_page"; - public static final String SUBREDDIT_BASE = "_subreddit_"; - public static final String USER_BASE = "_user_"; - public static final String MULTI_REDDIT_BASE = "_multireddit_"; - public static final String ANONYMOUS = ".anonymous"; -} 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 768db21a..783ab80c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -73,6 +73,13 @@ 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 PULL_NOTIFICATION_TIME = "pull_notification_time"; public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; public static final String TIME_FORMAT_KEY = "time_format"; @@ -132,4 +139,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"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 05c1c9de..9d918d75 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -348,6 +348,8 @@ <string name="settings_amoled_dark_title">Amoled Dark</string> <string name="settings_interface_title">Interface</string> <string name="settings_gestures_and_buttons_title">Gestures & Buttons</string> + <string name="settings_save_post_feed_scrolled_position_title">Save Scrolled Position in Post Feed</string> + <string name="settings_save_post_feed_scrolled_position_summary">Browse new posts after refreshing</string> <string name="settings_open_link_in_app_title">Open Link In App</string> <string name="settigns_video_title">Video</string> <string name="settings_video_autoplay_title">Video Autoplay</string> diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index af3f3898..3b1b129f 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -37,6 +37,12 @@ app:useSimpleSummaryProvider="true" /> <SwitchPreference + app:defaultValue="true" + app:key="save_post_feed_scrolled_position" + app:title="@string/settings_save_post_feed_scrolled_position_title" + app:summary="@string/settings_save_post_feed_scrolled_position_summary" /> + + <SwitchPreference app:defaultValue="false" app:icon="@drawable/ic_link" app:key="open_link_in_app" |