Search in sources :

Example 6 with EZMealDatabase

use of com.example.ezmeal.roomDatabase.EZMealDatabase 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 EZMealDatabase

use of com.example.ezmeal.roomDatabase.EZMealDatabase in project EZMeal by Jake-Sokol2.

the class CategoryFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_find_recipes_specific_recipe, container, false);
    bottomNav = (BottomNavigationView) getActivity().findViewById(R.id.bottomNavigationView);
    txtTitle = (TextView) view.findViewById(R.id.txtmyRecipesTitle);
    String categoryName = null;
    Bundle extras = getArguments();
    if (extras != null) {
        // retrieve category name from the Intent
        categoryName = extras.getString("category");
        txtTitle.setText(categoryName);
    }
    rvGroupList = (RecyclerView) view.findViewById(R.id.rvMyRecipes);
    adapter = new CategoryFragmentAdapter(categoryFragmentModel.getRecipeList(), categoryFragmentModel.getUrlList());
    rvGroupList.setAdapter(adapter);
    StaggeredGridLayoutManager staggeredManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    // GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
    /*
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                if (position == 6)
                {
                    return 3;
                }
                else
                {
                    return 1;
                }
            }
        });*/
    rvGroupList.setLayoutManager(staggeredManager);
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // get recipeId, pathToImage, and title for recipes of a specific category
    List<recipePathTitle> items = sqlDb.testDao().getCategoryRecipes(categoryName);
    recipeId = new ArrayList<String>();
    for (int i = 0; i < items.size(); i++) {
        categoryFragmentModel.addItem(items.get(i).getTitle(), items.get(i).getPathToImage());
        // keep list of recipeId's in memory so we know which recipeId to pass to the next fragment when the user clicks on a recipe
        recipeId.add(items.get(i).getRecipeId());
    }
    adapter.notifyDataSetChanged();
    adapter.setOnItemClickListener(new CategoryFragmentAdapter.MainAdapterListener() {

        @Override
        public void onItemClick(int position, CardView cardView) {
            Bundle bundle = new Bundle();
            // pass recipeId to specific recipe page so that it knows which recipe to use
            bundle.putString("id", recipeId.get(position));
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_myRecipesSpecificCategoryFragment_to_myRecipesSpecificRecipeFragment, bundle, new NavOptions.Builder().setEnterAnim(R.anim.slide_in).setExitAnim(R.anim.stall).setPopExitAnim(R.anim.slide_out).build());
        }
    });
    return view;
}
Also used : CategoryFragmentAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.CategoryFragmentAdapter) EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) CardView(androidx.cardview.widget.CardView) NavController(androidx.navigation.NavController) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) com.example.ezmeal.roomDatabase.recipePathTitle(com.example.ezmeal.roomDatabase.recipePathTitle) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 8 with EZMealDatabase

use of com.example.ezmeal.roomDatabase.EZMealDatabase in project EZMeal by Jake-Sokol2.

