diff options
Diffstat (limited to 'app/src/main/java/ml/docilealligator')
19 files changed, 338 insertions, 126 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MarkPostAsReadInterface.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MarkPostAsReadInterface.java new file mode 100644 index 00000000..d14c1c09 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MarkPostAsReadInterface.java @@ -0,0 +1,7 @@ +package ml.docilealligator.infinityforreddit; + +import ml.docilealligator.infinityforreddit.post.Post; + +public interface MarkPostAsReadInterface { + void markPostAsRead(Post post); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 73138eae..f7cd0e5b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -16,8 +16,8 @@ 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.readposts.ReadPost; -import ml.docilealligator.infinityforreddit.readposts.ReadPostDao; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; +import ml.docilealligator.infinityforreddit.readpost.ReadPostDao; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQueryDao; import ml.docilealligator.infinityforreddit.subreddit.SubredditDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index 46b474fc..9be885d1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -36,6 +36,11 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -43,16 +48,14 @@ import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.CommentsListingFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; public class AccountSavedThingActivity extends BaseActivity implements ActivityToolbarInterface, - PostLayoutBottomSheetFragment.PostLayoutSelectionCallback { + PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, MarkPostAsReadInterface { private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; @@ -335,6 +338,11 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT } } + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } + private class SectionsPagerAdapter extends FragmentStateAdapter { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredThingActivity.java index d2bfc20f..279a8346 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredThingActivity.java @@ -28,6 +28,13 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.SearchPostSortTypeBottomSheetFragment; @@ -37,18 +44,13 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.UserThingSortTy import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class FilteredThingActivity extends BaseActivity implements SortTypeSelectionCallback, - PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface { + PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, MarkPostAsReadInterface { public static final String EXTRA_NAME = "ESN"; public static final String EXTRA_QUERY = "EQ"; @@ -457,4 +459,9 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec ((PostFragment) mFragment).goBackToTop(); } } + + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index ed20aaf3..c963cf84 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -64,8 +64,17 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.account.AccountViewModel; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.FetchMyInfo; +import ml.docilealligator.infinityforreddit.FetchSubscribedThing; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.PullNotificationWorker; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.account.AccountViewModel; import ml.docilealligator.infinityforreddit.adapters.NavigationDrawerRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThingsAsyncTask; @@ -85,17 +94,11 @@ import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.events.ChangeRequireAuthToAccountSectionEvent; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; -import ml.docilealligator.infinityforreddit.FetchMyInfo; -import ml.docilealligator.infinityforreddit.FetchSubscribedThing; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.message.ReadMessage; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.PullNotificationWorker; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditViewModel; @@ -110,7 +113,8 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; public class MainActivity extends BaseActivity implements SortTypeSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, - ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback, RandomBottomSheetFragment.RandomOptionSelectionCallback { + ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback, + RandomBottomSheetFragment.RandomOptionSelectionCallback, MarkPostAsReadInterface { static final String EXTRA_MESSSAGE_FULLNAME = "ENF"; static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN"; @@ -1269,6 +1273,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb startActivity(intent); } + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } + private class SectionsPagerAdapter extends FragmentStateAdapter { int tabCount; boolean showFavoriteSubscribedSubreddits; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index 2caec58b..b0032263 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -29,6 +29,13 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTimeBottomSheetFragment; @@ -36,21 +43,17 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTypeBottomS import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RefreshMultiRedditsEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.multireddit.DeleteMultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import retrofit2.Retrofit; public class ViewMultiRedditDetailActivity extends BaseActivity implements SortTypeSelectionCallback, - PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface { + PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, MarkPostAsReadInterface { public static final String EXTRA_MULTIREDDIT_DATA = "EMD"; public static final String EXTRA_MULTIREDDIT_PATH = "EMP"; @@ -361,4 +364,9 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT Utils.displaySortTypeInToolbar(sortType, toolbar); } } + + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index c62d4ae2..9363a9d8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -63,6 +63,13 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubredditAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditDataAsyncTask; @@ -79,14 +86,10 @@ import ml.docilealligator.infinityforreddit.events.GoBackToMainPageEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.SidebarFragment; -import ml.docilealligator.infinityforreddit.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.message.ReadMessage; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditSubscription; @@ -99,7 +102,7 @@ import retrofit2.Retrofit; public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback, - RandomBottomSheetFragment.RandomOptionSelectionCallback { + RandomBottomSheetFragment.RandomOptionSelectionCallback, MarkPostAsReadInterface { public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; public static final String EXTRA_MESSAGE_FULLNAME = "ENF"; @@ -1196,6 +1199,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp startActivity(intent); } + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } + private class SectionsPagerAdapter extends FragmentStateAdapter { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 6b118b07..298cfc51 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -56,6 +56,14 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.DeleteThing; +import ml.docilealligator.infinityforreddit.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUserAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask; @@ -63,19 +71,14 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBotto import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTimeBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.UserThingSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.DeleteThing; import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.CommentsListingFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment; -import ml.docilealligator.infinityforreddit.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.message.ReadMessage; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao; import ml.docilealligator.infinityforreddit.user.BlockUser; import ml.docilealligator.infinityforreddit.user.FetchUserData; @@ -89,7 +92,7 @@ import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; public class ViewUserDetailActivity extends BaseActivity implements SortTypeSelectionCallback, - PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface { + PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, MarkPostAsReadInterface { public static final String EXTRA_USER_NAME_KEY = "EUNK"; public static final String EXTRA_MESSAGE_FULLNAME = "ENF"; @@ -835,6 +838,11 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } } + @Override + public void markPostAsRead(Post post) { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId()); + } + private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> { private UserDao userDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 285419a2..a77e3ad2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -70,6 +70,7 @@ import im.ene.toro.widget.Container; import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; +import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; @@ -141,10 +142,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private int mColorPrimaryLightTheme; private int mColorAccent; private int mCardViewBackgroundColor; + private int mReadPostCardViewBackgroundColor; private int mPrimaryTextColor; private int mSecondaryTextColor; private int mPostTitleColor; private int mPostContentColor; + private int mReadPostTitleColor; + private int mReadPostContentColor; private int mStickiedPostIconTint; private int mPostTypeBackgroundColor; private int mPostTypeTextColor; @@ -255,10 +259,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mColorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme(); mColorAccent = customThemeWrapper.getColorAccent(); mCardViewBackgroundColor = customThemeWrapper.getCardViewBackgroundColor(); + mReadPostCardViewBackgroundColor = customThemeWrapper.getReadPostCardViewBackgroundColor(); mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor(); mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); mPostTitleColor = customThemeWrapper.getPostTitleColor(); mPostContentColor = customThemeWrapper.getPostContentColor(); + mReadPostTitleColor = customThemeWrapper.getReadPostTitleColor(); + mReadPostContentColor = customThemeWrapper.getReadPostContentColor(); mStickiedPostIconTint = customThemeWrapper.getStickiedPostIconTint(); mPostTypeBackgroundColor = customThemeWrapper.getPostTypeBackgroundColor(); mPostTypeTextColor = customThemeWrapper.getPostTypeTextColor(); @@ -386,6 +393,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (holder instanceof PostBaseViewHolder) { Post post = getItem(position); if (post != null) { + if (post.isRead()) { + holder.itemView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor)); + ((PostBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor); + } String subredditNamePrefixed = post.getSubredditNamePrefixed(); String subredditName = subredditNamePrefixed.substring(2); String authorPrefixed = "u/" + post.getAuthor(); @@ -683,6 +694,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } else if (holder instanceof PostTextTypeViewHolder) { if (!post.isSpoiler() && post.getSelfTextPlainTrimmed() != null && !post.getSelfTextPlainTrimmed().equals("")) { ((PostTextTypeViewHolder) holder).contentTextView.setVisibility(View.VISIBLE); + if (post.isRead()) { + ((PostTextTypeViewHolder) holder).contentTextView.setTextColor(mReadPostContentColor); + } ((PostTextTypeViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed()); } } @@ -691,6 +705,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } else if (holder instanceof PostCompactBaseViewHolder) { Post post = getItem(position); if (post != null) { + if (post.isRead()) { + holder.itemView.setBackgroundColor(mReadPostCardViewBackgroundColor); + ((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor); + } final String subredditNamePrefixed = post.getSubredditNamePrefixed(); String subredditName = subredditNamePrefixed.substring(2); String authorPrefixed = "u/" + post.getAuthor(); @@ -1247,6 +1265,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { super.onViewRecycled(holder); if (holder instanceof PostBaseViewHolder) { + ((PostBaseViewHolder) holder).itemView.setBackgroundTintList(ColorStateList.valueOf(mCardViewBackgroundColor)); + ((PostBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor); if (holder instanceof PostVideoAutoplayViewHolder) { ((PostVideoAutoplayViewHolder) holder).mediaUri = null; if (((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks != null) { @@ -1267,6 +1287,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostWithPreviewTypeViewHolder) holder).linkTextView.setVisibility(View.GONE); } else if (holder instanceof PostTextTypeViewHolder) { ((PostTextTypeViewHolder) holder).contentTextView.setText(""); + ((PostTextTypeViewHolder) holder).contentTextView.setTextColor(mPostContentColor); ((PostTextTypeViewHolder) holder).contentTextView.setVisibility(View.GONE); } @@ -1285,6 +1306,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostBaseViewHolder) holder).scoreTextView.setTextColor(mPostIconAndInfoColor); ((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); } else if (holder instanceof PostCompactBaseViewHolder) { + ((PostCompactBaseViewHolder) holder).itemView.setBackgroundColor(mCardViewBackgroundColor); + ((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor); mGlide.clear(((PostCompactBaseViewHolder) holder).imageView); mGlide.clear(((PostCompactBaseViewHolder) holder).iconGifImageView); ((PostCompactBaseViewHolder) holder).stickiedPostImageView.setVisibility(View.GONE); @@ -1499,12 +1522,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView cardView.setOnClickListener(view -> { int position = getAdapterPosition(); if (position >= 0 && canStartActivity) { - canStartActivity = false; + Post post = getItem(position); + if (post != null) { + markPostRead(post); + canStartActivity = false; - Intent intent = new Intent(mActivity, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, getItem(position)); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getAdapterPosition()); - mActivity.startActivity(intent); + Intent intent = new Intent(mActivity, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getAdapterPosition()); + mActivity.startActivity(intent); + } } }); @@ -1827,6 +1854,18 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } }); } + + void markPostRead(Post post) { + post.markAsRead(); + cardView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor)); + titleTextView.setTextColor(mReadPostTitleColor); + if (this instanceof PostTextTypeViewHolder) { + ((PostTextTypeViewHolder) this).contentTextView.setTextColor(mReadPostContentColor); + } + if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) { + ((MarkPostAsReadInterface) mActivity).markPostAsRead(post); + } + } } class PostVideoAutoplayViewHolder extends PostBaseViewHolder implements ToroPlayer { @@ -2471,6 +2510,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } Post post = getItem(position); if (post != null && canStartActivity) { + markPostRead(post); canStartActivity = false; Intent intent = new Intent(mActivity, ViewPostDetailActivity.class); @@ -2839,6 +2879,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } }); } + + void markPostRead(Post post) { + post.markAsRead(); + itemView.setBackgroundColor(mReadPostCardViewBackgroundColor); + titleTextView.setTextColor(mReadPostTitleColor); + if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) { + ((MarkPostAsReadInterface) mActivity).markPostAsRead(post); + } + } } class PostCompactLeftThumbnailViewHolder extends PostCompactBaseViewHolder { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index de0d2c0f..6686e3ff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -104,8 +104,8 @@ import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInComp import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; import ml.docilealligator.infinityforreddit.post.PostViewModel; -import ml.docilealligator.infinityforreddit.readposts.FetchReadPosts; -import ml.docilealligator.infinityforreddit.readposts.ReadPost; +import ml.docilealligator.infinityforreddit.readpost.FetchReadPosts; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.FetchSubredditFilters; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -665,14 +665,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { this.readPosts = readPosts; this.subredditFilterList = subredditFilters; - initializeAndPostViewModel(accessToken, locale, filter, nsfw); + initializeAndBindPostViewModel(accessToken, locale, filter, nsfw); } }); } else { - initializeAndPostViewModel(accessToken, locale, filter, nsfw); + initializeAndBindPostViewModel(accessToken, locale, filter, nsfw); } } else { - initializeAndPostViewModelForAnonymous(accessToken, locale, filter, nsfw); + initializeAndBindPostViewModelForAnonymous(accessToken, locale, filter, nsfw); } vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); @@ -784,57 +784,57 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return rootView; } - private void initializeAndPostViewModel(String accessToken, Locale locale, int filter, boolean nsfw) { + private void initializeAndBindPostViewModel(String accessToken, Locale locale, int filter, boolean nsfw) { if (postType == PostDataSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { if (subredditName.equals("all") || subredditName.equals("popular")) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, subredditFilterList)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, readPosts, subredditFilterList)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw, readPosts)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, postFeedScrolledPositionSharedPreferences, - postType, sortType, filter, nsfw)).get(PostViewModel.class); + postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } bindPostViewModel(); } - private void initializeAndPostViewModelForAnonymous(String accessToken, Locale locale, int filter, boolean nsfw) { + private void initializeAndBindPostViewModelForAnonymous(String accessToken, Locale locale, int filter, boolean nsfw) { //For anonymous user if (postType == PostDataSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { if (subredditName.equals("all") || subredditName.equals("popular")) { if (subredditFilterList != null) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, subredditFilterList)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, readPosts, subredditFilterList)).get(PostViewModel.class); } else { FetchSubredditFilters.fetchSubredditFilters(mRedditDataRoomDatabase, subredditFilters -> { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { subredditFilterList = subredditFilters; mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, subredditFilterList)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, readPosts, subredditFilterList)).get(PostViewModel.class); bindPostViewModel(); } @@ -843,20 +843,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); + postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw, readPosts)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, accountName, locale, mSharedPreferences, postFeedScrolledPositionSharedPreferences, - postType, sortType, filter, nsfw)).get(PostViewModel.class); + postType, sortType, filter, nsfw, readPosts)).get(PostViewModel.class); } if (mPostViewModel != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 708f4e75..abf046af 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -9,10 +9,12 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import ml.docilealligator.infinityforreddit.fragments.PostFragment; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -22,14 +24,14 @@ import ml.docilealligator.infinityforreddit.utils.Utils; */ public class ParsePost { - public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, + public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, ParsePostsListingListener parsePostsListingListener) { - new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, parsePostsListingListener).execute(); + new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, readPostList, parsePostsListingListener).execute(); } - public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, + public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { - new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, subredditFilterList, parsePostsListingListener).execute(); + new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, readPostList, subredditFilterList, parsePostsListingListener).execute(); } public static void parsePost(String response, ParsePostListener parsePostListener) { @@ -490,6 +492,7 @@ public class ParsePost { private int nPosts; private int filter; private boolean nsfw; + private List<ReadPost> readPostList; private List<SubredditFilter> subredditFilterList; private ParsePostsListingListener parsePostsListingListener; private ParsePostListener parsePostListener; @@ -498,7 +501,7 @@ public class ParsePost { private String lastItem; private boolean parseFailed; - ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, + ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, ParsePostsListingListener parsePostsListingListener) { this.parsePostsListingListener = parsePostsListingListener; try { @@ -508,6 +511,7 @@ public class ParsePost { this.nPosts = nPosts; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; newPosts = new LinkedHashSet<>(); parseFailed = false; } catch (JSONException e) { @@ -516,9 +520,9 @@ public class ParsePost { } } - ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, + ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { - this(response, nPosts, filter, nsfw, parsePostsListingListener); + this(response, nPosts, filter, nsfw, readPostList, parsePostsListingListener); this.subredditFilterList = subredditFilterList; } @@ -564,12 +568,16 @@ public class ParsePost { size = nPosts; } + HashSet<ReadPost> readPostHashSet = new HashSet<>(readPostList); for (int i = 0; i < size; i++) { try { if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) { JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); Post post = parseBasicData(data); boolean availablePost = true; + if (readPostHashSet.contains(ReadPost.convertPost(post))) { + post.markAsRead(); + } if (subredditFilterList != null) { for (SubredditFilter subredditFilter : subredditFilterList) { if (subredditFilter.getSubredditName().equals(post.getSubredditName())) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index 1e220487..582c398d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -71,6 +71,7 @@ public class Post implements Parcelable { private boolean locked; private boolean saved; private boolean isCrosspost; + private boolean isRead; private String crosspostParentId; private ArrayList<Preview> previews = new ArrayList<>(); private ArrayList<Gallery> gallery = new ArrayList<>(); @@ -107,6 +108,7 @@ public class Post implements Parcelable { this.locked = locked; this.saved = saved; this.isCrosspost = isCrosspost; + isRead = false; } public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, @@ -142,6 +144,7 @@ public class Post implements Parcelable { this.locked = locked; this.saved = saved; this.isCrosspost = isCrosspost; + isRead = false; } protected Post(Parcel in) { @@ -183,6 +186,7 @@ public class Post implements Parcelable { locked = in.readByte() != 0; saved = in.readByte() != 0; isCrosspost = in.readByte() != 0; + isRead = in.readByte() != 0; crosspostParentId = in.readString(); in.readTypedList(previews, Preview.CREATOR); in.readTypedList(gallery, Gallery.CREATOR); @@ -446,6 +450,14 @@ public class Post implements Parcelable { return isCrosspost; } + public void markAsRead() { + isRead = true; + } + + public boolean isRead() { + return isRead; + } + public String getCrosspostParentId() { return crosspostParentId; } @@ -510,6 +522,7 @@ public class Post implements Parcelable { parcel.writeByte((byte) (locked ? 1 : 0)); parcel.writeByte((byte) (saved ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0)); + parcel.writeByte((byte) (isRead ? 1 : 0)); parcel.writeString(crosspostParentId); parcel.writeTypedList(previews); parcel.writeTypedList(gallery); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java index 20e43fce..e17a9a1c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java @@ -11,9 +11,10 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -48,6 +49,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private SortType sortType; private boolean nsfw; private int filter; + private List<ReadPost> readPostList; private List<SubredditFilter> subredditFilterList; private String userWhere; private String multiRedditPath; @@ -62,7 +64,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, - SortType sortType, int filter, boolean nsfw) { + SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -76,12 +78,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String path, int postType, SortType sortType, int filter, boolean nsfw, List<SubredditFilter> subredditFilterList) { + String path, int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList, + List<SubredditFilter> subredditFilterList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -116,13 +120,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; this.subredditFilterList = subredditFilterList; postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, - int postType, SortType sortType, String where, int filter, boolean nsfw) { + int postType, SortType sortType, String where, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -138,12 +143,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { userWhere = where; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, String accountName, Locale locale, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, - String query, int postType, SortType sortType, int filter, boolean nsfw) { + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String subredditOrUserName, String query, int postType, SortType sortType, int filter, + boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -160,6 +167,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.filter = filter; this.nsfw = nsfw; postLinkedHashSet = new LinkedHashSet<>(); + this.readPostList = readPostList; } MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() { @@ -250,7 +258,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -313,7 +321,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -373,7 +381,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, subredditFilterList, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, subredditFilterList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -447,7 +455,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, subredditFilterList, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, subredditFilterList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -508,7 +516,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -579,7 +587,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -662,7 +670,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -753,7 +761,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -813,7 +821,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -884,7 +892,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, nsfw, + ParsePost.parsePosts(response.body(), -1, filter, nsfw, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java index 5973e009..6d49e4d3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Locale; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import retrofit2.Retrofit; @@ -27,14 +28,16 @@ class PostDataSourceFactory extends DataSource.Factory { private String userWhere; private int filter; private boolean nsfw; + private List<ReadPost> readPostList; private List<SubredditFilter> subredditFilterList; private PostDataSource postDataSource; private MutableLiveData<PostDataSource> postDataSourceLiveData; PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - int postType, SortType sortType, int filter, boolean nsfw) { + SharedPreferences sharedPreferences, + SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, + SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -46,12 +49,13 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, int postType, SortType sortType, int filter, boolean nsfw, - List<SubredditFilter> subredditFilterList) { + List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -64,13 +68,14 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; this.subredditFilterList = subredditFilterList; } PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, int postType, SortType sortType, String where, int filter, - boolean nsfw) { + boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -84,12 +89,13 @@ class PostDataSourceFactory extends DataSource.Factory { userWhere = where; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, String query, int postType, SortType sortType, int filter, - boolean nsfw) { + boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -103,6 +109,7 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } @NonNull @@ -110,16 +117,20 @@ class PostDataSourceFactory extends DataSource.Factory { public DataSource<String, Post> create() { if (postType == PostDataSource.TYPE_FRONT_PAGE) { postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, - sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw); + sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, filter, + nsfw, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw); + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, query, + postType, sortType, filter, nsfw, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, subredditFilterList); + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, + sortType, filter, nsfw, readPostList, subredditFilterList); } else { postDataSource = new PostDataSource(retrofit, accessToken, accountName, locale, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw); + sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, + sortType, userWhere, filter, nsfw, readPostList); } postDataSourceLiveData.postValue(postDataSource); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java index 114c68ee..c9193429 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -18,6 +18,7 @@ import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import retrofit2.Retrofit; @@ -33,9 +34,9 @@ public class PostViewModel extends ViewModel { public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences cache, int postType, - SortType sortType, int filter, boolean nsfw) { + SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, - sharedPreferences, cache, postType, sortType, filter, nsfw); + sharedPreferences, cache, postType, sortType, filter, nsfw, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -65,9 +66,11 @@ public class PostViewModel extends ViewModel { public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, - int postType, SortType sortType, int filter, boolean nsfw, List<SubredditFilter> subredditFilterList) { + int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList, + List<SubredditFilter> subredditFilterList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, - sharedPreferences, cache, subredditName, postType, sortType, filter, nsfw, subredditFilterList); + sharedPreferences, cache, subredditName, postType, sortType, filter, nsfw, readPostList, + subredditFilterList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -97,10 +100,10 @@ public class PostViewModel extends ViewModel { public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, - int postType, SortType sortType, String where, int filter, - boolean nsfw) { + int postType, SortType sortType, String where, int filter, boolean nsfw, + List<ReadPost> readPostList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, - sharedPreferences, cache, subredditName, postType, sortType, where, filter, nsfw); + sharedPreferences, cache, subredditName, postType, sortType, where, filter, nsfw, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -130,10 +133,10 @@ public class PostViewModel extends ViewModel { public PostViewModel(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, - String query, int postType, SortType sortType, int filter, - boolean nsfw) { + String query, int postType, SortType sortType, int filter, boolean nsfw, + List<ReadPost> readPostList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, locale, - sharedPreferences, cache, subredditName, query, postType, sortType, filter, nsfw); + sharedPreferences, cache, subredditName, query, postType, sortType, filter, nsfw, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -207,11 +210,12 @@ public class PostViewModel extends ViewModel { private String userWhere; private int filter; private boolean nsfw; + private List<ReadPost> readPostList; private List<SubredditFilter> subredditFilterList; public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - int postType, SortType sortType, int filter, boolean nsfw) { + int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -222,11 +226,12 @@ public class PostViewModel extends ViewModel { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, - int postType, SortType sortType, int filter, boolean nsfw) { + int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -238,12 +243,14 @@ public class PostViewModel extends ViewModel { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } //With subreddit filter public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, - int postType, SortType sortType, int filter, boolean nsfw, List<SubredditFilter> subredditFilterList) { + int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList, + List<SubredditFilter> subredditFilterList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -255,14 +262,14 @@ public class PostViewModel extends ViewModel { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; this.subredditFilterList = subredditFilterList; } //User posts public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, - int postType, SortType sortType, String where, int filter, - boolean nsfw) { + int postType, SortType sortType, String where, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -275,11 +282,12 @@ public class PostViewModel extends ViewModel { userWhere = where; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } public Factory(Retrofit retrofit, String accessToken, String accountName, Locale locale, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, - String query, int postType, SortType sortType, int filter, boolean nsfw) { + String query, int postType, SortType sortType, int filter, boolean nsfw, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -292,6 +300,7 @@ public class PostViewModel extends ViewModel { this.sortType = sortType; this.filter = filter; this.nsfw = nsfw; + this.readPostList = readPostList; } @NonNull @@ -299,16 +308,19 @@ public class PostViewModel extends ViewModel { public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { if (postType == PostDataSource.TYPE_FRONT_PAGE) { return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, - postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw); + postFeedScrolledPositionSharedPreferences, postType, sortType, filter, nsfw, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw); + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, + filter, nsfw, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw, subredditFilterList); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, + filter, nsfw, readPostList, subredditFilterList); } else { return (T) new PostViewModel(retrofit, accessToken, accountName, locale, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, userWhere, filter, nsfw); + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, + userWhere, filter, nsfw, readPostList); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/FetchReadPosts.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java index cfb70789..33ade1f3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/FetchReadPosts.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit.readposts; +package ml.docilealligator.infinityforreddit.readpost; import android.os.AsyncTask; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/InsertReadPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/InsertReadPost.java new file mode 100644 index 00000000..b5d34c6f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/InsertReadPost.java @@ -0,0 +1,34 @@ +package ml.docilealligator.infinityforreddit.readpost; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class InsertReadPost { + public static void insertReadPost(RedditDataRoomDatabase redditDataRoomDatabase, String username, String postId) { + new InsertReadPostAsyncTask(redditDataRoomDatabase, username, postId).execute(); + } + + private static class InsertReadPostAsyncTask extends AsyncTask<Void, Void, Void> { + + private RedditDataRoomDatabase redditDataRoomDatabase; + private String username; + private String postId; + + public InsertReadPostAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String username, String postId) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.username = username; + this.postId = postId; + } + + @Override + protected Void doInBackground(Void... voids) { + ReadPostDao readPostDao = redditDataRoomDatabase.readPostDao(); + if (readPostDao.getReadPostsCount() > 500) { + readPostDao.deleteOldestReadPosts(username); + } + readPostDao.insert(new ReadPost(username, postId)); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/ReadPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPost.java index 27a76b85..df704f3d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/ReadPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPost.java @@ -1,14 +1,16 @@ -package ml.docilealligator.infinityforreddit.readposts; +package ml.docilealligator.infinityforreddit.readpost; import android.os.Parcel; import android.os.Parcelable; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.ForeignKey; import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.post.Post; @Entity(tableName = "read_posts", primaryKeys = {"username", "id"}, foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", @@ -21,6 +23,10 @@ public class ReadPost implements Parcelable { @ColumnInfo(name = "id") private String id; + public static ReadPost convertPost(Post post) { + return new ReadPost("temp", post.getId()); + } + public ReadPost(@NonNull String username, @NonNull String id) { this.username = username; this.id = id; @@ -71,4 +77,17 @@ public class ReadPost implements Parcelable { parcel.writeString(username); parcel.writeString(id); } + + @Override + public boolean equals(@Nullable Object obj) { + if (obj instanceof ReadPost) { + return ((ReadPost) obj).id.equals(id); + } + return false; + } + + @Override + public int hashCode() { + return id.hashCode(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/ReadPostDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java index 1363f14b..d129e109 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readposts/ReadPostDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit.readposts; +package ml.docilealligator.infinityforreddit.readpost; import androidx.room.Dao; import androidx.room.Insert; @@ -15,6 +15,9 @@ public interface ReadPostDao { @Query("SELECT * FROM read_posts WHERE username = :username") List<ReadPost> getAllReadPosts(String username); + @Query("SELECT COUNT(id) FROM read_posts") + int getReadPostsCount(); + @Query("DELETE FROM read_posts WHERE rowid IN (SELECT rowid FROM read_posts LIMIT 100) AND username = :username") void deleteOldestReadPosts(String username); } |