use of com.google.firebase.firestore.Query 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.Query in project quickstart-android by firebase.
the class MainFragment method onFilter.
@Override
public void onFilter(Filters filters) {
// Construct query basic query
Query query = mFirestore.collection("restaurants");
// Category (equality filter)
if (filters.hasCategory()) {
query = query.whereEqualTo(Restaurant.FIELD_CATEGORY, filters.getCategory());
}
// City (equality filter)
if (filters.hasCity()) {
query = query.whereEqualTo(Restaurant.FIELD_CITY, filters.getCity());
}
// Price (equality filter)
if (filters.hasPrice()) {
query = query.whereEqualTo(Restaurant.FIELD_PRICE, filters.getPrice());
}
// Sort by (orderBy with direction)
if (filters.hasSortBy()) {
query = query.orderBy(filters.getSortBy(), filters.getSortDirection());
}
// Limit items
query = query.limit(LIMIT);
// Update the query
mAdapter.setQuery(query);
// Set header
mBinding.textCurrentSearch.setText(HtmlCompat.fromHtml(filters.getSearchDescription(requireContext()), HtmlCompat.FROM_HTML_MODE_LEGACY));
mBinding.textCurrentSortBy.setText(filters.getOrderDescription(requireContext()));
// Save filters
mViewModel.setFilters(filters);
}
use of com.google.firebase.firestore.Query in project quickstart-android by firebase.
the class RestaurantDetailFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mBinding.restaurantButtonBack.setOnClickListener(this);
mBinding.fabShowRatingDialog.setOnClickListener(this);
String restaurantId = RestaurantDetailFragmentArgs.fromBundle(getArguments()).getKeyRestaurantId();
// Initialize Firestore
mFirestore = FirebaseFirestore.getInstance();
// Get reference to the restaurant
mRestaurantRef = mFirestore.collection("restaurants").document(restaurantId);
// Get ratings
Query ratingsQuery = mRestaurantRef.collection("ratings").orderBy("timestamp", Query.Direction.DESCENDING).limit(50);
// RecyclerView
mRatingAdapter = new RatingAdapter(ratingsQuery) {
@Override
protected void onDataChanged() {
if (getItemCount() == 0) {
mBinding.recyclerRatings.setVisibility(View.GONE);
mBinding.viewEmptyRatings.setVisibility(View.VISIBLE);
} else {
mBinding.recyclerRatings.setVisibility(View.VISIBLE);
mBinding.viewEmptyRatings.setVisibility(View.GONE);
}
}
};
mBinding.recyclerRatings.setLayoutManager(new LinearLayoutManager(requireContext()));
mBinding.recyclerRatings.setAdapter(mRatingAdapter);
mRatingDialog = new RatingDialogFragment();
}
use of com.google.firebase.firestore.Query 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.Query in project FirebaseUI-Android by firebase.
the class FirestorePagingActivity method setUpAdapter.
private void setUpAdapter() {
Query baseQuery = mItemsCollection.orderBy("value", Query.Direction.ASCENDING);
PagingConfig config = new PagingConfig(20, 10, false);
FirestorePagingOptions<Item> options = new FirestorePagingOptions.Builder<Item>().setLifecycleOwner(this).setQuery(baseQuery, config, Item.class).build();
final FirestorePagingAdapter<Item, ItemViewHolder> adapter = new FirestorePagingAdapter<Item, ItemViewHolder>(options) {
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
return new ItemViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
holder.bind(model);
}
};
adapter.addLoadStateListener(states -> {
LoadState refresh = states.getRefresh();
LoadState append = states.getAppend();
if (refresh instanceof LoadState.Error || append instanceof LoadState.Error) {
showToast("An error occurred.");
adapter.retry();
}
if (append instanceof LoadState.Loading) {
mBinding.swipeRefreshLayout.setRefreshing(true);
}
if (append instanceof LoadState.NotLoading) {
LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
if (notLoading.getEndOfPaginationReached()) {
// This indicates that the user has scrolled
// until the end of the data set.
mBinding.swipeRefreshLayout.setRefreshing(false);
showToast("Reached end of data set.");
return null;
}
if (refresh instanceof LoadState.NotLoading) {
// This indicates the most recent load
// has finished.
mBinding.swipeRefreshLayout.setRefreshing(false);
return null;
}
}
return null;
});
mBinding.pagingRecycler.setLayoutManager(new LinearLayoutManager(this));
mBinding.pagingRecycler.setAdapter(adapter);
mBinding.swipeRefreshLayout.setOnRefreshListener(() -> adapter.refresh());
}
Aggregations