From bb598531b5b575ca03d7efc4e79ab02a4b882bd8 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 30 Jul 2020 16:50:15 +0800 Subject: Start loading posts after the last viewed post in PostFragment when refreshing and first load. --- .../Activity/AccountPostsActivity.java | 1 + .../Activity/AccountSavedThingActivity.java | 1 + .../Activity/FilteredThingActivity.java | 6 + .../infinityforreddit/Activity/MainActivity.java | 8 + .../Activity/ViewMultiRedditDetailActivity.java | 1 + .../Activity/ViewSubredditDetailActivity.java | 1 + .../Activity/ViewUserDetailActivity.java | 1 + .../Adapter/PostRecyclerViewAdapter.java | 14 ++ .../infinityforreddit/AppModule.java | 7 + .../infinityforreddit/Fragment/PostFragment.java | 217 ++++++++++++++------- .../infinityforreddit/Post/PostDataSource.java | 42 ++-- .../Post/PostDataSourceFactory.java | 46 +++-- .../infinityforreddit/Post/PostViewModel.java | 80 +++++--- .../infinityforreddit/Utils/CacheUtils.java | 9 + 14 files changed, 306 insertions(+), 128 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java index 805674de..e88d70ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java @@ -191,6 +191,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect bundle.putString(PostFragment.EXTRA_USER_WHERE, mUserWhere); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java index 5a2ed18c..818e5c0f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java @@ -338,6 +338,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java index a0b8bd47..f97944c3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java @@ -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 NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; + private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @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 mNullAccessToken = false; private String mAccessToken; + private String mAccountName; private String name; private String userWhere; private int postType; @@ -148,6 +150,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); 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; } else { mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); } bindView(filter, true); }).execute(); @@ -291,6 +295,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec bundle.putInt(PostFragment.EXTRA_POST_TYPE, postType); bundle.putInt(PostFragment.EXTRA_FILTER, filter); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); if (postType == PostDataSource.TYPE_USER) { bundle.putString(PostFragment.EXTRA_USER_NAME, name); 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); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index bc15b896..b45992fd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -945,6 +945,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } else { @@ -954,6 +955,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bundle.putString(PostFragment.EXTRA_NAME, "all"); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); 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_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } 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.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } 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.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } 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_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } 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.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } else { @@ -1027,6 +1034,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java index 8e172c23..990b9388 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -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_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 3c8625c7..cdcf72a2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -904,6 +904,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index 1ceed6e1..2b777851 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -819,6 +819,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index b558db87..3561abfa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -612,6 +612,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0 && super.getItemCount() > position) { + return super.getItem(position); + } + + return null; + } + public void setVoteButtonsPosition(boolean voteButtonsOnTheRight) { mVoteButtonsOnTheRight = voteButtonsOnTheRight; } @@ -1231,6 +1243,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter provideQNCache() { + return new QNCache.Builder().build(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 67a5e8bd..d825c619 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -38,6 +38,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import com.fewlaps.quitnowcache.QNCache; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -86,6 +87,7 @@ import ml.docilealligator.infinityforreddit.Post.PostViewModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.Utils.CacheUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Retrofit; @@ -107,6 +109,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public static final String EXTRA_FILTER = "EF"; public static final int EXTRA_NO_FILTER = -2; 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 RECYCLER_VIEW_POSITION_STATE = "RVPS"; @@ -143,6 +146,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { CustomThemeWrapper customThemeWrapper; @Inject ExoCreator exoCreator; + @Inject + QNCache cache; private RequestManager mGlide; private AppCompatActivity activity; private LinearLayoutManager mLinearLayoutManager; @@ -151,6 +156,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean isInLazyMode = false; private boolean isLazyModePaused = false; private boolean hasPost = false; + private boolean isShown = false; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; private Window window; @@ -158,6 +164,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private LazyModeRunnable lazyModeRunnable; private CountDownTimer resumeLazyModeCountDownTimer; private float lazyModeInterval; + private String accountName; + private String subredditName; + private String username; + private String multiRedditPath; + private int maxPosition = -1; private int postLayout; private SortType sortType; @@ -168,6 +179,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void onResume() { super.onResume(); + isShown = true; if (mPostRecyclerView.getAdapter() != null) { ((PostRecyclerViewAdapter) mPostRecyclerView.getAdapter()).setCanStartActivity(true); } @@ -353,12 +365,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int filter = getArguments().getInt(EXTRA_FILTER); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); + accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { - String subredditName = getArguments().getString(EXTRA_NAME); + subredditName = getArguments().getString(EXTRA_NAME); String query = getArguments().getString(EXTRA_QUERY); String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.RELEVANCE.name()); @@ -369,33 +382,40 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { - @Override - public void retryLoadingMore() { - mPostViewModel.retryLoadingMore(); - } + @Override + public void retryLoadingMore() { + mPostViewModel.retryLoadingMore(); + } - @Override - public void typeChipClicked(int filter) { - Intent intent = new Intent(activity, FilteredThingActivity.class); - intent.putExtra(FilteredThingActivity.EXTRA_NAME, subredditName); - intent.putExtra(FilteredThingActivity.EXTRA_QUERY, query); - intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); - intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); - startActivity(intent); - } - }); + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredThingActivity.class); + intent.putExtra(FilteredThingActivity.EXTRA_NAME, subredditName); + intent.putExtra(FilteredThingActivity.EXTRA_QUERY, query); + intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } + }); if (accessToken == 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); } else { 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); } } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - String subredditName = getArguments().getString(EXTRA_NAME); + subredditName = getArguments().getString(EXTRA_NAME); String sort; String sortTime = null; @@ -431,32 +451,39 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, displaySubredditName, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { - @Override - public void retryLoadingMore() { - mPostViewModel.retryLoadingMore(); - } + @Override + public void retryLoadingMore() { + mPostViewModel.retryLoadingMore(); + } - @Override - public void typeChipClicked(int filter) { - Intent intent = new Intent(activity, FilteredThingActivity.class); - intent.putExtra(FilteredThingActivity.EXTRA_NAME, subredditName); - intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); - intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); - startActivity(intent); - } - }); + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredThingActivity.class); + intent.putExtra(FilteredThingActivity.EXTRA_NAME, subredditName); + intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } + }); if (accessToken == null) { 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); } else { 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); } } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { - String multiRedditPath = getArguments().getString(EXTRA_NAME); + multiRedditPath = getArguments().getString(EXTRA_NAME); String sort; String sortTime = null; @@ -491,19 +518,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator { intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); startActivity(intent); } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } }); if (accessToken == 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); } else { 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); } } 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); if (where != null && where.equals(PostDataSource.USER_WHERE_SUBMITTED)) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); @@ -523,29 +557,36 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { - @Override - public void retryLoadingMore() { - mPostViewModel.retryLoadingMore(); - } + @Override + public void retryLoadingMore() { + mPostViewModel.retryLoadingMore(); + } - @Override - public void typeChipClicked(int filter) { - Intent intent = new Intent(activity, FilteredThingActivity.class); - intent.putExtra(FilteredThingActivity.EXTRA_NAME, username); - intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); - intent.putExtra(FilteredThingActivity.EXTRA_USER_WHERE, where); - intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); - startActivity(intent); - } - }); + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredThingActivity.class); + intent.putExtra(FilteredThingActivity.EXTRA_NAME, username); + intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredThingActivity.EXTRA_USER_WHERE, where); + intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } + }); if (accessToken == null) { 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); } else { 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); } } else { @@ -561,23 +602,30 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { - @Override - public void retryLoadingMore() { - mPostViewModel.retryLoadingMore(); - } + @Override + public void retryLoadingMore() { + mPostViewModel.retryLoadingMore(); + } - @Override - public void typeChipClicked(int filter) { - Intent intent = new Intent(activity, FilteredThingActivity.class); - intent.putExtra(FilteredThingActivity.EXTRA_NAME, activity.getString(R.string.best)); - intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); - intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); - startActivity(intent); - } - }); + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredThingActivity.class); + intent.putExtra(FilteredThingActivity.EXTRA_NAME, activity.getString(R.string.best)); + intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredThingActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } + }); 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) { @@ -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 public void refresh() { mAdapter.removeFooter(); @@ -667,6 +749,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (isInLazyMode) { stopLazyMode(); } + saveCache(); mPostViewModel.refresh(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java index 56aff32d..e886d731 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -4,15 +4,18 @@ import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; +import com.fewlaps.quitnowcache.QNCache; + import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; -import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.CacheUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -34,7 +37,9 @@ public class PostDataSource extends PageKeyedDataSource { private Retrofit retrofit; private String accessToken; + private String accountName; private Locale locale; + private QNCache cache; private String subredditOrUserName; private String query; private int postType; @@ -52,11 +57,13 @@ public class PostDataSource extends PageKeyedDataSource { private LoadParams params; private LoadCallback callback; - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); @@ -67,11 +74,13 @@ public class PostDataSource extends PageKeyedDataSource { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType, - SortType sortType, int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String path, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; if (postType == TYPE_SUBREDDIT) { this.subredditOrUserName = path; } else { @@ -103,11 +112,14 @@ public class PostDataSource extends PageKeyedDataSource { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditOrUserName, int postType, SortType sortType, String where, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditOrUserName = subredditOrUserName; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); @@ -120,11 +132,14 @@ public class PostDataSource extends PageKeyedDataSource { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { + PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditOrUserName, String query, int postType, SortType sortType, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditOrUserName = subredditOrUserName; this.query = query; paginationNetworkStateLiveData = new MutableLiveData<>(); @@ -153,21 +168,22 @@ public class PostDataSource extends PageKeyedDataSource { public void loadInitial(@NonNull LoadInitialParams params, @NonNull final LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); + String accountNameForCache = accountName == null ? CacheUtils.ANONYMOUS : accountName; switch (postType) { case TYPE_FRONT_PAGE: - loadBestPostsInitial(callback, null); + loadBestPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.FRONT_PAGE_BASE)); break; case TYPE_SUBREDDIT: - loadSubredditPostsInitial(callback, null); + loadSubredditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.SUBREDDIT_BASE + subredditOrUserName)); break; case TYPE_USER: - loadUserPostsInitial(callback, null); + loadUserPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.USER_BASE + subredditOrUserName)); break; case TYPE_SEARCH: loadSearchPostsInitial(callback, null); break; case TYPE_MULTI_REDDIT: - loadMultiRedditPostsInitial(callback, null); + loadMultiRedditPostsInitial(callback, cache.get(accountNameForCache + CacheUtils.MULTI_REDDIT_BASE + multiRedditPath)); break; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java index eb953d7d..6ed8a51c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java @@ -4,6 +4,8 @@ import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; +import com.fewlaps.quitnowcache.QNCache; + import java.util.Locale; import ml.docilealligator.infinityforreddit.SortType; @@ -12,7 +14,9 @@ import retrofit2.Retrofit; class PostDataSourceFactory extends DataSource.Factory { private Retrofit retrofit; private String accessToken; + private String accountName; private Locale locale; + private QNCache cache; private String subredditName; private String query; private int postType; @@ -24,11 +28,13 @@ class PostDataSourceFactory extends DataSource.Factory { private PostDataSource postDataSource; private MutableLiveData postDataSourceLiveData; - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; this.sortType = sortType; @@ -36,11 +42,13 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, SortType sortType, int filter, boolean nsfw) { + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; @@ -49,11 +57,14 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, SortType sortType, String where, int filter, boolean nsfw) { + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, String where, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; @@ -63,11 +74,14 @@ class PostDataSourceFactory extends DataSource.Factory { this.nsfw = nsfw; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - String query, int postType, SortType sortType, int filter, boolean nsfw) { + PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, String query, int postType, SortType sortType, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; this.query = query; postDataSourceLiveData = new MutableLiveData<>(); @@ -81,17 +95,17 @@ class PostDataSourceFactory extends DataSource.Factory { @Override public DataSource create() { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, - filter, nsfw); - } else if (postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, 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) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, + subredditName, postType, sortType, filter, nsfw); } else { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, userWhere, filter, nsfw); + postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, cache, + subredditName, postType, sortType, userWhere, filter, nsfw); } postDataSourceLiveData.postValue(postDataSource); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java index 6c7464d3..3da11f53 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java @@ -11,6 +11,8 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; +import com.fewlaps.quitnowcache.QNCache; + import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; @@ -27,10 +29,10 @@ public class PostViewModel extends ViewModel { private MutableLiveData sortTypeLiveData; private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData; - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, - sortType, filter, nsfw); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + int postType, SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, + postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -58,10 +60,10 @@ public class PostViewModel extends ViewModel { }); } - 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); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, + subredditName, postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -89,10 +91,11 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - postType, sortType, where, filter, nsfw); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, String where, int filter, + boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, cache, + subredditName, postType, sortType, where, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -120,10 +123,11 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - query, postType, sortType, filter, nsfw); + public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache 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(), PostDataSource::getInitialLoadStateLiveData); @@ -186,7 +190,9 @@ public class PostViewModel extends ViewModel { public static class Factory extends ViewModelProvider.NewInstanceFactory { private Retrofit retrofit; private String accessToken; + private String accountName; private Locale locale; + private QNCache cache; private String subredditName; private String query; private int postType; @@ -195,22 +201,26 @@ public class PostViewModel extends ViewModel { private int filter; private boolean nsfw; - public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.postType = postType; this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; } - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, int filter, boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; this.postType = postType; this.sortType = sortType; @@ -219,11 +229,14 @@ public class PostViewModel extends ViewModel { } //User posts - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, int postType, SortType sortType, String where, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; this.postType = postType; this.sortType = sortType; @@ -232,11 +245,14 @@ public class PostViewModel extends ViewModel { this.nsfw = nsfw; } - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { + public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, QNCache cache, + String subredditName, String query, int postType, SortType sortType, int filter, + boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; + this.accountName = accountName; this.locale = locale; + this.cache = cache; this.subredditName = subredditName; this.query = query; this.postType = postType; @@ -249,17 +265,17 @@ public class PostViewModel extends ViewModel { @Override public T create(@NonNull Class modelClass) { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter, - nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, postType, + sortType, filter, nsfw); } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, - postType, sortType, filter, nsfw); + 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, locale, subredditName, postType, - sortType, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, + postType, sortType, filter, nsfw); } else { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, - sortType, userWhere, filter, nsfw); + return (T) new PostViewModel(retrofit, accessToken, accountName, locale, cache, subredditName, + postType, sortType, userWhere, filter, nsfw); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java new file mode 100644 index 00000000..e0280204 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CacheUtils.java @@ -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"; +} -- cgit v1.2.3