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() {