From ecb891748daa7744613faeb1c535e19474c10363 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 6 Sep 2021 23:31:18 +0800 Subject: [PATCH] Remove PostViewModel, PostDataSource and PostDataSourceFactory. NewPostViewModel is renamed to PostViewModel. --- .../activities/AccountPostsActivity.java | 12 +- .../activities/AccountSavedThingActivity.java | 6 +- .../activities/FilteredPostsActivity.java | 44 +- .../activities/MainActivity.java | 58 +- .../activities/SearchResultActivity.java | 4 +- .../ViewMultiRedditDetailActivity.java | 4 +- .../ViewSubredditDetailActivity.java | 14 +- .../activities/ViewUserDetailActivity.java | 6 +- .../PostDetailRecyclerViewAdapter.java | 8 +- .../adapters/PostRecyclerViewAdapter.java | 6 +- .../comment/CommentDataSource.java | 14 +- .../fragments/PostFragment.java | 116 +- .../post/NewPostViewModel.java | 355 ------ .../post/PostDataSource.java | 1062 ----------------- .../post/PostDataSourceFactory.java | 160 --- ...agingSource.java => PostPagingSource.java} | 34 +- .../infinityforreddit/post/PostViewModel.java | 265 ++-- 17 files changed, 302 insertions(+), 1866 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java rename app/src/main/java/ml/docilealligator/infinityforreddit/post/{PostPaging3PagingSource.java => PostPagingSource.java} (91%) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java index 7f4642ef..2f569409 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java @@ -38,7 +38,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class AccountPostsActivity extends BaseActivity implements SortTypeSelectionCallback, @@ -115,13 +115,13 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect } mUserWhere = getIntent().getExtras().getString(EXTRA_USER_WHERE); - if (mUserWhere.equals(PostDataSource.USER_WHERE_UPVOTED)) { + if (mUserWhere.equals(PostPagingSource.USER_WHERE_UPVOTED)) { toolbar.setTitle(R.string.upvoted); - } else if (mUserWhere.equals(PostDataSource.USER_WHERE_DOWNVOTED)) { + } else if (mUserWhere.equals(PostPagingSource.USER_WHERE_DOWNVOTED)) { toolbar.setTitle(R.string.downvoted); - } else if (mUserWhere.equals(PostDataSource.USER_WHERE_HIDDEN)) { + } else if (mUserWhere.equals(PostPagingSource.USER_WHERE_HIDDEN)) { toolbar.setTitle(R.string.hidden); - } else if (mUserWhere.equals(PostDataSource.USER_WHERE_GILDED)) { + } else if (mUserWhere.equals(PostPagingSource.USER_WHERE_GILDED)) { toolbar.setTitle(R.string.gilded); } @@ -174,7 +174,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect private void initializeFragment() { mFragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_USER); bundle.putString(PostFragment.EXTRA_USER_NAME, mAccountName); bundle.putString(PostFragment.EXTRA_USER_WHERE, mUserWhere); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index 02fe124f..a006ac26 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -50,7 +50,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.CommentsListingFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -336,9 +336,9 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT if (position == 0) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_USER); bundle.putString(PostFragment.EXTRA_USER_NAME, mAccountName); - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SAVED); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); bundle.putBoolean(PostFragment.EXTRA_DISABLE_READ_POSTS, true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index 4d2ff01f..c9c43953 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -50,7 +50,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -158,7 +158,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); name = getIntent().getStringExtra(EXTRA_NAME); - postType = getIntent().getIntExtra(EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); + postType = getIntent().getIntExtra(EXTRA_POST_TYPE, PostPagingSource.TYPE_FRONT_PAGE); int filter = getIntent().getIntExtra(EXTRA_FILTER, -1000); PostFilter postFilter = new PostFilter(); switch (filter) { @@ -220,9 +220,9 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec postFilter.containFlairs = flair; } - if (postType == PostDataSource.TYPE_USER) { + if (postType == PostPagingSource.TYPE_USER) { userWhere = getIntent().getStringExtra(EXTRA_USER_WHERE); - if (userWhere != null && !PostDataSource.USER_WHERE_SUBMITTED.equals(userWhere) && mMenu != null) { + if (userWhere != null && !PostPagingSource.USER_WHERE_SUBMITTED.equals(userWhere) && mMenu != null) { mMenu.findItem(R.id.action_sort_filtered_thing_activity).setVisible(false); } } @@ -268,13 +268,13 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec private void bindView(PostFilter postFilter, boolean initializeFragment) { switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: getSupportActionBar().setTitle(R.string.home); break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: getSupportActionBar().setTitle(R.string.search); break; - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: if (name.equals("popular") || name.equals("all")) { getSupportActionBar().setTitle(name.substring(0, 1).toUpperCase() + name.substring(1)); } else { @@ -282,7 +282,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec getSupportActionBar().setTitle(subredditNamePrefixed); } break; - case PostDataSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: String multiRedditName; if (name.endsWith("/")) { multiRedditName = name.substring(0, name.length() - 1); @@ -292,7 +292,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec } getSupportActionBar().setTitle(multiRedditName); break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: String usernamePrefixed = "u/" + name; getSupportActionBar().setTitle(usernamePrefixed); break; @@ -305,12 +305,12 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec bundle.putParcelable(PostFragment.EXTRA_FILTER, postFilter); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); - if (postType == PostDataSource.TYPE_USER) { + if (postType == PostPagingSource.TYPE_USER) { bundle.putString(PostFragment.EXTRA_USER_NAME, name); bundle.putString(PostFragment.EXTRA_USER_WHERE, userWhere); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { + } else if (postType == PostPagingSource.TYPE_SUBREDDIT || postType == PostPagingSource.TYPE_MULTI_REDDIT) { bundle.putString(PostFragment.EXTRA_NAME, name); - } else if (postType == PostDataSource.TYPE_SEARCH) { + } else if (postType == PostPagingSource.TYPE_SEARCH) { bundle.putString(PostFragment.EXTRA_NAME, name); bundle.putString(PostFragment.EXTRA_QUERY, getIntent().getStringExtra(EXTRA_QUERY)); bundle.putString(PostFragment.EXTRA_TRENDING_SOURCE, getIntent().getStringExtra(EXTRA_TRENDING_SOURCE)); @@ -353,7 +353,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec collapsingToolbarLayout.setLayoutParams(params); } - if (userWhere != null && !PostDataSource.USER_WHERE_SUBMITTED.equals(userWhere)) { + if (userWhere != null && !PostPagingSource.USER_WHERE_SUBMITTED.equals(userWhere)) { mMenu.findItem(R.id.action_sort_filtered_thing_activity).setVisible(false); } return true; @@ -367,20 +367,20 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec return true; } else if (itemId == R.id.action_sort_filtered_thing_activity) { switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); Bundle bestBundle = new Bundle(); bestBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, false); bestSortTypeBottomSheetFragment.setArguments(bestBundle); bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag()); break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment = new SearchPostSortTypeBottomSheetFragment(); Bundle searchBundle = new Bundle(); searchPostSortTypeBottomSheetFragment.setArguments(searchBundle); searchPostSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchPostSortTypeBottomSheetFragment.getTag()); break; - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: if (name.equals("popular") || name.equals("all")) { SortTypeBottomSheetFragment popularAndAllSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); Bundle popularBundle = new Bundle(); @@ -395,14 +395,14 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec subredditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), subredditSortTypeBottomSheetFragment.getTag()); } break; - case PostDataSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: SortTypeBottomSheetFragment multiRedditSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); Bundle multiRedditBundle = new Bundle(); multiRedditBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true); multiRedditSortTypeBottomSheetFragment.setArguments(multiRedditBundle); multiRedditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), multiRedditSortTypeBottomSheetFragment.getTag()); break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: UserThingSortTypeBottomSheetFragment userThingSortTypeBottomSheetFragment = new UserThingSortTypeBottomSheetFragment(); userThingSortTypeBottomSheetFragment.show(getSupportFragmentManager(), userThingSortTypeBottomSheetFragment.getTag()); } @@ -472,16 +472,16 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec public void postLayoutSelected(int postLayout) { if (mFragment != null) { switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply(); break; - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + name, postLayout).apply(); break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + name, postLayout).apply(); break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, postLayout).apply(); } ((FragmentCommunicator) mFragment).changePostLayout(postLayout); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index a9b9393d..430d7bc0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -111,7 +111,7 @@ import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; @@ -492,36 +492,34 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb break; case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_UPVOTED: { Intent intent = new Intent(this, AccountPostsActivity.class); - intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_UPVOTED); startActivity(intent); break; } case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_DOWNVOTED: { Intent intent = new Intent(this, AccountPostsActivity.class); - intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_DOWNVOTED); startActivity(intent); break; } case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_HIDDEN: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_HIDDEN); startActivity(intent); break; } case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_SAVED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SAVED); startActivity(intent); break; } case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_GILDED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_GILDED); startActivity(intent); break; } @@ -768,18 +766,18 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb intent = new Intent(MainActivity.this, TrendingActivity.class); } else if (stringId == R.string.upvoted) { intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_UPVOTED); } else if (stringId == R.string.downvoted) { intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_DOWNVOTED); } else if (stringId == R.string.hidden) { intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_HIDDEN); } else if (stringId == R.string.account_saved_thing_activity_label) { intent = new Intent(MainActivity.this, AccountSavedThingActivity.class); } else if (stringId == R.string.gilded) { intent = new Intent(MainActivity.this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_GILDED); } else if (stringId == R.string.light_theme) { mSharedPreferences.edit().putString(SharedPreferencesUtils.THEME_KEY, "0").apply(); AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); @@ -1099,7 +1097,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private void changeSortType() { int currentPostType = sectionsPagerAdapter.getCurrentPostType(); Bundle bundle = new Bundle(); - bundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, currentPostType != PostDataSource.TYPE_FRONT_PAGE); + bundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, currentPostType != PostPagingSource.TYPE_FRONT_PAGE); SortTypeBottomSheetFragment sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); sortTypeBottomSheetFragment.setArguments(bundle); sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); @@ -1588,13 +1586,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (position == 0) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE); fragment.setArguments(bundle); return fragment; } else if (position == 1) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); @@ -1603,7 +1601,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, "all"); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); @@ -1690,7 +1688,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_HOME) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_FRONT_PAGE); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); @@ -1698,7 +1696,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_ALL) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, "all"); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); @@ -1707,7 +1705,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, name); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); @@ -1717,7 +1715,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); bundle.putString(PostFragment.EXTRA_NAME, name); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_MULTI_REDDIT); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); @@ -1725,9 +1723,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_USER) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_USER); bundle.putString(PostFragment.EXTRA_USER_NAME, name); - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SUBMITTED); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); @@ -1739,22 +1737,22 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb || postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_GILDED) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_USER); bundle.putString(PostFragment.EXTRA_USER_NAME, mAccountName); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); bundle.putBoolean(PostFragment.EXTRA_DISABLE_READ_POSTS, true); if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_UPVOTED) { - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_UPVOTED); } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_DOWNVOTED) { - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_DOWNVOTED); } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_HIDDEN) { - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_HIDDEN); } else if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SAVED) { - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SAVED); } else { - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_GILDED); } fragment.setArguments(bundle); @@ -1762,7 +1760,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); @@ -1823,7 +1821,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (currentFragment != null) { return currentFragment.getPostType(); } - return PostDataSource.TYPE_SUBREDDIT; + return PostPagingSource.TYPE_SUBREDDIT; } void changeSortType(SortType sortType) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index f2096b97..4dca5fc6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -69,7 +69,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.SubredditListingFragment; import ml.docilealligator.infinityforreddit.fragments.UserListingFragment; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.recentsearchquery.InsertRecentSearchQuery; import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; @@ -771,7 +771,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect case 0: { PostFragment mFragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SEARCH); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SEARCH); bundle.putString(PostFragment.EXTRA_NAME, mSubredditName); bundle.putString(PostFragment.EXTRA_QUERY, mQuery); bundle.putString(PostFragment.EXTRA_TRENDING_SOURCE, getIntent().getStringExtra(EXTRA_TRENDING_SOURCE)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index 4c673726..c88822a2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -48,7 +48,7 @@ import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.multireddit.DeleteMultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -186,7 +186,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT mFragment = new PostFragment(); Bundle bundle = new Bundle(); bundle.putString(PostFragment.EXTRA_NAME, multiPath); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_MULTI_REDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_MULTI_REDDIT); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index eaf26230..4096a96b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -107,7 +107,7 @@ import ml.docilealligator.infinityforreddit.fragments.SidebarFragment; import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; @@ -666,35 +666,35 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_UPVOTED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_UPVOTED); startActivity(intent); break; } case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_DOWNVOTED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_DOWNVOTED); startActivity(intent); break; } case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HIDDEN: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_HIDDEN); startActivity(intent); break; } case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SAVED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SAVED); startActivity(intent); break; } case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_GILDED: { Intent intent = new Intent(this, AccountPostsActivity.class); intent = new Intent(this, AccountPostsActivity.class); - intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_GILDED); startActivity(intent); break; } @@ -1583,7 +1583,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); bundle.putString(PostFragment.EXTRA_NAME, subredditName); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index d6fbb3d4..d11fb1c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -99,7 +99,7 @@ import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; @@ -1446,9 +1446,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele if (position == 0) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_USER); bundle.putString(PostFragment.EXTRA_USER_NAME, username); - bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED); + bundle.putString(PostFragment.EXTRA_USER_WHERE, PostPagingSource.USER_WHERE_SUBMITTED); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 5529f1fd..e7a673cc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -110,7 +110,7 @@ import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFi import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -1212,7 +1212,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); - intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, mPost.getPostType()); mActivity.startActivity(intent); }); @@ -1224,7 +1224,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); - intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, mPost.getFlair()); mActivity.startActivity(intent); }); @@ -1235,7 +1235,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); - intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, PostPagingSource.TYPE_SUBREDDIT); intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, Post.NSFW_TYPE); mActivity.startActivity(intent); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 51d893f8..d7d7616d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -89,7 +89,7 @@ import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostDetailFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -639,7 +639,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { Call commentsCall; if (areSavedComments) { if (sortType.getTime() != null) { - commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, null, sortType.getType().value, sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { - commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, null, sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); } } else { @@ -158,10 +158,10 @@ public class CommentDataSource extends PageKeyedDataSource { Call commentsCall; if (areSavedComments) { if (sortType.getTime() != null) { - commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, params.key, + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, sortType.getType().value, sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { - commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, params.key, + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); } } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index ba317c36..59af5dd9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -122,9 +122,9 @@ import ml.docilealligator.infinityforreddit.events.NeedForPostListFromPostFragme import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.events.ShowDividerInCompactLayoutPreferenceEvent; import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInCompactLayoutEvent; -import ml.docilealligator.infinityforreddit.post.NewPostViewModel; import ml.docilealligator.infinityforreddit.post.Post; -import ml.docilealligator.infinityforreddit.post.PostDataSource; +import ml.docilealligator.infinityforreddit.post.PostPagingSource; +import ml.docilealligator.infinityforreddit.post.PostViewModel; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; import ml.docilealligator.infinityforreddit.readpost.ReadPost; @@ -167,7 +167,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ImageView mFetchPostInfoImageView; @BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView; - NewPostViewModel mPostViewModel; + PostViewModel mPostViewModel; @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -432,7 +432,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int usage; String nameOfUsage; - if (postType == PostDataSource.TYPE_SEARCH) { + if (postType == PostPagingSource.TYPE_SEARCH) { subredditName = getArguments().getString(EXTRA_NAME); query = getArguments().getString(EXTRA_QUERY); trendingSource = getArguments().getString(EXTRA_TRENDING_SOURCE); @@ -498,7 +498,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); } }); - } else if (postType == PostDataSource.TYPE_SUBREDDIT) { + } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { subredditName = getArguments().getString(EXTRA_NAME); if (savedInstanceState == null) { postFragmentId += subredditName.hashCode(); @@ -569,7 +569,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); } }); - } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { + } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { multiRedditPath = getArguments().getString(EXTRA_NAME); if (savedInstanceState == null) { postFragmentId += multiRedditPath.hashCode(); @@ -640,7 +640,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); } }); - } else if (postType == PostDataSource.TYPE_USER) { + } else if (postType == PostPagingSource.TYPE_USER) { username = getArguments().getString(EXTRA_USER_NAME); where = getArguments().getString(EXTRA_USER_WHERE); if (savedInstanceState == null) { @@ -708,7 +708,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); } }); - } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { usage = PostFilterUsage.HOME_TYPE; nameOfUsage = PostFilterUsage.NO_USAGE; @@ -886,7 +886,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } else { if (postFilter == null) { - if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { if (concatenatedSubredditNames == null) { FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), usage, nameOfUsage, (postFilter, concatenatedSubredditNames) -> { @@ -915,7 +915,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } } else { - if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { if (concatenatedSubredditNames == null) { FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), usage, nameOfUsage, (postFilter, concatenatedSubredditNames) -> { @@ -1078,35 +1078,35 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } private void initializeAndBindPostViewModel(String accessToken) { - if (postType == PostDataSource.TYPE_SEARCH) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + if (postType == PostPagingSource.TYPE_SEARCH) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, query, trendingSource, - postType, sortType, postFilter, readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + postType, sortType, postFilter, readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + postFilter, readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, - postFilter, readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_USER) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + postFilter, readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_USER) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, - where, readPosts)).get(NewPostViewModel.class); + where, readPosts)).get(PostViewModel.class); } else { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, - postType, sortType, postFilter, readPosts)).get(NewPostViewModel.class); + postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } bindPostViewModel(); @@ -1114,35 +1114,35 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void initializeAndBindPostViewModelForAnonymous(String concatenatedSubredditNames) { //For anonymous user - if (postType == PostDataSource.TYPE_SEARCH) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + if (postType == PostPagingSource.TYPE_SEARCH) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, query, trendingSource, - postType, sortType, postFilter, readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - mPostViewModel = new ViewModelProvider(this, new NewPostViewModel.Factory(mExecutor, + postType, sortType, postFilter, readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mExecutor, mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + postFilter, readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, postFilter, - readPosts)).get(NewPostViewModel.class); - } else if (postType == PostDataSource.TYPE_USER) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + readPosts)).get(PostViewModel.class); + } else if (postType == PostPagingSource.TYPE_USER) { + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, - where, readPosts)).get(NewPostViewModel.class); + where, readPosts)).get(PostViewModel.class); } else { //Anonymous Front Page - mPostViewModel = new ViewModelProvider(PostFragment.this, new NewPostViewModel.Factory(mExecutor, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, - mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)).get(NewPostViewModel.class); + mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)).get(PostViewModel.class); } bindPostViewModel(); @@ -1182,31 +1182,31 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (mPostViewModel != null) { if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_SORT_TYPE, true)) { switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, sortType.getType().name()).apply(); if (sortType.getTime() != null) { mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_BEST_POST, sortType.getTime().name()).apply(); } break; - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SUBREDDIT_POST_BASE + subredditName, sortType.getType().name()).apply(); if (sortType.getTime() != null) { mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE + subredditName, sortType.getTime().name()).apply(); } break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_USER_POST_BASE + username, sortType.getType().name()).apply(); if (sortType.getTime() != null) { mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_USER_POST_BASE + username, sortType.getTime().name()).apply(); } break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, sortType.getType().name()).apply(); if (sortType.getTime() != null) { mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, sortType.getTime().name()).apply(); } break; - case PostDataSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_MULTI_REDDIT_POST_BASE + multiRedditPath, sortType.getType().name()).apply(); if (sortType.getTime() != null) { @@ -1284,7 +1284,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } private void saveCache() { - if (savePostFeedScrolledPosition && postType == PostDataSource.TYPE_FRONT_PAGE && sortType != null && sortType.getType() == SortType.Type.BEST && mAdapter != null) { + if (savePostFeedScrolledPosition && postType == PostPagingSource.TYPE_FRONT_PAGE && sortType != null && sortType.getType() == SortType.Type.BEST && mAdapter != null) { Post currentPost = mAdapter.getItemByPosition(maxPosition); if (currentPost != null) { String accountNameForCache = accountName == null ? SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS : accountName; @@ -1408,19 +1408,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void changePostLayout(int postLayout) { this.postLayout = postLayout; switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply(); break; - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, postLayout).apply(); break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, postLayout).apply(); break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, postLayout).apply(); break; - case PostDataSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE + multiRedditPath, postLayout).apply(); break; } @@ -1493,24 +1493,24 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void filterPosts() { - if (postType == PostDataSource.TYPE_SEARCH) { + if (postType == PostPagingSource.TYPE_SEARCH) { Intent intent = new Intent(activity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, subredditName); intent.putExtra(FilteredPostsActivity.EXTRA_QUERY, query); intent.putExtra(FilteredPostsActivity.EXTRA_TRENDING_SOURCE, trendingSource); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); startActivity(intent); - } else if (postType == PostDataSource.TYPE_SUBREDDIT) { + } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { Intent intent = new Intent(activity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, subredditName); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); startActivity(intent); - } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { + } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { Intent intent = new Intent(activity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, multiRedditPath); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); startActivity(intent); - } else if (postType == PostDataSource.TYPE_USER) { + } else if (postType == PostPagingSource.TYPE_USER) { Intent intent = new Intent(activity, FilteredPostsActivity.class); intent.putExtra(FilteredPostsActivity.EXTRA_NAME, username); intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); @@ -1642,27 +1642,27 @@ public class PostFragment extends Fragment implements FragmentCommunicator { Bundle bundle = getArguments(); if (bundle != null) { switch (postType) { - case PostDataSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_SUBREDDIT: if (!mPostLayoutSharedPreferences.contains(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + bundle.getString(EXTRA_NAME))) { changePostLayout(changeDefaultPostLayoutEvent.defaultPostLayout); } break; - case PostDataSource.TYPE_USER: + case PostPagingSource.TYPE_USER: if (!mPostLayoutSharedPreferences.contains(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + bundle.getString(EXTRA_USER_NAME))) { changePostLayout(changeDefaultPostLayoutEvent.defaultPostLayout); } break; - case PostDataSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: if (!mPostLayoutSharedPreferences.contains(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE + bundle.getString(EXTRA_NAME))) { changePostLayout(changeDefaultPostLayoutEvent.defaultPostLayout); } break; - case PostDataSource.TYPE_SEARCH: + case PostPagingSource.TYPE_SEARCH: if (!mPostLayoutSharedPreferences.contains(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST)) { changePostLayout(changeDefaultPostLayoutEvent.defaultPostLayout); } break; - case PostDataSource.TYPE_FRONT_PAGE: + case PostPagingSource.TYPE_FRONT_PAGE: if (!mPostLayoutSharedPreferences.contains(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST)) { changePostLayout(changeDefaultPostLayoutEvent.defaultPostLayout); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java deleted file mode 100644 index 35f19198..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/NewPostViewModel.java +++ /dev/null @@ -1,355 +0,0 @@ -package ml.docilealligator.infinityforreddit.post; - -import android.content.SharedPreferences; - -import androidx.annotation.NonNull; -import androidx.core.util.Pair; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.Transformations; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelKt; -import androidx.lifecycle.ViewModelProvider; -import androidx.paging.Pager; -import androidx.paging.PagingConfig; -import androidx.paging.PagingData; -import androidx.paging.PagingLiveData; - -import java.util.List; -import java.util.concurrent.Executor; - -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; -import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import retrofit2.Retrofit; - -public class NewPostViewModel extends ViewModel { - private Executor executor; - private Retrofit retrofit; - private String accessToken; - private String accountName; - private SharedPreferences sharedPreferences; - private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String name; - private String query; - private String trendingSource; - private int postType; - private SortType sortType; - private PostFilter postFilter; - private String userWhere; - private List readPostList; - - private LiveData> posts; - - private MutableLiveData sortTypeLiveData; - private MutableLiveData postFilterLiveData; - private SortTypeAndPostFilterLiveData sortTypeAndPostFilterLiveData; - - public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - postFilterLiveData = new MutableLiveData<>(); - postFilterLiveData.postValue(postFilter); - - sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - - Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); - - posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - changeSortTypeAndPostFilter( - sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); - }); - } - - public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, int postType, SortType sortType, PostFilter postFilter, - List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - this.name = subredditName; - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - postFilterLiveData = new MutableLiveData<>(); - postFilterLiveData.postValue(postFilter); - - sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - - Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); - - posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - changeSortTypeAndPostFilter( - sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); - }); - } - - public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, String username, - int postType, SortType sortType, PostFilter postFilter, String userWhere, - List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - this.name = username; - this.userWhere = userWhere; - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - postFilterLiveData = new MutableLiveData<>(); - postFilterLiveData.postValue(postFilter); - - sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - - Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); - - posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - changeSortTypeAndPostFilter( - sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); - }); - } - - public NewPostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, String query, String trendingSource, int postType, SortType sortType, - PostFilter postFilter, List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - this.name = subredditName; - this.query = query; - this.trendingSource = trendingSource; - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - postFilterLiveData = new MutableLiveData<>(); - postFilterLiveData.postValue(postFilter); - - sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - - Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); - - posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - changeSortTypeAndPostFilter( - sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); - }); - } - - public LiveData> getPosts() { - return posts; - } - - public PostPaging3PagingSource returnPagingSoruce() { - PostPaging3PagingSource paging3PagingSource; - switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, - postFilter, readPostList); - break; - case PostDataSource.TYPE_SUBREDDIT: - case PostDataSource.TYPE_MULTI_REDDIT: - case PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE: - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, readPostList); - break; - case PostDataSource.TYPE_SEARCH: - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, - postType, sortType, postFilter, readPostList); - break; - default: - //User - paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, userWhere, readPostList); - break; - } - return paging3PagingSource; - } - - private void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) { - this.sortType = sortType; - this.postFilter = postFilter; - } - - public void changeSortType(SortType sortType) { - sortTypeLiveData.postValue(sortType); - } - - public void changePostFilter(PostFilter postFilter) { - postFilterLiveData.postValue(postFilter); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private Executor executor; - private Retrofit retrofit; - private String accessToken; - private String accountName; - private SharedPreferences sharedPreferences; - private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String name; - private String query; - private String trendingSource; - private int postType; - private SortType sortType; - private PostFilter postFilter; - private String userWhere; - private List readPostList; - - public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - int postType, SortType sortType, PostFilter postFilter, List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, int postType, SortType sortType, PostFilter postFilter, - List readPostList) {this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = name; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - //User posts - public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String username, int postType, SortType sortType, PostFilter postFilter, String where, - List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = username; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - userWhere = where; - this.readPostList = readPostList; - } - - public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, String query, String trendingSource, int postType, SortType sortType, - PostFilter postFilter, List readPostList) { - this.executor = executor; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = name; - this.query = query; - this.trendingSource = trendingSource; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - //Anonymous Front Page - public Factory(Executor executor, Retrofit retrofit, SharedPreferences sharedPreferences, - String concatenatedSubredditNames, int postType, SortType sortType, PostFilter postFilter) { - this.executor = executor; - this.retrofit = retrofit; - this.sharedPreferences = sharedPreferences; - this.name = concatenatedSubredditNames; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new NewPostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new NewPostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, query, trendingSource, postType, sortType, - postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - return (T) new NewPostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, postType, sortType, - postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { - return (T) new NewPostViewModel(executor, retrofit, null, null, sharedPreferences, - null, name, postType, sortType, - postFilter, null); - } else { - return (T) new NewPostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, postType, sortType, - postFilter, userWhere, readPostList); - } - } - } - - private static class SortTypeAndPostFilterLiveData extends MediatorLiveData> { - public SortTypeAndPostFilterLiveData(LiveData sortTypeLiveData, LiveData postFilterLiveData) { - addSource(sortTypeLiveData, sortType -> setValue(Pair.create(postFilterLiveData.getValue(), sortType))); - addSource(postFilterLiveData, postFilter -> setValue(Pair.create(postFilter, sortTypeLiveData.getValue()))); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java deleted file mode 100644 index 57a6b5e3..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ /dev/null @@ -1,1062 +0,0 @@ -package ml.docilealligator.infinityforreddit.post; - -import android.content.SharedPreferences; -import android.os.Handler; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.PageKeyedDataSource; - -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.concurrent.Executor; - -import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; -import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import ml.docilealligator.infinityforreddit.utils.APIUtils; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Retrofit; - -public class PostDataSource extends PageKeyedDataSource { - - public static final int TYPE_FRONT_PAGE = 0; - public static final int TYPE_SUBREDDIT = 1; - public static final int TYPE_USER = 2; - public static final int TYPE_SEARCH = 3; - public static final int TYPE_MULTI_REDDIT = 4; - public static final int TYPE_ANONYMOUS_FRONT_PAGE = 5; - - public static final String USER_WHERE_SUBMITTED = "submitted"; - public static final String USER_WHERE_UPVOTED = "upvoted"; - public static final String USER_WHERE_DOWNVOTED = "downvoted"; - public static final String USER_WHERE_HIDDEN = "hidden"; - public static final String USER_WHERE_SAVED = "saved"; - public static final String USER_WHERE_GILDED = "gilded"; - - private Executor executor; - private Handler handler; - private Retrofit retrofit; - private String accessToken; - private String accountName; - private SharedPreferences sharedPreferences; - private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String subredditOrUserName; - private String query; - private String trendingSource; - private int postType; - private SortType sortType; - private PostFilter postFilter; - private List readPostList; - private String userWhere; - private String multiRedditPath; - private LinkedHashSet postLinkedHashSet; - - private MutableLiveData paginationNetworkStateLiveData; - private MutableLiveData initialLoadStateLiveData; - private MutableLiveData hasPostLiveData; - - private LoadParams params; - private LoadCallback callback; - - PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - postLinkedHashSet = new LinkedHashSet<>(); - } - - PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String path, int postType, SortType sortType, PostFilter postFilter, - List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - if (postType == TYPE_SUBREDDIT || postType == TYPE_ANONYMOUS_FRONT_PAGE) { - this.subredditOrUserName = path; - } else { - if (sortType != null) { - if (path.endsWith("/")) { - multiRedditPath = path + sortType.getType().value; - } else { - multiRedditPath = path + "/" + sortType.getType().value; - } - } else { - multiRedditPath = path; - } - } - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - if (sortType == null) { - if (path.equals("popular") || path.equals("all")) { - this.sortType = new SortType(SortType.Type.HOT); - } else { - this.sortType = new SortType(SortType.Type.BEST); - } - } else { - this.sortType = sortType; - } - this.postFilter = postFilter; - this.readPostList = readPostList; - postLinkedHashSet = new LinkedHashSet<>(); - } - - PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditOrUserName, int postType, SortType sortType, PostFilter postFilter, - String where, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditOrUserName = subredditOrUserName; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType == null ? new SortType(SortType.Type.NEW) : sortType; - this.postFilter = postFilter; - userWhere = where; - this.readPostList = readPostList; - postLinkedHashSet = new LinkedHashSet<>(); - } - - PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditOrUserName, String query, String trendingSource, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditOrUserName = subredditOrUserName; - this.query = query; - this.trendingSource = trendingSource; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType == null ? new SortType(SortType.Type.RELEVANCE) : sortType; - this.postFilter = postFilter; - postLinkedHashSet = new LinkedHashSet<>(); - this.readPostList = readPostList; - } - - MutableLiveData getPaginationNetworkStateLiveData() { - return paginationNetworkStateLiveData; - } - - MutableLiveData getInitialLoadStateLiveData() { - return initialLoadStateLiveData; - } - - MutableLiveData hasPostLiveData() { - return hasPostLiveData; - } - - @Override - public void loadInitial(@NonNull LoadInitialParams params, @NonNull final LoadInitialCallback callback) { - initialLoadStateLiveData.postValue(NetworkState.LOADING); - switch (postType) { - case TYPE_FRONT_PAGE: - boolean savePostFeedScrolledPosition = sortType != null && sortType.getType() == SortType.Type.BEST && sharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); - String accountNameForCache = accountName == null ? SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS : accountName; - if (savePostFeedScrolledPosition) { - loadBestPostsInitial(callback, postFeedScrolledPositionSharedPreferences.getString(accountNameForCache + SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_FRONT_PAGE_BASE, null)); - } else { - loadBestPostsInitial(callback, null); - } - break; - case TYPE_SUBREDDIT: - loadSubredditPostsInitial(callback, null); - break; - case TYPE_USER: - loadUserPostsInitial(callback, null); - break; - case TYPE_SEARCH: - loadSearchPostsInitial(callback, null); - break; - case TYPE_MULTI_REDDIT: - loadMultiRedditPostsInitial(callback, null); - break; - case TYPE_ANONYMOUS_FRONT_PAGE: - loadAnonymousFrontPagePostsInitial(callback, null); - break; - } - } - - @Override - public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { - - } - - @Override - public void loadAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { - this.params = params; - this.callback = callback; - - if (params == null || "".equals(params.key) || "null".equals(params.key)) { - return; - } - - paginationNetworkStateLiveData.postValue(NetworkState.LOADING); - - switch (postType) { - case TYPE_FRONT_PAGE: - loadBestPostsAfter(params, callback, null); - break; - case TYPE_SUBREDDIT: - loadSubredditPostsAfter(params, callback, null); - break; - case TYPE_USER: - loadUserPostsAfter(params, callback, null); - break; - case TYPE_SEARCH: - loadSearchPostsAfter(params, callback, null); - break; - case TYPE_MULTI_REDDIT: - loadMultiRedditPostsAfter(params, callback, null); - break; - case TYPE_ANONYMOUS_FRONT_PAGE: - loadAnonymousFrontPagePostsAfter(params, callback, null); - break; - } - } - - private void loadBestPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost; - if(sortType.getTime() != null) { - bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, lastItem, - APIUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPosts(sortType.getType().value, lastItem, APIUtils.getOAuthHeader(accessToken)); - } - bestPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadBestPostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing posts")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - "code: " + response.code() + " message: " + response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - errorMessage + " " + call.request().url().toString())); - } - }); - } - - private void loadBestPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost; - if(sortType.getTime() != null) { - bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, after, - APIUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPosts(sortType.getType().value, after, APIUtils.getOAuthHeader(accessToken)); - } - - bestPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadBestPostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadBestPostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing more posts")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, sortType.getTime().value, lastItem); - } else { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, lastItem); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, lastItem, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - lastItem, APIUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadSubredditPostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing posts")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - "code: " + response + " message: " + response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - errorMessage + " " + call.request().url().toString())); - } - }); - } - - private void loadSubredditPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, after); - } else { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, after); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, after, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - after, APIUtils.getOAuthHeader(accessToken)); - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadSubredditPostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadSubredditPostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getUserPosts(subredditOrUserName, lastItem, sortType.getType().value, - sortType.getTime().value); - } else { - getPost = api.getUserPosts(subredditOrUserName, lastItem, sortType.getType().value); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, - APIUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadUserPostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadUserPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getUserPosts(subredditOrUserName, after, sortType.getType().value, - sortType.getTime().value); - } else { - getPost = api.getUserPosts(subredditOrUserName, after, sortType.getType().value); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, - APIUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadUserPostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadUserPostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadSearchPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost; - - if (subredditOrUserName == null) { - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.searchPosts(query, lastItem, sortType.getType().value, sortType.getTime().value, - trendingSource); - } else { - getPost = api.searchPosts(query, lastItem, sortType.getType().value, trendingSource); - } - } else { - if(sortType.getTime() != null) { - getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, - sortType.getTime().value, trendingSource, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, trendingSource, - APIUtils.getOAuthHeader(accessToken)); - } - } - } else { - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, lastItem); - } else { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, - sortType.getType().value, lastItem); - } - } else { - if (sortType.getTime() != null) { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, lastItem, - APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, lastItem, - APIUtils.getOAuthHeader(accessToken)); - } - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadSearchPostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadSearchPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost; - - if (subredditOrUserName == null) { - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.searchPosts(query, after, sortType.getType().value, sortType.getTime().value, - trendingSource); - } else { - getPost = api.searchPosts(query, after, sortType.getType().value, trendingSource); - } - } else { - if (sortType.getTime() != null) { - getPost = api.searchPostsOauth(query, after, sortType.getType().value, - sortType.getTime().value, trendingSource, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsOauth(query, after, sortType.getType().value, trendingSource, - APIUtils.getOAuthHeader(accessToken)); - } - } - } else { - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, after); - } else { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, - sortType.getType().value, after); - } - } else { - if (sortType.getTime() != null) { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, after, - APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, after, APIUtils.getOAuthHeader(accessToken)); - } - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadSearchPostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadSearchPostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadMultiRedditPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getMultiRedditPosts(multiRedditPath, lastItem, sortType.getTime().value); - } else { - getPost = api.getMultiRedditPosts(multiRedditPath, lastItem); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, - APIUtils.getOAuthHeader(accessToken)); - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadMultiRedditPostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadMultiRedditPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (accessToken == null) { - if (sortType.getTime() != null) { - getPost = api.getMultiRedditPosts(multiRedditPath, after, - sortType.getTime().value); - } else { - getPost = api.getMultiRedditPosts(multiRedditPath, after); - } - } else { - if (sortType.getTime() != null) { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, - APIUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadMultiRedditPostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadMultiRedditPostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - private void loadAnonymousFrontPagePostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, sortType.getTime().value, lastItem); - } else { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, lastItem); - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, null, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadAnonymousFrontPagePostsInitial(callback, nextPageKey); - return; - } else { - postLinkedHashSet.addAll(newPosts); - callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); - hasPostLiveData.postValue(false); - } - - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostsListingFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing posts")); - } - }); - } else { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - "code: " + response + " message: " + response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, - errorMessage + " " + call.request().url().toString())); - } - }); - } - - private void loadAnonymousFrontPagePostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback, String lastItem) { - String after = lastItem == null ? params.key : lastItem; - - RedditAPI api = retrofit.create(RedditAPI.class); - - Call getPost; - if (sortType.getTime() != null) { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, after); - } else { - getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, after); - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, null, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadAnonymousFrontPagePostsAfter(params, callback, lastItem); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - loadAnonymousFrontPagePostsAfter(params, callback, lastItem); - } else { - List newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); - callback.onResult(newPostsList, lastItem); - } - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - } - - @Override - public void onParsePostsListingFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - String errorMessage = t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); - } - }); - } - - void retryLoadingMore() { - loadAfter(params, callback); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java deleted file mode 100644 index dd8312b7..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java +++ /dev/null @@ -1,160 +0,0 @@ -package ml.docilealligator.infinityforreddit.post; - -import android.content.SharedPreferences; -import android.os.Handler; -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.DataSource; - -import java.util.List; -import java.util.concurrent.Executor; - -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; -import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import retrofit2.Retrofit; - -class PostDataSourceFactory extends DataSource.Factory { - private Executor executor; - private Handler handler; - private Retrofit retrofit; - private String accessToken; - private String accountName; - private SharedPreferences sharedPreferences; - private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String name; - private String query; - private String trendingSource; - private int postType; - private SortType sortType; - private PostFilter postFilter; - private String userWhere; - private List readPostList; - - private PostDataSource postDataSource; - private MutableLiveData postDataSourceLiveData; - - PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, int postType, SortType sortType, PostFilter postFilter, - List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = name; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, int postType, SortType sortType, PostFilter postFilter, - String where, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = name; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - userWhere = where; - this.readPostList = readPostList; - } - - PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, String query, String trendingSource, int postType, SortType sortType, - PostFilter postFilter, List readPostList) { - this.executor = executor; - this.handler = handler; - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.name = name; - this.query = query; - this.trendingSource = trendingSource; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - } - - @NonNull - @Override - public DataSource create() { - if (postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, - postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, - postType, sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - Log.i("asdasfd", "s5 " + (postFilter == null)); - postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { - postDataSource = new PostDataSource(executor, handler, retrofit, null, null, - sharedPreferences, null, name, postType, - sortType, postFilter, null); - } else { - postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, - sortType, postFilter, userWhere, readPostList); - } - - postDataSourceLiveData.postValue(postDataSource); - return postDataSource; - } - - public MutableLiveData getPostDataSourceLiveData() { - return postDataSourceLiveData; - } - - PostDataSource getPostDataSource() { - return postDataSource; - } - - void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) { - Log.i("asdasfd", "s6 " + (postFilter == null)); - this.sortType = sortType; - this.postFilter = postFilter; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java similarity index 91% rename from app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java rename to app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java index dee6edd1..6a821d5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java @@ -28,7 +28,7 @@ import retrofit2.HttpException; import retrofit2.Response; import retrofit2.Retrofit; -public class PostPaging3PagingSource extends ListenableFuturePagingSource { +public class PostPagingSource extends ListenableFuturePagingSource { public static final int TYPE_FRONT_PAGE = 0; public static final int TYPE_SUBREDDIT = 1; public static final int TYPE_USER = 2; @@ -60,10 +60,10 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource postLinkedHashSet; - PostPaging3PagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { + PostPagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, + SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, + SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -77,10 +77,10 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource(); } - PostPaging3PagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String path, int postType, SortType sortType, PostFilter postFilter, - List readPostList) { + PostPagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String path, int postType, SortType sortType, PostFilter postFilter, + List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -115,10 +115,10 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource(); } - PostPaging3PagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditOrUserName, int postType, SortType sortType, PostFilter postFilter, - String where, List readPostList) { + PostPagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String subredditOrUserName, int postType, SortType sortType, PostFilter postFilter, + String where, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -134,10 +134,10 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource(); } - PostPaging3PagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditOrUserName, String query, String trendingSource, int postType, - SortType sortType, PostFilter postFilter, List readPostList) { + PostPagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String subredditOrUserName, String query, String trendingSource, int postType, + SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; 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 ea56f54e..0f0b366f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; -import android.os.Handler; import androidx.annotation.NonNull; import androidx.core.util.Pair; @@ -10,41 +9,56 @@ import androidx.lifecycle.MediatorLiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Transformations; import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelKt; import androidx.lifecycle.ViewModelProvider; -import androidx.paging.LivePagedListBuilder; -import androidx.paging.PagedList; +import androidx.paging.Pager; +import androidx.paging.PagingConfig; +import androidx.paging.PagingData; +import androidx.paging.PagingLiveData; import java.util.List; import java.util.concurrent.Executor; -import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; import retrofit2.Retrofit; public class PostViewModel extends ViewModel { - private PostDataSourceFactory postDataSourceFactory; - private LiveData paginationNetworkState; - private LiveData initialLoadingState; - private LiveData hasPostLiveData; - private LiveData> posts; + private Executor executor; + private Retrofit retrofit; + private String accessToken; + private String accountName; + private SharedPreferences sharedPreferences; + private SharedPreferences postFeedScrolledPositionSharedPreferences; + private String name; + private String query; + private String trendingSource; + private int postType; + private SortType sortType; + private PostFilter postFilter; + private String userWhere; + private List readPostList; + + private LiveData> posts; + private MutableLiveData sortTypeLiveData; private MutableLiveData postFilterLiveData; private SortTypeAndPostFilterLiveData sortTypeAndPostFilterLiveData; - public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences cache, int postType, + public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, cache, postType, sortType, postFilter, readPostList); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); @@ -53,33 +67,30 @@ public class PostViewModel extends ViewModel { sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - postDataSourceFactory.changeSortTypeAndPostFilter( + changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); } - public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, - int postType, SortType sortType, PostFilter postFilter, + public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String subredditName, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, cache, subredditName, postType, sortType, postFilter, - readPostList); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = subredditName; sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); @@ -88,32 +99,32 @@ public class PostViewModel extends ViewModel { sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - postDataSourceFactory.changeSortTypeAndPostFilter( + changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); } - public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences cache, String username, - int postType, SortType sortType, PostFilter postFilter, String where, + public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, + SharedPreferences postFeedScrolledPositionSharedPreferences, String username, + int postType, SortType sortType, PostFilter postFilter, String userWhere, List readPostList) { - postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, cache, username, postType, sortType, postFilter, where, readPostList); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = username; + this.userWhere = userWhere; sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); @@ -122,33 +133,32 @@ public class PostViewModel extends ViewModel { sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - postDataSourceFactory.changeSortTypeAndPostFilter( + changeSortTypeAndPostFilter( sortTypeLiveData.getValue(), postFilterLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); } - public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, - String query, String trendingSource, int postType, SortType sortType, + public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String subredditName, String query, String trendingSource, int postType, SortType sortType, PostFilter postFilter, List readPostList) { - postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, - sharedPreferences, cache, subredditName, query, trendingSource, postType, sortType, postFilter, - readPostList); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); + this.executor = executor; + this.retrofit = retrofit; + this.accessToken = accessToken; + this.accountName = accountName; + this.sharedPreferences = sharedPreferences; + this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + this.readPostList = readPostList; + this.name = subredditName; + this.query = query; + this.trendingSource = trendingSource; sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); @@ -157,41 +167,52 @@ public class PostViewModel extends ViewModel { sortTypeAndPostFilterLiveData = new SortTypeAndPostFilterLiveData(sortTypeLiveData, postFilterLiveData); - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); posts = Transformations.switchMap(sortTypeAndPostFilterLiveData, sortAndPostFilter -> { - postDataSourceFactory.changeSortTypeAndPostFilter(sortTypeLiveData.getValue(), - postFilterLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + changeSortTypeAndPostFilter( + sortTypeLiveData.getValue(), postFilterLiveData.getValue()); + return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); } - public LiveData> getPosts() { + public LiveData> getPosts() { return posts; } - public LiveData getPaginationNetworkState() { - return paginationNetworkState; + public PostPagingSource returnPagingSoruce() { + PostPagingSource paging3PagingSource; + switch (postType) { + case PostPagingSource.TYPE_FRONT_PAGE: + paging3PagingSource = new PostPagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, + postFilter, readPostList); + break; + case PostPagingSource.TYPE_SUBREDDIT: + case PostPagingSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE: + paging3PagingSource = new PostPagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, + sortType, postFilter, readPostList); + break; + case PostPagingSource.TYPE_SEARCH: + paging3PagingSource = new PostPagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, + postType, sortType, postFilter, readPostList); + break; + default: + //User + paging3PagingSource = new PostPagingSource(executor, retrofit, accessToken, accountName, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, + sortType, postFilter, userWhere, readPostList); + break; + } + return paging3PagingSource; } - public LiveData getInitialLoadingState() { - return initialLoadingState; - } - - public LiveData hasPost() { - return hasPostLiveData; - } - - public void refresh() { - postDataSourceFactory.getPostDataSource().invalidate(); - } - - public void retryLoadingMore() { - postDataSourceFactory.getPostDataSource().retryLoadingMore(); + private void changeSortTypeAndPostFilter(SortType sortType, PostFilter postFilter) { + this.sortType = sortType; + this.postFilter = postFilter; } public void changeSortType(SortType sortType) { @@ -204,7 +225,6 @@ public class PostViewModel extends ViewModel { public static class Factory extends ViewModelProvider.NewInstanceFactory { private Executor executor; - private Handler handler; private Retrofit retrofit; private String accessToken; private String accountName; @@ -219,11 +239,10 @@ public class PostViewModel extends ViewModel { private String userWhere; private List readPostList; - public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; - this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -235,11 +254,10 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } - public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, List readPostList) {this.executor = executor; - this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -253,12 +271,11 @@ public class PostViewModel extends ViewModel { } //User posts - public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String username, int postType, SortType sortType, PostFilter postFilter, String where, List readPostList) { this.executor = executor; - this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -272,12 +289,11 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } - public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, String query, String trendingSource, int postType, SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; - this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -293,10 +309,9 @@ public class PostViewModel extends ViewModel { } //Anonymous Front Page - public Factory(Executor executor, Handler handler, Retrofit retrofit, SharedPreferences sharedPreferences, + public Factory(Executor executor, Retrofit retrofit, SharedPreferences sharedPreferences, String concatenatedSubredditNames, int postType, SortType sortType, PostFilter postFilter) { this.executor = executor; - this.handler = handler; this.retrofit = retrofit; this.sharedPreferences = sharedPreferences; this.name = concatenatedSubredditNames; @@ -308,23 +323,23 @@ public class PostViewModel extends ViewModel { @NonNull @Override public T create(@NonNull Class modelClass) { - if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, + if (postType == PostPagingSource.TYPE_FRONT_PAGE) { + return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, + } else if (postType == PostPagingSource.TYPE_SEARCH) { + return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, trendingSource, postType, sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, + } else if (postType == PostPagingSource.TYPE_SUBREDDIT || postType == PostPagingSource.TYPE_MULTI_REDDIT) { + return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); - } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { - return (T) new PostViewModel(executor, handler, retrofit, null, null, sharedPreferences, + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { + return (T) new PostViewModel(executor, retrofit, null, null, sharedPreferences, null, name, postType, sortType, postFilter, null); } else { - return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, userWhere, readPostList); }