Search in sources :

Example 1 with DocumentChange

use of com.google.firebase.firestore.DocumentChange in project PhotoBlog-Android-Blog-App by akshayejh.

the class HomeFragment method loadMorePost.

public void loadMorePost() {
    if (firebaseAuth.getCurrentUser() != null) {
        Query nextQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).startAfter(lastVisible).limit(3);
        nextQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                if (!documentSnapshots.isEmpty()) {
                    lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                            blog_list.add(blogPost);
                            blogRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });
    }
}
Also used : Query(com.google.firebase.firestore.Query) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) DocumentChange(com.google.firebase.firestore.DocumentChange) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot)

Example 2 with DocumentChange

use of com.google.firebase.firestore.DocumentChange in project PhotoBlog-Android-Blog-App by akshayejh.

the class HomeFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    blog_list = new ArrayList<>();
    blog_list_view = view.findViewById(R.id.blog_list_view);
    firebaseAuth = FirebaseAuth.getInstance();
    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(container.getContext()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);
    if (firebaseAuth.getCurrentUser() != null) {
        firebaseFirestore = FirebaseFirestore.getInstance();
        blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Boolean reachedBottom = !recyclerView.canScrollVertically(1);
                if (reachedBottom) {
                    loadMorePost();
                }
            }
        });
        Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).limit(3);
        firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                if (!documentSnapshots.isEmpty()) {
                    if (isFirstPageFirstLoad) {
                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                        blog_list.clear();
                    }
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                            if (isFirstPageFirstLoad) {
                                blog_list.add(blogPost);
                            } else {
                                blog_list.add(0, blogPost);
                            }
                            blogRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                    isFirstPageFirstLoad = false;
                }
            }
        });
    }
    // Inflate the layout for this fragment
    return view;
}
Also used : Query(com.google.firebase.firestore.Query) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DocumentChange(com.google.firebase.firestore.DocumentChange) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) Point(android.graphics.Point) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) RecyclerView(android.support.v7.widget.RecyclerView)

Example 3 with DocumentChange

use of com.google.firebase.firestore.DocumentChange in project quickstart-android by firebase.

the class FirestoreAdapter method onEvent.

@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
    if (e != null) {
        Log.w(TAG, "onEvent:error", e);
        onError(e);
        return;
    }
    // Dispatch the event
    Log.d(TAG, "onEvent:numChanges:" + documentSnapshots.getDocumentChanges().size());
    for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
        switch(change.getType()) {
            case ADDED:
                onDocumentAdded(change);
                break;
            case MODIFIED:
                onDocumentModified(change);
                break;
            case REMOVED:
                onDocumentRemoved(change);
                break;
        }
    }
    onDataChanged();
}
Also used : DocumentChange(com.google.firebase.firestore.DocumentChange)

Example 4 with DocumentChange

use of com.google.firebase.firestore.DocumentChange in project FirebaseUI-Android by firebase.

the class FirestoreArray method onEvent.

@Override
public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
    if (e != null) {
        notifyOnError(e);
        return;
    }
    // Break down each document event
    List<DocumentChange> changes = snapshots.getDocumentChanges(mMetadataChanges);
    for (DocumentChange change : changes) {
        switch(change.getType()) {
            case ADDED:
                onDocumentAdded(change);
                break;
            case REMOVED:
                onDocumentRemoved(change);
                break;
            case MODIFIED:
                onDocumentModified(change);
                break;
        }
    }
    notifyOnDataChanged();
}
Also used : DocumentChange(com.google.firebase.firestore.DocumentChange)

Aggregations

DocumentChange (com.google.firebase.firestore.DocumentChange)4 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)2 Query (com.google.firebase.firestore.Query)2 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)2 Point (android.graphics.Point)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1