Start loading posts after the last viewed post in PostFragment when refreshing and first load.

This commit is contained in:
Alex Ning 2020-07-30 16:50:15 +08:00
parent 2c03eb8d15
commit bb598531b5
15 changed files with 334 additions and 155 deletions

View File

@ -95,6 +95,7 @@ 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

@ -191,6 +191,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect
bundle.putString(PostFragment.EXTRA_USER_WHERE, mUserWhere); bundle.putString(PostFragment.EXTRA_USER_WHERE, mUserWhere);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
mFragment.setArguments(bundle); mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit();
} }

View File

@ -338,6 +338,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }

View File

@ -58,6 +58,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String FRAGMENT_OUT_STATE = "FOS"; private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.coordinator_layout_filtered_thing_activity) @BindView(R.id.coordinator_layout_filtered_thing_activity)
@ -81,6 +82,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
private String mAccountName;
private String name; private String name;
private String userWhere; private String userWhere;
private int postType; private int postType;
@ -148,6 +150,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit();
@ -195,6 +198,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
mAccessToken = account.getAccessToken(); mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
} }
bindView(filter, true); bindView(filter, true);
}).execute(); }).execute();
@ -291,6 +295,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
bundle.putInt(PostFragment.EXTRA_POST_TYPE, postType); bundle.putInt(PostFragment.EXTRA_POST_TYPE, postType);
bundle.putInt(PostFragment.EXTRA_FILTER, filter); bundle.putInt(PostFragment.EXTRA_FILTER, filter);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
if (postType == PostDataSource.TYPE_USER) { if (postType == PostDataSource.TYPE_USER) {
bundle.putString(PostFragment.EXTRA_USER_NAME, name); bundle.putString(PostFragment.EXTRA_USER_NAME, name);
bundle.putString(PostFragment.EXTRA_USER_WHERE, userWhere); bundle.putString(PostFragment.EXTRA_USER_WHERE, userWhere);
@ -393,6 +398,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
} }

View File

@ -945,6 +945,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putString(PostFragment.EXTRA_NAME, "popular");
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else { } else {
@ -954,6 +955,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_NAME, "all"); bundle.putString(PostFragment.EXTRA_NAME, "all");
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }
@ -981,6 +983,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_ALL)) { } else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_ALL)) {
@ -990,6 +993,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_NAME, "all"); bundle.putString(PostFragment.EXTRA_NAME, "all");
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT)) { } else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT)) {
@ -999,6 +1003,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_NAME, name); bundle.putString(PostFragment.EXTRA_NAME, name);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT)) { } else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT)) {
@ -1008,6 +1013,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_USER)) { } else if (postType.equals(SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_USER)) {
@ -1018,6 +1024,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED); bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} else { } else {
@ -1027,6 +1034,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putString(PostFragment.EXTRA_NAME, "popular");
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }

View File

@ -193,6 +193,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
mFragment.setArguments(bundle); mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit();
} }

View File

@ -904,6 +904,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }

View File

@ -819,6 +819,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED); bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }

View File