the class RecipeFragment 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_my_recipes_specific_recipe, container, false);
    Bundle extras = getArguments();
    recipeId = extras.getString("id");
    ImageView imageRecipe = view.findViewById(R.id.imageRecipeImage);
    TextView txtRecipeTitle = view.findViewById(R.id.txtRecipeTitle);
    String queryString = "";
    String imageUrl = "";
    boolean containsCondition = false;
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    RecipeCategoryTuple recipeCategoryTuple = sqlDb.testDao().getSpecificCategoryItems(recipeId);
    // insert text and image from Room database query into the ImageView
    txtRecipeTitle.setText(recipeCategoryTuple.getTitle());
    Glide.with(getContext()).load(Uri.parse(recipeCategoryTuple.getPathToImage())).into(imageRecipe);
    confirmChoice = false;
    // listener for deleting recipes from My Recipes
    btnDeleteFromMyRecipes = view.findViewById(R.id.btnDeleteFromMyRecipes);
    btnDeleteFromMyRecipes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Remove " + recipeCategoryTuple.getTitle()).setMessage("Are you sure you want to delete this recipe?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    confirmChoice = true;
                    dialogInterface.dismiss();
                    // if user clicked yes in confirm deletion dialog, delete the recipe and navigate up to the previous screen
                    if (confirmChoice) {
                        sqlDb.testDao().deleteSingleRecipeFromItem(recipeId);
                        sqlDb.testDao().deleteSingleRecipeFromRecipe(recipeId);
                        Navigation.findNavController(getActivity(), R.id.fragContainer).navigateUp();
                    }
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
    nestedScrollView = view.findViewById(R.id.nestedScrollNutrition);
    vpRecipe = view.findViewById(R.id.vpRecipe);
    tabRecipe = view.findViewById(R.id.tabRecipe);
    FragmentManager fragmentManager = getChildFragmentManager();
    vpAdapter = new RecipeFragmentViewPagerAdapter(fragmentManager, getLifecycle(), recipeId);
    vpRecipe.setAdapter(vpAdapter);
    vpRecipe.requestDisallowInterceptTouchEvent(true);
    TextView txt = (TextView) LayoutInflater.from(requireContext()).inflate(R.layout.tab_name, null);
    new TabLayoutMediator(tabRecipe, vpRecipe, new TabLayoutMediator.TabConfigurationStrategy() {

        @Override
        public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
            switch(position) {
                case 0:
                    {
                        TextView txtIngredients = (TextView) LayoutInflater.from(requireContext()).inflate(R.layout.tab_name, null);
                        txtIngredients.setText("Ingredients");
                        nestedScrollView = view.findViewById(R.id.nestedScrollIngredients);
                        tab.setCustomView(txtIngredients);
                        break;
                    }
                case 1:
                    {
                        TextView txtDirections = (TextView) LayoutInflater.from(requireContext()).inflate(R.layout.tab_name, null);
                        txtDirections.setText("Directions");
                        nestedScrollView = view.findViewById(R.id.nestedScrollDirections);
                        tab.setCustomView(txtDirections);
                        break;
                    }
                case 2:
                    {
                        TextView txtNutrition = (TextView) LayoutInflater.from(requireContext()).inflate(R.layout.tab_name, null);
                        txtNutrition.setText("Nutrition");
                        nestedScrollView = view.findViewById(R.id.nestedScrollNutrition);
                        tab.setCustomView(txtNutrition);
                        break;
                    }
            }
        }
    }).attach();
    return view;
}
Also used : RecipeCategoryTuple(com.example.ezmeal.roomDatabase.RecipeCategoryTuple) AlertDialog(android.app.AlertDialog) EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) NestedScrollView(androidx.core.widget.NestedScrollView) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) FragmentManager(androidx.fragment.app.FragmentManager) RecipeFragmentViewPagerAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeFragmentViewPagerAdapter) NonNull(androidx.annotation.NonNull) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Example 9 with EZMealDatabase

use of com.example.ezmeal.roomDatabase.EZMealDatabase 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 10 with EZMealDatabase

use of com.example.ezmeal.roomDatabase.EZMealDatabase 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)10 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)10 RecyclerView (androidx.recyclerview.widget.RecyclerView)9 View (android.view.View)8 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)8 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)5 NonNull (androidx.annotation.NonNull)4 TextView (android.widget.TextView)3 StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)3 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)3 CollectionReference (com.google.firebase.firestore.CollectionReference)3 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)3 ArrayList (java.util.ArrayList)3 Intent (android.content.Intent)2 ImageView (android.widget.ImageView)2 CardView (androidx.cardview.widget.CardView)2 NestedScrollView (androidx.core.widget.NestedScrollView)2 FragmentManager (androidx.fragment.app.FragmentManager)2 CategoryFragmentChildHorizontalRecyclerModel (com.example.ezmeal.FindRecipes.FindRecipesModels.CategoryFragmentChildHorizontalRecyclerModel)2 RecipeActivity (com.example.ezmeal.FindRecipes.RecipeActivity)2