From a143501f9f3571c9f6c28490e8e826ece3dfa205 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 4 Jan 2020 16:30:16 +0800 Subject: Fix the fab in MainActivity shows in wrong places. Put all classes related to Post to Package Post. --- .../Activity/AccountPostsActivity.java | 2 +- .../Activity/AccountSavedThingActivity.java | 2 +- .../Activity/FilteredThingActivity.java | 4 +- .../infinityforreddit/Activity/MainActivity.java | 17 +- .../Activity/SearchResultActivity.java | 2 +- .../Activity/ViewMultiRedditDetailActivity.java | 2 +- .../Activity/ViewPostDetailActivity.java | 2 +- .../Activity/ViewSubredditDetailActivity.java | 2 +- .../Activity/ViewUserDetailActivity.java | 2 +- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 4 +- .../Adapter/PostRecyclerViewAdapter.java | 4 +- .../infinityforreddit/CommentDataSource.java | 1 + .../Event/PostUpdateEventToDetailActivity.java | 2 +- .../Event/PostUpdateEventToPostList.java | 2 +- .../Event/SubmitTextOrLinkPostEvent.java | 2 +- .../infinityforreddit/FetchPost.java | 1 + .../infinityforreddit/Fragment/PostFragment.java | 46 +- .../infinityforreddit/NetworkState.java | 6 +- .../infinityforreddit/ParsePost.java | 7 +- .../ml/docilealligator/infinityforreddit/Post.java | 466 ----------- .../infinityforreddit/Post/Post.java | 466 +++++++++++ .../infinityforreddit/Post/PostDataSource.java | 882 +++++++++++++++++++++ .../Post/PostDataSourceFactory.java | 113 +++ .../infinityforreddit/Post/PostViewModel.java | 273 +++++++ .../infinityforreddit/PostDataSource.java | 878 -------------------- .../infinityforreddit/PostDataSourceFactory.java | 112 --- .../infinityforreddit/PostViewModel.java | 271 ------- .../Service/SubmitPostService.java | 2 +- .../infinityforreddit/SubmitPost.java | 1 + 29 files changed, 1793 insertions(+), 1781 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java index ad93f0c1..ede80722 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java @@ -35,7 +35,7 @@ import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.Fragment.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java index 44f4014d..1e1e7447 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java @@ -40,7 +40,7 @@ import ml.docilealligator.infinityforreddit.Fragment.CommentsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java index 6fc27fb1..71123450 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java @@ -38,8 +38,8 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment import ml.docilealligator.infinityforreddit.Fragment.UserThingSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.Post; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index d6eeba0c..c5eb9ac2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -85,7 +85,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseAndSaveAccountInfo; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.PullNotificationWorker; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; @@ -211,6 +211,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private String mNewAccountName; private Menu mMenu; private boolean isInLazyMode = false; + private boolean showBottomAppBar; @Override protected void onCreate(Bundle savedInstanceState) { @@ -310,6 +311,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); + showBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY, false); + if (savedInstanceState != null) { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE); @@ -454,7 +457,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bottomNavigationView.setVisibility(View.GONE); fab.setVisibility(View.GONE); } else { - if (mSharedPreferences.getBoolean(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY, false)) { + if (showBottomAppBar) { bottomNavigationView.setVisibility(View.VISIBLE); } else { CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); @@ -489,8 +492,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onPageSelected(int position) { - bottomNavigationView.performShow(); - fab.show(); + if (mAccessToken != null && showBottomAppBar) { + bottomNavigationView.performShow(); + fab.show(); + } if (isInLazyMode) { if (position == sectionsPagerAdapter.getCurrentLazyModeFragmentPosition()) { sectionsPagerAdapter.resumeLazyMode(); @@ -943,14 +948,14 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } public void postScrollUp() { - if (mAccessToken != null) { + if (mAccessToken != null && showBottomAppBar) { bottomNavigationView.performShow(); fab.show(); } } public void postScrollDown() { - if (mAccessToken != null) { + if (mAccessToken != null && showBottomAppBar) { fab.hide(); bottomNavigationView.performHide(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java index 03684be1..f077ee54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java @@ -44,7 +44,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SubredditListingFragment; import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java index 34ee2e1e..b5bca5b5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -35,7 +35,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 11cc629d..c5dfed9f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -72,7 +72,7 @@ import ml.docilealligator.infinityforreddit.HidePost; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; import ml.docilealligator.infinityforreddit.ParsePost; -import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditAPI; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 62ab002e..1dc855f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -55,7 +55,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index 5ffaa717..90695fde 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -61,7 +61,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment import ml.docilealligator.infinityforreddit.Fragment.UserThingSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index ea37f174..edaefc75 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -76,8 +76,8 @@ import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.FetchComment; import ml.docilealligator.infinityforreddit.Fragment.ModifyCommentBottomSheetFragment; -import ml.docilealligator.infinityforreddit.Post; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index a4e897de..ca3d7111 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -55,8 +55,8 @@ import ml.docilealligator.infinityforreddit.AsyncTask.LoadUserDataAsyncTask; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.Post; -import ml.docilealligator.infinityforreddit.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index fac8ddd4..e82115ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -14,6 +14,7 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.Locale; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import retrofit2.Call; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToDetailActivity.java index 0d502aa6..bb935590 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToDetailActivity.java @@ -1,6 +1,6 @@ package ml.docilealligator.infinityforreddit.Event; -import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.Post.Post; public class PostUpdateEventToDetailActivity { public final Post post; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToPostList.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToPostList.java index 10dadb7d..f37ae7bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToPostList.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/PostUpdateEventToPostList.java @@ -1,6 +1,6 @@ package ml.docilealligator.infinityforreddit.Event; -import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.Post.Post; public class PostUpdateEventToPostList { public final Post post; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitTextOrLinkPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitTextOrLinkPostEvent.java index 3d43664e..0e164b60 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitTextOrLinkPostEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/SubmitTextOrLinkPostEvent.java @@ -1,6 +1,6 @@ package ml.docilealligator.infinityforreddit.Event; -import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.Post.Post; public class SubmitTextOrLinkPostEvent { public boolean postSuccess; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java index 07523722..9f0c93ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import java.util.Locale; +import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 37d48957..5052234e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -59,9 +59,9 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.Post; -import ml.docilealligator.infinityforreddit.PostDataSource; -import ml.docilealligator.infinityforreddit.PostViewModel; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Post.PostDataSource; +import ml.docilealligator.infinityforreddit.Post.PostViewModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; @@ -318,8 +318,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { boolean voteButtonsOnTheRight = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); boolean showElapsedTime = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); - PostViewModel.Factory factory; - if (postType == PostDataSource.TYPE_SEARCH) { String subredditName = getArguments().getString(EXTRA_NAME); String query = getArguments().getString(EXTRA_QUERY); @@ -349,13 +347,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); if (accessToken == null) { - factory = new PostViewModel.Factory(mRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, getResources().getConfiguration().locale, subredditName, query, postType, - sortType, filter, nsfw); + sortType, filter, nsfw)).get(PostViewModel.class); } else { - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, getResources().getConfiguration().locale, subredditName, query, postType, - sortType, filter, nsfw); + sortType, filter, nsfw)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_SUBREDDIT) { String subredditName = getArguments().getString(EXTRA_NAME); @@ -411,13 +409,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); if (accessToken == null) { - factory = new PostViewModel.Factory(mRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, getResources().getConfiguration().locale, subredditName, postType, sortType, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } else { - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, getResources().getConfiguration().locale, subredditName, postType, sortType, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { String multiRedditPath = getArguments().getString(EXTRA_NAME); @@ -459,13 +457,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); if (accessToken == null) { - factory = new PostViewModel.Factory(mRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, getResources().getConfiguration().locale, multiRedditPath, postType, sortType, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } else { - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, getResources().getConfiguration().locale, multiRedditPath, postType, sortType, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_USER) { String username = getArguments().getString(EXTRA_USER_NAME); @@ -506,13 +504,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); if (accessToken == null) { - factory = new PostViewModel.Factory(mRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, getResources().getConfiguration().locale, username, postType, sortType, where, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } else { - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, getResources().getConfiguration().locale, username, postType, sortType, where, - filter, nsfw); + filter, nsfw)).get(PostViewModel.class); } } else { String sort = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name()); @@ -543,13 +541,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, postType, sortType, filter, nsfw); + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, postType, sortType, filter, nsfw)).get(PostViewModel.class); } mPostRecyclerView.setAdapter(mAdapter); - - mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); mPostViewModel.hasPost().observe(this, hasPost -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkState.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkState.java index 264fea2f..319de6b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkState.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkState.java @@ -1,8 +1,8 @@ package ml.docilealligator.infinityforreddit; public class NetworkState { - static final NetworkState LOADED; - static final NetworkState LOADING; + public static final NetworkState LOADED; + public static final NetworkState LOADING; static { LOADED = new NetworkState(Status.SUCCESS, "Success"); @@ -12,7 +12,7 @@ public class NetworkState { private final Status status; private final String msg; - NetworkState(Status status, String msg) { + public NetworkState(Status status, String msg) { this.status = status; this.msg = msg; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 2cdf4be1..25a0e1cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -13,6 +13,7 @@ import java.util.Calendar; import java.util.Locale; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; +import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; @@ -21,8 +22,8 @@ import ml.docilealligator.infinityforreddit.Utils.Utils; */ public class ParsePost { - static void parsePosts(String response, Locale locale, int nPosts, int filter, boolean nsfw, - ParsePostsListingListener parsePostsListingListener) { + public static void parsePosts(String response, Locale locale, int nPosts, int filter, boolean nsfw, + ParsePostsListingListener parsePostsListingListener) { new ParsePostDataAsyncTask(response, locale, nPosts, filter, nsfw, parsePostsListingListener).execute(); } @@ -284,7 +285,7 @@ public class ParsePost { return post; } - interface ParsePostsListingListener { + public interface ParsePostsListingListener { void onParsePostsListingSuccess(ArrayList newPostData, String lastItem); void onParsePostsListingFail(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java deleted file mode 100644 index 58c1ccea..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ /dev/null @@ -1,466 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.Parcel; -import android.os.Parcelable; - -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; - -/** - * Created by alex on 3/1/18. - */ - -public class Post implements Parcelable { - public static final int NSFW_TYPE = -1; - public static final int TEXT_TYPE = 0; - public static final int IMAGE_TYPE = 1; - public static final int LINK_TYPE = 2; - public static final int VIDEO_TYPE = 3; - public static final int GIF_TYPE = 4; - public static final int NO_PREVIEW_LINK_TYPE = 5; - public static final Creator CREATOR = new Creator() { - @Override - public Post createFromParcel(Parcel in) { - return new Post(in); - } - - @Override - public Post[] newArray(int size) { - return new Post[size]; - } - }; - private String id; - private String fullName; - private String subredditName; - private String subredditNamePrefixed; - private String subredditIconUrl; - private String author; - private String authorNamePrefixed; - private String authorIconUrl; - private String postTime; - private String title; - private String selfText; - private String selfTextPlainTrimmed; - private String previewUrl; - private String thumbnailPreviewUrl; - private String url; - private String videoUrl; - private String videoDownloadUrl; - private String permalink; - private String flair; - private long postTimeMillis; - private int score; - private int postType; - private int voteType; - private int gilded; - private int previewWidth; - private int previewHeight; - private int nComments; - private boolean hidden; - private boolean spoiler; - private boolean nsfw; - private boolean stickied; - private boolean archived; - private boolean locked; - private boolean saved; - private boolean isCrosspost; - private String crosspostParentId; - - public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String postTime, long postTimeMillis, String title, String previewUrl, - String thumbnailPreviewUrl, String permalink, int score, int postType, - int voteType, int gilded, int nComments, String flair, boolean hidden, boolean spoiler, - boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, - boolean isCrosspost) { - this.id = id; - this.fullName = fullName; - this.subredditName = subredditName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.author = author; - this.authorNamePrefixed = "u/" + author; - this.postTime = postTime; - this.postTimeMillis = postTimeMillis; - this.title = title; - this.previewUrl = previewUrl; - this.thumbnailPreviewUrl = thumbnailPreviewUrl; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nComments = nComments; - this.flair = flair; - this.hidden = hidden; - this.spoiler = spoiler; - this.nsfw = nsfw; - this.stickied = stickied; - this.archived = archived; - this.locked = locked; - this.saved = saved; - this.isCrosspost = isCrosspost; - } - - public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String postTime, long postTimeMillis, String title, String previewUrl, - String thumbnailPreviewUrl, String url, String permalink, int score, - int postType, int voteType, int gilded, int nComments, String flair, boolean hidden, - boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, - boolean saved, boolean isCrosspost) { - this.id = id; - this.fullName = fullName; - this.subredditName = subredditName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.author = author; - this.authorNamePrefixed = "u/" + author; - this.postTime = postTime; - this.postTimeMillis = postTimeMillis; - this.title = title; - this.previewUrl = previewUrl; - this.thumbnailPreviewUrl = thumbnailPreviewUrl; - this.url = url; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nComments = nComments; - this.flair = flair; - this.hidden = hidden; - this.spoiler = spoiler; - this.nsfw = nsfw; - this.stickied = stickied; - this.archived = archived; - this.locked = locked; - this.saved = saved; - this.isCrosspost = isCrosspost; - } - - public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, - String postTime, long postTimeMillis, String title, String permalink, int score, int postType, int voteType, int gilded, - int nComments, String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, - boolean archived, boolean locked, boolean saved, boolean isCrosspost) { - this.id = id; - this.fullName = fullName; - this.subredditName = subredditName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.author = author; - this.authorNamePrefixed = "u/" + author; - this.postTime = postTime; - this.postTimeMillis = postTimeMillis; - this.title = title; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nComments = nComments; - this.flair = flair; - this.hidden = hidden; - this.spoiler = spoiler; - this.nsfw = nsfw; - this.stickied = stickied; - this.archived = archived; - this.locked = locked; - this.saved = saved; - this.isCrosspost = isCrosspost; - } - - protected Post(Parcel in) { - id = in.readString(); - fullName = in.readString(); - subredditName = in.readString(); - subredditNamePrefixed = in.readString(); - subredditIconUrl = in.readString(); - author = in.readString(); - authorNamePrefixed = in.readString(); - authorIconUrl = in.readString(); - postTime = in.readString(); - postTimeMillis = in.readLong(); - title = in.readString(); - selfText = in.readString(); - selfTextPlainTrimmed = in.readString(); - previewUrl = in.readString(); - thumbnailPreviewUrl = in.readString(); - url = in.readString(); - videoUrl = in.readString(); - videoDownloadUrl = in.readString(); - permalink = in.readString(); - flair = in.readString(); - score = in.readInt(); - postType = in.readInt(); - voteType = in.readInt(); - gilded = in.readInt(); - previewWidth = in.readInt(); - previewHeight = in.readInt(); - nComments = in.readInt(); - hidden = in.readByte() != 0; - spoiler = in.readByte() != 0; - nsfw = in.readByte() != 0; - stickied = in.readByte() != 0; - archived = in.readByte() != 0; - locked = in.readByte() != 0; - saved = in.readByte() != 0; - isCrosspost = in.readByte() != 0; - crosspostParentId = in.readString(); - } - - public String getId() { - return id; - } - - public String getFullName() { - return fullName; - } - - public String getSubredditName() { - return subredditName; - } - - public String getSubredditNamePrefixed() { - return subredditNamePrefixed; - } - - public String getSubredditIconUrl() { - return subredditIconUrl; - } - - public void setSubredditIconUrl(String subredditIconUrl) { - this.subredditIconUrl = subredditIconUrl; - } - - public String getAuthor() { - return author; - } - - public String getAuthorNamePrefixed() { - return authorNamePrefixed; - } - - public String getAuthorIconUrl() { - return authorIconUrl; - } - - public void setAuthorIconUrl(String authorIconUrl) { - this.authorIconUrl = authorIconUrl; - } - - public String getPostTime() { - return postTime; - } - - public long getPostTimeMillis() { - return postTimeMillis; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getSelfText() { - return selfText; - } - - public void setSelfText(String selfText) { - this.selfText = selfText; - } - - public String getSelfTextPlainTrimmed() { - return selfTextPlainTrimmed; - } - - public void setSelfTextPlainTrimmed(String selfTextPlainTrimmed) { - this.selfTextPlainTrimmed = selfTextPlainTrimmed; - } - - public String getPreviewUrl() { - return previewUrl; - } - - public String getThumbnailPreviewUrl() { - return thumbnailPreviewUrl; - } - - public String getUrl() { - return url; - } - - public String getVideoUrl() { - return videoUrl; - } - - public void setVideoUrl(String videoUrl) { - this.videoUrl = videoUrl; - } - - public String getVideoDownloadUrl() { - return videoDownloadUrl; - } - - public void setVideoDownloadUrl(String videoDownloadUrl) { - this.videoDownloadUrl = videoDownloadUrl; - } - - public String getPermalink() { - return permalink; - } - - public String getFlair() { - return flair; - } - - public void setFlair(String flair) { - this.flair = flair; - } - - public int getScore() { - return score; - } - - public void setScore(int score) { - this.score = score; - } - - public int getPostType() { - return postType; - } - - public int getVoteType() { - return voteType; - } - - public void setVoteType(int voteType) { - this.voteType = voteType; - } - - public int getGilded() { - return gilded; - } - - public int getPreviewWidth() { - return previewWidth; - } - - public void setPreviewWidth(int previewWidth) { - this.previewWidth = previewWidth; - } - - public int getPreviewHeight() { - return previewHeight; - } - - public void setPreviewHeight(int previewHeight) { - this.previewHeight = previewHeight; - } - - public int getNComments() { - return nComments; - } - - public void setNComments(int nComments) { - this.nComments = nComments; - } - - public boolean isHidden() { - return hidden; - } - - public void setHidden(boolean hidden) { - this.hidden = hidden; - } - - public boolean isSpoiler() { - return spoiler; - } - - public void setSpoiler(boolean spoiler) { - this.spoiler = spoiler; - } - - public boolean isNSFW() { - return nsfw; - } - - public void setNSFW(boolean nsfw) { - this.nsfw = nsfw; - } - - @Override - public int describeContents() { - return 0; - } - - public boolean isStickied() { - return stickied; - } - - public boolean isArchived() { - return archived; - } - - public boolean isLocked() { - return locked; - } - - public boolean isSaved() { - return saved; - } - - public void setSaved(boolean saved) { - this.saved = saved; - } - - public boolean isCrosspost() { - return isCrosspost; - } - - public String getCrosspostParentId() { - return crosspostParentId; - } - - public void setCrosspostParentId(String crosspostParentId) { - this.crosspostParentId = crosspostParentId; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(id); - parcel.writeString(fullName); - parcel.writeString(subredditName); - parcel.writeString(subredditNamePrefixed); - parcel.writeString(subredditIconUrl); - parcel.writeString(author); - parcel.writeString(authorNamePrefixed); - parcel.writeString(authorIconUrl); - parcel.writeString(postTime); - parcel.writeLong(postTimeMillis); - parcel.writeString(title); - parcel.writeString(selfText); - parcel.writeString(selfTextPlainTrimmed); - parcel.writeString(previewUrl); - parcel.writeString(thumbnailPreviewUrl); - parcel.writeString(url); - parcel.writeString(videoUrl); - parcel.writeString(videoDownloadUrl); - parcel.writeString(permalink); - parcel.writeString(flair); - parcel.writeInt(score); - parcel.writeInt(postType); - parcel.writeInt(voteType); - parcel.writeInt(gilded); - parcel.writeInt(previewWidth); - parcel.writeInt(previewHeight); - parcel.writeInt(nComments); - parcel.writeByte((byte) (hidden ? 1 : 0)); - parcel.writeByte((byte) (spoiler ? 1 : 0)); - parcel.writeByte((byte) (nsfw ? 1 : 0)); - parcel.writeByte((byte) (stickied ? 1 : 0)); - parcel.writeByte((byte) (archived ? 1 : 0)); - parcel.writeByte((byte) (locked ? 1 : 0)); - parcel.writeByte((byte) (saved ? 1 : 0)); - parcel.writeByte((byte) (isCrosspost ? 1 : 0)); - parcel.writeString(crosspostParentId); - } -} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java new file mode 100644 index 00000000..e16893c2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -0,0 +1,466 @@ +package ml.docilealligator.infinityforreddit.Post; + +import android.os.Parcel; +import android.os.Parcelable; + +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; + +/** + * Created by alex on 3/1/18. + */ + +public class Post implements Parcelable { + public static final int NSFW_TYPE = -1; + public static final int TEXT_TYPE = 0; + public static final int IMAGE_TYPE = 1; + public static final int LINK_TYPE = 2; + public static final int VIDEO_TYPE = 3; + public static final int GIF_TYPE = 4; + public static final int NO_PREVIEW_LINK_TYPE = 5; + public static final Creator CREATOR = new Creator() { + @Override + public Post createFromParcel(Parcel in) { + return new Post(in); + } + + @Override + public Post[] newArray(int size) { + return new Post[size]; + } + }; + private String id; + private String fullName; + private String subredditName; + private String subredditNamePrefixed; + private String subredditIconUrl; + private String author; + private String authorNamePrefixed; + private String authorIconUrl; + private String postTime; + private String title; + private String selfText; + private String selfTextPlainTrimmed; + private String previewUrl; + private String thumbnailPreviewUrl; + private String url; + private String videoUrl; + private String videoDownloadUrl; + private String permalink; + private String flair; + private long postTimeMillis; + private int score; + private int postType; + private int voteType; + private int gilded; + private int previewWidth; + private int previewHeight; + private int nComments; + private boolean hidden; + private boolean spoiler; + private boolean nsfw; + private boolean stickied; + private boolean archived; + private boolean locked; + private boolean saved; + private boolean isCrosspost; + private String crosspostParentId; + + public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, + String author, String postTime, long postTimeMillis, String title, String previewUrl, + String thumbnailPreviewUrl, String permalink, int score, int postType, + int voteType, int gilded, int nComments, String flair, boolean hidden, boolean spoiler, + boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, + boolean isCrosspost) { + this.id = id; + this.fullName = fullName; + this.subredditName = subredditName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.author = author; + this.authorNamePrefixed = "u/" + author; + this.postTime = postTime; + this.postTimeMillis = postTimeMillis; + this.title = title; + this.previewUrl = previewUrl; + this.thumbnailPreviewUrl = thumbnailPreviewUrl; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nComments = nComments; + this.flair = flair; + this.hidden = hidden; + this.spoiler = spoiler; + this.nsfw = nsfw; + this.stickied = stickied; + this.archived = archived; + this.locked = locked; + this.saved = saved; + this.isCrosspost = isCrosspost; + } + + public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, + String author, String postTime, long postTimeMillis, String title, String previewUrl, + String thumbnailPreviewUrl, String url, String permalink, int score, + int postType, int voteType, int gilded, int nComments, String flair, boolean hidden, + boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, + boolean saved, boolean isCrosspost) { + this.id = id; + this.fullName = fullName; + this.subredditName = subredditName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.author = author; + this.authorNamePrefixed = "u/" + author; + this.postTime = postTime; + this.postTimeMillis = postTimeMillis; + this.title = title; + this.previewUrl = previewUrl; + this.thumbnailPreviewUrl = thumbnailPreviewUrl; + this.url = url; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nComments = nComments; + this.flair = flair; + this.hidden = hidden; + this.spoiler = spoiler; + this.nsfw = nsfw; + this.stickied = stickied; + this.archived = archived; + this.locked = locked; + this.saved = saved; + this.isCrosspost = isCrosspost; + } + + public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, + String postTime, long postTimeMillis, String title, String permalink, int score, int postType, int voteType, int gilded, + int nComments, String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, + boolean archived, boolean locked, boolean saved, boolean isCrosspost) { + this.id = id; + this.fullName = fullName; + this.subredditName = subredditName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.author = author; + this.authorNamePrefixed = "u/" + author; + this.postTime = postTime; + this.postTimeMillis = postTimeMillis; + this.title = title; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nComments = nComments; + this.flair = flair; + this.hidden = hidden; + this.spoiler = spoiler; + this.nsfw = nsfw; + this.stickied = stickied; + this.archived = archived; + this.locked = locked; + this.saved = saved; + this.isCrosspost = isCrosspost; + } + + protected Post(Parcel in) { + id = in.readString(); + fullName = in.readString(); + subredditName = in.readString(); + subredditNamePrefixed = in.readString(); + subredditIconUrl = in.readString(); + author = in.readString(); + authorNamePrefixed = in.readString(); + authorIconUrl = in.readString(); + postTime = in.readString(); + postTimeMillis = in.readLong(); + title = in.readString(); + selfText = in.readString(); + selfTextPlainTrimmed = in.readString(); + previewUrl = in.readString(); + thumbnailPreviewUrl = in.readString(); + url = in.readString(); + videoUrl = in.readString(); + videoDownloadUrl = in.readString(); + permalink = in.readString(); + flair = in.readString(); + score = in.readInt(); + postType = in.readInt(); + voteType = in.readInt(); + gilded = in.readInt(); + previewWidth = in.readInt(); + previewHeight = in.readInt(); + nComments = in.readInt(); + hidden = in.readByte() != 0; + spoiler = in.readByte() != 0; + nsfw = in.readByte() != 0; + stickied = in.readByte() != 0; + archived = in.readByte() != 0; + locked = in.readByte() != 0; + saved = in.readByte() != 0; + isCrosspost = in.readByte() != 0; + crosspostParentId = in.readString(); + } + + public String getId() { + return id; + } + + public String getFullName() { + return fullName; + } + + public String getSubredditName() { + return subredditName; + } + + public String getSubredditNamePrefixed() { + return subredditNamePrefixed; + } + + public String getSubredditIconUrl() { + return subredditIconUrl; + } + + public void setSubredditIconUrl(String subredditIconUrl) { + this.subredditIconUrl = subredditIconUrl; + } + + public String getAuthor() { + return author; + } + + public String getAuthorNamePrefixed() { + return authorNamePrefixed; + } + + public String getAuthorIconUrl() { + return authorIconUrl; + } + + public void setAuthorIconUrl(String authorIconUrl) { + this.authorIconUrl = authorIconUrl; + } + + public String getPostTime() { + return postTime; + } + + public long getPostTimeMillis() { + return postTimeMillis; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getSelfText() { + return selfText; + } + + public void setSelfText(String selfText) { + this.selfText = selfText; + } + + public String getSelfTextPlainTrimmed() { + return selfTextPlainTrimmed; + } + + public void setSelfTextPlainTrimmed(String selfTextPlainTrimmed) { + this.selfTextPlainTrimmed = selfTextPlainTrimmed; + } + + public String getPreviewUrl() { + return previewUrl; + } + + public String getThumbnailPreviewUrl() { + return thumbnailPreviewUrl; + } + + public String getUrl() { + return url; + } + + public String getVideoUrl() { + return videoUrl; + } + + public void setVideoUrl(String videoUrl) { + this.videoUrl = videoUrl; + } + + public String getVideoDownloadUrl() { + return videoDownloadUrl; + } + + public void setVideoDownloadUrl(String videoDownloadUrl) { + this.videoDownloadUrl = videoDownloadUrl; + } + + public String getPermalink() { + return permalink; + } + + public String getFlair() { + return flair; + } + + public void setFlair(String flair) { + this.flair = flair; + } + + public int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + public int getPostType() { + return postType; + } + + public int getVoteType() { + return voteType; + } + + public void setVoteType(int voteType) { + this.voteType = voteType; + } + + public int getGilded() { + return gilded; + } + + public int getPreviewWidth() { + return previewWidth; + } + + public void setPreviewWidth(int previewWidth) { + this.previewWidth = previewWidth; + } + + public int getPreviewHeight() { + return previewHeight; + } + + public void setPreviewHeight(int previewHeight) { + this.previewHeight = previewHeight; + } + + public int getNComments() { + return nComments; + } + + public void setNComments(int nComments) { + this.nComments = nComments; + } + + public boolean isHidden() { + return hidden; + } + + public void setHidden(boolean hidden) { + this.hidden = hidden; + } + + public boolean isSpoiler() { + return spoiler; + } + + public void setSpoiler(boolean spoiler) { + this.spoiler = spoiler; + } + + public boolean isNSFW() { + return nsfw; + } + + public void setNSFW(boolean nsfw) { + this.nsfw = nsfw; + } + + @Override + public int describeContents() { + return 0; + } + + public boolean isStickied() { + return stickied; + } + + public boolean isArchived() { + return archived; + } + + public boolean isLocked() { + return locked; + } + + public boolean isSaved() { + return saved; + } + + public void setSaved(boolean saved) { + this.saved = saved; + } + + public boolean isCrosspost() { + return isCrosspost; + } + + public String getCrosspostParentId() { + return crosspostParentId; + } + + public void setCrosspostParentId(String crosspostParentId) { + this.crosspostParentId = crosspostParentId; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(id); + parcel.writeString(fullName); + parcel.writeString(subredditName); + parcel.writeString(subredditNamePrefixed); + parcel.writeString(subredditIconUrl); + parcel.writeString(author); + parcel.writeString(authorNamePrefixed); + parcel.writeString(authorIconUrl); + parcel.writeString(postTime); + parcel.writeLong(postTimeMillis); + parcel.writeString(title); + parcel.writeString(selfText); + parcel.writeString(selfTextPlainTrimmed); + parcel.writeString(previewUrl); + parcel.writeString(thumbnailPreviewUrl); + parcel.writeString(url); + parcel.writeString(videoUrl); + parcel.writeString(videoDownloadUrl); + parcel.writeString(permalink); + parcel.writeString(flair); + parcel.writeInt(score); + parcel.writeInt(postType); + parcel.writeInt(voteType); + parcel.writeInt(gilded); + parcel.writeInt(previewWidth); + parcel.writeInt(previewHeight); + parcel.writeInt(nComments); + parcel.writeByte((byte) (hidden ? 1 : 0)); + parcel.writeByte((byte) (spoiler ? 1 : 0)); + parcel.writeByte((byte) (nsfw ? 1 : 0)); + parcel.writeByte((byte) (stickied ? 1 : 0)); + parcel.writeByte((byte) (archived ? 1 : 0)); + parcel.writeByte((byte) (locked ? 1 : 0)); + parcel.writeByte((byte) (saved ? 1 : 0)); + parcel.writeByte((byte) (isCrosspost ? 1 : 0)); + parcel.writeString(crosspostParentId); + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java new file mode 100644 index 00000000..19536c47 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -0,0 +1,882 @@ +package ml.docilealligator.infinityforreddit.Post; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.PageKeyedDataSource; + +import java.util.ArrayList; +import java.util.Locale; + +import ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.ParsePost; +import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +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 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 Retrofit retrofit; + private String accessToken; + private Locale locale; + private String subredditOrUserName; + private String query; + private int postType; + private SortType sortType; + private boolean nsfw; + private int filter; + private String userWhere; + private String multiRedditPath; + + private MutableLiveData paginationNetworkStateLiveData; + private MutableLiveData initialLoadStateLiveData; + private MutableLiveData hasPostLiveData; + + private LoadParams params; + private LoadCallback callback; + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, + int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + paginationNetworkStateLiveData = new MutableLiveData<>(); + initialLoadStateLiveData = new MutableLiveData<>(); + hasPostLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType, + SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + if (postType == TYPE_SUBREDDIT) { + 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.filter = filter; + this.nsfw = nsfw; + } + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, + SortType sortType, String where, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + 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; + userWhere = where; + this.filter = filter; + this.nsfw = nsfw; + } + + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, + int postType, SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditOrUserName = subredditOrUserName; + this.query = query; + paginationNetworkStateLiveData = new MutableLiveData<>(); + initialLoadStateLiveData = new MutableLiveData<>(); + hasPostLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType == null ? new SortType(SortType.Type.RELEVANCE) : sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + 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: + 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; + } + } + + @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; + } + } + + 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, + RedditUtils.getOAuthHeader(accessToken)); + } else { + bestPost = api.getBestPosts(sortType.getType().value, lastItem, RedditUtils.getOAuthHeader(accessToken)); + } + bestPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + if (sortType.getType().value.equals(SortType.Type.RANDOM.value)) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(Post post) { + ArrayList singlePostList = new ArrayList<>(); + singlePostList.add(post); + callback.onResult(singlePostList, null, null); + hasPostLiveData.postValue(true); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onParsePostFail() { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } else { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + callback.onResult(newPosts, null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadBestPostsInitial(callback, nextPageKey); + return; + } else { + callback.onResult(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, + RedditUtils.getOAuthHeader(accessToken)); + } else { + bestPost = api.getBestPosts(sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); + } + + bestPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadBestPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, + lastItem, RedditUtils.getOAuthHeader(accessToken)); + } + } + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + if (sortType.getType().value.equals(SortType.Type.RANDOM.value)) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(Post post) { + ArrayList singlePostList = new ArrayList<>(); + singlePostList.add(post); + callback.onResult(singlePostList, null, null); + hasPostLiveData.postValue(true); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onParsePostFail() { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } else { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + callback.onResult(newPosts, null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadSubredditPostsInitial(callback, nextPageKey); + return; + } else { + callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, + after, RedditUtils.getOAuthHeader(accessToken)); + } + } + + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadSubredditPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, + RedditUtils.getOAuthHeader(accessToken)); + } + } + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + callback.onResult(newPosts, null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadUserPostsInitial(callback, nextPageKey); + return; + } else { + callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, + RedditUtils.getOAuthHeader(accessToken)); + } + } + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadUserPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, 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); + } else { + getPost = api.searchPosts(query, lastItem, sortType.getType().value); + } + } else { + if(sortType.getTime() != null) { + getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, + sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, + RedditUtils.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, + RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, + sortType.getType().value, lastItem, + RedditUtils.getOAuthHeader(accessToken)); + } + } + } + + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + callback.onResult(newPosts, null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadSearchPostsInitial(callback, nextPageKey); + return; + } else { + callback.onResult(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); + } else { + getPost = api.searchPosts(query, after, sortType.getType().value); + } + } else { + if (sortType.getTime() != null) { + getPost = api.searchPostsOauth(query, after, sortType.getType().value, + sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsOauth(query, after, sortType.getType().value, RedditUtils.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, + RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, + sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); + } + } + } + + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadSearchPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, + RedditUtils.getOAuthHeader(accessToken)); + } + } + + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + callback.onResult(newPosts, null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadMultiRedditPostsInitial(callback, nextPageKey); + return; + } else { + callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); + } else { + getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, + RedditUtils.getOAuthHeader(accessToken)); + } + } + getPost.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadMultiRedditPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, 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 new file mode 100644 index 00000000..eb953d7d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSourceFactory.java @@ -0,0 +1,113 @@ +package ml.docilealligator.infinityforreddit.Post; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.DataSource; + +import java.util.Locale; + +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +class PostDataSourceFactory extends DataSource.Factory { + private Retrofit retrofit; + private String accessToken; + private Locale locale; + private String subredditName; + private String query; + private int postType; + private SortType sortType; + private String userWhere; + private int filter; + private boolean nsfw; + + private PostDataSource postDataSource; + private MutableLiveData postDataSourceLiveData; + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, + int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + int postType, SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + int postType, SortType sortType, String where, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType; + userWhere = where; + this.filter = filter; + this.nsfw = nsfw; + } + + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, + String query, int postType, SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.query = query; + postDataSourceLiveData = new MutableLiveData<>(); + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + @NonNull + @Override + public DataSource create() { + if (postType == PostDataSource.TYPE_FRONT_PAGE) { + postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, + filter, nsfw); + } else if (postType == PostDataSource.TYPE_SEARCH) { + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, + postType, sortType, filter, nsfw); + } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, + sortType, filter, nsfw); + } else { + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, + sortType, userWhere, filter, nsfw); + } + + postDataSourceLiveData.postValue(postDataSource); + return postDataSource; + } + + public MutableLiveData getPostDataSourceLiveData() { + return postDataSourceLiveData; + } + + PostDataSource getPostDataSource() { + return postDataSource; + } + + void changeNSFWAndSortType(boolean nsfw, SortType sortType) { + this.nsfw = nsfw; + this.sortType = sortType; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java new file mode 100644 index 00000000..6c7464d3 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostViewModel.java @@ -0,0 +1,273 @@ +package ml.docilealligator.infinityforreddit.Post; + +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.ViewModelProvider; +import androidx.paging.LivePagedListBuilder; +import androidx.paging.PagedList; + +import java.util.Locale; + +import ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class PostViewModel extends ViewModel { + private PostDataSourceFactory postDataSourceFactory; + private LiveData paginationNetworkState; + private LiveData initialLoadingState; + private LiveData hasPostLiveData; + private LiveData> posts; + private MutableLiveData nsfwLiveData; + private MutableLiveData sortTypeLiveData; + private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData; + + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, + int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, + sortType, filter, nsfw); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getPaginationNetworkStateLiveData); + hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::hasPostLiveData); + + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); + return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + }); + } + + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, + postType, sortType, filter, nsfw); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getPaginationNetworkStateLiveData); + hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::hasPostLiveData); + + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); + return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + }); + } + + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + SortType sortType, String where, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, + postType, sortType, where, filter, nsfw); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getPaginationNetworkStateLiveData); + hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::hasPostLiveData); + + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); + return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + }); + } + + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, + int postType, SortType sortType, int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, + query, postType, sortType, filter, nsfw); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::getPaginationNetworkStateLiveData); + hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), + PostDataSource::hasPostLiveData); + + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); + return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + }); + } + + public LiveData> getPosts() { + return posts; + } + + public LiveData getPaginationNetworkState() { + return paginationNetworkState; + } + + public LiveData getInitialLoadingState() { + return initialLoadingState; + } + + public LiveData hasPost() { + return hasPostLiveData; + } + + public void refresh() { + postDataSourceFactory.getPostDataSource().invalidate(); + } + + public void retryLoadingMore() { + postDataSourceFactory.getPostDataSource().retryLoadingMore(); + } + + public void changeSortType(SortType sortType) { + sortTypeLiveData.postValue(sortType); + } + + public void changeNSFW(boolean nsfw) { + nsfwLiveData.postValue(nsfw); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Retrofit retrofit; + private String accessToken; + private Locale locale; + private String subredditName; + private String query; + private int postType; + private SortType sortType; + private String userWhere; + private int filter; + private boolean nsfw; + + public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, + int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + //User posts + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, + SortType sortType, String where, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.postType = postType; + this.sortType = sortType; + userWhere = where; + this.filter = filter; + this.nsfw = nsfw; + } + + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, + int postType, SortType sortType, int filter, boolean nsfw) { + this.retrofit = retrofit; + this.accessToken = accessToken; + this.locale = locale; + this.subredditName = subredditName; + this.query = query; + this.postType = postType; + this.sortType = sortType; + this.filter = filter; + this.nsfw = nsfw; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + if (postType == PostDataSource.TYPE_FRONT_PAGE) { + return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter, + nsfw); + } else if (postType == PostDataSource.TYPE_SEARCH) { + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, + postType, sortType, filter, nsfw); + } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, + sortType, filter, nsfw); + } else { + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, + sortType, userWhere, filter, nsfw); + } + } + } + + private static class NSFWAndSortTypeLiveData extends MediatorLiveData> { + public NSFWAndSortTypeLiveData(LiveData nsfw, LiveData sortType) { + addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); + addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1))); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java deleted file mode 100644 index 7244cb85..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ /dev/null @@ -1,878 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.PageKeyedDataSource; - -import java.util.ArrayList; -import java.util.Locale; - -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; -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 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 Retrofit retrofit; - private String accessToken; - private Locale locale; - private String subredditOrUserName; - private String query; - private int postType; - private SortType sortType; - private boolean nsfw; - private int filter; - private String userWhere; - private String multiRedditPath; - - private MutableLiveData paginationNetworkStateLiveData; - private MutableLiveData initialLoadStateLiveData; - private MutableLiveData hasPostLiveData; - - private LoadParams params; - private LoadCallback callback; - - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType, - SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - if (postType == TYPE_SUBREDDIT) { - 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.filter = filter; - this.nsfw = nsfw; - } - - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - 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; - userWhere = where; - this.filter = filter; - this.nsfw = nsfw; - } - - PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditOrUserName = subredditOrUserName; - this.query = query; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasPostLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType == null ? new SortType(SortType.Type.RELEVANCE) : sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - 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: - 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; - } - } - - @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; - } - } - - 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, - RedditUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPosts(sortType.getType().value, lastItem, RedditUtils.getOAuthHeader(accessToken)); - } - bestPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - if (sortType.getType().value.equals(SortType.Type.RANDOM.value)) { - ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(Post post) { - ArrayList singlePostList = new ArrayList<>(); - singlePostList.add(post); - callback.onResult(singlePostList, null, null); - hasPostLiveData.postValue(true); - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadBestPostsInitial(callback, nextPageKey); - return; - } else { - callback.onResult(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, - RedditUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPosts(sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); - } - - bestPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadBestPostsAfter(params, callback, lastItem); - } else { - callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - lastItem, RedditUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - if (sortType.getType().value.equals(SortType.Type.RANDOM.value)) { - ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(Post post) { - ArrayList singlePostList = new ArrayList<>(); - singlePostList.add(post); - callback.onResult(singlePostList, null, null); - hasPostLiveData.postValue(true); - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onParsePostFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); - } - }); - } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadSubredditPostsInitial(callback, nextPageKey); - return; - } else { - callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - after, RedditUtils.getOAuthHeader(accessToken)); - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadSubredditPostsAfter(params, callback, lastItem); - } else { - callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, - RedditUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadUserPostsInitial(callback, nextPageKey); - return; - } else { - callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, - RedditUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadUserPostsAfter(params, callback, lastItem); - } else { - callback.onResult(newPosts, 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); - } else { - getPost = api.searchPosts(query, lastItem, sortType.getType().value); - } - } else { - if(sortType.getTime() != null) { - getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, - RedditUtils.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, - RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, lastItem, - RedditUtils.getOAuthHeader(accessToken)); - } - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadSearchPostsInitial(callback, nextPageKey); - return; - } else { - callback.onResult(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); - } else { - getPost = api.searchPosts(query, after, sortType.getType().value); - } - } else { - if (sortType.getTime() != null) { - getPost = api.searchPostsOauth(query, after, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsOauth(query, after, sortType.getType().value, RedditUtils.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, - RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); - } - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadSearchPostsAfter(params, callback, lastItem); - } else { - callback.onResult(newPosts, 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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, - RedditUtils.getOAuthHeader(accessToken)); - } - } - - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - String nextPageKey; - if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { - nextPageKey = null; - } else { - nextPageKey = lastItem; - } - - if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); - hasPostLiveData.postValue(true); - } else if (nextPageKey != null) { - loadMultiRedditPostsInitial(callback, nextPageKey); - return; - } else { - callback.onResult(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, RedditUtils.getOAuthHeader(accessToken)); - } else { - getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, - RedditUtils.getOAuthHeader(accessToken)); - } - } - getPost.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { - if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { - loadMultiRedditPostsAfter(params, callback, lastItem); - } else { - callback.onResult(newPosts, 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/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java deleted file mode 100644 index 568efbf0..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ /dev/null @@ -1,112 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.DataSource; - -import java.util.Locale; - -import retrofit2.Retrofit; - -class PostDataSourceFactory extends DataSource.Factory { - private Retrofit retrofit; - private String accessToken; - private Locale locale; - private String subredditName; - private String query; - private int postType; - private SortType sortType; - private String userWhere; - private int filter; - private boolean nsfw; - - private PostDataSource postDataSource; - private MutableLiveData postDataSourceLiveData; - - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, SortType sortType, String where, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - userWhere = where; - this.filter = filter; - this.nsfw = nsfw; - } - - PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - String query, int postType, SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - this.query = query; - postDataSourceLiveData = new MutableLiveData<>(); - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - @NonNull - @Override - public DataSource create() { - if (postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, - filter, nsfw); - } else if (postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, - postType, sortType, filter, nsfw); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, filter, nsfw); - } else { - postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, userWhere, filter, nsfw); - } - - postDataSourceLiveData.postValue(postDataSource); - return postDataSource; - } - - public MutableLiveData getPostDataSourceLiveData() { - return postDataSourceLiveData; - } - - PostDataSource getPostDataSource() { - return postDataSource; - } - - void changeNSFWAndSortType(boolean nsfw, SortType sortType) { - this.nsfw = nsfw; - this.sortType = sortType; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java deleted file mode 100644 index ffba16c7..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ /dev/null @@ -1,271 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -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.ViewModelProvider; -import androidx.paging.LivePagedListBuilder; -import androidx.paging.PagedList; - -import java.util.Locale; - -import retrofit2.Retrofit; - -public class PostViewModel extends ViewModel { - private PostDataSourceFactory postDataSourceFactory; - private LiveData paginationNetworkState; - private LiveData initialLoadingState; - private LiveData hasPostLiveData; - private LiveData> posts; - private MutableLiveData nsfwLiveData; - private MutableLiveData sortTypeLiveData; - private NSFWAndSortTypeLiveData nsfwAndSortTypeLiveData; - - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, - sortType, filter, nsfw); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); - - nsfwLiveData = new MutableLiveData<>(); - nsfwLiveData.postValue(nsfw); - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { - postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); - }); - } - - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - postType, sortType, filter, nsfw); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); - - nsfwLiveData = new MutableLiveData<>(); - nsfwLiveData.postValue(nsfw); - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { - postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); - }); - } - - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - postType, sortType, where, filter, nsfw); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); - - nsfwLiveData = new MutableLiveData<>(); - nsfwLiveData.postValue(nsfw); - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { - postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); - }); - } - - public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - query, postType, sortType, filter, nsfw); - - initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::getPaginationNetworkStateLiveData); - hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), - PostDataSource::hasPostLiveData); - - nsfwLiveData = new MutableLiveData<>(); - nsfwLiveData.postValue(nsfw); - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> { - postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); - return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); - }); - } - - public LiveData> getPosts() { - return posts; - } - - public LiveData getPaginationNetworkState() { - return paginationNetworkState; - } - - public LiveData getInitialLoadingState() { - return initialLoadingState; - } - - public LiveData hasPost() { - return hasPostLiveData; - } - - public void refresh() { - postDataSourceFactory.getPostDataSource().invalidate(); - } - - public void retryLoadingMore() { - postDataSourceFactory.getPostDataSource().retryLoadingMore(); - } - - public void changeSortType(SortType sortType) { - sortTypeLiveData.postValue(sortType); - } - - public void changeNSFW(boolean nsfw) { - nsfwLiveData.postValue(nsfw); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private Retrofit retrofit; - private String accessToken; - private Locale locale; - private String subredditName; - private String query; - private int postType; - private SortType sortType; - private String userWhere; - private int filter; - private boolean nsfw; - - public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType, - int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - //User posts - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - SortType sortType, String where, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - this.postType = postType; - this.sortType = sortType; - userWhere = where; - this.filter = filter; - this.nsfw = nsfw; - } - - public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, SortType sortType, int filter, boolean nsfw) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.locale = locale; - this.subredditName = subredditName; - this.query = query; - this.postType = postType; - this.sortType = sortType; - this.filter = filter; - this.nsfw = nsfw; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter, - nsfw); - } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, - postType, sortType, filter, nsfw); - } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, - sortType, filter, nsfw); - } else { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, - sortType, userWhere, filter, nsfw); - } - } - } - - private static class NSFWAndSortTypeLiveData extends MediatorLiveData> { - public NSFWAndSortTypeLiveData(LiveData nsfw, LiveData sortType) { - addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); - addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1))); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java index 2b6ad9ce..0fd5e8dc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java @@ -35,7 +35,7 @@ import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitVideoPostEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NotificationUtils; -import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SubmitPost; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index ace57b38..6a93edf1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import okhttp3.MediaType; -- cgit v1.2.3