Search in sources :

Example 16 with InfoFullscreenHelper

use of xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper in project grocy-android by patzly.

the class MasterProductCatConversionsEditFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductCatConversionsEditFragmentArgs args = MasterProductCatConversionsEditFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductCatConversionsEditViewModelFactory(activity.getApplication(), args)).get(MasterProductCatConversionsEditViewModel.class);
    binding.setActivity(activity);
    binding.setViewModel(viewModel);
    binding.setFormData(viewModel.getFormData());
    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.SET_SHOPPING_LIST_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.SELECTED_ID);
            setForDestination(R.id.shoppingListFragment, Constants.ARGUMENT.SELECTED_ID, id);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getOfflineLive().observe(getViewLifecycleOwner(), offline -> {
        InfoFullscreen infoFullscreen = offline ? new InfoFullscreen(InfoFullscreen.ERROR_OFFLINE, () -> updateConnectivity(true)) : null;
        viewModel.getInfoFullscreenLive().setValue(infoFullscreen);
    });
    // necessary because else getValue() doesn't give current value (?)
    viewModel.getFormData().getQuantityUnitsLive().observe(getViewLifecycleOwner(), qUs -> {
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI();
}
Also used : SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) MasterProductCatConversionsEditViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatConversionsEditViewModel) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) MasterProductCatConversionsEditViewModelFactory(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatConversionsEditViewModel.MasterProductCatConversionsEditViewModelFactory) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 17 with InfoFullscreenHelper

use of xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper in project grocy-android by patzly.

the class MasterProductCatDueDateFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductCatDueDateViewModel.MasterProductCatDueDateViewModelFactory(activity.getApplication(), args)).get(MasterProductCatDueDateViewModel.class);
    binding.setActivity(activity);
    binding.setFormData(viewModel.getFormData());
    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.SET_SHOPPING_LIST_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.SELECTED_ID);
            setForDestination(R.id.shoppingListFragment, Constants.ARGUMENT.SELECTED_ID, id);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    if (savedInstanceState == null) {
        viewModel.fillData();
    }
    updateUI(savedInstanceState == null);
}
Also used : SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) MasterProductCatDueDateViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatDueDateViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 18 with InfoFullscreenHelper

use of xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper in project grocy-android by patzly.

the class MasterProductCatOptionalFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductCatOptionalViewModel.MasterProductCatOptionalViewModelFactory(activity.getApplication(), args)).get(MasterProductCatOptionalViewModel.class);
    binding.setActivity(activity);
    binding.setFormData(viewModel.getFormData());
    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.SET_SHOPPING_LIST_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.SELECTED_ID);
            setForDestination(R.id.shoppingListFragment, Constants.ARGUMENT.SELECTED_ID, id);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : MasterProductCatOptionalViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatOptionalViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 19 with InfoFullscreenHelper

use of xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper in project grocy-android by patzly.

