Search in sources :

Example 1 with RestaurantAdapter

use of com.google.firebase.example.fireeats.java.adapter.RestaurantAdapter in project quickstart-android by firebase.

the class MainFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mBinding.filterBar.setOnClickListener(this);
    mBinding.buttonClearFilter.setOnClickListener(this);
    // View model
    mViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);
    // Firestore
    mFirestore = FirebaseFirestore.getInstance();
    // Get ${LIMIT} restaurants
    mQuery = mFirestore.collection("restaurants").orderBy("avgRating", Query.Direction.DESCENDING).limit(LIMIT);
    // RecyclerView
    mAdapter = new RestaurantAdapter(mQuery, this) {

        @Override
        protected void onDataChanged() {
            // Show/hide content if the query returns empty.
            if (getItemCount() == 0) {
                mBinding.recyclerRestaurants.setVisibility(View.GONE);
                mBinding.viewEmpty.setVisibility(View.VISIBLE);
            } else {
                mBinding.recyclerRestaurants.setVisibility(View.VISIBLE);
                mBinding.viewEmpty.setVisibility(View.GONE);
            }
        }

        @Override
        protected void onError(FirebaseFirestoreException e) {
            // Show a snackbar on errors
            Snackbar.make(mBinding.getRoot(), "Error: check logs for info.", Snackbar.LENGTH_LONG).show();
        }
    };
    mBinding.recyclerRestaurants.setLayoutManager(new LinearLayoutManager(requireContext()));
    mBinding.recyclerRestaurants.setAdapter(mAdapter);
    // Filter Dialog
    mFilterDialog = new FilterDialogFragment();
}
Also used : MainActivityViewModel(com.google.firebase.example.fireeats.java.viewmodel.MainActivityViewModel) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RestaurantAdapter(com.google.firebase.example.fireeats.java.adapter.RestaurantAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RestaurantAdapter (com.google.firebase.example.fireeats.java.adapter.RestaurantAdapter)1 MainActivityViewModel (com.google.firebase.example.fireeats.java.viewmodel.MainActivityViewModel)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1