Search in sources :

Example 1 with RecipeNutritionFragmentRecyclerAdapter

use of com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeNutritionFragmentRecyclerAdapter in project EZMeal by Jake-Sokol2.

the class RecipeNutritionFragment 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_nutrition, container, false);
    Bundle extras = getArguments();
    String recipeId = extras.getString("id");
    rvNutrition = (RecyclerView) view.findViewById(R.id.rvNutritionList);
    recipeNutritionFragmentRecyclerAdapter = new RecipeNutritionFragmentRecyclerAdapter(recipeNutritionFragmentModel.getNutritionList());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
    rvNutrition.setAdapter(recipeNutritionFragmentRecyclerAdapter);
    rvNutrition.setLayoutManager(layoutManager);
    // Recyclerview borders
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvNutrition.getContext(), DividerItemDecoration.VERTICAL);
    rvNutrition.addItemDecoration(dividerItemDecoration);
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // retrieve nutrition for current recipe from Room and populate in recyclerview
    nutrition = sqlDb.testDao().getNutrition(recipeId);
    for (int i = 0; i < nutrition.size(); i++) {
        if (nutrition.get(i) != null) {
            // insert into recyclerview
            recipeNutritionFragmentModel.addItem(nutrition.get(i));
        } else {
            i = nutrition.size();
        }
    }
    recipeNutritionFragmentRecyclerAdapter.notifyDataSetChanged();
    return view;
}
Also used : EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) RecyclerView(androidx.recyclerview.widget.RecyclerView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) RecipeNutritionFragmentRecyclerAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeNutritionFragmentRecyclerAdapter)

Example 2 with RecipeNutritionFragmentRecyclerAdapter

use of com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeNutritionFragmentRecyclerAdapter in project EZMeal by Jake-Sokol2.

the class RecipeNutritionFragment 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_nutrition, container, false);
    Bundle extras = getArguments();
    String recipeId = extras.getString("id");
    rvNutrition = (RecyclerView) view.findViewById(R.id.rvNutritionList);
    recipeNutritionFragmentRecyclerAdapter = new RecipeNutritionFragmentRecyclerAdapter(recipeNutritionFragmentModel.getNutritionList());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
    rvNutrition.setAdapter(recipeNutritionFragmentRecyclerAdapter);
    rvNutrition.setLayoutManager(layoutManager);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvNutrition.getContext(), DividerItemDecoration.VERTICAL);
    rvNutrition.addItemDecoration(dividerItemDecoration);
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // todo: RecipesRating
    db.collection("Recipes").document(recipeId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            calories = (String) task.getResult().get("calories");
            protein = (String) task.getResult().get("protein");
            carbohydrates = (String) task.getResult().get("carbohydrates");
            fat = (String) task.getResult().get("fat");
            cholesterol = (String) task.getResult().get("cholesterol");
            sodium = (String) task.getResult().get("sodium");
            // ArrayList<String>) task.getResult().get("nutrition");
            nutrition = new ArrayList<>();
            nutrition.add("Calories - " + calories);
            nutrition.add("Protein - " + protein);
            nutrition.add("Carbohydrates - " + carbohydrates);
            nutrition.add("Fat - " + fat);
            nutrition.add("Cholesterol - " + cholesterol);
            nutrition.add("Sodium - " + sodium);
            for (int i = 0; i < nutrition.size(); i++) {
                if (nutrition.get(i) != null) {
                    recipeNutritionFragmentModel.addItem(nutrition.get(i));
                } else {
                    i = nutrition.size();
                }
            }
            recipeNutritionFragmentRecyclerAdapter.notifyDataSetChanged();
        }
    });
    return view;
}
Also used : EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) RecyclerView(androidx.recyclerview.widget.RecyclerView) RecipeNutritionFragmentRecyclerAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeNutritionFragmentRecyclerAdapter)

Aggregations

Bundle (android.os.Bundle)2 View (android.view.View)2 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 RecipeNutritionFragmentRecyclerAdapter (com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeNutritionFragmentRecyclerAdapter)2 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)2 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)1 ArrayList (java.util.ArrayList)1