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();
}
}
}
}
});
}
}
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;
}
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();
}
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();
}
Aggregations