From 8a10733d8c8d8f944c40f6a40bcf479e2baceb05 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 15 Sep 2021 22:44:34 +0800 Subject: Use Transformations.map and PagingDataTransforms.filter to remove read posts. It is an ugly design but I cannot think of a better way. --- .../adapters/PostRecyclerViewAdapter.java | 3 ++ .../infinityforreddit/fragments/PostFragment.java | 12 +++++- .../infinityforreddit/post/PostViewModel.java | 47 +++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) (limited to 'app/src/main') 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 d7d7616d..39762f79 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2636,6 +2636,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter readPosts; + private Set currentlyReadPostIds = new HashSet<>(); private Unbinder unbinder; private Map subredditOrUserIcons = new HashMap<>(); @@ -1317,6 +1320,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + public void addCurrentlyReadPostId(String id) { + currentlyReadPostIds.add(id); + } + @Override public void changeNSFW(boolean nsfw) { postFilter.allowNSFW = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && nsfw; @@ -1473,10 +1480,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void hideReadPosts() { - if (mAdapter != null) { + /*if (mAdapter != null) { mAdapter.prepareToHideReadPosts(); refreshAdapter(); - } + }*/ + mPostViewModel.setCurrentlyReadPostIds(currentlyReadPostIds); } @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 0f0b366f..09cfc0f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -14,9 +14,12 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.Pager; import androidx.paging.PagingConfig; 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; import ml.docilealligator.infinityforreddit.SortType; @@ -39,8 +42,10 @@ public class PostViewModel extends ViewModel { private PostFilter postFilter; private String userWhere; private List readPostList; + private MutableLiveData> currentlyReadPostIdsLiveData = new MutableLiveData<>(); private LiveData> posts; + private LiveData> postsWithReadPostsHidden; private MutableLiveData sortTypeLiveData; private MutableLiveData postFilterLiveData; @@ -74,6 +79,15 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + + postsWithReadPostsHidden = PagingLiveData.cachedIn(Transformations.switchMap(currentlyReadPostIdsLiveData, + currentlyReadPostIds -> Transformations.map( + posts, + postPagingData -> PagingDataTransforms.filter( + postPagingData, executor, + post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + + currentlyReadPostIdsLiveData.setValue(new HashSet<>()); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -106,6 +120,15 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + + postsWithReadPostsHidden = PagingLiveData.cachedIn(Transformations.switchMap(currentlyReadPostIdsLiveData, + currentlyReadPostIds -> Transformations.map( + posts, + postPagingData -> PagingDataTransforms.filter( + postPagingData, executor, + post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + + currentlyReadPostIdsLiveData.setValue(new HashSet<>()); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -140,6 +163,15 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + + postsWithReadPostsHidden = PagingLiveData.cachedIn(Transformations.switchMap(currentlyReadPostIdsLiveData, + currentlyReadPostIds -> Transformations.map( + posts, + postPagingData -> PagingDataTransforms.filter( + postPagingData, executor, + post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + + currentlyReadPostIdsLiveData.setValue(new HashSet<>()); } public PostViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, @@ -174,10 +206,23 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.getValue(), postFilterLiveData.getValue()); return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); }); + + postsWithReadPostsHidden = PagingLiveData.cachedIn(Transformations.switchMap(currentlyReadPostIdsLiveData, + currentlyReadPostIds -> Transformations.map( + posts, + postPagingData -> PagingDataTransforms.filter( + postPagingData, executor, + post -> !currentlyReadPostIds.contains(post.getId())))), ViewModelKt.getViewModelScope(this)); + + currentlyReadPostIdsLiveData.setValue(new HashSet<>()); } public LiveData> getPosts() { - return posts; + return postsWithReadPostsHidden; + } + + public void setCurrentlyReadPostIds(Set currentlyReadPostIds) { + currentlyReadPostIdsLiveData.setValue(currentlyReadPostIds); } public PostPagingSource returnPagingSoruce() { -- cgit v1.2.3