the class PurchaseFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    args = PurchaseFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new PurchaseViewModel.PurchaseViewModelFactory(activity.getApplication(), args)).get(PurchaseViewModel.class);
    binding.setActivity(activity);
    binding.setViewModel(viewModel);
    binding.setFragment(this);
    binding.setFormData(viewModel.getFormData());
    binding.setLifecycleOwner(getViewLifecycleOwner());
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    if (args.getShoppingListItems() != null) {
        binding.containerBatchMode.setVisibility(View.VISIBLE);
        binding.linearPurchaseShoppingListItem.containerRow.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_list_item_visible_ripple));
    }
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isDownloading -> binding.swipePurchase.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 (args.getShoppingListItems() != null) {
                clearInputFocus();
                viewModel.getFormData().clearForm();
                boolean nextItemValid = viewModel.batchModeNextItem();
                if (!nextItemValid)
                    activity.navigateUp();
            } else if (PurchaseFragmentArgs.fromBundle(getArguments()).getCloseWhenFinished()) {
                activity.navigateUp();
            } else {
                viewModel.getFormData().clearForm();
                focusProductInputIfNecessary();
                embeddedFragmentScanner.startScannerIfVisible();
            }
        } 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(PurchaseFragmentDirections.actionPurchaseFragmentToChooseProductFragment(barcode));
        }
    });
    Integer productIdSavedSate = (Integer) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT_ID);
    if (productIdSavedSate != null) {
        removeForThisDestination(Constants.ARGUMENT.PRODUCT_ID);
        viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productIdSavedSate, null, null));
    } else if (NumUtil.isStringInt(args.getProductId())) {
        int productId = Integer.parseInt(args.getProductId());
        setArguments(new PurchaseFragmentArgs.Builder(args).setProductId(null).build().toBundle());
        viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productId, null, null));
    }
    String barcode = (String) getFromThisDestinationNow(ARGUMENT.BARCODE);
    if (barcode != null) {
        removeForThisDestination(Constants.ARGUMENT.BARCODE);
        viewModel.addBarcodeToExistingProduct(barcode);
    }
    pluralUtil = new PluralUtil(activity);
    viewModel.getFormData().getShoppingListItemLive().observe(getViewLifecycleOwner(), item -> {
        if (args.getShoppingListItems() == null || item == null)
            return;
        ShoppingListItemAdapter.fillShoppingListItem(requireContext(), item, binding.linearPurchaseShoppingListItem, viewModel.getProductHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getShoppingListItemAmountsHashMap(), pluralUtil);
    });
    embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
    // following lines are necessary because no observers are set in Views
    viewModel.getFormData().getPriceStockLive().observe(getViewLifecycleOwner(), i -> {
    });
    viewModel.getFormData().getQuantityUnitStockLive().observe(getViewLifecycleOwner(), i -> {
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    focusProductInputIfNecessary();
    setHasOptionsMenu(true);
    updateUI(args.getAnimateStart() && savedInstanceState == null);
}
Also used : PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) PurchaseViewModel(xyz.zedler.patrick.grocy.viewmodel.PurchaseViewModel) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 20 with InfoFullscreenHelper

use of xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper in project grocy-android by patzly.

the class ShoppingListEditFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this, new ShoppingListEditViewModel.ShoppingListEditViewModelFactory(activity.getApplication(), getStartupShoppingList())).get(ShoppingListEditViewModel.class);
    binding.setFormData(viewModel.getFormData());
    binding.setViewModel(viewModel);
    binding.setActivity(activity);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    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.NAVIGATE_UP) {
            activity.navigateUp();
        } else if (event.getType() == Event.SET_SHOPPING_LIST_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.SELECTED_ID);
            setForDestination(R.id.shoppingListFragment, Constants.ARGUMENT.SELECTED_ID, id);
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getOfflineLive().observe(getViewLifecycleOwner(), offline -> {
        InfoFullscreen infoFullscreen = offline ? new InfoFullscreen(InfoFullscreen.ERROR_OFFLINE, () -> updateConnectivity(true)) : null;
        viewModel.getInfoFullscreenLive().setValue(infoFullscreen);
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(ShoppingListEditFragmentArgs.fromBundle(requireArguments()).getAnimateStart() && savedInstanceState == null);
}
Also used : InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ShoppingListEditViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingListEditViewModel) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)23 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)23 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)16 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)15 Snackbar (com.google.android.material.snackbar.Snackbar)14 InfoFullscreen (xyz.zedler.patrick.grocy.model.InfoFullscreen)9 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)8 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)7 MenuItem (android.view.MenuItem)4 MasterPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter)4 AppBarBehavior (xyz.zedler.patrick.grocy.behavior.AppBarBehavior)4 Bundle (android.os.Bundle)3 Handler (android.os.Handler)3 LayoutInflater (android.view.LayoutInflater)3 View (android.view.View)3 ViewGroup (android.view.ViewGroup)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3