From 50a357267985b14894e275ef515778f33b3023af Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 16 Sep 2021 23:26:56 +0800 Subject: No need to pass a HashSet to hide read posts. --- .../infinityforreddit/fragments/PostFragment.java | 4 +++- .../infinityforreddit/post/PostViewModel.java | 23 +++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'app/src/main/java/ml') 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 768d3672..307bbd52 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -49,6 +49,7 @@ import androidx.transition.TransitionManager; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import com.google.common.collect.Lists; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -871,6 +872,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postFilter.allowNSFW = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.NSFW_BASE, false); } this.readPosts = readPostList; + currentlyReadPostIds.addAll(Lists.transform(readPosts, ReadPost::getId)); initializeAndBindPostViewModel(accessToken); } }); @@ -1494,7 +1496,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter.prepareToHideReadPosts(); refreshAdapter(); }*/ - mPostViewModel.setCurrentlyReadPostIds(currentlyReadPostIds); + mPostViewModel.hideReadPosts(); } @Override 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 09cfc0f5..4f9a3890 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -17,7 +17,6 @@ import androidx.paging.PagingData; import androidx.paging.PagingDataTransforms; import androidx.paging.PagingLiveData; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.Executor; @@ -42,7 +41,7 @@ public class PostViewModel extends ViewModel { private PostFilter postFilter; private String userWhere; private List readPostList; - private MutableLiveData> currentlyReadPostIdsLiveData = new MutableLiveData<>(); + private MutableLiveData currentlyReadPostIdsLiveData = new MutableLiveData<>(); private LiveData> posts; private LiveData> postsWithReadPostsHidden; @@ -85,9 +84,9 @@ public class PostViewModel extends ViewModel { posts, postPagingData -> PagingDataTransforms.filter( postPagingData, executor, - post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(new HashSet<>()); + currentlyReadPostIdsLiveData.setValue(false); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -126,9 +125,9 @@ public class PostViewModel extends ViewModel { posts, postPagingData -> PagingDataTransforms.filter( postPagingData, executor, - post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(new HashSet<>()); + currentlyReadPostIdsLiveData.setValue(false); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -169,9 +168,9 @@ public class PostViewModel extends ViewModel { posts, postPagingData -> PagingDataTransforms.filter( postPagingData, executor, - post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(new HashSet<>()); + currentlyReadPostIdsLiveData.setValue(false); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -212,17 +211,17 @@ public class PostViewModel extends ViewModel { posts, postPagingData -> PagingDataTransforms.filter( postPagingData, executor, - post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + post -> !post.isRead() || !currentlyReadPostIdsLiveData.getValue()))), ViewModelKt.getViewModelScope(this)); - currentlyReadPostIdsLiveData.setValue(new HashSet<>()); + currentlyReadPostIdsLiveData.setValue(false); } public LiveData> getPosts() { return postsWithReadPostsHidden; } - public void setCurrentlyReadPostIds(Set currentlyReadPostIds) { - currentlyReadPostIdsLiveData.setValue(currentlyReadPostIds); + public void hideReadPosts() { + currentlyReadPostIdsLiveData.setValue(true); } public PostPagingSource returnPagingSoruce() { -- cgit v1.2.3