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;
}
Aggregations