I'm trying to show all the posts in the database except the users I follow & my posts. Here is what I've done so far:
public final List<Post> mDefaultPost = new ArrayList<>();
public void readDefaultPosts()
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mDefaultPost.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (snapshot.child("ingrediant").getValue() != null) {
ArrayList<String> mList = new ArrayList();
for (DataSnapshot ds : snapshot.child("ingrediant").getChildren()) {
mList.add(ds.getKey() + ":" + ds.getValue());
}
post.setIngrediantsList(mList);
}
post.setVideo((Boolean) snapshot.child("isVideo").getValue());
if(!post.getPublisher().equals(firebaseUser.getUid()))
{
mDefaultPost.add(post);
}
}
postDefaultAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
currently, it shows all posts except the current user's posts which is great, but I want to exclude the posts of the users I follow also. How can I do that?
To make it easier for you to help me, Here is how I show the posts of the users I follow on the home page:
private void checkFollowing()
{
followingList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
followingList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
followingList.add(snapshot.getKey());
}
readPost();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
private void readPost()
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
Post post = snapshot.getValue(Post.class);
for(String id : followingList)
{
if(post.getPublisher().equals(id))
{
if (snapshot.child("ingrediant").getValue() != null){
ArrayList<String> mList = new ArrayList();
for (DataSnapshot ds:snapshot.child("ingrediant").getChildren()){
mList.add(ds.getKey()+":"+ds.getValue());
}
post.setIngrediantsList(mList);
}
post.setVideo((Boolean) snapshot.child("isVideo").getValue());
postList.add(post);
}
}
}
postAdatper.notifyDataSetChanged();
}
I'm struggling to combine the two code snippet to achieve the result I want. please help me