Use SharedPreferences to store post feed scrolled position instead of QNCache. Add an option to save the scrolled position.

This commit is contained in:
Alex Ning 2020-07-30 23:12:34 +08:00
parent bb598531b5
commit 61d3302a30
12 changed files with 234 additions and 152 deletions

View File

@ -95,7 +95,6 @@ dependencies {
implementation 'me.zhanghai.android.fastscroll:library:1.1.2' implementation 'me.zhanghai.android.fastscroll:library:1.1.2'
implementation "com.thefuntasty.hauler:core:3.1.0" implementation "com.thefuntasty.hauler:core:3.1.0"
implementation 'com.github.Piasy:BigImageViewer:1.6.5' implementation 'com.github.Piasy:BigImageViewer:1.6.5'
implementation 'com.fewlaps.quitnowcache:quitnow-cache:3.4.0'
def toroVersion = '3.7.0.2010003' def toroVersion = '3.7.0.2010003'
implementation "im.ene.toro3:toro:$toroVersion" implementation "im.ene.toro3:toro:$toroVersion"

View File

@ -6,7 +6,6 @@ import android.content.SharedPreferences;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.fewlaps.quitnowcache.QNCache;
import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache; 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.MediaSourceBuilder;
import im.ene.toro.exoplayer.ToroExo; import im.ene.toro.exoplayer.ToroExo;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils;
import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import okhttp3.ConnectionPool; import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -200,6 +199,12 @@ class AppModule {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
} }
@Provides
@Named("post_feed_scrolled_position_cache")
SharedPreferences providePostFeedScrolledPositionSharedPreferences() {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
}
@Provides @Provides
@Singleton @Singleton
CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences,
@ -217,10 +222,4 @@ class AppModule {
.build(); .build();
return ToroExo.with(mApplication).getCreator(config); return ToroExo.with(mApplication).getCreator(config);
} }
@Provides
@Singleton
QNCache<String> provideQNCache() {
return new QNCache.Builder().build();
}
} }

View File

@ -0,0 +1,9 @@
package ml.docilealligator.infinityforreddit.Event;
public class ChangeSavePostFeedScrolledPositionEvent {
public boolean savePostFeedScrolledPosition;
public ChangeSavePostFeedScrolledPositionEvent(boolean savePostFeedScrolledPosition) {
this.savePostFeedScrolledPosition = savePostFeedScrolledPosition;
}
}

View File

