Search in sources :

Example 6 with HorizontalRecipe

use of com.example.ezmeal.FindRecipes.FindRecipesModels.HorizontalRecipe in project EZMeal by Jake-Sokol2.

the class CategoryFragmentRepository method retrieveHorizontal.

// returns number of recipes that were actually retrieved
private List<HorizontalRecipe> retrieveHorizontal(RetrievedRecipeLists recipeList, String category, String typeOfRecyclerItem, Task<QuerySnapshot> task) {
    // delete all existing featured recipes in SQL
    deleteAllRecycler2();
    int numRetrieved = 0;
    List<HorizontalRecipe> returnList = new ArrayList<>();
    for (QueryDocumentSnapshot document : task.getResult()) {
        String imageUrl = document.getString("imageUrl");
        String title = document.getString("title");
        String highRatedRecipeId = document.getId();
        Double highRatedRecipeIdDouble = document.getDouble("recipeId");
        Integer highRatedRecipeIdInt = highRatedRecipeIdDouble.intValue();
        Double countRating = document.getDouble("countRating");
        Double avgRating;
        if (countRating != null) {
            avgRating = document.getDouble("rating") / countRating;
        } else {
            countRating = 0.0;
            avgRating = 0.0;
        }
        if (Double.isNaN(avgRating)) {
            avgRating = 0.0;
        }
        Integer totalRating = countRating.intValue();
        int sizeOfSet = recipeList.getSetOfUniqueHorizontalRecipes().size();
        recipeList.addToSetOfUniqueHorizontalRecipes(highRatedRecipeIdInt);
        // the current recipe is not a duplicate - it may be added to the recyclerview
        if ((sizeOfSet != recipeList.getSetOfUniqueHorizontalRecipes().size())) {
            // todo: may need to uncomment and modify class
            // numRetrieved = numRetrieved + 1;
            // viewModel.incrementNumOfRetrievedHighRatedRecipes(1);
            HorizontalRecipe newRecipe = new HorizontalRecipe(title, imageUrl, highRatedRecipeId, avgRating);
            // add this recipe to the most recently added vertical item
            // horizontalLists.add(newRecipe);
            returnList.add(newRecipe);
            RecyclerRecipe2 horizontalRecyclerRecipe2 = new RecyclerRecipe2("RecommendedForYou", highRatedRecipeId, title, imageUrl, avgRating, typeOfRecyclerItem, true, totalRating);
            roomRepository.insertRecycler2(horizontalRecyclerRecipe2);
        // todo: fix this and other inserts to Room
        // insertRecycler2(recyclerRecipePopular2);
        }
    }
    return returnList;
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) HorizontalRecipe(com.example.ezmeal.FindRecipes.FindRecipesModels.HorizontalRecipe) RecyclerRecipe2(com.example.ezmeal.roomDatabase.RecyclerRecipe2)

Aggregations

HorizontalRecipe (com.example.ezmeal.FindRecipes.FindRecipesModels.HorizontalRecipe)6 RecyclerRecipe2 (com.example.ezmeal.roomDatabase.RecyclerRecipe2)4 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)4 ArrayList (java.util.ArrayList)3 RetrievedRecipeLists (com.example.ezmeal.FindRecipes.FindRecipesModels.RetrievedRecipeLists)2 VerticalRecipe (com.example.ezmeal.FindRecipes.FindRecipesModels.VerticalRecipe)2 List (java.util.List)2 Random (java.util.Random)2 ExecutorService (java.util.concurrent.ExecutorService)2 Application (android.app.Application)1 Log (android.util.Log)1 NonNull (androidx.annotation.NonNull)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 CategoryFragmentViewModel (com.example.ezmeal.FindRecipes.FindRecipesViewModels.CategoryFragmentViewModel)1 Recipe (com.example.ezmeal.roomDatabase.Recipe)1 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)1 Task (com.google.android.gms.tasks.Task)1 CollectionReference (com.google.firebase.firestore.CollectionReference)1 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)1 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)1