Search in sources :

Example 1 with Query

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

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);
}
Also used : Query(com.google.firebase.firestore.Query)

Example 3 with Query

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();
}
Also used : Query(com.google.firebase.firestore.Query) RatingAdapter(com.google.firebase.example.fireeats.java.adapter.RatingAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 4 with Query

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;
}
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 5 with Query

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());
}
Also used : FirestorePagingAdapter(com.firebase.ui.firestore.paging.FirestorePagingAdapter) Query(com.google.firebase.firestore.Query) ViewGroup(android.view.ViewGroup) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) MenuItem(android.view.MenuItem) FirestorePagingOptions(com.firebase.ui.firestore.paging.FirestorePagingOptions) NonNull(androidx.annotation.NonNull) LoadState(androidx.paging.LoadState) PagingConfig(androidx.paging.PagingConfig)

Aggregations

Query (com.google.firebase.firestore.Query)5 View (android.view.View)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 DocumentChange (com.google.firebase.firestore.DocumentChange)2 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)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 MenuItem (android.view.MenuItem)1 ViewGroup (android.view.ViewGroup)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 LoadState (androidx.paging.LoadState)1 PagingConfig (androidx.paging.PagingConfig)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 FirestorePagingAdapter (com.firebase.ui.firestore.paging.FirestorePagingAdapter)1 FirestorePagingOptions (com.firebase.ui.firestore.paging.FirestorePagingOptions)1 RatingAdapter (com.google.firebase.example.fireeats.java.adapter.RatingAdapter)1