@ -612,6 +612,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostTextTypeViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed()); ((PostTextTypeViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed());
} }
} }
mCallback.currentlyBindItem(holder.getAdapterPosition());
} }
} else if (holder instanceof PostCompactBaseViewHolder) { } else if (holder instanceof PostCompactBaseViewHolder) {
Post post = getItem(position); Post post = getItem(position);
@ -864,6 +865,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} else { } else {
((PostCompactBaseViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); ((PostCompactBaseViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
} }
mCallback.currentlyBindItem(holder.getAdapterPosition());
} }
} }
} }
@ -1052,6 +1055,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
return super.getItemCount(); return super.getItemCount();
} }
@Nullable
public Post getItemByPosition(int position) {
if (position >= 0 && super.getItemCount() > position) {
return super.getItem(position);
}
return null;
}
public void setVoteButtonsPosition(boolean voteButtonsOnTheRight) { public void setVoteButtonsPosition(boolean voteButtonsOnTheRight) {
mVoteButtonsOnTheRight = voteButtonsOnTheRight; mVoteButtonsOnTheRight = voteButtonsOnTheRight;
} }
@ -1231,6 +1243,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
void retryLoadingMore(); void retryLoadingMore();
void typeChipClicked(int filter); void typeChipClicked(int filter);
void currentlyBindItem(int position);
} }
class PostBaseViewHolder extends RecyclerView.ViewHolder { class PostBaseViewHolder extends RecyclerView.ViewHolder {

View File

@ -6,6 +6,7 @@ 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;
@ -216,4 +217,10 @@ 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

@ -38,6 +38,7 @@ 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;
@ -86,6 +87,7 @@ 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;
@ -107,6 +109,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public static final String EXTRA_FILTER = "EF"; public static final String EXTRA_FILTER = "EF";
public static final int EXTRA_NO_FILTER = -2; public static final int EXTRA_NO_FILTER = -2;
public static final String EXTRA_ACCESS_TOKEN = "EAT"; public static final String EXTRA_ACCESS_TOKEN = "EAT";
public static final String EXTRA_ACCOUNT_NAME = "EAN";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS"; private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS";
@ -143,6 +146,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
CustomThemeWrapper customThemeWrapper; CustomThemeWrapper customThemeWrapper;
@Inject @Inject
ExoCreator exoCreator; ExoCreator exoCreator;
@Inject
QNCache<String> cache;
private RequestManager mGlide; private RequestManager mGlide;
private AppCompatActivity activity; private AppCompatActivity activity;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
@ -151,6 +156,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private boolean isLazyModePaused = false; private boolean isLazyModePaused = false;
private boolean hasPost = false; private boolean hasPost = false;
private boolean isShown = false;
private PostRecyclerViewAdapter mAdapter; private PostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller smoothScroller; private RecyclerView.SmoothScroller smoothScroller;
private Window window; private Window window;
@ -158,6 +164,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private LazyModeRunnable lazyModeRunnable; private LazyModeRunnable lazyModeRunnable;
private CountDownTimer resumeLazyModeCountDownTimer; private CountDownTimer resumeLazyModeCountDownTimer;
private float lazyModeInterval; private float lazyModeInterval;
private String accountName;
private String subredditName;
private String username;
private String multiRedditPath;
private int maxPosition = -1;
private int postLayout; private int postLayout;
private SortType sortType; private SortType sortType;
@ -168,6 +179,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
isShown = true;
if (mPostRecyclerView.getAdapter() != null) { if (mPostRecyclerView.getAdapter() != null) {
((PostRecyclerViewAdapter) mPostRecyclerView.getAdapter()).setCanStartActivity(true); ((PostRecyclerViewAdapter) mPostRecyclerView.getAdapter()).setCanStartActivity(true);
} }
@ -353,12 +365,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
int filter = getArguments().getInt(EXTRA_FILTER); int filter = getArguments().getInt(EXTRA_FILTER);
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
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"));
Locale locale = getResources().getConfiguration().locale; Locale locale = getResources().getConfiguration().locale;
if (postType == PostDataSource.TYPE_SEARCH) { if (postType == PostDataSource.TYPE_SEARCH) {
String subredditName = getArguments().getString(EXTRA_NAME); subredditName = getArguments().getString(EXTRA_NAME);
String query = getArguments().getString(EXTRA_QUERY); String query = getArguments().getString(EXTRA_QUERY);
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.RELEVANCE.name()); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.RELEVANCE.name());
@ -383,19 +396,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent); startActivity(intent);
} }
@Override
public void currentlyBindItem(int position) {
if (maxPosition < position) {
maxPosition = position;
}
}
}); });
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null,
getResources().getConfiguration().locale, subredditName, query, postType, accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType,
sortType, filter, nsfw)).get(PostViewModel.class); 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,
getResources().getConfiguration().locale, subredditName, query, postType, accountName, getResources().getConfiguration().locale, cache, subredditName, query, postType,
sortType, filter, nsfw)).get(PostViewModel.class); sortType, filter, nsfw)).get(PostViewModel.class);
} }
} else if (postType == PostDataSource.TYPE_SUBREDDIT) { } else if (postType == PostDataSource.TYPE_SUBREDDIT) {
String subredditName = getArguments().getString(EXTRA_NAME); subredditName = getArguments().getString(EXTRA_NAME);
String sort; String sort;
String sortTime = null; String sortTime = null;
@ -444,19 +464,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent); startActivity(intent);
} }
@Override
public void currentlyBindItem(int position) {
if (maxPosition < position) {
maxPosition = position;
}
}
}); });
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken,
getResources().getConfiguration().locale, subredditName, postType, sortType, accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType,
filter, nsfw)).get(PostViewModel.class); 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,
getResources().getConfiguration().locale, subredditName, postType, sortType, accountName, getResources().getConfiguration().locale, cache, subredditName, postType, sortType,
filter, nsfw)).get(PostViewModel.class); filter, nsfw)).get(PostViewModel.class);
} }
} else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) {
String multiRedditPath = getArguments().getString(EXTRA_NAME); multiRedditPath = getArguments().getString(EXTRA_NAME);
String sort; String sort;
String sortTime = null; String sortTime = null;
@ -491,19 +518,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent); startActivity(intent);
} }
@Override
public void currentlyBindItem(int position) {
if (maxPosition < position) {
maxPosition = position;
}
}
}); });
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null,
getResources().getConfiguration().locale, multiRedditPath, postType, sortType, accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType,
filter, nsfw)).get(PostViewModel.class); 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,
getResources().getConfiguration().locale, multiRedditPath, postType, sortType, accountName, getResources().getConfiguration().locale, cache, multiRedditPath, postType, sortType,
filter, nsfw)).get(PostViewModel.class); filter, nsfw)).get(PostViewModel.class);
} }
} else if (postType == PostDataSource.TYPE_USER) { } else if (postType == PostDataSource.TYPE_USER) {
String username = getArguments().getString(EXTRA_USER_NAME); username = getArguments().getString(EXTRA_USER_NAME);
String where = getArguments().getString(EXTRA_USER_WHERE); String where = getArguments().getString(EXTRA_USER_WHERE);
if (where != null && where.equals(PostDataSource.USER_WHERE_SUBMITTED)) { if (where != null && where.equals(PostDataSource.USER_WHERE_SUBMITTED)) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams();
@ -537,15 +571,22 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent); startActivity(intent);
} }
@Override
public void currentlyBindItem(int position) {
if (maxPosition < position) {
maxPosition = position;
}
}
}); });
if (accessToken == null) { if (accessToken == null) {
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken,
getResources().getConfiguration().locale, username, postType, sortType, where, accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where,
filter, nsfw)).get(PostViewModel.class); 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,
getResources().getConfiguration().locale, username, postType, sortType, where, accountName, getResources().getConfiguration().locale, cache, username, postType, sortType, where,
filter, nsfw)).get(PostViewModel.class); filter, nsfw)).get(PostViewModel.class);
} }
} else { } else {
@ -574,10 +615,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter);
startActivity(intent); startActivity(intent);
} }
@Override
public void currentlyBindItem(int position) {
if (maxPosition < position) {
maxPosition = position;
}
}
}); });
mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, postType, sortType, filter, nsfw)).get(PostViewModel.class); accountName, getResources().getConfiguration().locale, cache, postType, sortType, filter, nsfw)).get(PostViewModel.class);
} }
if (activity instanceof ActivityToolbarInterface) { if (activity instanceof ActivityToolbarInterface) {
@ -659,6 +707,40 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
} }
@Override
public void onStop() {
super.onStop();
saveCache();
}
private void saveCache() {
if (isShown && mAdapter != null) {
String key;
Post currentPost = mAdapter.getItemByPosition(maxPosition);
if (currentPost != null) {
String accountNameForCache = accountName == null ? CacheUtils.ANONYMOUS : accountName;
String value = currentPost.getFullName();
switch (postType) {
case PostDataSource.TYPE_FRONT_PAGE:
key = accountNameForCache + CacheUtils.FRONT_PAGE_BASE;
break;
case PostDataSource.TYPE_SUBREDDIT:
key = accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditName;
break;
case PostDataSource.TYPE_USER:
key = accountNameForCache + CacheUtils.USER_BASE + username;
break;
case PostDataSource.TYPE_MULTI_REDDIT:
key = accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath;
break;
default:
return;
}
cache.set(key, value);
}
}
}
@Override @Override
public void refresh() { public void refresh() {
mAdapter.removeFooter(); mAdapter.removeFooter();
@ -667,6 +749,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (isInLazyMode) { if (isInLazyMode) {
stopLazyMode(); stopLazyMode();
} }
saveCache();
mPostViewModel.refresh(); mPostViewModel.refresh();
} }

