Search in sources :

Example 1 with StockOverviewItemAdapter

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

the class StockOverviewFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(StockOverviewViewModel.class);
    viewModel.setOfflineLive(!activity.isOnline());
    binding.setViewModel(viewModel);
    binding.setActivity(activity);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    infoFullscreenHelper = new InfoFullscreenHelper(binding.frame);
    clickUtil = new ClickUtil();
    // APP BAR BEHAVIOR
    appBarBehavior = new AppBarBehavior(activity, binding.appBarDefault, binding.appBarSearch, savedInstanceState);
    binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    binding.recycler.setAdapter(new StockPlaceholderAdapter());
    if (savedInstanceState == null) {
        binding.recycler.scrollToPosition(0);
        viewModel.resetSearch();
    }
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getFilteredStockItemsLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null)
            return;
        if (binding.recycler.getAdapter() instanceof StockOverviewItemAdapter) {
            ((StockOverviewItemAdapter) binding.recycler.getAdapter()).updateData(requireContext(), items, viewModel.getShoppingListItemsProductIds(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getProductHashMap(), viewModel.getLocationHashMap(), viewModel.getProductIdsMissingStockItems(), viewModel.getSortMode(), viewModel.isSortAscending(), viewModel.getGroupingMode());
        } else {
            binding.recycler.setAdapter(new StockOverviewItemAdapter(requireContext(), items, viewModel.getShoppingListItemsProductIds(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getProductHashMap(), viewModel.getLocationHashMap(), viewModel.getProductIdsMissingStockItems(), this, viewModel.isFeatureEnabled(PREF.FEATURE_STOCK_BBD_TRACKING), viewModel.isFeatureEnabled(PREF.FEATURE_SHOPPING_LIST), viewModel.getDaysExpriringSoon(), viewModel.getCurrency(), viewModel.getSortMode(), viewModel.isSortAscending(), viewModel.getGroupingMode()));
            binding.recycler.scheduleLayoutAnimation();
        }
    });
    embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getScannerVisibilityLive());
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
        }
    });
    if (swipeBehavior == null) {
        swipeBehavior = new SwipeBehavior(activity, swipeStarted -> binding.swipe.setEnabled(!swipeStarted)) {

            @Override
            public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
                if (viewHolder.getItemViewType() != GroupedListItem.TYPE_ENTRY)
                    return;
                if (!(binding.recycler.getAdapter() instanceof StockOverviewItemAdapter))
                    return;
                int position = viewHolder.getAdapterPosition();
                ArrayList<GroupedListItem> groupedListItems = ((StockOverviewItemAdapter) binding.recycler.getAdapter()).getGroupedListItems();
                if (groupedListItems == null || position < 0 || position >= groupedListItems.size()) {
                    return;
                }
                GroupedListItem item = groupedListItems.get(position);
                if (!(item instanceof StockItem)) {
                    return;
                }
                StockItem stockItem = (StockItem) item;
                if (stockItem.getAmountAggregatedDouble() > 0 && stockItem.getProduct().getEnableTareWeightHandlingInt() == 0) {
                    underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_consume_product, pos -> {
                        if (pos >= groupedListItems.size()) {
                            return;
                        }
                        swipeBehavior.recoverLatestSwipedItem();
                        viewModel.performAction(Constants.ACTION.CONSUME, stockItem);
                    }));
                }
                if (stockItem.getAmountAggregatedDouble() > stockItem.getAmountOpenedAggregatedDouble() && stockItem.getProduct().getEnableTareWeightHandlingInt() == 0 && viewModel.isFeatureEnabled(Constants.PREF.FEATURE_STOCK_OPENED_TRACKING)) {
                    underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_open, pos -> {
                        if (pos >= groupedListItems.size()) {
                            return;
                        }
                        swipeBehavior.recoverLatestSwipedItem();
                        viewModel.performAction(Constants.ACTION.OPEN, stockItem);
                    }));
                }
                if (underlayButtons.isEmpty()) {
                    underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_close, pos -> swipeBehavior.recoverLatestSwipedItem()));
                }
            }
        };
    }
    swipeBehavior.attachToRecyclerView(binding.recycler);
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI();
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) FragmentStockOverviewBinding(xyz.zedler.patrick.grocy.databinding.FragmentStockOverviewBinding) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Event(xyz.zedler.patrick.grocy.model.Event) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) R(xyz.zedler.patrick.grocy.R) ViewUtil(xyz.zedler.patrick.grocy.util.ViewUtil) View(android.view.View) Animation(android.view.animation.Animation) RecyclerView(androidx.recyclerview.widget.RecyclerView) StockItem(xyz.zedler.patrick.grocy.model.StockItem) BarcodeListener(xyz.zedler.patrick.grocy.scanner.EmbeddedFragmentScanner.BarcodeListener) EmbeddedFragmentScannerBundle(xyz.zedler.patrick.grocy.scanner.EmbeddedFragmentScannerBundle) ViewModelProvider(androidx.lifecycle.ViewModelProvider) PREF(xyz.zedler.patrick.grocy.util.Constants.PREF) LayoutInflater(android.view.LayoutInflater) StockPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.StockPlaceholderAdapter) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) StockOverviewItemAdapter(xyz.zedler.patrick.grocy.adapter.StockOverviewItemAdapter) Constants(xyz.zedler.patrick.grocy.util.Constants) Location(xyz.zedler.patrick.grocy.model.Location) ViewGroup(android.view.ViewGroup) EmbeddedFragmentScanner(xyz.zedler.patrick.grocy.scanner.EmbeddedFragmentScanner) List(java.util.List) Nullable(androidx.annotation.Nullable) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) StockOverviewViewModel(xyz.zedler.patrick.grocy.viewmodel.StockOverviewViewModel) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) ArrayList(java.util.ArrayList) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) StockItem(xyz.zedler.patrick.grocy.model.StockItem) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) StockOverviewViewModel(xyz.zedler.patrick.grocy.viewmodel.StockOverviewViewModel) StockPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.StockPlaceholderAdapter) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) RecyclerView(androidx.recyclerview.widget.RecyclerView) StockOverviewItemAdapter(xyz.zedler.patrick.grocy.adapter.StockOverviewItemAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 Animation (android.view.animation.Animation)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 R (xyz.zedler.patrick.grocy.R)1 MainActivity (xyz.zedler.patrick.grocy.activity.MainActivity)1 StockOverviewItemAdapter (xyz.zedler.patrick.grocy.adapter.StockOverviewItemAdapter)1 StockPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.StockPlaceholderAdapter)1 AppBarBehavior (xyz.zedler.patrick.grocy.behavior.AppBarBehavior)1 SwipeBehavior (xyz.zedler.patrick.grocy.behavior.SwipeBehavior)1 FragmentStockOverviewBinding (xyz.zedler.patrick.grocy.databinding.FragmentStockOverviewBinding)1