Skip to content Skip to sidebar Skip to footer

Firebase See Friends Posts

I want to populate all posts of user and their friends. My Database implementation is attached User Implementation Posts Implementation Text Json:- { 'UserTable' : { 'Khusb

Solution 1:

I am now implementing the above in this way. If anyone is having a better way to implement please post. first checking 20 posts and then on scroll from the user fetching next data set.

postQuery=posts.orderByKey().startAt("").limitToLast(20);

        friends.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot friendSnap) {
                for(DataSnapshot snap:friendSnap.getChildren()) {
                    friendStringList.add(snap.getValue(String.class));
                }
                postQuery.addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot postSnap) {
                        postsTest.clear();
                        int count=0;
                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                            if(count==0) {
                                start = snapshot.getKey();
                                count++;
                            }
                           // start=snapshot.getKey();
                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                    postsTest.add(snapshot.getValue(PostPOJO.class));
                            }
                        }
                        Collections.reverse(postsTest);
                        hide();
                        count=0;
                        postAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
            {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy)
                {
                    if(dy > 0) //check for scroll down
                    {
                        visibleItemCount = mLayoutManager.getChildCount();
                        totalItemCount = mLayoutManager.getItemCount();
                        pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                        if (loading)
                        {
                            if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                            {
                                loading = false;
                                Log.v("...", "Last Item Wow !");

                                //Do pagination.. i.e. fetch new data
                                show();
                                postQuery=posts.orderByKey().endAt(start).limitToLast(20);

                                postQuery.addValueEventListener(new ValueEventListener() {

                                    @Override
                                    public void onDataChange(DataSnapshot postSnap) {
                                        postsList.clear();
                                        int count=0;
                                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                                            if(count==0) {
                                                start = snapshot.getKey();
                                                count++;
                                            }
                                            //start=snapshot.getKey();
                                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                                postsList.add(snapshot.getValue(PostPOJO.class));
                                                loading=true;

                                            }
                                            else if(snapshot.getKey().equals(FirstPostKey)){
                                                loading=false;
                                                Snackbar.make(mView,"Reached End",Snackbar.LENGTH_LONG).show();
                                            }
                                        }
                                        postsList.remove(postsList.size()-1);
                                        Collections.reverse(postsList);
                                        postsTest.addAll(postsList);
                                        hide();
                                        count=0;
                                        postAdapter.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });


                            }
                        }
                    }
                }
            });

Post a Comment for "Firebase See Friends Posts"