View File

@ -4,15 +4,18 @@ 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;
import java.util.Locale; import java.util.Locale;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.API.RedditAPI;
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 retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -34,7 +37,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
private Retrofit retrofit; private Retrofit retrofit;
private String accessToken; private String accessToken;
private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache;
private String subredditOrUserName; private String subredditOrUserName;
private String query; private String query;
private int postType; private int postType;
@ -52,11 +57,13 @@ 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, Locale locale, int postType, SortType sortType, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
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.locale = locale; this.locale = locale;
this.cache = cache;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>();
@ -67,11 +74,13 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
SortType sortType, int filter, boolean nsfw) { 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.locale = locale; this.locale = locale;
this.cache = cache;
if (postType == TYPE_SUBREDDIT) { if (postType == TYPE_SUBREDDIT) {
this.subredditOrUserName = path; this.subredditOrUserName = path;
} else { } else {
@ -103,11 +112,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
SortType sortType, String where, int filter, boolean nsfw) { String subredditOrUserName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>();
@ -120,11 +132,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
postLinkedHashSet = new LinkedHashSet<>(); postLinkedHashSet = new LinkedHashSet<>();
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
int postType, SortType sortType, int filter, boolean nsfw) { String subredditOrUserName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
this.query = query; this.query = query;
paginationNetworkStateLiveData = new MutableLiveData<>(); paginationNetworkStateLiveData = new MutableLiveData<>();
@ -153,21 +168,22 @@ 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;
switch (postType) { switch (postType) {
case TYPE_FRONT_PAGE: case TYPE_FRONT_PAGE:
loadBestPostsInitial(callback, null); loadBestPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.FRONT_PAGE_BASE));
break; break;
case TYPE_SUBREDDIT: case TYPE_SUBREDDIT:
loadSubredditPostsInitial(callback, null); loadSubredditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditOrUserName));
break; break;
case TYPE_USER: case TYPE_USER:
loadUserPostsInitial(callback, null); loadUserPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.USER_BASE + subredditOrUserName));
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, null); loadMultiRedditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath));
break; break;
} }
} }

