Search in sources :

Example 1 with RecipeFragmentViewPagerAdapter

use of com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeFragmentViewPagerAdapter 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)

Aggregations

AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 NestedScrollView (androidx.core.widget.NestedScrollView)1 FragmentManager (androidx.fragment.app.FragmentManager)1 RecipeFragmentViewPagerAdapter (com.example.ezmeal.MyRecipes.RecipeAdapters.RecipeFragmentViewPagerAdapter)1 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)1 RecipeCategoryTuple (com.example.ezmeal.roomDatabase.RecipeCategoryTuple)1 TabLayoutMediator (com.google.android.material.tabs.TabLayoutMediator)1