Search in sources :

Example 1 with InventoryViewModelFactory

use of xyz.zedler.patrick.grocy.viewmodel.InventoryViewModel.InventoryViewModelFactory in project grocy-android by patzly.

the class InventoryFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    InventoryFragmentArgs args = InventoryFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new InventoryViewModelFactory(activity.getApplication(), args)).get(InventoryViewModel.class);
    binding.setActivity(activity);
    binding.setViewModel(viewModel);
    binding.setFragment(this);
    binding.setFormData(viewModel.getFormData());
    binding.setLifecycleOwner(getViewLifecycleOwner());
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    // INITIALIZE VIEWS
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isDownloading -> binding.swipe.setRefreshing(isDownloading));
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
        } else if (event.getType() == Event.TRANSACTION_SUCCESS) {
            assert getArguments() != null;
            if (InventoryFragmentArgs.fromBundle(getArguments()).getCloseWhenFinished()) {
                activity.navigateUp();
            } else {
                clearFormAndFocusProductInput();
            }
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        } else if (event.getType() == Event.FOCUS_INVALID_VIEWS) {
            focusNextInvalidView();
        } else if (event.getType() == Event.QUICK_MODE_ENABLED) {
            focusProductInputIfNecessary();
        } else if (event.getType() == Event.QUICK_MODE_DISABLED) {
            clearInputFocus();
        } else if (event.getType() == Event.CONTINUE_SCANNING) {
            embeddedFragmentScanner.startScannerIfVisible();
        } else if (event.getType() == Event.CHOOSE_PRODUCT) {
            String barcode = event.getBundle().getString(ARGUMENT.BARCODE);
            navigate(InventoryFragmentDirections.actionInventoryFragmentToChooseProductFragment(barcode));
        }
    });
    Integer productIdSavedSate = (Integer) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT_ID);
    if (productIdSavedSate != null) {
        removeForThisDestination(Constants.ARGUMENT.PRODUCT_ID);
        viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productIdSavedSate));
    } else if (NumUtil.isStringInt(args.getProductId())) {
        int productId = Integer.parseInt(args.getProductId());
        setArguments(new InventoryFragmentArgs.Builder(args).setProductId(null).build().toBundle());
        viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productId));
    }
    String barcode = (String) getFromThisDestinationNow(ARGUMENT.BARCODE);
    if (barcode != null) {
        removeForThisDestination(Constants.ARGUMENT.BARCODE);
        viewModel.addBarcodeToExistingProduct(barcode);
    }
    embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
    // following lines are necessary because no observers are set in Views
    viewModel.getFormData().getQuantityUnitStockLive().observe(getViewLifecycleOwner(), i -> {
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    focusProductInputIfNecessary();
    setHasOptionsMenu(true);
    updateUI(args.getAnimateStart() && savedInstanceState == null);
}
Also used : InventoryViewModelFactory(xyz.zedler.patrick.grocy.viewmodel.InventoryViewModel.InventoryViewModelFactory) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) InventoryViewModel(xyz.zedler.patrick.grocy.viewmodel.InventoryViewModel) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)1 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)1 InventoryViewModel (xyz.zedler.patrick.grocy.viewmodel.InventoryViewModel)1 InventoryViewModelFactory (xyz.zedler.patrick.grocy.viewmodel.InventoryViewModel.InventoryViewModelFactory)1