aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-09-15 14:44:34 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-09-15 14:44:34 +0000
commit8a10733d8c8d8f944c40f6a40bcf479e2baceb05 (patch)
treec05e7d5aedacb2a7f7b95d28975af59a07763a77 /app/src/main/java/ml
parent1d0ab483d183dcffbee4f2d64ccc7b10fbca505b (diff)
downloadinfinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar.gz
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar.bz2
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar.lz
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar.xz
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.tar.zst
infinity-for-reddit-8a10733d8c8d8f944c40f6a40bcf479e2baceb05.zip
Use Transformations.map and PagingDataTransforms.filter to remove read posts. It is an ugly design but I cannot think of a better way.
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java12
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java47
3 files changed, 59 insertions, 3 deletions
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<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());
}
}
}
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 1c4565b4..6acd1336 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java
@@ -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
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<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() {