From 474b3cf32f53818d5ce430119830052e27f17387 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 17 Sep 2021 21:06:53 +0800 Subject: Fix hiding read posts automatically. --- .../adapters/PostRecyclerViewAdapter.java | 2 - .../infinityforreddit/fragments/PostFragment.java | 53 ++++++++--------- .../infinityforreddit/post/PostViewModel.java | 68 +++++++++++++--------- 3 files changed, 65 insertions(+), 58 deletions(-) (limited to 'app/src') 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 1fd1664b..e8935f5f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -194,7 +194,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter readPostList) { this.executor = executor; this.retrofit = retrofit; @@ -86,13 +88,14 @@ public class PostViewModel extends ViewModel { postPagingData, executor, post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(false); + currentlyReadPostIdsLiveData.setValue(postHistorySharedPreferences != null + && postHistorySharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.HIDE_READ_POSTS_AUTOMATICALLY_BASE, false)); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, int postType, SortType sortType, PostFilter postFilter, - List readPostList) { + @Nullable SharedPreferences postHistorySharedPreferences, String subredditName, int postType, + SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -127,12 +130,14 @@ public class PostViewModel extends ViewModel { postPagingData, executor, post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(false); + currentlyReadPostIdsLiveData.setValue(postHistorySharedPreferences != null + && postHistorySharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.HIDE_READ_POSTS_AUTOMATICALLY_BASE, false)); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, String username, + SharedPreferences postFeedScrolledPositionSharedPreferences, + @Nullable SharedPreferences postHistorySharedPreferences, String username, int postType, SortType sortType, PostFilter postFilter, String userWhere, List readPostList) { this.executor = executor; @@ -170,13 +175,15 @@ public class PostViewModel extends ViewModel { postPagingData, executor, post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(false); + currentlyReadPostIdsLiveData.setValue(postHistorySharedPreferences != null + && postHistorySharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.HIDE_READ_POSTS_AUTOMATICALLY_BASE, false)); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, String query, String trendingSource, int postType, SortType sortType, - PostFilter postFilter, List readPostList) { + @Nullable SharedPreferences postHistorySharedPreferences, String subredditName, String query, + String trendingSource, int postType, SortType sortType, PostFilter postFilter, + List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -213,7 +220,8 @@ public class PostViewModel extends ViewModel { postPagingData, executor, post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(false); + currentlyReadPostIdsLiveData.setValue(postHistorySharedPreferences != null + && postHistorySharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.HIDE_READ_POSTS_AUTOMATICALLY_BASE, false)); } public LiveData> getPosts() { @@ -274,6 +282,7 @@ public class PostViewModel extends ViewModel { private String accountName; private SharedPreferences sharedPreferences; private SharedPreferences postFeedScrolledPositionSharedPreferences; + private SharedPreferences postHistorySharedPreferences; private String name; private String query; private String trendingSource; @@ -285,13 +294,15 @@ public class PostViewModel extends ViewModel { public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - int postType, SortType sortType, PostFilter postFilter, List readPostList) { + SharedPreferences postHistorySharedPreferences, int postType, SortType sortType, + PostFilter postFilter, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postHistorySharedPreferences = postHistorySharedPreferences; this.postType = postType; this.sortType = sortType; this.postFilter = postFilter; @@ -300,13 +311,15 @@ public class PostViewModel extends ViewModel { public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, int postType, SortType sortType, PostFilter postFilter, - List readPostList) {this.executor = executor; + SharedPreferences postHistorySharedPreferences, String name, int postType, SortType sortType, + PostFilter postFilter, List readPostList) { + this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postHistorySharedPreferences = postHistorySharedPreferences; this.name = name; this.postType = postType; this.sortType = sortType; @@ -317,14 +330,15 @@ public class PostViewModel extends ViewModel { //User posts public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String username, int postType, SortType sortType, PostFilter postFilter, String where, - List readPostList) { + SharedPreferences postHistorySharedPreferences, String username, int postType, + SortType sortType, PostFilter postFilter, String where, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postHistorySharedPreferences = postHistorySharedPreferences; this.name = username; this.postType = postType; this.sortType = sortType; @@ -335,14 +349,15 @@ public class PostViewModel extends ViewModel { public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String name, String query, String trendingSource, int postType, SortType sortType, - PostFilter postFilter, List readPostList) { + SharedPreferences postHistorySharedPreferences, String name, String query, String trendingSource, + int postType, SortType sortType, PostFilter postFilter, List readPostList) { this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; + this.postHistorySharedPreferences = postHistorySharedPreferences; this.name = name; this.query = query; this.trendingSource = trendingSource; @@ -369,23 +384,24 @@ public class PostViewModel extends ViewModel { public T create(@NonNull Class modelClass) { if (postType == PostPagingSource.TYPE_FRONT_PAGE) { return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); + postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, postType, + sortType, postFilter, readPostList); } else if (postType == PostPagingSource.TYPE_SEARCH) { return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, query, trendingSource, postType, sortType, - postFilter, readPostList); + postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, name, query, + trendingSource, postType, sortType, postFilter, readPostList); } else if (postType == PostPagingSource.TYPE_SUBREDDIT || postType == PostPagingSource.TYPE_MULTI_REDDIT) { return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, postType, sortType, - postFilter, readPostList); + postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, name, + postType, sortType, postFilter, readPostList); } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { return (T) new PostViewModel(executor, retrofit, null, null, sharedPreferences, - null, name, postType, sortType, + null, null, name, postType, sortType, postFilter, null); } else { return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, name, postType, sortType, - postFilter, userWhere, readPostList); + postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, name, + postType, sortType, postFilter, userWhere, readPostList); } } } -- cgit v1.2.3