View File

@ -4,6 +4,8 @@ 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;
@ -12,7 +14,9 @@ import retrofit2.Retrofit;
class PostDataSourceFactory extends DataSource.Factory { class PostDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit; private Retrofit retrofit;
private String accessToken; private String accessToken;
private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache;
private String subredditName; private String subredditName;
private String query; private String query;
private int postType; private int postType;
@ -24,11 +28,13 @@ class PostDataSourceFactory extends DataSource.Factory {
private PostDataSource postDataSource; private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData; private MutableLiveData<PostDataSource> postDataSourceLiveData;
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
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.locale = locale; this.locale = locale;
this.cache = cache;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -36,11 +42,13 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
@ -49,11 +57,14 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
int postType, SortType sortType, String where, int filter, boolean nsfw) { String subredditName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
@ -63,11 +74,14 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
String query, int postType, SortType sortType, int filter, boolean nsfw) { String subredditName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
this.query = query; this.query = query;
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
@ -81,17 +95,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, locale, postType, sortType, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache,
filter, nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query,
postType, sortType, filter, nsfw); postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) {
postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache,
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, locale, subredditName, postType, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache,
sortType, filter, nsfw); subredditName, postType, sortType, filter, nsfw);
} else { } else {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache,
sortType, userWhere, filter, nsfw); subredditName, postType, sortType, userWhere, filter, nsfw);
} }
postDataSourceLiveData.postValue(postDataSource); postDataSourceLiveData.postValue(postDataSource);

View File

@ -11,6 +11,8 @@ 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;
@ -27,40 +29,9 @@ 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, Locale locale, int postType, SortType sortType, public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
int filter, boolean nsfw) { int postType, SortType sortType, int filter, boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache,
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, Locale locale, String subredditName, int postType,
SortType sortType, int filter, boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
postType, sortType, filter, nsfw); postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
@ -89,10 +60,10 @@ public class PostViewModel extends ViewModel {
}); });
} }
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
SortType sortType, String where, int filter, boolean nsfw) { String subredditName, int postType, SortType sortType, int filter, boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache,
postType, sortType, where, filter, nsfw); subredditName, postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
@ -120,10 +91,43 @@ public class PostViewModel extends ViewModel {
}); });
} }
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
int postType, SortType sortType, int filter, boolean nsfw) { String subredditName, int postType, SortType sortType, String where, int filter,
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, boolean nsfw) {
query, postType, sortType, filter, nsfw); postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache,
subredditName, postType, sortType, where, 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, String query, int postType, SortType sortType, int filter,
boolean nsfw) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache,
subredditName, query, postType, sortType, filter, nsfw);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
@ -186,7 +190,9 @@ public class PostViewModel extends ViewModel {
public static class Factory extends ViewModelProvider.NewInstanceFactory { public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Retrofit retrofit; private Retrofit retrofit;
private String accessToken; private String accessToken;
private String accountName;
private Locale locale; private Locale locale;
private QNCache<String> cache;
private String subredditName; private String subredditName;
private String query; private String query;
private int postType; private int postType;
@ -195,22 +201,26 @@ public class PostViewModel extends ViewModel {
private int filter; private int filter;
private boolean nsfw; private boolean nsfw;
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
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.locale = locale; this.locale = locale;
this.cache = cache;
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, Locale locale, String subredditName, int postType, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -219,11 +229,14 @@ public class PostViewModel extends ViewModel {
} }
//User posts //User posts
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
SortType sortType, String where, int filter, boolean nsfw) { String subredditName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
@ -232,11 +245,14 @@ public class PostViewModel extends ViewModel {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache<String> cache,
int postType, SortType sortType, int filter, boolean nsfw) { String subredditName, 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.locale = locale; this.locale = locale;
this.cache = cache;
this.subredditName = subredditName; this.subredditName = subredditName;
this.query = query; this.query = query;
this.postType = postType; this.postType = postType;
@ -249,17 +265,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, locale, postType, sortType, filter, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, postType,
nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query,
postType, sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType,
sortType, filter, nsfw); sortType, filter, nsfw);
} else if (postType == PostDataSource.TYPE_SEARCH) {
return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, 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);
} else { } else {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName,
sortType, userWhere, filter, nsfw); postType, sortType, userWhere, filter, nsfw);
} }
} }
} }

View File

@ -0,0 +1,9 @@
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";
}