Search in sources :

Example 1 with QuantityUnitConversionAdapter

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);
}
Also used : MasterProductCatConversionsViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatConversionsViewModel) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) QuantityUnitConversionAdapter(xyz.zedler.patrick.grocy.adapter.QuantityUnitConversionAdapter) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 Snackbar (com.google.android.material.snackbar.Snackbar)1 MasterPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter)1 QuantityUnitConversionAdapter (xyz.zedler.patrick.grocy.adapter.QuantityUnitConversionAdapter)1 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)1 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)1 InfoFullscreen (xyz.zedler.patrick.grocy.model.InfoFullscreen)1 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)1 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)1 MasterProductCatConversionsViewModel (xyz.zedler.patrick.grocy.viewmodel.MasterProductCatConversionsViewModel)1