From 4654090b314a8ffa8c9ee5d74ddf0f7b07aee2fb Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 6 Jul 2020 12:19:21 +0800 Subject: Remove ParseAndSaveAccountInfo and move the code to FetchMyInfo. Move some classes to packages. --- .../infinityforreddit/Activity/LoginActivity.java | 28 ++-- .../infinityforreddit/Activity/MainActivity.java | 26 +--- .../Activity/SubredditMultiselectionActivity.java | 2 +- .../Activity/SubredditSelectionActivity.java | 4 +- .../Activity/SubscribedThingListingActivity.java | 4 +- .../Activity/ViewPostDetailActivity.java | 4 +- .../Activity/ViewSubredditDetailActivity.java | 8 +- .../Activity/ViewUserDetailActivity.java | 4 +- .../NavigationDrawerRecyclerViewAdapter.java | 2 +- .../SubredditListingRecyclerViewAdapter.java | 4 +- ...SubredditMultiselectionRecyclerViewAdapter.java | 4 +- .../SubscribedSubredditsRecyclerViewAdapter.java | 2 +- .../Adapter/UserListingRecyclerViewAdapter.java | 2 +- .../CheckIsSubscribedToSubredditAsyncTask.java | 2 +- .../AsyncTask/InsertSubredditDataAsyncTask.java | 4 +- .../AsyncTask/InsertSubscribedThingsAsyncTask.java | 8 +- .../AsyncTask/LoadSubredditIconAsyncTask.java | 6 +- .../AsyncTask/LoadUserDataAsyncTask.java | 2 +- .../Comment/FetchRemovedComment.java | 98 +++++++++++++ .../infinityforreddit/FavoriteThing.java | 2 +- .../infinityforreddit/FetchMyInfo.java | 75 +++++++++- .../infinityforreddit/FetchRemovedComment.java | 98 ------------- .../infinityforreddit/FetchRemovedPost.java | 141 ------------------ .../infinityforreddit/FetchSubredditData.java | 89 ----------- .../infinityforreddit/FetchSubscribedThing.java | 4 +- .../infinityforreddit/FetchUserData.java | 88 ----------- .../Fragment/SidebarFragment.java | 6 +- .../Fragment/SubredditListingFragment.java | 3 +- .../SubscribedSubredditsListingFragment.java | 2 +- .../Fragment/UserListingFragment.java | 3 +- .../infinityforreddit/ParseAndSaveAccountInfo.java | 74 ---------- .../infinityforreddit/ParseSubredditData.java | 162 --------------------- .../infinityforreddit/ParseSubscribedThing.java | 4 +- .../infinityforreddit/ParseUserData.java | 153 ------------------- .../infinityforreddit/Post/FetchRemovedPost.java | 141 ++++++++++++++++++ .../infinityforreddit/RedditDataRoomDatabase.java | 8 +- .../Subreddit/FetchSubredditData.java | 88 +++++++++++ .../Subreddit/ParseSubredditData.java | 162 +++++++++++++++++++++ .../infinityforreddit/Subreddit/SubredditDao.java | 22 +++ .../infinityforreddit/Subreddit/SubredditData.java | 73 ++++++++++ .../Subreddit/SubredditListingDataSource.java | 104 +++++++++++++ .../SubredditListingDataSourceFactory.java | 44 ++++++ .../Subreddit/SubredditListingViewModel.java | 94 ++++++++++++ .../Subreddit/SubredditRepository.java | 40 +++++ .../Subreddit/SubredditSubscription.java | 120 +++++++++++++++ .../Subreddit/SubredditViewModel.java | 51 +++++++ .../Subreddit/SubredditWithSelection.java | 101 +++++++++++++ .../SubredditDatabase/SubredditDao.java | 22 --- .../SubredditDatabase/SubredditData.java | 73 ---------- .../SubredditDatabase/SubredditRepository.java | 40 ----- .../SubredditDatabase/SubredditViewModel.java | 51 ------- .../SubredditListingDataSource.java | 103 ------------- .../SubredditListingDataSourceFactory.java | 43 ------ .../SubredditListingViewModel.java | 93 ------------ .../infinityforreddit/SubredditSubscription.java | 120 --------------- .../infinityforreddit/SubredditWithSelection.java | 102 ------------- .../SubscribedSubredditDao.java | 33 +++++ .../SubscribedSubredditData.java | 65 +++++++++ .../SubscribedSubredditRepository.java | 48 ++++++ .../SubscribedSubredditViewModel.java | 56 +++++++ .../SubscribedSubredditDao.java | 33 ----- .../SubscribedSubredditData.java | 65 --------- .../SubscribedSubredditRepository.java | 48 ------ .../SubscribedSubredditViewModel.java | 56 ------- .../infinityforreddit/User/FetchUserData.java | 87 +++++++++++ .../infinityforreddit/User/ParseUserData.java | 152 +++++++++++++++++++ .../infinityforreddit/User/UserFollowing.java | 118 +++++++++++++++ .../User/UserListingDataSource.java | 104 +++++++++++++ .../User/UserListingDataSourceFactory.java | 44 ++++++ .../User/UserListingViewModel.java | 94 ++++++++++++ .../infinityforreddit/UserFollowing.java | 119 --------------- .../infinityforreddit/UserListingDataSource.java | 103 ------------- .../UserListingDataSourceFactory.java | 43 ------ .../infinityforreddit/UserListingViewModel.java | 93 ------------ 74 files changed, 2071 insertions(+), 2103 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Comment/FetchRemovedComment.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditSubscription.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditWithSelection.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/ParseUserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserFollowing.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java index a15536a9..239effc5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java @@ -31,13 +31,12 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.AsyncTask.ParseAndInsertNewAccountAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.FetchMyInfo; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.ParseAndSaveAccountInfo; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -144,12 +143,10 @@ public class LoginActivity extends BaseActivity { String accessToken = responseJSON.getString(APIUtils.ACCESS_TOKEN_KEY); String refreshToken = responseJSON.getString(APIUtils.REFRESH_TOKEN_KEY); - FetchMyInfo.fetchAccountInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() { - @Override - public void onFetchMyInfoSuccess(String response) { - ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() { + FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mRedditDataRoomDatabase, + accessToken, new FetchMyInfo.FetchMyInfoListener() { @Override - public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { + public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { new ParseAndInsertNewAccountAsyncTask(name, accessToken, refreshToken, profileImageUrl, bannerImageUrl, karma, authCode, mRedditDataRoomDatabase.accountDao(), () -> { @@ -160,18 +157,15 @@ public class LoginActivity extends BaseActivity { } @Override - public void onParseMyInfoFail() { - Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show(); + public void onFetchMyInfoFailed(boolean parseFailed) { + if (parseFailed) { + Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); + } + finish(); } - }); - } - - @Override - public void onFetchMyInfoFail() { - Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); - finish(); - } }); } catch (JSONException e) { e.printStackTrace(); 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 1c840c63..4b488bfa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -78,17 +78,16 @@ import ml.docilealligator.infinityforreddit.FetchMyInfo; import ml.docilealligator.infinityforreddit.FetchSubscribedThing; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.ParseAndSaveAccountInfo; +import ml.docilealligator.infinityforreddit.Message.ReadMessage; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.PullNotificationWorker; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Message.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -673,12 +672,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private void loadUserData() { if (!mFetchUserInfoSuccess) { - FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() { - @Override - public void onFetchMyInfoSuccess(String response) { - ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() { + FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + new FetchMyInfo.FetchMyInfoListener() { @Override - public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { + public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mAccountName = name; mProfileImageUrl = profileImageUrl; mBannerImageUrl = bannerImageUrl; @@ -687,16 +684,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } @Override - public void onParseMyInfoFail() { + public void onFetchMyInfoFailed(boolean parseFailed) { mFetchUserInfoSuccess = false; } - }); - } - - @Override - public void onFetchMyInfoFail() { - mFetchUserInfoSuccess = false; - } }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java index 18aeed17..c3a1e4f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditMultiselectionActivity.java @@ -42,7 +42,7 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java index 18601f89..4bf4234c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubredditSelectionActivity.java @@ -39,8 +39,8 @@ import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListing import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java index cf8e5bfc..797a1b63 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java @@ -52,8 +52,8 @@ import ml.docilealligator.infinityforreddit.MultiReddit.FetchMyMultiReddits; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; 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 000e677d..50c85c54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -73,8 +73,8 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivit import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Comment.FetchComment; -import ml.docilealligator.infinityforreddit.FetchRemovedComment; -import ml.docilealligator.infinityforreddit.FetchRemovedPost; +import ml.docilealligator.infinityforreddit.Comment.FetchRemovedComment; +import ml.docilealligator.infinityforreddit.Post.FetchRemovedPost; import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Comment.ParseComment; 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 aa42d49a..638ca37e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -66,7 +66,7 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.GoBackToMainPageEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; -import ml.docilealligator.infinityforreddit.FetchSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.Fragment.SidebarFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; @@ -77,9 +77,9 @@ import ml.docilealligator.infinityforreddit.Message.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; -import ml.docilealligator.infinityforreddit.SubredditSubscription; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditViewModel; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditSubscription; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import pl.droidsonroids.gif.GifImageView; 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 8134ebcf..30891f38 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -60,7 +60,7 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.DeleteThing; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; -import ml.docilealligator.infinityforreddit.FetchUserData; +import ml.docilealligator.infinityforreddit.User.FetchUserData; import ml.docilealligator.infinityforreddit.Fragment.CommentsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment; @@ -78,7 +78,7 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse import ml.docilealligator.infinityforreddit.User.UserDao; import ml.docilealligator.infinityforreddit.User.UserData; import ml.docilealligator.infinityforreddit.User.UserViewModel; -import ml.docilealligator.infinityforreddit.UserFollowing; +import ml.docilealligator.infinityforreddit.User.UserFollowing; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import pl.droidsonroids.gif.GifImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java index 7d038fa0..4e2f5b7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java @@ -25,7 +25,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Account.Account; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import pl.droidsonroids.gif.GifImageView; public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditListingRecyclerViewAdapter.java index 52caf4e1..964ecd5d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditListingRecyclerViewAdapter.java @@ -30,8 +30,8 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubredditSubscription; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditSubscription; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java index 25b57611..6c2d0b87 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubredditMultiselectionRecyclerViewAdapter.java @@ -23,8 +23,8 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.SubredditWithSelection; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditWithSelection; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import pl.droidsonroids.gif.GifImageView; public class SubredditMultiselectionRecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java index d5cb996a..3e557594 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java @@ -27,7 +27,7 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.FavoriteThing; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/UserListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/UserListingRecyclerViewAdapter.java index 33867a0d..fad8c30a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/UserListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/UserListingRecyclerViewAdapter.java @@ -33,7 +33,7 @@ import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; import ml.docilealligator.infinityforreddit.User.UserData; -import ml.docilealligator.infinityforreddit.UserFollowing; +import ml.docilealligator.infinityforreddit.User.UserFollowing; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/CheckIsSubscribedToSubredditAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/CheckIsSubscribedToSubredditAsyncTask.java index 2965f6de..a9388bf5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/CheckIsSubscribedToSubredditAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/CheckIsSubscribedToSubredditAsyncTask.java @@ -3,7 +3,7 @@ package ml.docilealligator.infinityforreddit.AsyncTask; import android.os.AsyncTask; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; public class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java index 8f7bb88e..7b3e40f9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java @@ -3,8 +3,8 @@ package ml.docilealligator.infinityforreddit.AsyncTask; import android.os.AsyncTask; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditDao; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; public class InsertSubredditDataAsyncTask extends AsyncTask { private SubredditDao mSubredditDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java index 0d46da69..0a73135e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java @@ -9,10 +9,10 @@ import java.util.Collections; import java.util.List; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditDao; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditDao; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditDao; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadSubredditIconAsyncTask.java index 0c2d289a..a89f1064 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadSubredditIconAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadSubredditIconAsyncTask.java @@ -4,10 +4,10 @@ import android.os.AsyncTask; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.FetchSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditDao; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; import retrofit2.Retrofit; public class LoadSubredditIconAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadUserDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadUserDataAsyncTask.java index 6bfb922b..30c9e039 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadUserDataAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/LoadUserDataAsyncTask.java @@ -2,7 +2,7 @@ package ml.docilealligator.infinityforreddit.AsyncTask; import android.os.AsyncTask; -import ml.docilealligator.infinityforreddit.FetchUserData; +import ml.docilealligator.infinityforreddit.User.FetchUserData; import ml.docilealligator.infinityforreddit.User.UserDao; import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/FetchRemovedComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/FetchRemovedComment.java new file mode 100644 index 00000000..ad774da9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/FetchRemovedComment.java @@ -0,0 +1,98 @@ +package ml.docilealligator.infinityforreddit.Comment; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Comment.Comment; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedComment { + + public static void fetchRemovedComment(Retrofit retrofit, Comment comment, FetchRemovedCommentListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId()) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParseCommentAsyncTask(response.body(), comment, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + t.printStackTrace(); + listener.fetchFailed(); + } + }); + } + + private static Comment parseRemovedComment(JSONObject result, Comment comment) throws JSONException { + String id = result.getString(JSONUtils.ID_KEY); + String author = result.getString(JSONUtils.AUTHOR_KEY); + String body = Utils.modifyMarkdown(result.optString(JSONUtils.BODY_KEY).trim()); + + if ( id.equals(comment.getId()) && + (!author.equals(comment.getAuthor()) || + !body.equals(comment.getCommentRawText())) + ) { + comment.setAuthor(author); + comment.setCommentMarkdown(body); + comment.setCommentRawText(body); + return comment; + } else { + return null; + } + } + + public interface FetchRemovedCommentListener { + void fetchSuccess(Comment comment); + + void fetchFailed(); + } + + private static class ParseCommentAsyncTask extends AsyncTask { + + private String responseBody; + private FetchRemovedCommentListener listener; + Comment comment; + + public ParseCommentAsyncTask(String responseBody, Comment comment, FetchRemovedCommentListener listener) { + this.responseBody = responseBody; + this.comment = comment; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject commentJSON = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + comment = parseRemovedComment(commentJSON, comment); + } catch (JSONException e) { + e.printStackTrace(); + comment = null; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (comment != null) + listener.fetchSuccess(comment); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java index c0d429f7..0333a6df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java @@ -7,7 +7,7 @@ import java.util.Map; import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java index 3467599e..1bf1b8a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java @@ -1,17 +1,24 @@ package ml.docilealligator.infinityforreddit; +import android.os.AsyncTask; +import android.text.Html; + import androidx.annotation.NonNull; +import org.json.JSONException; +import org.json.JSONObject; + import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; public class FetchMyInfo { - public static void fetchAccountInfo(final Retrofit retrofit, String accessToken, - final FetchUserMyListener fetchUserMyListener) { + public static void fetchAccountInfo(final Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, final FetchMyInfoListener fetchMyInfoListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call userInfo = api.getMyInfo(APIUtils.getOAuthHeader(accessToken)); @@ -19,22 +26,74 @@ public class FetchMyInfo { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - fetchUserMyListener.onFetchMyInfoSuccess(response.body()); + new ParseAndSaveAccountInfoAsyncTask(response.body(), redditDataRoomDatabase, fetchMyInfoListener).execute(); } else { - fetchUserMyListener.onFetchMyInfoFail(); + fetchMyInfoListener.onFetchMyInfoFailed(false); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchUserMyListener.onFetchMyInfoFail(); + fetchMyInfoListener.onFetchMyInfoFailed(false); } }); } - public interface FetchUserMyListener { - void onFetchMyInfoSuccess(String response); + public interface FetchMyInfoListener { + void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma); + + void onFetchMyInfoFailed(boolean parseFailed); + } - void onFetchMyInfoFail(); + private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private RedditDataRoomDatabase redditDataRoomDatabase; + private FetchMyInfoListener fetchMyInfoListener; + private boolean parseFailed; + + private String name; + private String profileImageUrl; + private String bannerImageUrl; + private int karma; + + ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase, + FetchMyInfoListener fetchMyInfoListener) { + try { + jsonResponse = new JSONObject(response); + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.fetchMyInfoListener = fetchMyInfoListener; + parseFailed = false; + } catch (JSONException e) { + fetchMyInfoListener.onFetchMyInfoFailed(true); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + name = jsonResponse.getString(JSONUtils.NAME_KEY); + profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); + if (!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) { + bannerImageUrl = Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString(); + } + int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); + int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); + karma = linkKarma + commentKarma; + + redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma); + } catch (JSONException e) { + parseFailed = true; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if (!parseFailed) { + fetchMyInfoListener.onFetchMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); + } else { + fetchMyInfoListener.onFetchMyInfoFailed(true); + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java deleted file mode 100644 index 658ddc88..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java +++ /dev/null @@ -1,98 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import ml.docilealligator.infinityforreddit.API.PushshiftAPI; -import ml.docilealligator.infinityforreddit.Comment.Comment; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.Utils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class FetchRemovedComment { - - public static void fetchRemovedComment(Retrofit retrofit, Comment comment, FetchRemovedCommentListener listener) { - retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId()) - .enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - new ParseCommentAsyncTask(response.body(), comment, listener).execute(); - } else { - listener.fetchFailed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - t.printStackTrace(); - listener.fetchFailed(); - } - }); - } - - private static Comment parseRemovedComment(JSONObject result, Comment comment) throws JSONException { - String id = result.getString(JSONUtils.ID_KEY); - String author = result.getString(JSONUtils.AUTHOR_KEY); - String body = Utils.modifyMarkdown(result.optString(JSONUtils.BODY_KEY).trim()); - - if ( id.equals(comment.getId()) && - (!author.equals(comment.getAuthor()) || - !body.equals(comment.getCommentRawText())) - ) { - comment.setAuthor(author); - comment.setCommentMarkdown(body); - comment.setCommentRawText(body); - return comment; - } else { - return null; - } - } - - public interface FetchRemovedCommentListener { - void fetchSuccess(Comment comment); - - void fetchFailed(); - } - - private static class ParseCommentAsyncTask extends AsyncTask { - - private String responseBody; - private FetchRemovedCommentListener listener; - Comment comment; - - public ParseCommentAsyncTask(String responseBody, Comment comment, FetchRemovedCommentListener listener) { - this.responseBody = responseBody; - this.comment = comment; - this.listener = listener; - } - - @Override - protected Void doInBackground(Void... voids) { - try { - JSONObject commentJSON = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); - comment = parseRemovedComment(commentJSON, comment); - } catch (JSONException e) { - e.printStackTrace(); - comment = null; - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if (comment != null) - listener.fetchSuccess(comment); - else - listener.fetchFailed(); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java deleted file mode 100644 index 646dfe19..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java +++ /dev/null @@ -1,141 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.net.Uri; -import android.os.AsyncTask; -import android.text.Html; - -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import ml.docilealligator.infinityforreddit.API.PushshiftAPI; -import ml.docilealligator.infinityforreddit.Post.Post; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.Utils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class FetchRemovedPost { - - public static void fetchRemovedPost(Retrofit retrofit, Post post, FetchRemovedPostListener listener) { - retrofit.create(PushshiftAPI.class).getRemovedPost(post.getId()) - .enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - new ParsePostAsyncTask(response.body(), post, listener).execute(); - } else { - listener.fetchFailed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - t.printStackTrace(); - listener.fetchFailed(); - } - }); - } - - private static Post parseRemovedPost(JSONObject result, Post post) throws JSONException { - String id = result.getString(JSONUtils.ID_KEY); - String author = result.getString(JSONUtils.AUTHOR_KEY); - String title = result.getString(JSONUtils.TITLE_KEY); - String body = Utils.modifyMarkdown(result.getString(JSONUtils.SELFTEXT_KEY).trim()); - - if ( id.equals(post.getId()) && - (!author.equals(post.getAuthor()) || - foundSelfText(post.getSelfText(), body)) - ) { - post.setAuthor(author); - post.setTitle(title); - post.setSelfText(body); - post.setSelfTextPlain(""); - post.setSelfTextPlainTrimmed(""); - - String url = result.optString(JSONUtils.URL_KEY); - - if (url.endsWith("gif") || url.endsWith("mp4")) { - post.setVideoUrl(url); - post.setVideoDownloadUrl(url); - } else if (post.getPostType() == Post.VIDEO_TYPE || post.getPostType() == Post.GIF_TYPE) { - JSONObject redditVideoObject = result.getJSONObject("secure_media").getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); - String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); - String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); - - post.setVideoUrl(videoUrl); - post.setVideoDownloadUrl(videoDownloadUrl); - } else if (post.getPostType() == Post.LINK_TYPE) { - post.setUrl(url); - } - - if (post.getPostType() == Post.VIDEO_TYPE) { - try { - Uri uri = Uri.parse(url); - String authority = uri.getAuthority(); - if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { - post.setPostType(Post.LINK_TYPE); - post.setUrl(url); - } - } catch (IllegalArgumentException ignore) { - } - } - - if (!result.isNull("thumbnail")) { - post.setThumbnailPreviewUrl(result.getString("thumbnail")); - } - - return post; - } else { - return null; - } - } - - private static boolean foundSelfText(String oldText, String newText) { - return !(newText.equals("[deleted]") || newText.equals("[removed]")) && - !newText.equals(oldText); - } - - public interface FetchRemovedPostListener { - void fetchSuccess(Post post); - - void fetchFailed(); - } - - private static class ParsePostAsyncTask extends AsyncTask { - - private String responseBody; - private FetchRemovedPostListener listener; - Post post; - - public ParsePostAsyncTask(String responseBody, Post post, FetchRemovedPostListener listener) { - this.responseBody = responseBody; - this.post = post; - this.listener = listener; - } - - @Override - protected Void doInBackground(Void... voids) { - try { - JSONObject postJson = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); - post = parseRemovedPost(postJson, post); - } catch (JSONException e) { - e.printStackTrace(); - post = null; - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if (post != null) - listener.fetchSuccess(post); - else - listener.fetchFailed(); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java deleted file mode 100644 index b9623b91..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ /dev/null @@ -1,89 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class FetchSubredditData { - public static void fetchSubredditData(Retrofit retrofit, String subredditName, final FetchSubredditDataListener fetchSubredditDataListener) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call subredditData = api.getSubredditData(subredditName); - subredditData.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditData(response.body(), new ParseSubredditData.ParseSubredditDataListener() { - @Override - public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { - fetchSubredditDataListener.onFetchSubredditDataSuccess(subredditData, nCurrentOnlineSubscribers); - } - - @Override - public void onParseSubredditDataFail() { - fetchSubredditDataListener.onFetchSubredditDataFail(); - } - }); - } else { - fetchSubredditDataListener.onFetchSubredditDataFail(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchSubredditDataListener.onFetchSubredditDataFail(); - } - }); - } - - static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, - final FetchSubredditListingDataListener fetchSubredditListingDataListener) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call subredditDataCall = api.searchSubreddits(query, after, sortType); - subredditDataCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(response.body(), new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - fetchSubredditListingDataListener.onFetchSubredditListingDataSuccess(subredditData, after); - } - - @Override - public void onParseSubredditListingDataFail() { - fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); - } - }); - } else { - fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); - } - }); - } - - public interface FetchSubredditDataListener { - void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); - - void onFetchSubredditDataFail(); - } - - interface FetchSubredditListingDataListener { - void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after); - - void onFetchSubredditListingDataFail(); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index 0fbfd856..7f2a4773 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -5,8 +5,8 @@ import androidx.annotation.NonNull; import java.util.ArrayList; import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java deleted file mode 100644 index b698d8d1..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ /dev/null @@ -1,88 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.User.UserData; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Retrofit; - -public class FetchUserData { - public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call userInfo = api.getUserData(userName); - userInfo.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParseUserData.parseUserData(response.body(), new ParseUserData.ParseUserDataListener() { - @Override - public void onParseUserDataSuccess(UserData userData) { - fetchUserDataListener.onFetchUserDataSuccess(userData); - } - - @Override - public void onParseUserDataFailed() { - fetchUserDataListener.onFetchUserDataFailed(); - } - }); - } else { - fetchUserDataListener.onFetchUserDataFailed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchUserDataListener.onFetchUserDataFailed(); - } - }); - } - - public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType, - FetchUserListingDataListener fetchUserListingDataListener) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call userInfo = api.searchUsers(query, after, sortType); - userInfo.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - ParseUserData.parseUserListingData(response.body(), new ParseUserData.ParseUserListingDataListener() { - @Override - public void onParseUserListingDataSuccess(ArrayList userData, String after) { - fetchUserListingDataListener.onFetchUserListingDataSuccess(userData, after); - } - - @Override - public void onParseUserListingDataFailed() { - fetchUserListingDataListener.onFetchUserListingDataFailed(); - } - }); - } else { - fetchUserListingDataListener.onFetchUserListingDataFailed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchUserListingDataListener.onFetchUserListingDataFailed(); - } - }); - } - - public interface FetchUserDataListener { - void onFetchUserDataSuccess(UserData userData); - - void onFetchUserDataFailed(); - } - - public interface FetchUserListingDataListener { - void onFetchUserListingDataSuccess(ArrayList userData, String after); - - void onFetchUserListingDataFailed(); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SidebarFragment.java index 6e070c5e..c7431b20 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SidebarFragment.java @@ -43,12 +43,12 @@ import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubredditDataAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.FetchSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditViewModel; import retrofit2.Retrofit; public class SidebarFragment extends Fragment { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java index 80cb6541..3fefdb2d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java @@ -41,9 +41,8 @@ import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SubredditListingViewModel; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditListingViewModel; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; -import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java index 728b80ce..63d6aade 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java @@ -39,7 +39,7 @@ import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java index 532346c5..62019b81 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java @@ -38,9 +38,8 @@ import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.UserListingViewModel; +import ml.docilealligator.infinityforreddit.User.UserListingViewModel; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; -import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java deleted file mode 100644 index 387b75f4..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndSaveAccountInfo.java +++ /dev/null @@ -1,74 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; -import android.text.Html; - -import org.json.JSONException; -import org.json.JSONObject; - -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; - -public class ParseAndSaveAccountInfo { - public static void parseAndSaveAccountInfo(String response, RedditDataRoomDatabase redditDataRoomDatabase, - ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener) { - new ParseAndSaveAccountInfoAsyncTask(response, redditDataRoomDatabase, parseAndSaveAccountInfoListener).execute(); - } - - public interface ParseAndSaveAccountInfoListener { - void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma); - - void onParseMyInfoFail(); - } - - private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private RedditDataRoomDatabase redditDataRoomDatabase; - private ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener; - private boolean parseFailed; - - private String name; - private String profileImageUrl; - private String bannerImageUrl; - private int karma; - - ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase, - ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener) { - try { - jsonResponse = new JSONObject(response); - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.parseAndSaveAccountInfoListener = parseAndSaveAccountInfoListener; - parseFailed = false; - } catch (JSONException e) { - parseAndSaveAccountInfoListener.onParseMyInfoFail(); - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - name = jsonResponse.getString(JSONUtils.NAME_KEY); - profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); - if (!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) { - bannerImageUrl = Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString(); - } - int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); - int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); - karma = linkKarma + commentKarma; - - redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma); - } catch (JSONException e) { - parseFailed = true; - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - parseAndSaveAccountInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); - } else { - parseAndSaveAccountInfoListener.onParseMyInfoFail(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java deleted file mode 100644 index 23bd9910..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ /dev/null @@ -1,162 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.Utils; - -class ParseSubredditData { - static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { - new ParseSubredditDataAsyncTask(response, parseSubredditDataListener).execute(); - } - - static void parseSubredditListingData(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { - new ParseSubredditListingDataAsyncTask(response, parseSubredditListingDataListener).execute(); - } - - private static SubredditData parseSubredditData(JSONObject subredditDataJsonObject) throws JSONException { - String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY); - String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME_KEY); - String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); - String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim()); - long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; - - String bannerImageUrl; - if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { - bannerImageUrl = ""; - } else { - bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY); - } - if (bannerImageUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.BANNER_IMG_KEY)) { - bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_IMG_KEY); - } - - String iconUrl; - if (subredditDataJsonObject.isNull(JSONUtils.COMMUNITY_ICON_KEY)) { - iconUrl = ""; - } else { - iconUrl = subredditDataJsonObject.getString(JSONUtils.COMMUNITY_ICON_KEY); - } - if (iconUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.ICON_IMG_KEY)) { - iconUrl = subredditDataJsonObject.getString(JSONUtils.ICON_IMG_KEY); - } - - int nSubscribers = 0; - if (!subredditDataJsonObject.isNull(JSONUtils.SUBSCRIBERS_KEY)) { - nSubscribers = subredditDataJsonObject.getInt(JSONUtils.SUBSCRIBERS_KEY); - } - - return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, - sidebarDescription, nSubscribers, createdUTC); - } - - interface ParseSubredditDataListener { - void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); - - void onParseSubredditDataFail(); - } - - interface ParseSubredditListingDataListener { - void onParseSubredditListingDataSuccess(ArrayList subredditData, String after); - - void onParseSubredditListingDataFail(); - } - - private static class ParseSubredditDataAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private boolean parseFailed; - private ParseSubredditDataListener parseSubredditDataListener; - private SubredditData subredditData; - private int mNCurrentOnlineSubscribers; - - ParseSubredditDataAsyncTask(String response, ParseSubredditDataListener parseSubredditDataListener) { - this.parseSubredditDataListener = parseSubredditDataListener; - try { - jsonResponse = new JSONObject(response); - parseFailed = false; - } catch (JSONException e) { - e.printStackTrace(); - parseSubredditDataListener.onParseSubredditDataFail(); - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); - mNCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); - subredditData = parseSubredditData(data); - } catch (JSONException e) { - parseFailed = true; - e.printStackTrace(); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - parseSubredditDataListener.onParseSubredditDataSuccess(subredditData, mNCurrentOnlineSubscribers); - } else { - parseSubredditDataListener.onParseSubredditDataFail(); - } - } - } - - private static class ParseSubredditListingDataAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private boolean parseFailed; - private ParseSubredditListingDataListener parseSubredditListingDataListener; - private ArrayList subredditListingData; - private String after; - - ParseSubredditListingDataAsyncTask(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { - this.parseSubredditListingDataListener = parseSubredditListingDataListener; - try { - jsonResponse = new JSONObject(response); - parseFailed = false; - subredditListingData = new ArrayList<>(); - } catch (JSONException e) { - e.printStackTrace(); - parseFailed = true; - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - if (!parseFailed) { - JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY) - .getJSONArray(JSONUtils.CHILDREN_KEY); - for (int i = 0; i < children.length(); i++) { - JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - SubredditData subredditData = parseSubredditData(data); - subredditListingData.add(subredditData); - } - after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); - } - } catch (JSONException e) { - e.printStackTrace(); - parseFailed = true; - parseSubredditListingDataListener.onParseSubredditListingDataFail(); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - parseSubredditListingDataListener.onParseSubredditListingDataSuccess(subredditListingData, after); - } else { - parseSubredditListingDataListener.onParseSubredditListingDataFail(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 97da7db6..c8666fa7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -8,8 +8,8 @@ import org.json.JSONObject; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java deleted file mode 100644 index f3766d05..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java +++ /dev/null @@ -1,153 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.User.UserData; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; - -public class ParseUserData { - static void parseUserData(String response, ParseUserDataListener parseUserDataListener) { - new ParseUserDataAsyncTask(response, parseUserDataListener).execute(); - } - - static void parseUserListingData(String response, ParseUserListingDataListener parseUserListingDataListener) { - new ParseUserListingDataAsyncTask(response, parseUserListingDataListener).execute(); - } - - private static UserData parseUserDataBase(JSONObject userDataJson) throws JSONException { - if(userDataJson == null) { - return null; - } - - userDataJson = userDataJson.getJSONObject(JSONUtils.DATA_KEY); - String userName = userDataJson.getString(JSONUtils.NAME_KEY); - String iconImageUrl = userDataJson.getString(JSONUtils.ICON_IMG_KEY); - String bannerImageUrl = ""; - boolean canBeFollowed; - if (userDataJson.has(JSONUtils.SUBREDDIT_KEY) && !userDataJson.isNull(JSONUtils.SUBREDDIT_KEY)) { - bannerImageUrl = userDataJson.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY); - canBeFollowed = true; - } else { - canBeFollowed = false; - } - int linkKarma = userDataJson.getInt(JSONUtils.LINK_KARMA_KEY); - int commentKarma = userDataJson.getInt(JSONUtils.COMMENT_KARMA_KEY); - long cakeday = userDataJson.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; - boolean isGold = userDataJson.getBoolean(JSONUtils.IS_GOLD_KEY); - boolean isFriend = userDataJson.getBoolean(JSONUtils.IS_FRIEND_KEY); - String description = userDataJson.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.PUBLIC_DESCRIPTION_KEY); - - return new UserData(userName, iconImageUrl, bannerImageUrl, linkKarma, commentKarma, cakeday, - isGold, isFriend, canBeFollowed, description); - } - - interface ParseUserDataListener { - void onParseUserDataSuccess(UserData userData); - - void onParseUserDataFailed(); - } - - interface ParseUserListingDataListener { - void onParseUserListingDataSuccess(ArrayList userData, String after); - - void onParseUserListingDataFailed(); - } - - private static class ParseUserDataAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private ParseUserDataListener parseUserDataListener; - private boolean parseFailed; - - private UserData userData; - - ParseUserDataAsyncTask(String response, ParseUserDataListener parseUserDataListener) { - try { - jsonResponse = new JSONObject(response); - this.parseUserDataListener = parseUserDataListener; - parseFailed = false; - } catch (JSONException e) { - e.printStackTrace(); - parseUserDataListener.onParseUserDataFailed(); - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - userData = parseUserDataBase(jsonResponse); - } catch (JSONException e) { - parseFailed = true; - e.printStackTrace(); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - parseUserDataListener.onParseUserDataSuccess(userData); - } else { - parseUserDataListener.onParseUserDataFailed(); - } - } - } - - private static class ParseUserListingDataAsyncTask extends AsyncTask { - private String response; - private JSONObject jsonResponse; - private ParseUserListingDataListener parseUserListingDataListener; - private String after; - private boolean parseFailed; - - private ArrayList userDataArrayList; - - ParseUserListingDataAsyncTask(String response, ParseUserListingDataListener parseUserListingDataListener) { - this.parseUserListingDataListener = parseUserListingDataListener; - this.response = response; - try { - jsonResponse = new JSONObject(response); - parseFailed = false; - userDataArrayList = new ArrayList<>(); - } catch (JSONException e) { - e.printStackTrace(); - parseFailed = true; - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - if (!parseFailed) { - after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); - JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - for (int i = 0; i < children.length(); i++) { - userDataArrayList.add(parseUserDataBase(children.getJSONObject(i))); - } - } - } catch (JSONException e) { - parseFailed = true; - e.printStackTrace(); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - parseUserListingDataListener.onParseUserListingDataSuccess(userDataArrayList, after); - } else { - if (response.equals("\"{}\"")) { - parseUserListingDataListener.onParseUserListingDataSuccess(new ArrayList<>(), null); - } else { - parseUserListingDataListener.onParseUserListingDataFailed(); - } - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java new file mode 100644 index 00000000..f24e995b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchRemovedPost.java @@ -0,0 +1,141 @@ +package ml.docilealligator.infinityforreddit.Post; + +import android.net.Uri; +import android.os.AsyncTask; +import android.text.Html; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedPost { + + public static void fetchRemovedPost(Retrofit retrofit, Post post, FetchRemovedPostListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedPost(post.getId()) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParsePostAsyncTask(response.body(), post, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + t.printStackTrace(); + listener.fetchFailed(); + } + }); + } + + private static Post parseRemovedPost(JSONObject result, Post post) throws JSONException { + String id = result.getString(JSONUtils.ID_KEY); + String author = result.getString(JSONUtils.AUTHOR_KEY); + String title = result.getString(JSONUtils.TITLE_KEY); + String body = Utils.modifyMarkdown(result.getString(JSONUtils.SELFTEXT_KEY).trim()); + + if ( id.equals(post.getId()) && + (!author.equals(post.getAuthor()) || + foundSelfText(post.getSelfText(), body)) + ) { + post.setAuthor(author); + post.setTitle(title); + post.setSelfText(body); + post.setSelfTextPlain(""); + post.setSelfTextPlainTrimmed(""); + + String url = result.optString(JSONUtils.URL_KEY); + + if (url.endsWith("gif") || url.endsWith("mp4")) { + post.setVideoUrl(url); + post.setVideoDownloadUrl(url); + } else if (post.getPostType() == Post.VIDEO_TYPE || post.getPostType() == Post.GIF_TYPE) { + JSONObject redditVideoObject = result.getJSONObject("secure_media").getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); + String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); + String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); + + post.setVideoUrl(videoUrl); + post.setVideoDownloadUrl(videoDownloadUrl); + } else if (post.getPostType() == Post.LINK_TYPE) { + post.setUrl(url); + } + + if (post.getPostType() == Post.VIDEO_TYPE) { + try { + Uri uri = Uri.parse(url); + String authority = uri.getAuthority(); + if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { + post.setPostType(Post.LINK_TYPE); + post.setUrl(url); + } + } catch (IllegalArgumentException ignore) { + } + } + + if (!result.isNull("thumbnail")) { + post.setThumbnailPreviewUrl(result.getString("thumbnail")); + } + + return post; + } else { + return null; + } + } + + private static boolean foundSelfText(String oldText, String newText) { + return !(newText.equals("[deleted]") || newText.equals("[removed]")) && + !newText.equals(oldText); + } + + public interface FetchRemovedPostListener { + void fetchSuccess(Post post); + + void fetchFailed(); + } + + private static class ParsePostAsyncTask extends AsyncTask { + + private String responseBody; + private FetchRemovedPostListener listener; + Post post; + + public ParsePostAsyncTask(String responseBody, Post post, FetchRemovedPostListener listener) { + this.responseBody = responseBody; + this.post = post; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject postJson = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + post = parseRemovedPost(postJson, post); + } catch (JSONException e) { + e.printStackTrace(); + post = null; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (post != null) + listener.fetchSuccess(post); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 3bac4b1b..d2d0d079 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -16,10 +16,10 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeDao; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditDao; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditDao; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditDao; +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditDao; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.User.UserDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java new file mode 100644 index 00000000..c6bc2914 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java @@ -0,0 +1,88 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.annotation.NonNull; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchSubredditData { + public static void fetchSubredditData(Retrofit retrofit, String subredditName, final FetchSubredditDataListener fetchSubredditDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call subredditData = api.getSubredditData(subredditName); + subredditData.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditData(response.body(), new ParseSubredditData.ParseSubredditDataListener() { + @Override + public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + fetchSubredditDataListener.onFetchSubredditDataSuccess(subredditData, nCurrentOnlineSubscribers); + } + + @Override + public void onParseSubredditDataFail() { + fetchSubredditDataListener.onFetchSubredditDataFail(); + } + }); + } else { + fetchSubredditDataListener.onFetchSubredditDataFail(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchSubredditDataListener.onFetchSubredditDataFail(); + } + }); + } + + static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, + final FetchSubredditListingDataListener fetchSubredditListingDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call subredditDataCall = api.searchSubreddits(query, after, sortType); + subredditDataCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(response.body(), new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { + fetchSubredditListingDataListener.onFetchSubredditListingDataSuccess(subredditData, after); + } + + @Override + public void onParseSubredditListingDataFail() { + fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); + } + }); + } else { + fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); + } + }); + } + + public interface FetchSubredditDataListener { + void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); + + void onFetchSubredditDataFail(); + } + + interface FetchSubredditListingDataListener { + void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after); + + void onFetchSubredditListingDataFail(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java new file mode 100644 index 00000000..7c75b35d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java @@ -0,0 +1,162 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import android.os.AsyncTask; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; + +class ParseSubredditData { + static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { + new ParseSubredditDataAsyncTask(response, parseSubredditDataListener).execute(); + } + + static void parseSubredditListingData(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { + new ParseSubredditListingDataAsyncTask(response, parseSubredditListingDataListener).execute(); + } + + private static SubredditData parseSubredditData(JSONObject subredditDataJsonObject) throws JSONException { + String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY); + String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME_KEY); + String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); + String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim()); + long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; + + String bannerImageUrl; + if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { + bannerImageUrl = ""; + } else { + bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY); + } + if (bannerImageUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.BANNER_IMG_KEY)) { + bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_IMG_KEY); + } + + String iconUrl; + if (subredditDataJsonObject.isNull(JSONUtils.COMMUNITY_ICON_KEY)) { + iconUrl = ""; + } else { + iconUrl = subredditDataJsonObject.getString(JSONUtils.COMMUNITY_ICON_KEY); + } + if (iconUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.ICON_IMG_KEY)) { + iconUrl = subredditDataJsonObject.getString(JSONUtils.ICON_IMG_KEY); + } + + int nSubscribers = 0; + if (!subredditDataJsonObject.isNull(JSONUtils.SUBSCRIBERS_KEY)) { + nSubscribers = subredditDataJsonObject.getInt(JSONUtils.SUBSCRIBERS_KEY); + } + + return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, + sidebarDescription, nSubscribers, createdUTC); + } + + interface ParseSubredditDataListener { + void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); + + void onParseSubredditDataFail(); + } + + interface ParseSubredditListingDataListener { + void onParseSubredditListingDataSuccess(ArrayList subredditData, String after); + + void onParseSubredditListingDataFail(); + } + + private static class ParseSubredditDataAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private boolean parseFailed; + private ParseSubredditDataListener parseSubredditDataListener; + private SubredditData subredditData; + private int mNCurrentOnlineSubscribers; + + ParseSubredditDataAsyncTask(String response, ParseSubredditDataListener parseSubredditDataListener) { + this.parseSubredditDataListener = parseSubredditDataListener; + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + } catch (JSONException e) { + e.printStackTrace(); + parseSubredditDataListener.onParseSubredditDataFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); + mNCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); + subredditData = parseSubredditData(data); + } catch (JSONException e) { + parseFailed = true; + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if (!parseFailed) { + parseSubredditDataListener.onParseSubredditDataSuccess(subredditData, mNCurrentOnlineSubscribers); + } else { + parseSubredditDataListener.onParseSubredditDataFail(); + } + } + } + + private static class ParseSubredditListingDataAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private boolean parseFailed; + private ParseSubredditListingDataListener parseSubredditListingDataListener; + private ArrayList subredditListingData; + private String after; + + ParseSubredditListingDataAsyncTask(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { + this.parseSubredditListingDataListener = parseSubredditListingDataListener; + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + subredditListingData = new ArrayList<>(); + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + if (!parseFailed) { + JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY) + .getJSONArray(JSONUtils.CHILDREN_KEY); + for (int i = 0; i < children.length(); i++) { + JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); + SubredditData subredditData = parseSubredditData(data); + subredditListingData.add(subredditData); + } + after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + } + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + parseSubredditListingDataListener.onParseSubredditListingDataFail(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if (!parseFailed) { + parseSubredditListingDataListener.onParseSubredditListingDataSuccess(subredditListingData, after); + } else { + parseSubredditListingDataListener.onParseSubredditListingDataFail(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditDao.java new file mode 100644 index 00000000..d6b07312 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditDao.java @@ -0,0 +1,22 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +@Dao +public interface SubredditDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubredditData SubredditData); + + @Query("DELETE FROM subreddits") + void deleteAllSubreddits(); + + @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1") + LiveData getSubredditLiveDataByName(String namePrefixed); + + @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1") + SubredditData getSubredditData(String namePrefixed); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java new file mode 100644 index 00000000..1fb628bf --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java @@ -0,0 +1,73 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "subreddits") +public class SubredditData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @ColumnInfo(name = "banner") + private String bannerUrl; + @ColumnInfo(name = "description") + private String description; + @ColumnInfo(name = "sidebar_description") + private String sidebarDescription; + @ColumnInfo(name = "subscribers_count") + private int nSubscribers; + @ColumnInfo(name = "created_utc") + private long createdUTC; + + public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, + String description, String sidebarDescription, int nSubscribers, long createdUTC) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + this.bannerUrl = bannerUrl; + this.description = description; + this.sidebarDescription = sidebarDescription; + this.nSubscribers = nSubscribers; + this.createdUTC = createdUTC; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public String getBannerUrl() { + return bannerUrl; + } + + public String getDescription() { + return description; + } + + public String getSidebarDescription() { + return sidebarDescription; + } + + public int getNSubscribers() { + return nSubscribers; + } + + public long getCreatedUTC() { + return createdUTC; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java new file mode 100644 index 00000000..05a0b7d2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSource.java @@ -0,0 +1,104 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.PageKeyedDataSource; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class SubredditListingDataSource extends PageKeyedDataSource { + + private Retrofit retrofit; + private String query; + private SortType sortType; + + private MutableLiveData paginationNetworkStateLiveData; + private MutableLiveData initialLoadStateLiveData; + private MutableLiveData hasSubredditLiveData; + + private LoadParams params; + private LoadCallback callback; + + SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + paginationNetworkStateLiveData = new MutableLiveData<>(); + initialLoadStateLiveData = new MutableLiveData<>(); + hasSubredditLiveData = new MutableLiveData<>(); + } + + MutableLiveData getPaginationNetworkStateLiveData() { + return paginationNetworkStateLiveData; + } + + MutableLiveData getInitialLoadStateLiveData() { + return initialLoadStateLiveData; + } + + MutableLiveData hasSubredditLiveData() { + return hasSubredditLiveData; + } + + @Override + public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) { + initialLoadStateLiveData.postValue(NetworkState.LOADING); + + FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, + new FetchSubredditData.FetchSubredditListingDataListener() { + @Override + public void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after) { + if (subredditData.size() == 0) { + hasSubredditLiveData.postValue(false); + } else { + hasSubredditLiveData.postValue(true); + } + + callback.onResult(subredditData, null, after); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onFetchSubredditListingDataFail() { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving subreddit list")); + } + }); + } + + @Override + public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { + + } + + @Override + public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { + this.params = params; + this.callback = callback; + + if (params.key.equals("") || params.key.equals("null")) { + return; + } + + FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, + new FetchSubredditData.FetchSubredditListingDataListener() { + @Override + public void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after) { + callback.onResult(subredditData, after); + paginationNetworkStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onFetchSubredditListingDataFail() { + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving subreddit list")); + } + }); + } + + void retryLoadingMore() { + loadAfter(params, callback); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java new file mode 100644 index 00000000..f18b3691 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingDataSourceFactory.java @@ -0,0 +1,44 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.DataSource; + +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class SubredditListingDataSourceFactory extends DataSource.Factory { + private Retrofit retrofit; + private String query; + private SortType sortType; + + private SubredditListingDataSource subredditListingDataSource; + private MutableLiveData subredditListingDataSourceMutableLiveData; + + SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); + } + + @NonNull + @Override + public DataSource create() { + subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType); + subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource); + return subredditListingDataSource; + } + + public MutableLiveData getSubredditListingDataSourceMutableLiveData() { + return subredditListingDataSourceMutableLiveData; + } + + SubredditListingDataSource getSubredditListingDataSource() { + return subredditListingDataSource; + } + + void changeSortType(SortType sortType) { + this.sortType = sortType; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java new file mode 100644 index 00000000..ed8df032 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditListingViewModel.java @@ -0,0 +1,94 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +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 ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class SubredditListingViewModel extends ViewModel { + private SubredditListingDataSourceFactory subredditListingDataSourceFactory; + private LiveData paginationNetworkState; + private LiveData initialLoadingState; + private LiveData hasSubredditLiveData; + private LiveData> subreddits; + private MutableLiveData sortTypeLiveData; + + public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType) { + subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType); + + initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), + SubredditListingDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), + SubredditListingDataSource::getPaginationNetworkStateLiveData); + hasSubredditLiveData = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), + SubredditListingDataSource::hasSubredditLiveData); + + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + subreddits = Transformations.switchMap(sortTypeLiveData, sort -> { + subredditListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); + return new LivePagedListBuilder(subredditListingDataSourceFactory, pagedListConfig).build(); + }); + } + + public LiveData> getSubreddits() { + return subreddits; + } + + public LiveData getPaginationNetworkState() { + return paginationNetworkState; + } + + public LiveData getInitialLoadingState() { + return initialLoadingState; + } + + public LiveData hasSubredditLiveData() { + return hasSubredditLiveData; + } + + public void refresh() { + subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate(); + } + + public void retryLoadingMore() { + subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore(); + } + + public void changeSortType(SortType sortType) { + sortTypeLiveData.postValue(sortType); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Retrofit retrofit; + private String query; + private SortType sortType; + + public Factory(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + return (T) new SubredditListingViewModel(retrofit, query, sortType); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditRepository.java new file mode 100644 index 00000000..66817e40 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditRepository.java @@ -0,0 +1,40 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubredditRepository { + private SubredditDao mSubredditDao; + private LiveData mSubredditLiveData; + + SubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName) { + mSubredditDao = redditDataRoomDatabase.subredditDao(); + mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName); + } + + LiveData getSubredditLiveData() { + return mSubredditLiveData; + } + + public void insert(SubredditData subredditData) { + new InsertAsyncTask(mSubredditDao).execute(subredditData); + } + + private static class InsertAsyncTask extends AsyncTask { + + private SubredditDao mAsyncTaskDao; + + InsertAsyncTask(SubredditDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubredditData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditSubscription.java new file mode 100644 index 00000000..5475e319 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditSubscription.java @@ -0,0 +1,120 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; + +public class SubredditSubscription { + public static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit, + String accessToken, String subredditName, String accountName, + RedditDataRoomDatabase redditDataRoomDatabase, + SubredditSubscriptionListener subredditSubscriptionListener) { + subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub", + redditDataRoomDatabase, subredditSubscriptionListener); + } + + public static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken, + String subredditName, String accountName, + RedditDataRoomDatabase redditDataRoomDatabase, + SubredditSubscriptionListener subredditSubscriptionListener) { + subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub", + redditDataRoomDatabase, subredditSubscriptionListener); + } + + private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, + String subredditName, String accountName, String action, + RedditDataRoomDatabase redditDataRoomDatabase, + SubredditSubscriptionListener subredditSubscriptionListener) { + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + + Map params = new HashMap<>(); + params.put(APIUtils.ACTION_KEY, action); + params.put(APIUtils.SR_NAME_KEY, subredditName); + + Call subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); + subredditSubscriptionCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + if (action.equals("sub")) { + FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, + subredditData, accountName, true).execute(); + } + + @Override + public void onFetchSubredditDataFail() { + + } + }); + } else { + new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute(); + } + subredditSubscriptionListener.onSubredditSubscriptionSuccess(); + } else { + subredditSubscriptionListener.onSubredditSubscriptionFail(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + subredditSubscriptionListener.onSubredditSubscriptionFail(); + } + }); + } + + public interface SubredditSubscriptionListener { + void onSubredditSubscriptionSuccess(); + + void onSubredditSubscriptionFail(); + } + + private static class UpdateSubscriptionAsyncTask extends AsyncTask { + + private RedditDataRoomDatabase redditDataRoomDatabase; + private String subredditName; + private String accountName; + private SubscribedSubredditData subscribedSubredditData; + private boolean isSubscribing; + + UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, + String accountName, boolean isSubscribing) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.subredditName = subredditName; + this.accountName = accountName; + this.isSubscribing = isSubscribing; + } + + UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditData subredditData, + String accountName, boolean isSubscribing) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), + subredditData.getIconUrl(), accountName, false); + this.accountName = accountName; + this.isSubscribing = isSubscribing; + } + + @Override + protected Void doInBackground(Void... voids) { + if (isSubscribing) { + redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); + } else { + redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); + } + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditViewModel.java new file mode 100644 index 00000000..46829c3b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditViewModel.java @@ -0,0 +1,51 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubredditViewModel extends AndroidViewModel { + private SubredditRepository mSubredditRepository; + private LiveData mSubredditLiveData; + + public SubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { + super(application); + mSubredditRepository = new SubredditRepository(redditDataRoomDatabase, id); + mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); + } + + public LiveData getSubredditLiveData() { + return mSubredditLiveData; + } + + public void insert(SubredditData subredditData) { + mSubredditRepository.insert(subredditData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mSubredditName; + + public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String subredditname) { + mApplication = application; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mSubredditName = subredditname; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + //noinspection unchecked + return (T) new SubredditViewModel(mApplication, mRedditDataRoomDatabase, mSubredditName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditWithSelection.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditWithSelection.java new file mode 100644 index 00000000..a8cd289f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditWithSelection.java @@ -0,0 +1,101 @@ +package ml.docilealligator.infinityforreddit.Subreddit; + +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.Nullable; + +import java.util.ArrayList; +import java.util.List; + +import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditData; + +public class SubredditWithSelection implements Parcelable { + private String name; + private String iconUrl; + private boolean selected; + + public SubredditWithSelection(String name, String iconUrl) { + this.name = name; + this.iconUrl = iconUrl; + selected = false; + } + + protected SubredditWithSelection(Parcel in) { + super(); + name = in.readString(); + iconUrl = in.readString(); + selected = in.readByte() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public SubredditWithSelection createFromParcel(Parcel in) { + return new SubredditWithSelection(in); + } + + @Override + public SubredditWithSelection[] newArray(int size) { + return new SubredditWithSelection[size]; + } + }; + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public boolean isSelected() { + return selected; + } + + public void setSelected(boolean selected) { + this.selected = selected; + } + + public static ArrayList convertSubscribedSubreddits( + List subscribedSubredditData) { + ArrayList subredditWithSelections = new ArrayList<>(); + for (SubscribedSubredditData s : subscribedSubredditData) { + subredditWithSelections.add(new SubredditWithSelection(s.getName(), s.getIconUrl())); + } + + return subredditWithSelections; + } + + public static SubredditWithSelection convertSubreddit(SubredditData subreddit) { + return new SubredditWithSelection(subreddit.getName(), subreddit.getIconUrl()); + } + + public int compareName(SubredditWithSelection subredditWithSelection) { + if (subredditWithSelection != null) { + return name.compareToIgnoreCase(subredditWithSelection.getName()); + } else { + return -1; + } + } + + @Override + public boolean equals(@Nullable Object obj) { + if (!(obj instanceof SubredditWithSelection)) { + return false; + } else { + return this.getName().compareToIgnoreCase(((SubredditWithSelection) obj).getName()) == 0; + } + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); + parcel.writeString(iconUrl); + parcel.writeByte((byte) (selected ? 1 : 0)); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java deleted file mode 100644 index bb058295..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java +++ /dev/null @@ -1,22 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubredditDatabase; - -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -@Dao -public interface SubredditDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(SubredditData SubredditData); - - @Query("DELETE FROM subreddits") - void deleteAllSubreddits(); - - @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1") - LiveData getSubredditLiveDataByName(String namePrefixed); - - @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1") - SubredditData getSubredditData(String namePrefixed); -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java deleted file mode 100644 index 6c73f459..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java +++ /dev/null @@ -1,73 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubredditDatabase; - -import androidx.annotation.NonNull; -import androidx.room.ColumnInfo; -import androidx.room.Entity; -import androidx.room.PrimaryKey; - -@Entity(tableName = "subreddits") -public class SubredditData { - @PrimaryKey - @NonNull - @ColumnInfo(name = "id") - private String id; - @ColumnInfo(name = "name") - private String name; - @ColumnInfo(name = "icon") - private String iconUrl; - @ColumnInfo(name = "banner") - private String bannerUrl; - @ColumnInfo(name = "description") - private String description; - @ColumnInfo(name = "sidebar_description") - private String sidebarDescription; - @ColumnInfo(name = "subscribers_count") - private int nSubscribers; - @ColumnInfo(name = "created_utc") - private long createdUTC; - - public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, - String description, String sidebarDescription, int nSubscribers, long createdUTC) { - this.id = id; - this.name = name; - this.iconUrl = iconUrl; - this.bannerUrl = bannerUrl; - this.description = description; - this.sidebarDescription = sidebarDescription; - this.nSubscribers = nSubscribers; - this.createdUTC = createdUTC; - } - - @NonNull - public String getId() { - return id; - } - - public String getName() { - return name; - } - - public String getIconUrl() { - return iconUrl; - } - - public String getBannerUrl() { - return bannerUrl; - } - - public String getDescription() { - return description; - } - - public String getSidebarDescription() { - return sidebarDescription; - } - - public int getNSubscribers() { - return nSubscribers; - } - - public long getCreatedUTC() { - return createdUTC; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java deleted file mode 100644 index 2978fdca..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java +++ /dev/null @@ -1,40 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubredditDatabase; - -import android.os.AsyncTask; - -import androidx.lifecycle.LiveData; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class SubredditRepository { - private SubredditDao mSubredditDao; - private LiveData mSubredditLiveData; - - SubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName) { - mSubredditDao = redditDataRoomDatabase.subredditDao(); - mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName); - } - - LiveData getSubredditLiveData() { - return mSubredditLiveData; - } - - public void insert(SubredditData subredditData) { - new InsertAsyncTask(mSubredditDao).execute(subredditData); - } - - private static class InsertAsyncTask extends AsyncTask { - - private SubredditDao mAsyncTaskDao; - - InsertAsyncTask(SubredditDao dao) { - mAsyncTaskDao = dao; - } - - @Override - protected Void doInBackground(final SubredditData... params) { - mAsyncTaskDao.insert(params[0]); - return null; - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java deleted file mode 100644 index ee152d09..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java +++ /dev/null @@ -1,51 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubredditDatabase; - -import android.app.Application; - -import androidx.annotation.NonNull; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelProvider; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class SubredditViewModel extends AndroidViewModel { - private SubredditRepository mSubredditRepository; - private LiveData mSubredditLiveData; - - public SubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { - super(application); - mSubredditRepository = new SubredditRepository(redditDataRoomDatabase, id); - mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); - } - - public LiveData getSubredditLiveData() { - return mSubredditLiveData; - } - - public void insert(SubredditData subredditData) { - mSubredditRepository.insert(subredditData); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - - @NonNull - private final Application mApplication; - private final RedditDataRoomDatabase mRedditDataRoomDatabase; - private final String mSubredditName; - - public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String subredditname) { - mApplication = application; - mRedditDataRoomDatabase = redditDataRoomDatabase; - mSubredditName = subredditname; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - //noinspection unchecked - return (T) new SubredditViewModel(mApplication, mRedditDataRoomDatabase, mSubredditName); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java deleted file mode 100644 index 32bde86d..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java +++ /dev/null @@ -1,103 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.PageKeyedDataSource; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import retrofit2.Retrofit; - -public class SubredditListingDataSource extends PageKeyedDataSource { - - private Retrofit retrofit; - private String query; - private SortType sortType; - - private MutableLiveData paginationNetworkStateLiveData; - private MutableLiveData initialLoadStateLiveData; - private MutableLiveData hasSubredditLiveData; - - private LoadParams params; - private LoadCallback callback; - - SubredditListingDataSource(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasSubredditLiveData = new MutableLiveData<>(); - } - - MutableLiveData getPaginationNetworkStateLiveData() { - return paginationNetworkStateLiveData; - } - - MutableLiveData getInitialLoadStateLiveData() { - return initialLoadStateLiveData; - } - - MutableLiveData hasSubredditLiveData() { - return hasSubredditLiveData; - } - - @Override - public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) { - initialLoadStateLiveData.postValue(NetworkState.LOADING); - - FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType.getType().value, - new FetchSubredditData.FetchSubredditListingDataListener() { - @Override - public void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after) { - if (subredditData.size() == 0) { - hasSubredditLiveData.postValue(false); - } else { - hasSubredditLiveData.postValue(true); - } - - callback.onResult(subredditData, null, after); - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onFetchSubredditListingDataFail() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving subreddit list")); - } - }); - } - - @Override - public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { - - } - - @Override - public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { - this.params = params; - this.callback = callback; - - if (params.key.equals("") || params.key.equals("null")) { - return; - } - - FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType.getType().value, - new FetchSubredditData.FetchSubredditListingDataListener() { - @Override - public void onFetchSubredditListingDataSuccess(ArrayList subredditData, String after) { - callback.onResult(subredditData, after); - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onFetchSubredditListingDataFail() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving subreddit list")); - } - }); - } - - void retryLoadingMore() { - loadAfter(params, callback); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java deleted file mode 100644 index fa683f47..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.DataSource; - -import retrofit2.Retrofit; - -public class SubredditListingDataSourceFactory extends DataSource.Factory { - private Retrofit retrofit; - private String query; - private SortType sortType; - - private SubredditListingDataSource subredditListingDataSource; - private MutableLiveData subredditListingDataSourceMutableLiveData; - - SubredditListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); - } - - @NonNull - @Override - public DataSource create() { - subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType); - subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource); - return subredditListingDataSource; - } - - public MutableLiveData getSubredditListingDataSourceMutableLiveData() { - return subredditListingDataSourceMutableLiveData; - } - - SubredditListingDataSource getSubredditListingDataSource() { - return subredditListingDataSource; - } - - void changeSortType(SortType sortType) { - this.sortType = sortType; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java deleted file mode 100644 index b2023541..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java +++ /dev/null @@ -1,93 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -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 ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import retrofit2.Retrofit; - -public class SubredditListingViewModel extends ViewModel { - private SubredditListingDataSourceFactory subredditListingDataSourceFactory; - private LiveData paginationNetworkState; - private LiveData initialLoadingState; - private LiveData hasSubredditLiveData; - private LiveData> subreddits; - private MutableLiveData sortTypeLiveData; - - public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType) { - subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType); - - initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), - SubredditListingDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), - SubredditListingDataSource::getPaginationNetworkStateLiveData); - hasSubredditLiveData = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), - SubredditListingDataSource::hasSubredditLiveData); - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - subreddits = Transformations.switchMap(sortTypeLiveData, sort -> { - subredditListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); - return new LivePagedListBuilder(subredditListingDataSourceFactory, pagedListConfig).build(); - }); - } - - public LiveData> getSubreddits() { - return subreddits; - } - - public LiveData getPaginationNetworkState() { - return paginationNetworkState; - } - - public LiveData getInitialLoadingState() { - return initialLoadingState; - } - - public LiveData hasSubredditLiveData() { - return hasSubredditLiveData; - } - - public void refresh() { - subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate(); - } - - public void retryLoadingMore() { - subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore(); - } - - public void changeSortType(SortType sortType) { - sortTypeLiveData.postValue(sortType); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private Retrofit retrofit; - private String query; - private SortType sortType; - - public Factory(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - return (T) new SubredditListingViewModel(retrofit, query, sortType); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java deleted file mode 100644 index 3f2225e8..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java +++ /dev/null @@ -1,120 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import java.util.HashMap; -import java.util.Map; - -import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; -import ml.docilealligator.infinityforreddit.Utils.APIUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Retrofit; - -public class SubredditSubscription { - public static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit, - String accessToken, String subredditName, String accountName, - RedditDataRoomDatabase redditDataRoomDatabase, - SubredditSubscriptionListener subredditSubscriptionListener) { - subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub", - redditDataRoomDatabase, subredditSubscriptionListener); - } - - public static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken, - String subredditName, String accountName, - RedditDataRoomDatabase redditDataRoomDatabase, - SubredditSubscriptionListener subredditSubscriptionListener) { - subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub", - redditDataRoomDatabase, subredditSubscriptionListener); - } - - private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, - String subredditName, String accountName, String action, - RedditDataRoomDatabase redditDataRoomDatabase, - SubredditSubscriptionListener subredditSubscriptionListener) { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); - - Map params = new HashMap<>(); - params.put(APIUtils.ACTION_KEY, action); - params.put(APIUtils.SR_NAME_KEY, subredditName); - - Call subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); - subredditSubscriptionCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - if (action.equals("sub")) { - FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { - @Override - public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { - new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, - subredditData, accountName, true).execute(); - } - - @Override - public void onFetchSubredditDataFail() { - - } - }); - } else { - new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute(); - } - subredditSubscriptionListener.onSubredditSubscriptionSuccess(); - } else { - subredditSubscriptionListener.onSubredditSubscriptionFail(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - subredditSubscriptionListener.onSubredditSubscriptionFail(); - } - }); - } - - public interface SubredditSubscriptionListener { - void onSubredditSubscriptionSuccess(); - - void onSubredditSubscriptionFail(); - } - - private static class UpdateSubscriptionAsyncTask extends AsyncTask { - - private RedditDataRoomDatabase redditDataRoomDatabase; - private String subredditName; - private String accountName; - private SubscribedSubredditData subscribedSubredditData; - private boolean isSubscribing; - - UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, - String accountName, boolean isSubscribing) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.subredditName = subredditName; - this.accountName = accountName; - this.isSubscribing = isSubscribing; - } - - UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditData subredditData, - String accountName, boolean isSubscribing) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), - subredditData.getIconUrl(), accountName, false); - this.accountName = accountName; - this.isSubscribing = isSubscribing; - } - - @Override - protected Void doInBackground(Void... voids) { - if (isSubscribing) { - redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); - } else { - redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); - } - return null; - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java deleted file mode 100644 index bb95c446..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditWithSelection.java +++ /dev/null @@ -1,102 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.Parcel; -import android.os.Parcelable; - -import androidx.annotation.Nullable; - -import java.util.ArrayList; -import java.util.List; - -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; -import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; - -public class SubredditWithSelection implements Parcelable { - private String name; - private String iconUrl; - private boolean selected; - - public SubredditWithSelection(String name, String iconUrl) { - this.name = name; - this.iconUrl = iconUrl; - selected = false; - } - - protected SubredditWithSelection(Parcel in) { - super(); - name = in.readString(); - iconUrl = in.readString(); - selected = in.readByte() != 0; - } - - public static final Creator CREATOR = new Creator() { - @Override - public SubredditWithSelection createFromParcel(Parcel in) { - return new SubredditWithSelection(in); - } - - @Override - public SubredditWithSelection[] newArray(int size) { - return new SubredditWithSelection[size]; - } - }; - - public String getName() { - return name; - } - - public String getIconUrl() { - return iconUrl; - } - - public boolean isSelected() { - return selected; - } - - public void setSelected(boolean selected) { - this.selected = selected; - } - - public static ArrayList convertSubscribedSubreddits( - List subscribedSubredditData) { - ArrayList subredditWithSelections = new ArrayList<>(); - for (SubscribedSubredditData s : subscribedSubredditData) { - subredditWithSelections.add(new SubredditWithSelection(s.getName(), s.getIconUrl())); - } - - return subredditWithSelections; - } - - public static SubredditWithSelection convertSubreddit(SubredditData subreddit) { - return new SubredditWithSelection(subreddit.getName(), subreddit.getIconUrl()); - } - - public int compareName(SubredditWithSelection subredditWithSelection) { - if (subredditWithSelection != null) { - return name.compareToIgnoreCase(subredditWithSelection.getName()); - } else { - return -1; - } - } - - @Override - public boolean equals(@Nullable Object obj) { - if (!(obj instanceof SubredditWithSelection)) { - return false; - } else { - return this.getName().compareToIgnoreCase(((SubredditWithSelection) obj).getName()) == 0; - } - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(name); - parcel.writeString(iconUrl); - parcel.writeByte((byte) (selected ? 1 : 0)); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditDao.java new file mode 100644 index 00000000..aa62e088 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditDao.java @@ -0,0 +1,33 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubreddit; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +@Dao +public interface SubscribedSubredditDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubscribedSubredditData subscribedSubredditData); + + @Query("DELETE FROM subscribed_subreddits") + void deleteAllSubscribedSubreddits(); + + @Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedSubreddits(String accountName); + + @Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") + List getAllSubscribedSubredditsList(String accountName); + + @Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllFavoriteSubscribedSubreddits(String accountName); + + @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName AND username = :accountName COLLATE NOCASE LIMIT 1") + SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); + + @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName AND username = :accountName") + void deleteSubscribedSubreddit(String subredditName, String accountName); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditData.java new file mode 100644 index 00000000..167e6423 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditData.java @@ -0,0 +1,65 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubreddit; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; + +import ml.docilealligator.infinityforreddit.Account.Account; + +@Entity(tableName = "subscribed_subreddits", primaryKeys = {"id", "username"}, + foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", + childColumns = "username", onDelete = ForeignKey.CASCADE)) +public class SubscribedSubredditData { + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @NonNull + @ColumnInfo(name = "username") + private String username; + @ColumnInfo(name = "is_favorite") + private boolean favorite; + + public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, @NonNull String username, + boolean favorite) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + this.username = username; + this.favorite = favorite; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + @NonNull + public String getUsername() { + return username; + } + + public void setUsername(@NonNull String username) { + this.username = username; + } + + public boolean isFavorite() { + return favorite; + } + + public void setFavorite(boolean favorite) { + this.favorite = favorite; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditRepository.java new file mode 100644 index 00000000..aa5fbc0f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditRepository.java @@ -0,0 +1,48 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubreddit; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedSubredditRepository { + private SubscribedSubredditDao mSubscribedSubredditDao; + private LiveData> mAllSubscribedSubreddits; + private LiveData> mAllFavoriteSubscribedSubreddits; + + SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); + mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName); + mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName); + } + + LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public LiveData> getAllFavoriteSubscribedSubreddits() { + return mAllFavoriteSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData); + } + + private static class insertAsyncTask extends AsyncTask { + + private SubscribedSubredditDao mAsyncTaskDao; + + insertAsyncTask(SubscribedSubredditDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubscribedSubredditData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditViewModel.java new file mode 100644 index 00000000..79e3dd2b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubreddit/SubscribedSubredditViewModel.java @@ -0,0 +1,56 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubreddit; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedSubredditViewModel extends AndroidViewModel { + private SubscribedSubredditRepository mSubscribedSubredditRepository; + private LiveData> mAllSubscribedSubreddits; + private LiveData> mAllFavoriteSubscribedSubreddits; + + public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + super(application); + mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName); + mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); + mAllFavoriteSubscribedSubreddits = mSubscribedSubredditRepository.getAllFavoriteSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public LiveData> getAllFavoriteSubscribedSubreddits() { + return mAllFavoriteSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + mSubscribedSubredditRepository.insert(subscribedSubredditData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Application mApplication; + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mAccountName; + + public Factory(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + this.mApplication = application; + this.mRedditDataRoomDatabase = redditDataRoomDatabase; + this.mAccountName = accountName; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + return (T) new SubscribedSubredditViewModel(mApplication, mRedditDataRoomDatabase, mAccountName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java deleted file mode 100644 index cc0434d7..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java +++ /dev/null @@ -1,33 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; - -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -@Dao -public interface SubscribedSubredditDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(SubscribedSubredditData subscribedSubredditData); - - @Query("DELETE FROM subscribed_subreddits") - void deleteAllSubscribedSubreddits(); - - @Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") - LiveData> getAllSubscribedSubreddits(String accountName); - - @Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") - List getAllSubscribedSubredditsList(String accountName); - - @Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") - LiveData> getAllFavoriteSubscribedSubreddits(String accountName); - - @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName AND username = :accountName COLLATE NOCASE LIMIT 1") - SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); - - @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName AND username = :accountName") - void deleteSubscribedSubreddit(String subredditName, String accountName); -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java deleted file mode 100644 index 04b8700e..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java +++ /dev/null @@ -1,65 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; - -import androidx.annotation.NonNull; -import androidx.room.ColumnInfo; -import androidx.room.Entity; -import androidx.room.ForeignKey; - -import ml.docilealligator.infinityforreddit.Account.Account; - -@Entity(tableName = "subscribed_subreddits", primaryKeys = {"id", "username"}, - foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", - childColumns = "username", onDelete = ForeignKey.CASCADE)) -public class SubscribedSubredditData { - @NonNull - @ColumnInfo(name = "id") - private String id; - @ColumnInfo(name = "name") - private String name; - @ColumnInfo(name = "icon") - private String iconUrl; - @NonNull - @ColumnInfo(name = "username") - private String username; - @ColumnInfo(name = "is_favorite") - private boolean favorite; - - public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, @NonNull String username, - boolean favorite) { - this.id = id; - this.name = name; - this.iconUrl = iconUrl; - this.username = username; - this.favorite = favorite; - } - - @NonNull - public String getId() { - return id; - } - - public String getName() { - return name; - } - - public String getIconUrl() { - return iconUrl; - } - - @NonNull - public String getUsername() { - return username; - } - - public void setUsername(@NonNull String username) { - this.username = username; - } - - public boolean isFavorite() { - return favorite; - } - - public void setFavorite(boolean favorite) { - this.favorite = favorite; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java deleted file mode 100644 index 3f2da07d..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java +++ /dev/null @@ -1,48 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; - -import android.os.AsyncTask; - -import androidx.lifecycle.LiveData; - -import java.util.List; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class SubscribedSubredditRepository { - private SubscribedSubredditDao mSubscribedSubredditDao; - private LiveData> mAllSubscribedSubreddits; - private LiveData> mAllFavoriteSubscribedSubreddits; - - SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { - mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); - mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName); - mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName); - } - - LiveData> getAllSubscribedSubreddits() { - return mAllSubscribedSubreddits; - } - - public LiveData> getAllFavoriteSubscribedSubreddits() { - return mAllFavoriteSubscribedSubreddits; - } - - public void insert(SubscribedSubredditData subscribedSubredditData) { - new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData); - } - - private static class insertAsyncTask extends AsyncTask { - - private SubscribedSubredditDao mAsyncTaskDao; - - insertAsyncTask(SubscribedSubredditDao dao) { - mAsyncTaskDao = dao; - } - - @Override - protected Void doInBackground(final SubscribedSubredditData... params) { - mAsyncTaskDao.insert(params[0]); - return null; - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java deleted file mode 100644 index 56ff6c5e..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java +++ /dev/null @@ -1,56 +0,0 @@ -package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; - -import android.app.Application; - -import androidx.annotation.NonNull; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelProvider; - -import java.util.List; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class SubscribedSubredditViewModel extends AndroidViewModel { - private SubscribedSubredditRepository mSubscribedSubredditRepository; - private LiveData> mAllSubscribedSubreddits; - private LiveData> mAllFavoriteSubscribedSubreddits; - - public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { - super(application); - mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName); - mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); - mAllFavoriteSubscribedSubreddits = mSubscribedSubredditRepository.getAllFavoriteSubscribedSubreddits(); - } - - public LiveData> getAllSubscribedSubreddits() { - return mAllSubscribedSubreddits; - } - - public LiveData> getAllFavoriteSubscribedSubreddits() { - return mAllFavoriteSubscribedSubreddits; - } - - public void insert(SubscribedSubredditData subscribedSubredditData) { - mSubscribedSubredditRepository.insert(subscribedSubredditData); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private Application mApplication; - private RedditDataRoomDatabase mRedditDataRoomDatabase; - private String mAccountName; - - public Factory(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { - this.mApplication = application; - this.mRedditDataRoomDatabase = redditDataRoomDatabase; - this.mAccountName = accountName; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - return (T) new SubscribedSubredditViewModel(mApplication, mRedditDataRoomDatabase, mAccountName); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java new file mode 100644 index 00000000..5d9a35d9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/FetchUserData.java @@ -0,0 +1,87 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.annotation.NonNull; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; + +public class FetchUserData { + public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call userInfo = api.getUserData(userName); + userInfo.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParseUserData.parseUserData(response.body(), new ParseUserData.ParseUserDataListener() { + @Override + public void onParseUserDataSuccess(UserData userData) { + fetchUserDataListener.onFetchUserDataSuccess(userData); + } + + @Override + public void onParseUserDataFailed() { + fetchUserDataListener.onFetchUserDataFailed(); + } + }); + } else { + fetchUserDataListener.onFetchUserDataFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchUserDataListener.onFetchUserDataFailed(); + } + }); + } + + public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType, + FetchUserListingDataListener fetchUserListingDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call userInfo = api.searchUsers(query, after, sortType); + userInfo.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + ParseUserData.parseUserListingData(response.body(), new ParseUserData.ParseUserListingDataListener() { + @Override + public void onParseUserListingDataSuccess(ArrayList userData, String after) { + fetchUserListingDataListener.onFetchUserListingDataSuccess(userData, after); + } + + @Override + public void onParseUserListingDataFailed() { + fetchUserListingDataListener.onFetchUserListingDataFailed(); + } + }); + } else { + fetchUserListingDataListener.onFetchUserListingDataFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchUserListingDataListener.onFetchUserListingDataFailed(); + } + }); + } + + public interface FetchUserDataListener { + void onFetchUserDataSuccess(UserData userData); + + void onFetchUserDataFailed(); + } + + public interface FetchUserListingDataListener { + void onFetchUserListingDataSuccess(ArrayList userData, String after); + + void onFetchUserListingDataFailed(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/ParseUserData.java new file mode 100644 index 00000000..65e32933 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/ParseUserData.java @@ -0,0 +1,152 @@ +package ml.docilealligator.infinityforreddit.User; + +import android.os.AsyncTask; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; + +public class ParseUserData { + static void parseUserData(String response, ParseUserDataListener parseUserDataListener) { + new ParseUserDataAsyncTask(response, parseUserDataListener).execute(); + } + + static void parseUserListingData(String response, ParseUserListingDataListener parseUserListingDataListener) { + new ParseUserListingDataAsyncTask(response, parseUserListingDataListener).execute(); + } + + private static UserData parseUserDataBase(JSONObject userDataJson) throws JSONException { + if(userDataJson == null) { + return null; + } + + userDataJson = userDataJson.getJSONObject(JSONUtils.DATA_KEY); + String userName = userDataJson.getString(JSONUtils.NAME_KEY); + String iconImageUrl = userDataJson.getString(JSONUtils.ICON_IMG_KEY); + String bannerImageUrl = ""; + boolean canBeFollowed; + if (userDataJson.has(JSONUtils.SUBREDDIT_KEY) && !userDataJson.isNull(JSONUtils.SUBREDDIT_KEY)) { + bannerImageUrl = userDataJson.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY); + canBeFollowed = true; + } else { + canBeFollowed = false; + } + int linkKarma = userDataJson.getInt(JSONUtils.LINK_KARMA_KEY); + int commentKarma = userDataJson.getInt(JSONUtils.COMMENT_KARMA_KEY); + long cakeday = userDataJson.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; + boolean isGold = userDataJson.getBoolean(JSONUtils.IS_GOLD_KEY); + boolean isFriend = userDataJson.getBoolean(JSONUtils.IS_FRIEND_KEY); + String description = userDataJson.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.PUBLIC_DESCRIPTION_KEY); + + return new UserData(userName, iconImageUrl, bannerImageUrl, linkKarma, commentKarma, cakeday, + isGold, isFriend, canBeFollowed, description); + } + + interface ParseUserDataListener { + void onParseUserDataSuccess(UserData userData); + + void onParseUserDataFailed(); + } + + interface ParseUserListingDataListener { + void onParseUserListingDataSuccess(ArrayList userData, String after); + + void onParseUserListingDataFailed(); + } + + private static class ParseUserDataAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private ParseUserDataListener parseUserDataListener; + private boolean parseFailed; + + private UserData userData; + + ParseUserDataAsyncTask(String response, ParseUserDataListener parseUserDataListener) { + try { + jsonResponse = new JSONObject(response); + this.parseUserDataListener = parseUserDataListener; + parseFailed = false; + } catch (JSONException e) { + e.printStackTrace(); + parseUserDataListener.onParseUserDataFailed(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + userData = parseUserDataBase(jsonResponse); + } catch (JSONException e) { + parseFailed = true; + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if (!parseFailed) { + parseUserDataListener.onParseUserDataSuccess(userData); + } else { + parseUserDataListener.onParseUserDataFailed(); + } + } + } + + private static class ParseUserListingDataAsyncTask extends AsyncTask { + private String response; + private JSONObject jsonResponse; + private ParseUserListingDataListener parseUserListingDataListener; + private String after; + private boolean parseFailed; + + private ArrayList userDataArrayList; + + ParseUserListingDataAsyncTask(String response, ParseUserListingDataListener parseUserListingDataListener) { + this.parseUserListingDataListener = parseUserListingDataListener; + this.response = response; + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + userDataArrayList = new ArrayList<>(); + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + if (!parseFailed) { + after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + for (int i = 0; i < children.length(); i++) { + userDataArrayList.add(parseUserDataBase(children.getJSONObject(i))); + } + } + } catch (JSONException e) { + parseFailed = true; + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if (!parseFailed) { + parseUserListingDataListener.onParseUserListingDataSuccess(userDataArrayList, after); + } else { + if (response.equals("\"{}\"")) { + parseUserListingDataListener.onParseUserListingDataSuccess(new ArrayList<>(), null); + } else { + parseUserListingDataListener.onParseUserListingDataFailed(); + } + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserFollowing.java new file mode 100644 index 00000000..d49acf25 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserFollowing.java @@ -0,0 +1,118 @@ +package ml.docilealligator.infinityforreddit.User; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; + +public class UserFollowing { + public static void followUser(Retrofit oauthRetrofit, Retrofit retrofit, + String accessToken, String username, String accountName, + SubscribedUserDao subscribedUserDao, + UserFollowingListener userFollowingListener) { + userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "sub", + subscribedUserDao, userFollowingListener); + } + + public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit, + String accessToken, String username, String accountName, + SubscribedUserDao subscribedUserDao, + UserFollowingListener userFollowingListener) { + userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "unsub", + subscribedUserDao, userFollowingListener); + } + + private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, + String username, String accountName, String action, SubscribedUserDao subscribedUserDao, + UserFollowingListener userFollowingListener) { + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + + Map params = new HashMap<>(); + params.put(APIUtils.ACTION_KEY, action); + params.put(APIUtils.SR_NAME_KEY, "u_" + username); + + Call subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); + subredditSubscriptionCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + if (action.equals("sub")) { + FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { + @Override + public void onFetchUserDataSuccess(UserData userData) { + new UpdateSubscriptionAsyncTask(subscribedUserDao, userData, accountName, true).execute(); + } + + @Override + public void onFetchUserDataFailed() { + + } + }); + } else { + new UpdateSubscriptionAsyncTask(subscribedUserDao, username, accountName, false).execute(); + } + userFollowingListener.onUserFollowingSuccess(); + } else { + userFollowingListener.onUserFollowingFail(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + userFollowingListener.onUserFollowingFail(); + } + }); + } + + public interface UserFollowingListener { + void onUserFollowingSuccess(); + + void onUserFollowingFail(); + } + + private static class UpdateSubscriptionAsyncTask extends AsyncTask { + + private SubscribedUserDao subscribedUserDao; + private String username; + private String accountName; + private SubscribedUserData subscribedUserData; + private boolean isSubscribing; + + UpdateSubscriptionAsyncTask(SubscribedUserDao subscribedUserDao, String username, + String accountName, boolean isSubscribing) { + this.subscribedUserDao = subscribedUserDao; + this.username = username; + this.accountName = accountName; + this.isSubscribing = isSubscribing; + } + + UpdateSubscriptionAsyncTask(SubscribedUserDao subscribedUserDao, UserData userData, + String accountName, boolean isSubscribing) { + this.subscribedUserDao = subscribedUserDao; + this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(), + accountName, false); + this.accountName = accountName; + this.isSubscribing = isSubscribing; + } + + @Override + protected Void doInBackground(Void... voids) { + if (isSubscribing) { + subscribedUserDao.insert(subscribedUserData); + } else { + subscribedUserDao.deleteSubscribedUser(username, accountName); + } + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java new file mode 100644 index 00000000..a7606588 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSource.java @@ -0,0 +1,104 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.PageKeyedDataSource; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class UserListingDataSource extends PageKeyedDataSource { + + private Retrofit retrofit; + private String query; + private SortType sortType; + + private MutableLiveData paginationNetworkStateLiveData; + private MutableLiveData initialLoadStateLiveData; + private MutableLiveData hasUserLiveData; + + private PageKeyedDataSource.LoadParams params; + private PageKeyedDataSource.LoadCallback callback; + + UserListingDataSource(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + paginationNetworkStateLiveData = new MutableLiveData<>(); + initialLoadStateLiveData = new MutableLiveData<>(); + hasUserLiveData = new MutableLiveData<>(); + } + + MutableLiveData getPaginationNetworkStateLiveData() { + return paginationNetworkStateLiveData; + } + + MutableLiveData getInitialLoadStateLiveData() { + return initialLoadStateLiveData; + } + + MutableLiveData hasUserLiveData() { + return hasUserLiveData; + } + + @Override + public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams params, @NonNull PageKeyedDataSource.LoadInitialCallback callback) { + initialLoadStateLiveData.postValue(NetworkState.LOADING); + + FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType().value, + new FetchUserData.FetchUserListingDataListener() { + @Override + public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { + if (UserData.size() == 0) { + hasUserLiveData.postValue(false); + } else { + hasUserLiveData.postValue(true); + } + + callback.onResult(UserData, null, after); + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onFetchUserListingDataFailed() { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); + } + }); + } + + @Override + public void loadBefore(@NonNull PageKeyedDataSource.LoadParams params, @NonNull PageKeyedDataSource.LoadCallback callback) { + + } + + @Override + public void loadAfter(@NonNull PageKeyedDataSource.LoadParams params, @NonNull PageKeyedDataSource.LoadCallback callback) { + this.params = params; + this.callback = callback; + + if (params.key.equals("null") || params.key.equals("")) { + return; + } + + FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType.getType().value, + new FetchUserData.FetchUserListingDataListener() { + @Override + public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { + callback.onResult(UserData, after); + paginationNetworkStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onFetchUserListingDataFailed() { + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); + } + }); + } + + void retryLoadingMore() { + loadAfter(params, callback); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java new file mode 100644 index 00000000..e0bf6288 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingDataSourceFactory.java @@ -0,0 +1,44 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.DataSource; + +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class UserListingDataSourceFactory extends DataSource.Factory { + private Retrofit retrofit; + private String query; + private SortType sortType; + + private UserListingDataSource userListingDataSource; + private MutableLiveData userListingDataSourceMutableLiveData; + + UserListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + userListingDataSourceMutableLiveData = new MutableLiveData<>(); + } + + @NonNull + @Override + public DataSource create() { + userListingDataSource = new UserListingDataSource(retrofit, query, sortType); + userListingDataSourceMutableLiveData.postValue(userListingDataSource); + return userListingDataSource; + } + + public MutableLiveData getUserListingDataSourceMutableLiveData() { + return userListingDataSourceMutableLiveData; + } + + UserListingDataSource getUserListingDataSource() { + return userListingDataSource; + } + + void changeSortType(SortType sortType) { + this.sortType = sortType; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java new file mode 100644 index 00000000..d15bbd62 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserListingViewModel.java @@ -0,0 +1,94 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +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 ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Retrofit; + +public class UserListingViewModel extends ViewModel { + private UserListingDataSourceFactory userListingDataSourceFactory; + private LiveData paginationNetworkState; + private LiveData initialLoadingState; + private LiveData hasUserLiveData; + private LiveData> users; + private MutableLiveData sortTypeLiveData; + + public UserListingViewModel(Retrofit retrofit, String query, SortType sortType) { + userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); + + initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), + UserListingDataSource::getInitialLoadStateLiveData); + paginationNetworkState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), + UserListingDataSource::getPaginationNetworkStateLiveData); + hasUserLiveData = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), + UserListingDataSource::hasUserLiveData); + + sortTypeLiveData = new MutableLiveData<>(); + sortTypeLiveData.postValue(sortType); + + PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setEnablePlaceholders(false) + .setPageSize(25) + .build(); + + users = Transformations.switchMap(sortTypeLiveData, sort -> { + userListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); + return (new LivePagedListBuilder(userListingDataSourceFactory, pagedListConfig)).build(); + }); + } + + public LiveData> getUsers() { + return users; + } + + public LiveData getPaginationNetworkState() { + return paginationNetworkState; + } + + public LiveData getInitialLoadingState() { + return initialLoadingState; + } + + public LiveData hasUser() { + return hasUserLiveData; + } + + public void refresh() { + userListingDataSourceFactory.getUserListingDataSource().invalidate(); + } + + public void retryLoadingMore() { + userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore(); + } + + public void changeSortType(SortType sortType) { + sortTypeLiveData.postValue(sortType); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Retrofit retrofit; + private String query; + private SortType sortType; + + public Factory(Retrofit retrofit, String query, SortType sortType) { + this.retrofit = retrofit; + this.query = query; + this.sortType = sortType; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + return (T) new UserListingViewModel(retrofit, query, sortType); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java deleted file mode 100644 index 93ec3bc3..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java +++ /dev/null @@ -1,119 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import java.util.HashMap; -import java.util.Map; - -import ml.docilealligator.infinityforreddit.API.RedditAPI; -import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; -import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; -import ml.docilealligator.infinityforreddit.User.UserData; -import ml.docilealligator.infinityforreddit.Utils.APIUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Retrofit; - -public class UserFollowing { - public static void followUser(Retrofit oauthRetrofit, Retrofit retrofit, - String accessToken, String username, String accountName, - SubscribedUserDao subscribedUserDao, - UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "sub", - subscribedUserDao, userFollowingListener); - } - - public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit, - String accessToken, String username, String accountName, - SubscribedUserDao subscribedUserDao, - UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "unsub", - subscribedUserDao, userFollowingListener); - } - - private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, - String username, String accountName, String action, SubscribedUserDao subscribedUserDao, - UserFollowingListener userFollowingListener) { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); - - Map params = new HashMap<>(); - params.put(APIUtils.ACTION_KEY, action); - params.put(APIUtils.SR_NAME_KEY, "u_" + username); - - Call subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); - subredditSubscriptionCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - if (action.equals("sub")) { - FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { - @Override - public void onFetchUserDataSuccess(UserData userData) { - new UpdateSubscriptionAsyncTask(subscribedUserDao, userData, accountName, true).execute(); - } - - @Override - public void onFetchUserDataFailed() { - - } - }); - } else { - new UpdateSubscriptionAsyncTask(subscribedUserDao, username, accountName, false).execute(); - } - userFollowingListener.onUserFollowingSuccess(); - } else { - userFollowingListener.onUserFollowingFail(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - userFollowingListener.onUserFollowingFail(); - } - }); - } - - public interface UserFollowingListener { - void onUserFollowingSuccess(); - - void onUserFollowingFail(); - } - - private static class UpdateSubscriptionAsyncTask extends AsyncTask { - - private SubscribedUserDao subscribedUserDao; - private String username; - private String accountName; - private SubscribedUserData subscribedUserData; - private boolean isSubscribing; - - UpdateSubscriptionAsyncTask(SubscribedUserDao subscribedUserDao, String username, - String accountName, boolean isSubscribing) { - this.subscribedUserDao = subscribedUserDao; - this.username = username; - this.accountName = accountName; - this.isSubscribing = isSubscribing; - } - - UpdateSubscriptionAsyncTask(SubscribedUserDao subscribedUserDao, UserData userData, - String accountName, boolean isSubscribing) { - this.subscribedUserDao = subscribedUserDao; - this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(), - accountName, false); - this.accountName = accountName; - this.isSubscribing = isSubscribing; - } - - @Override - protected Void doInBackground(Void... voids) { - if (isSubscribing) { - subscribedUserDao.insert(subscribedUserData); - } else { - subscribedUserDao.deleteSubscribedUser(username, accountName); - } - return null; - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java deleted file mode 100644 index 0d181035..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java +++ /dev/null @@ -1,103 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.PageKeyedDataSource; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.User.UserData; -import retrofit2.Retrofit; - -public class UserListingDataSource extends PageKeyedDataSource { - - private Retrofit retrofit; - private String query; - private SortType sortType; - - private MutableLiveData paginationNetworkStateLiveData; - private MutableLiveData initialLoadStateLiveData; - private MutableLiveData hasUserLiveData; - - private PageKeyedDataSource.LoadParams params; - private PageKeyedDataSource.LoadCallback callback; - - UserListingDataSource(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - paginationNetworkStateLiveData = new MutableLiveData<>(); - initialLoadStateLiveData = new MutableLiveData<>(); - hasUserLiveData = new MutableLiveData<>(); - } - - MutableLiveData getPaginationNetworkStateLiveData() { - return paginationNetworkStateLiveData; - } - - MutableLiveData getInitialLoadStateLiveData() { - return initialLoadStateLiveData; - } - - MutableLiveData hasUserLiveData() { - return hasUserLiveData; - } - - @Override - public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams params, @NonNull PageKeyedDataSource.LoadInitialCallback callback) { - initialLoadStateLiveData.postValue(NetworkState.LOADING); - - FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType().value, - new FetchUserData.FetchUserListingDataListener() { - @Override - public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { - if (UserData.size() == 0) { - hasUserLiveData.postValue(false); - } else { - hasUserLiveData.postValue(true); - } - - callback.onResult(UserData, null, after); - initialLoadStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onFetchUserListingDataFailed() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); - } - }); - } - - @Override - public void loadBefore(@NonNull PageKeyedDataSource.LoadParams params, @NonNull PageKeyedDataSource.LoadCallback callback) { - - } - - @Override - public void loadAfter(@NonNull PageKeyedDataSource.LoadParams params, @NonNull PageKeyedDataSource.LoadCallback callback) { - this.params = params; - this.callback = callback; - - if (params.key.equals("null") || params.key.equals("")) { - return; - } - - FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType.getType().value, - new FetchUserData.FetchUserListingDataListener() { - @Override - public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { - callback.onResult(UserData, after); - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); - } - - @Override - public void onFetchUserListingDataFailed() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); - } - }); - } - - void retryLoadingMore() { - loadAfter(params, callback); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java deleted file mode 100644 index d391514f..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.DataSource; - -import retrofit2.Retrofit; - -public class UserListingDataSourceFactory extends DataSource.Factory { - private Retrofit retrofit; - private String query; - private SortType sortType; - - private UserListingDataSource userListingDataSource; - private MutableLiveData userListingDataSourceMutableLiveData; - - UserListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - userListingDataSourceMutableLiveData = new MutableLiveData<>(); - } - - @NonNull - @Override - public DataSource create() { - userListingDataSource = new UserListingDataSource(retrofit, query, sortType); - userListingDataSourceMutableLiveData.postValue(userListingDataSource); - return userListingDataSource; - } - - public MutableLiveData getUserListingDataSourceMutableLiveData() { - return userListingDataSourceMutableLiveData; - } - - UserListingDataSource getUserListingDataSource() { - return userListingDataSource; - } - - void changeSortType(SortType sortType) { - this.sortType = sortType; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java deleted file mode 100644 index abe3cef2..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java +++ /dev/null @@ -1,93 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -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 ml.docilealligator.infinityforreddit.User.UserData; -import retrofit2.Retrofit; - -public class UserListingViewModel extends ViewModel { - private UserListingDataSourceFactory userListingDataSourceFactory; - private LiveData paginationNetworkState; - private LiveData initialLoadingState; - private LiveData hasUserLiveData; - private LiveData> users; - private MutableLiveData sortTypeLiveData; - - public UserListingViewModel(Retrofit retrofit, String query, SortType sortType) { - userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); - - initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), - UserListingDataSource::getInitialLoadStateLiveData); - paginationNetworkState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), - UserListingDataSource::getPaginationNetworkStateLiveData); - hasUserLiveData = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), - UserListingDataSource::hasUserLiveData); - - sortTypeLiveData = new MutableLiveData<>(); - sortTypeLiveData.postValue(sortType); - - PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setEnablePlaceholders(false) - .setPageSize(25) - .build(); - - users = Transformations.switchMap(sortTypeLiveData, sort -> { - userListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); - return (new LivePagedListBuilder(userListingDataSourceFactory, pagedListConfig)).build(); - }); - } - - public LiveData> getUsers() { - return users; - } - - public LiveData getPaginationNetworkState() { - return paginationNetworkState; - } - - public LiveData getInitialLoadingState() { - return initialLoadingState; - } - - public LiveData hasUser() { - return hasUserLiveData; - } - - public void refresh() { - userListingDataSourceFactory.getUserListingDataSource().invalidate(); - } - - public void retryLoadingMore() { - userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore(); - } - - public void changeSortType(SortType sortType) { - sortTypeLiveData.postValue(sortType); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private Retrofit retrofit; - private String query; - private SortType sortType; - - public Factory(Retrofit retrofit, String query, SortType sortType) { - this.retrofit = retrofit; - this.query = query; - this.sortType = sortType; - } - - @NonNull - @Override - public T create(@NonNull Class modelClass) { - return (T) new UserListingViewModel(retrofit, query, sortType); - } - } -} -- cgit v1.2.3