@ -38,7 +38,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import com.fewlaps.quitnowcache.QNCache;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; 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.ChangeMuteNSFWVideoEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent;
import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; 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.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.Utils.CacheUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.Utils.Utils;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -147,7 +146,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Inject @Inject
ExoCreator exoCreator; ExoCreator exoCreator;
@Inject @Inject
QNCache<String> cache; @Named("post_feed_scrolled_position_cache")
SharedPreferences postFeedScrolledPositionSharedPreferences;
private RequestManager mGlide; private RequestManager mGlide;
private AppCompatActivity activity; private AppCompatActivity activity;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
@ -157,6 +157,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private boolean isLazyModePaused = false; private boolean isLazyModePaused = false;
private boolean hasPost = false; private boolean hasPost = false;
private boolean isShown = false; private boolean isShown = false;
private boolean savePostFeedScrolledPosition;
private PostRecyclerViewAdapter mAdapter; private PostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller smoothScroller; private RecyclerView.SmoothScroller smoothScroller;
private Window window; private Window window;
@ -368,6 +369,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); 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; Locale locale = getResources().getConfiguration().locale;
if (postType == PostDataSource.TYPE_SEARCH) { if (postType == PostDataSource.TYPE_SEARCH) {
@ -407,12 +409,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null,
accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
sortType, filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} else { } else {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
sortType, filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} }
} else if (postType == PostDataSource.TYPE_SUBREDDIT) { } else if (postType == PostDataSource.TYPE_SUBREDDIT) {
subredditName = getArguments().getString(EXTRA_NAME); subredditName = getArguments().getString(EXTRA_NAME);
@ -475,12 +477,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} else { } else {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} }
} else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) {
multiRedditPath = getArguments().getString(EXTRA_NAME); multiRedditPath = getArguments().getString(EXTRA_NAME);
@ -529,12 +531,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null,
accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} else { } else {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} }
} else if (postType == PostDataSource.TYPE_USER) { } else if (postType == PostDataSource.TYPE_USER) {
username = getArguments().getString(EXTRA_USER_NAME); username = getArguments().getString(EXTRA_USER_NAME);
@ -582,12 +584,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class);
} else { } else {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken,
accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where, accountName, getResources().getConfiguration().locale, mSharedPreferences,
filter, nsfw)).get(PostViewModel.class); postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class);
} }
} else { } else {
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name()); 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, 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) { if (activity instanceof ActivityToolbarInterface) {
@ -714,29 +717,29 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
private void saveCache() { private void saveCache() {
if (isShown && mAdapter != null) { if (savePostFeedScrolledPosition && isShown && mAdapter != null) {
String key; String key;
Post currentPost = mAdapter.getItemByPosition(maxPosition); Post currentPost = mAdapter.getItemByPosition(maxPosition);
if (currentPost != null) { 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(); String value = currentPost.getFullName();
switch (postType) { switch (postType) {
case PostDataSource.TYPE_FRONT_PAGE: case PostDataSource.TYPE_FRONT_PAGE:
key = accountNameForCache + CacheUtils.FRONT_PAGE_BASE; key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_FRONT_PAGE_BASE;
break; break;
case PostDataSource.TYPE_SUBREDDIT: case PostDataSource.TYPE_SUBREDDIT:
key = accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditName; key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_SUBREDDIT_BASE + subredditName;
break; break;
case PostDataSource.TYPE_USER: case PostDataSource.TYPE_USER:
key = accountNameForCache + CacheUtils.USER_BASE + username; key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_USER_BASE + username;
break; break;
case PostDataSource.TYPE_MULTI_REDDIT: case PostDataSource.TYPE_MULTI_REDDIT:
key = accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath; key = accountNameForCache + SharedPreferencesUtils.POST_FEED_SCROLLED_POSITION_MULTI_REDDIT_BASE + multiRedditPath;
break; break;
default: default:
return; 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() { private void refreshAdapter() {
int previousPosition = -1; int previousPosition = -1;
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {

View File

@ -1,11 +1,11 @@
package ml.docilealligator.infinityforreddit.Post; package ml.docilealligator.infinityforreddit.Post;
import android.content.SharedPreferences;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.paging.PageKeyedDataSource; import androidx.paging.PageKeyedDataSource;
import com.fewlaps.quitnowcache.QNCache;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -15,7 +15,7 @@ import ml.docilealligator.infinityforreddit.API.RedditAPI;
import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.APIUtils;
import ml.docilealligator.infinityforreddit.Utils.CacheUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -39,7 +39,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
private String accessToken; private String accessToken;
private String accountName; private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache; private SharedPreferences sharedPreferences;
private SharedPreferences postFeedScrolledPositionSharedPreferences;
private String subredditOrUserName; private String subredditOrUserName;
private String query; private String query;
private int postType; private int postType;
@ -57,13 +58,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, Post> callback; private LoadCallback<String, Post> callback;
PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale,
int postType, SortType sortType, int filter, boolean nsfw) { SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType,
SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>();
@ -74,13 +77,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String path, int postType, SortType sortType, int filter, boolean nsfw) { SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String path, int postType,
SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
if (postType == TYPE_SUBREDDIT) { if (postType == TYPE_SUBREDDIT) {
this.subredditOrUserName = path; this.subredditOrUserName = path;
} else { } else {
@ -112,14 +117,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditOrUserName, int postType, SortType sortType, String where, int filter, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName,
boolean nsfw) { int postType, SortType sortType, String where, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>();
@ -132,14 +138,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditOrUserName, String query, int postType, SortType sortType, int filter, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName,
boolean nsfw) { String query, int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
this.query = query; this.query = query;
paginationNetworkStateLiveData = new MutableLiveData<>(); 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) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING); 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) { switch (postType) {
case TYPE_FRONT_PAGE: 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; break;
case TYPE_SUBREDDIT: 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; break;
case TYPE_USER: 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; break;
case TYPE_SEARCH: case TYPE_SEARCH:
loadSearchPostsInitial(callback, null); loadSearchPostsInitial(callback, null);
break; break;
case TYPE_MULTI_REDDIT: 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; break;
} }
} }

View File

@ -1,11 +1,11 @@
package ml.docilealligator.infinityforreddit.Post; package ml.docilealligator.infinityforreddit.Post;
import android.content.SharedPreferences;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.paging.DataSource; import androidx.paging.DataSource;
import com.fewlaps.quitnowcache.QNCache;
import java.util.Locale; import java.util.Locale;
import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortType;
@ -16,7 +16,8 @@ class PostDataSourceFactory extends DataSource.Factory {
private String accessToken; private String accessToken;
private String accountName; private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache; private SharedPreferences sharedPreferences;
private SharedPreferences postFeedScrolledPositionSharedPreferences;
private String subredditName; private String subredditName;
private String query; private String query;
private int postType; private int postType;
@ -28,13 +29,15 @@ class PostDataSourceFactory extends DataSource.Factory {
private PostDataSource postDataSource; private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData; 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) { int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -42,13 +45,15 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; 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) { String subredditName, int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
@ -57,14 +62,16 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; 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, String subredditName, int postType, SortType sortType, String where, int filter,
boolean nsfw) { boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
@ -74,14 +81,16 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; 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, String subredditName, String query, int postType, SortType sortType, int filter,
boolean nsfw) { boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
this.query = query; this.query = query;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
@ -95,17 +104,17 @@ class PostDataSourceFactory extends DataSource.Factory {
@Override @Override
public DataSource<String, Post> create() { public DataSource<String, Post> create() {
if (postType == PostDataSource.TYPE_FRONT_PAGE) { if (postType == PostDataSource.TYPE_FRONT_PAGE) {
postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale,
postType, sortType, filter, nsfw); sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) { } else if (postType == PostDataSource.TYPE_SEARCH) {
postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale,
subredditName, query, postType, sortType, filter, nsfw); sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale,
subredditName, postType, sortType, filter, nsfw); sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw);
} else { } else {
postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale,
subredditName, postType, sortType, userWhere, filter, nsfw); sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw);
} }
postDataSourceLiveData.postValue(postDataSource); postDataSourceLiveData.postValue(postDataSource);

View File

@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.Post; package ml.docilealligator.infinityforreddit.Post;
import android.content.SharedPreferences;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.util.Pair; import androidx.core.util.Pair;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
@ -11,8 +13,6 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LivePagedListBuilder; import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList; import androidx.paging.PagedList;
import com.fewlaps.quitnowcache.QNCache;
import java.util.Locale; import java.util.Locale;
import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.NetworkState;
@ -29,10 +29,43 @@ public class PostViewModel extends ViewModel {
private MutableLiveData<SortType> sortTypeLiveData; private MutableLiveData<SortType> sortTypeLiveData;
private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData; private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData;
public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, 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);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
nsfwLiveData = new MutableLiveData<>();
nsfwLiveData.postValue(nsfw);
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
});
}
public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale,
SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName,
int postType, SortType sortType, int filter, boolean nsfw) { int postType, SortType sortType, int filter, boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale,
postType, sortType, filter, nsfw); sharedPreferences, cache, subredditName, postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
@ -60,42 +93,12 @@ public class PostViewModel extends ViewModel {
}); });
} }
public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName,
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, int postType, SortType sortType, String where, int filter,
subredditName, postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
nsfwLiveData = new MutableLiveData<>();
nsfwLiveData.postValue(nsfw);
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(25)
.build();
posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
});
}
public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
String subredditName, int postType, SortType sortType, String where, int filter,
boolean nsfw) { boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale,
subredditName, postType, sortType, where, filter, nsfw); sharedPreferences, cache, subredditName, postType, sortType, where, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
@ -123,11 +126,12 @@ public class PostViewModel extends ViewModel {
}); });
} }
public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditName, String query, int postType, SortType sortType, int filter, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName,
String query, int postType, SortType sortType, int filter,
boolean nsfw) { boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale,
subredditName, query, postType, sortType, filter, nsfw); sharedPreferences, cache, subredditName, query, postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
@ -192,7 +196,8 @@ public class PostViewModel extends ViewModel {
private String accessToken; private String accessToken;
private String accountName; private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache; private SharedPreferences sharedPreferences;
private SharedPreferences postFeedScrolledPositionSharedPreferences;
private String subredditName; private String subredditName;
private String query; private String query;
private int postType; private int postType;
@ -201,26 +206,30 @@ public class PostViewModel extends ViewModel {
private int filter; private int filter;
private boolean nsfw; 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) { int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.nsfw = nsfw; this.nsfw = nsfw;
} }
public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName,
int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -229,14 +238,16 @@ public class PostViewModel extends ViewModel {
} }
//User posts //User posts
public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditName, int postType, SortType sortType, String where, int filter, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName,
int postType, SortType sortType, String where, int filter,
boolean nsfw) { boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -245,14 +256,15 @@ public class PostViewModel extends ViewModel {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale,
String subredditName, String query, int postType, SortType sortType, int filter, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName,
boolean nsfw) { String query, int postType, SortType sortType, int filter, boolean nsfw) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.locale = locale; this.locale = locale;
this.cache = cache; this.sharedPreferences = sharedPreferences;
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
this.subredditName = subredditName; this.subredditName = subredditName;
this.query = query; this.query = query;
this.postType = postType; this.postType = postType;
@ -265,17 +277,17 @@ public class PostViewModel extends ViewModel {
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (postType == PostDataSource.TYPE_FRONT_PAGE) { if (postType == PostDataSource.TYPE_FRONT_PAGE) {
return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, postType, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences,
sortType, filter, nsfw); postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) { } else if (postType == PostDataSource.TYPE_SEARCH) {
return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences,
query, postType, sortType, filter, nsfw); postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences,
postType, sortType, filter, nsfw); postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw);
} else { } else {
return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences,
postType, sortType, userWhere, filter, nsfw); postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw);
} }
} }
} }

