Search in sources :

Example 1 with CategoryFragmentAdapter

use of com.example.ezmeal.FindRecipes.FindRecipesAdapters.CategoryFragmentAdapter 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.fragment_find_recipes_category, container, false);
    viewModel = new ViewModelProvider(requireActivity()).get(CategoryFragmentViewModel.class);
    rvFindRecipes = (RecyclerView) view.findViewById(R.id.rvFindRecipes);
    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    rvFindRecipes.setLayoutManager(staggeredGridLayoutManager);
    rvFindRecipes.suppressLayout(true);
    categoryFragmentAdapter = new CategoryFragmentAdapter(verticalRecipes, horizontalLists);
    rvFindRecipes.setAdapter(categoryFragmentAdapter);
    categoryFragmentAdapter.setOnItemClickListener(new CategoryFragmentAdapter.MainAdapterListener() {

        @Override
        public void onItemClick(int position, boolean isClickable) {
            // certain positions may be an entire horizontal recyclerview - only allow clicking for positions that are actual individual recipes, not holders for horizontal rv's
            if (isClickable) {
                Intent intent = new Intent(getContext(), RecipeActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", recipeIdList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }
    });
    rvFindRecipes.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            rvFindRecipes.setVisibility(View.VISIBLE);
            rvFindRecipes.suppressLayout(false);
            rvFindRecipes.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().enableMultiInstanceInvalidation().build();
    Bundle extras = getArguments();
    if (extras != null) {
        category = extras.getString("cat");
    }
    swipeLayout = view.findViewById(R.id.swipeCategory);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            // clear all category recipes from Room, to be overwritten with new ones
            deleteEntireCategory(category);
            verticalRecipes.clear();
            horizontalLists.clear();
            categoryFragmentAdapter.notifyDataSetChanged();
            viewModel.setDataOther(category);
            // turn off refreshing logo
            swipeLayout.setRefreshing(false);
        }
    });
    // Observers
    viewModel.getIsPopulated().observe(getViewLifecycleOwner(), returnIsPopulated -> {
        isPopulated = returnIsPopulated;
    });
    viewModel.getDataOther().observe(getViewLifecycleOwner(), new Observer<RetrievedRecipeLists>() {

        @Override
        public void onChanged(RetrievedRecipeLists retrievedRecipeLists) {
            if (// && list.size() > 0)
            retrievedRecipeLists != null) {
                horizontalLists.clear();
                verticalRecipes.clear();
                horizontalLists.add(retrievedRecipeLists.getHorizontalList());
                List<VerticalRecipe> tempVerticalList = new ArrayList<>();
                tempVerticalList = retrievedRecipeLists.getVerticalList();
                for (int i = 0; i < tempVerticalList.size(); i++) {
                    verticalRecipes.add(tempVerticalList.get(i));
                    recipeIdList.add(tempVerticalList.get(i).getRecipeId());
                }
                categoryFragmentAdapter.notifyDataSetChanged();
            }
        }
    });
    viewModel.getLastCategory().observe(getViewLifecycleOwner(), returnedCategory -> {
        lastCategory = returnedCategory;
    });
    return view;
}
Also used : CategoryFragmentAdapter(com.example.ezmeal.FindRecipes.FindRecipesAdapters.CategoryFragmentAdapter) EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) Intent(android.content.Intent) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) CategoryFragmentViewModel(com.example.ezmeal.FindRecipes.FindRecipesViewModels.CategoryFragmentViewModel) RetrievedRecipeLists(com.example.ezmeal.FindRecipes.FindRecipesModels.RetrievedRecipeLists) ArrayList(java.util.ArrayList) List(java.util.List) ViewTreeObserver(android.view.ViewTreeObserver) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 CategoryFragmentAdapter (com.example.ezmeal.FindRecipes.FindRecipesAdapters.CategoryFragmentAdapter)1 RetrievedRecipeLists (com.example.ezmeal.FindRecipes.FindRecipesModels.RetrievedRecipeLists)1 CategoryFragmentViewModel (com.example.ezmeal.FindRecipes.FindRecipesViewModels.CategoryFragmentViewModel)1 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1