use of xyz.zedler.patrick.grocy.adapter.QuantityUnitConversionAdapter in project grocy-android by patzly.
the class MasterProductCatConversionsFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
clickUtil = new ClickUtil();
MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
viewModel = new ViewModelProvider(this, new MasterProductCatConversionsViewModel.MasterProductCatConversionsViewModelFactory(activity.getApplication(), args)).get(MasterProductCatConversionsViewModel.class);
binding.setActivity(activity);
binding.setViewModel(viewModel);
binding.setFragment(this);
binding.setLifecycleOwner(getViewLifecycleOwner());
viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
SnackbarMessage message = (SnackbarMessage) event;
Snackbar snack = message.getSnackbar(activity, activity.binding.frameMainContainer);
activity.showSnackbar(snack);
} else if (event.getType() == Event.NAVIGATE_UP) {
activity.navigateUp();
} else if (event.getType() == Event.BOTTOM_SHEET) {
BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
}
});
infoFullscreenHelper = new InfoFullscreenHelper(binding.frameContainer);
viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
if (!isLoading) {
viewModel.setCurrentQueueLoading(null);
}
});
binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
binding.recycler.setItemAnimator(new DefaultItemAnimator());
binding.recycler.setAdapter(new MasterPlaceholderAdapter());
viewModel.getQuantityUnitConversionsLive().observe(getViewLifecycleOwner(), conversions -> {
if (conversions == null) {
return;
}
if (conversions.isEmpty()) {
InfoFullscreen info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_UNIT_CONVERSIONS);
viewModel.getInfoFullscreenLive().setValue(info);
} else {
viewModel.getInfoFullscreenLive().setValue(null);
}
if (binding.recycler.getAdapter() instanceof QuantityUnitConversionAdapter) {
((QuantityUnitConversionAdapter) binding.recycler.getAdapter()).updateData(conversions);
} else {
binding.recycler.setAdapter(new QuantityUnitConversionAdapter(requireContext(), conversions, this, viewModel.getQuantityUnitHashMap()));
}
});
if (savedInstanceState == null) {
viewModel.loadFromDatabase(true);
}
updateUI(savedInstanceState == null);
}
Aggregations