View File

@ -17,6 +17,7 @@ import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent;
import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
@ -28,6 +29,9 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
@Inject @Inject
@Named("default") @Named("default")
SharedPreferences sharedPreferences; SharedPreferences sharedPreferences;
@Inject
@Named("post_feed_scrolled_position_cache")
SharedPreferences cache;
private Activity activity; private Activity activity;
@Override @Override
@ -35,11 +39,22 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
setPreferencesFromResource(R.xml.main_preferences, rootKey); setPreferencesFromResource(R.xml.main_preferences, rootKey);
((Infinity) activity.getApplication()).getAppComponent().inject(this); ((Infinity) activity.getApplication()).getAppComponent().inject(this);
SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_POST_FEED_SCROLLED_POSITION);
SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT);
SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY);
SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_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) { if (confirmToExitSwitch != null) {
confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> { confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new RecreateActivityEvent()); EventBus.getDefault().post(new RecreateActivityEvent());

View File

@ -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";
}

View File

@ -73,6 +73,13 @@ public class SharedPreferencesUtils {
public static final int POST_LAYOUT_CARD = 0; public static final int POST_LAYOUT_CARD = 0;
public static final int POST_LAYOUT_COMPACT = 1; 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 PULL_NOTIFICATION_TIME = "pull_notification_time";
public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time";
public static final String TIME_FORMAT_KEY = "time_format"; 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 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 MUTE_NSFW_VIDEO = "mute_nsfw_video";
public static final String VIDEO_PLAYER_IGNORE_NAV_BAR = "video_player_ignore_nav_bar"; 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";
} }

View File

@ -348,6 +348,8 @@
<string name="settings_amoled_dark_title">Amoled Dark</string> <string name="settings_amoled_dark_title">Amoled Dark</string>
<string name="settings_interface_title">Interface</string> <string name="settings_interface_title">Interface</string>
<string name="settings_gestures_and_buttons_title">Gestures &amp; Buttons</string> <string name="settings_gestures_and_buttons_title">Gestures &amp; 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="settings_open_link_in_app_title">Open Link In App</string>
<string name="settigns_video_title">Video</string> <string name="settigns_video_title">Video</string>
<string name="settings_video_autoplay_title">Video Autoplay</string> <string name="settings_video_autoplay_title">Video Autoplay</string>

View File

@ -36,6 +36,12 @@
app:title="@string/settings_lazy_mode_interval_title" app:title="@string/settings_lazy_mode_interval_title"
app:useSimpleSummaryProvider="true" /> 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 <SwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:icon="@drawable/ic_link" app:icon="@drawable/ic_link"