Search in sources :

Example 6 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project EZMeal by Jake-Sokol2.

the class RecipeIngredientsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_recipe_ingredients, container, false);
    rvIngredients = (RecyclerView) view.findViewById(R.id.rvIngredientList);
    recipeIngredientsFragmentRecyclerAdapter = new RecipeIngredientsFragmentRecyclerAdapter(recipeIngredientsFragmentModel.getIngredients());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
    rvIngredients.setAdapter(recipeIngredientsFragmentRecyclerAdapter);
    rvIngredients.setLayoutManager(layoutManager);
    // Recyclerview borders
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvIngredients.getContext(), DividerItemDecoration.VERTICAL);
    rvIngredients.addItemDecoration(dividerItemDecoration);
    Bundle extras = getArguments();
    String recipeId = extras.getString("id");
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // retrieve ingredients for current recipe from Room and populate in recyclerview
    ingredients = sqlDb.testDao().getIngredients(recipeId);
    for (int i = 0; i < ingredients.size(); i++) {
        if (ingredients.get(i) != null) {
            // insert into recyclerview
            recipeIngredientsFragmentModel.addItem(ingredients.get(i));
        } else {
            i = ingredients.size();
        }
    }
    recipeIngredientsFragmentRecyclerAdapter.notifyDataSetChanged();
    // add all ingredients for this recipe to the user's Item collection in Firestore
    btnAddToList = view.findViewById(R.id.btnAddToList);
    btnAddToList.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // in case user edited ingredient list, re-read ingredients from database
            ingredients = sqlDb.testDao().getIngredients(recipeId);
            FirebaseUser mCurrentUser = mAuth.getCurrentUser();
            String email = mCurrentUser.getEmail();
            for (int i = 0; i < ingredients.size(); i++) {
                if (ingredients.get(i) != null) {
                    mAuth = FirebaseAuth.getInstance();
                    CollectionReference dbItems = db.collection("Items");
                    Item item = new Item(ingredients.get(i), null, email);
                    // prevent user from adding same list of ingredients twice
                    dbItems.whereEqualTo("name", ingredients.get(i)).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.getResult().isEmpty()) {
                                dbItems.add(item).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {

                                    @Override
                                    public void onSuccess(DocumentReference documentReference) {
                                        // keep track that an item was added so we can tell the user with a toast later
                                        itemAdded = true;
                                        Toast.makeText(getContext(), "Item added", Toast.LENGTH_SHORT).show();
                                    }
                                }).addOnFailureListener(new OnFailureListener() {

                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        e.printStackTrace();
                                    }
                                });
                                // if any items were actually added, tell the user
                                if (itemAdded) {
                                    Toast.makeText(getContext(), "Item added", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }).addOnFailureListener(new OnFailureListener() {

                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.i("q", "get failed");
                        }
                    });
                } else {
                    // escape loop if rest of ingredients list is null (nulls are a symptom of database insertion)
                    i = ingredients.size();
                }
            }
            btnAddToList.setEnabled(false);
            btnAddToList.setTextColor(Color.parseColor("#808080"));
        }
    });
    return view;
}
Also used : RecipeIngredientsFragmentRecyclerAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeIngredientsFragmentRecyclerAdapter) EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) FirebaseUser(com.google.firebase.auth.FirebaseUser) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) Item(com.example.ezmeal.GroupLists.Model.Item) NonNull(androidx.annotation.NonNull) RecyclerView(androidx.recyclerview.widget.RecyclerView) DocumentReference(com.google.firebase.firestore.DocumentReference) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 7 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project House-Organizer by House-Organizer.

the class FirestoreTask method storeTaskList.

// Might be unnecessary in the future
public static void storeTaskList(TaskList taskList, CollectionReference taskListRoot, String documentName) throws ExecutionException, InterruptedException {
    Map<String, Object> data = new HashMap<>();
    data.put("title", taskList.getTitle());
    data.put("owner", taskList.getOwner().uid());
    com.google.android.gms.tasks.Task<Void> task = taskListRoot.document(documentName).set(data);
    Tasks.await(task);
    if (task.isSuccessful()) {
        DocumentReference documentReference = taskListRoot.document(documentName);
        CollectionReference taskListRef = documentReference.collection("tasks");
        for (Task t : taskList.getTasks()) {
            Tasks.await(storeTask(t, taskListRef));
        }
    }
}
Also used : HashMap(java.util.HashMap) DocumentReference(com.google.firebase.firestore.DocumentReference) CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 8 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method compoundQueries.

public void compoundQueries() {
    CollectionReference citiesRef = db.collection("cities");
    // [START chain_filters]
    citiesRef.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
    citiesRef.whereEqualTo("state", "CA").whereLessThan("population", 1000000);
    // [END chain_filters]
    // [START valid_range_filters]
    citiesRef.whereGreaterThanOrEqualTo("state", "CA").whereLessThanOrEqualTo("state", "IN");
    citiesRef.whereEqualTo("state", "CA").whereGreaterThan("population", 1000000);
// [END valid_range_filters]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 9 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method orderAndLimit.

public void orderAndLimit() {
    CollectionReference citiesRef = db.collection("cities");
    // [START order_and_limit]
    citiesRef.orderBy("name").limit(3);
    // [END order_and_limit]
    // [START order_and_limit_desc]
    citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
    // [END order_and_limit_desc]
    // [START order_by_multiple]
    citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
    // [END order_by_multiple]
    // [START filter_and_order]
    citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
    // [END filter_and_order]
    // [START valid_filter_and_order]
    citiesRef.whereGreaterThan("population", 100000).orderBy("population");
// [END valid_filter_and_order]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 10 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method compoundQueriesInvalid.

public void compoundQueriesInvalid() {
    CollectionReference citiesRef = db.collection("cities");
    // [START invalid_range_filters]
    citiesRef.whereGreaterThanOrEqualTo("state", "CA").whereGreaterThan("population", 100000);
// [END invalid_range_filters]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Aggregations

CollectionReference (com.google.firebase.firestore.CollectionReference)43 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)15 Intent (android.content.Intent)14 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)14 View (android.view.View)13 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)13 NonNull (androidx.annotation.NonNull)10 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 DocumentReference (com.google.firebase.firestore.DocumentReference)10 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)10 ArrayList (java.util.ArrayList)8 Bundle (android.os.Bundle)7 ListView (android.widget.ListView)7 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)7 AdapterView (android.widget.AdapterView)6 TextView (android.widget.TextView)6 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)5 Task (com.google.android.gms.tasks.Task)5 List (java.util.List)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)4