Search in sources :

Example 1 with ProductBarcodeAdapter

use of xyz.zedler.patrick.grocy.adapter.ProductBarcodeAdapter in project grocy-android by patzly.

the class MasterProductCatBarcodesFragment 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 MasterProductCatBarcodesViewModel.MasterProductCatBarcodesViewModelFactory(activity.getApplication(), args)).get(MasterProductCatBarcodesViewModel.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.getProductBarcodesLive().observe(getViewLifecycleOwner(), barcodes -> {
        if (barcodes == null) {
            return;
        }
        if (barcodes.isEmpty()) {
            InfoFullscreen info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_PRODUCT_BARCODES);
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof ProductBarcodeAdapter) {
            ((ProductBarcodeAdapter) binding.recycler.getAdapter()).updateData(barcodes);
        } else {
            binding.recycler.setAdapter(new ProductBarcodeAdapter(barcodes, this, viewModel.getQuantityUnits(), viewModel.getStores()));
        }
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) MasterProductCatBarcodesViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatBarcodesViewModel) 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) ProductBarcodeAdapter(xyz.zedler.patrick.grocy.adapter.ProductBarcodeAdapter)

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 ProductBarcodeAdapter (xyz.zedler.patrick.grocy.adapter.ProductBarcodeAdapter)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 MasterProductCatBarcodesViewModel (xyz.zedler.patrick.grocy.viewmodel.MasterProductCatBarcodesViewModel)1