Use Transformations.map and PagingDataTransforms.filter to remove read posts. It is an ugly design but I cannot think of a better way.

This commit is contained in:
Alex Ning 2021-09-15 22:44:34 +08:00
parent 1d0ab483d1
commit 8a10733d8c
3 changed files with 59 additions and 3 deletions

View File

@ -2636,6 +2636,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) {
((MarkPostAsReadInterface) mActivity).markPostAsRead(post);
}
mFragment.addCurrentlyReadPostId(post.getId());
}
}
}
@ -3620,6 +3621,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) {
((MarkPostAsReadInterface) mActivity).markPostAsRead(post);
}
mFragment.addCurrentlyReadPostId(post.getId());
}
}
}
@ -3854,6 +3856,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) {
((MarkPostAsReadInterface) mActivity).markPostAsRead(post);
}
mFragment.addCurrentlyReadPostId(post.getId());
}
}
}

View File

@ -55,9 +55,11 @@ import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@ -250,6 +252,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private float swipeActionThreshold;
private ItemTouchHelper touchHelper;
private ArrayList<ReadPost> readPosts;
private Set<String> currentlyReadPostIds = new HashSet<>();
private Unbinder unbinder;
private Map<String, String> 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

View File

@ -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<ReadPost> readPostList;
private MutableLiveData<Set<String>> currentlyReadPostIdsLiveData = new MutableLiveData<>();
private LiveData<PagingData<Post>> posts;
private LiveData<PagingData<Post>> postsWithReadPostsHidden;
private MutableLiveData<SortType> sortTypeLiveData;
private MutableLiveData<PostFilter> 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<PagingData<Post>> getPosts() {
return posts;
return postsWithReadPostsHidden;
}
public void setCurrentlyReadPostIds(Set<String> currentlyReadPostIds) {
currentlyReadPostIdsLiveData.setValue(currentlyReadPostIds);
}
public PostPagingSource returnPagingSoruce() {