Search in sources :

Example 1 with Item

use of com.example.ezmeal.GroupLists.Model.Item 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);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvIngredients.getContext(), DividerItemDecoration.VERTICAL);
    rvIngredients.addItemDecoration(dividerItemDecoration);
    Bundle extras = getArguments();
    String recipeId = extras.getString("id");
    // todo: RecipesRating
    db.collection("Recipes").document(recipeId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            ingredients = (ArrayList<String>) task.getResult().get("ingredients");
            for (int i = 0; i < ingredients.size(); i++) {
                if (!Objects.equals(ingredients.get(i), "")) {
                    recipeIngredientsFragmentModel.addItem(ingredients.get(i));
                } else {
                    i = ingredients.size();
                }
            }
            recipeIngredientsFragmentRecyclerAdapter.notifyDataSetChanged();
        }
    });
    // ingredientsArray = extras.getStringArrayList("ingredients");
    /*EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user")
                .allowMainThreadQueries().fallbackToDestructiveMigration().build();

        ingredients = sqlDb.testDao().getIngredients(recipeId);

        for (int i = 0; i < ingredients.size(); i++)
        {
            if (ingredients.get(i) != null)
            {
                ingredientsModel.addItem(ingredients.get(i));
            }
            else
            {
                i = ingredients.size();
            }
        }
        ingredientsRecyclerAdapter.notifyDataSetChanged();*/
    // add all ingredients for this recipe to the user's Item collection in Firestore
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // 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();
                                }
                            }
                        /*if (task.isSuccessful())
                                {
                                    for (DocumentSnapshot document : task.getResult())
                                    {
                                        // if ingredient isn't already added to database, add it now
                                        if (!document.exists())
                                        {
                                            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;

                                                }
                                            }).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();
                                    }
                                }
                                else
                                {
                                    Log.i("q", "task not successful");
                                }*/
                        }
                    }).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 : EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) ArrayList(java.util.ArrayList) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) Item(com.example.ezmeal.GroupLists.Model.Item) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) NonNull(androidx.annotation.NonNull) DocumentReference(com.google.firebase.firestore.DocumentReference) RecipeIngredientsFragmentRecyclerAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeIngredientsFragmentRecyclerAdapter) Bundle(android.os.Bundle) FirebaseUser(com.google.firebase.auth.FirebaseUser) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) RecyclerView(androidx.recyclerview.widget.RecyclerView) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 2 with Item

use of com.example.ezmeal.GroupLists.Model.Item 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)

Aggregations

Bundle (android.os.Bundle)2 View (android.view.View)2 NonNull (androidx.annotation.NonNull)2 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 Item (com.example.ezmeal.GroupLists.Model.Item)2 RecipeIngredientsFragmentRecyclerAdapter (com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeIngredientsFragmentRecyclerAdapter)2 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 CollectionReference (com.google.firebase.firestore.CollectionReference)2 DocumentReference (com.google.firebase.firestore.DocumentReference)2 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)2 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)1 ArrayList (java.util.ArrayList)1