mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-28 11:58:23 +01:00
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:
parent
1d0ab483d1
commit
8a10733d8c
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user