Search in sources :

Example 1 with AppBarBehavior

use of xyz.zedler.patrick.grocy.behavior.AppBarBehavior in project grocy-android by patzly.

the class MasterObjectListFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    clickUtil = new ClickUtil();
    viewModel = new ViewModelProvider(this, new MasterObjectListViewModel.MasterObjectListViewModelFactory(activity.getApplication(), entity)).get(MasterObjectListViewModel.class);
    viewModel.setOfflineLive(!activity.isOnline());
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        binding.swipe.setRefreshing(state);
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    binding.swipe.setOnRefreshListener(() -> viewModel.downloadDataForceUpdate());
    // for offline info in app bar
    binding.swipe.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
    binding.swipe.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
    binding.swipe.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
    viewModel.getDisplayedItemsLive().observe(getViewLifecycleOwner(), objects -> {
        if (objects == null) {
            return;
        }
        if (objects.isEmpty()) {
            InfoFullscreen info;
            if (viewModel.isSearchActive()) {
                info = new InfoFullscreen(InfoFullscreen.INFO_NO_SEARCH_RESULTS);
            } else {
                int fullscreenType;
                switch(entity) {
                    case GrocyApi.ENTITY.PRODUCTS:
                        fullscreenType = InfoFullscreen.INFO_EMPTY_PRODUCTS;
                        break;
                    case GrocyApi.ENTITY.QUANTITY_UNITS:
                        fullscreenType = InfoFullscreen.INFO_EMPTY_QUS;
                        break;
                    case GrocyApi.ENTITY.LOCATIONS:
                        fullscreenType = InfoFullscreen.INFO_EMPTY_LOCATIONS;
                        break;
                    case GrocyApi.ENTITY.PRODUCT_GROUPS:
                        fullscreenType = InfoFullscreen.INFO_EMPTY_PRODUCT_GROUPS;
                        break;
                    case ENTITY.TASK_CATEGORIES:
                        fullscreenType = InfoFullscreen.INFO_EMPTY_TASK_CATEGORIES;
                        break;
                    default:
                        // STORES
                        fullscreenType = InfoFullscreen.INFO_EMPTY_STORES;
                }
                info = new InfoFullscreen(fullscreenType);
            }
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof MasterObjectListAdapter) {
            ((MasterObjectListAdapter) binding.recycler.getAdapter()).updateData(objects);
        } else {
            binding.recycler.setAdapter(new MasterObjectListAdapter(getContext(), entity, objects, this, viewModel.getHorizontalFilterBarMulti()));
            binding.recycler.scheduleLayoutAnimation();
        }
    });
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            SnackbarMessage msg = (SnackbarMessage) event;
            Snackbar snackbar = msg.getSnackbar(activity, activity.binding.frameMainContainer);
            activity.showSnackbar(snackbar);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    viewModel.getOfflineLive().observe(getViewLifecycleOwner(), this::appBarOfflineInfo);
    if (savedInstanceState == null) {
        // delete search if navigating back from other fragment
        viewModel.deleteSearch();
    }
    // INITIALIZE VIEWS
    binding.back.setOnClickListener(v -> activity.onBackPressed());
    binding.searchClose.setOnClickListener(v -> dismissSearch());
    binding.editTextSearch.addTextChangedListener(new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void afterTextChanged(Editable s) {
            if (appBarBehavior.isPrimaryLayout()) {
                return;
            }
            viewModel.setSearch(s != null ? s.toString() : "");
        }
    });
    binding.editTextSearch.setOnEditorActionListener((TextView v, int actionId, KeyEvent event) -> {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            activity.hideKeyboard();
            return true;
        }
        return false;
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.frameContainer);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    // APP BAR BEHAVIOR
    appBarBehavior = new AppBarBehavior(activity, binding.appBarDefault, binding.appBarSearch, savedInstanceState);
    if (viewModel.isSearchActive()) {
        appBarBehavior.switchToSecondary();
    }
    binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    binding.recycler.setItemAnimator(new DefaultItemAnimator());
    binding.recycler.setAdapter(new MasterPlaceholderAdapter());
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    // UPDATE UI
    updateUI(true);
}
Also used : MasterObjectListViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterObjectListViewModel) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) 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) KeyEvent(android.view.KeyEvent) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) MasterObjectListAdapter(xyz.zedler.patrick.grocy.adapter.MasterObjectListAdapter) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 2 with AppBarBehavior

use of xyz.zedler.patrick.grocy.behavior.AppBarBehavior in project grocy-android by patzly.

the class TasksFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(TasksViewModel.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 MasterPlaceholderAdapter());
    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.getFilteredTasksLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null) {
            return;
        }
        if (items.isEmpty()) {
            InfoFullscreen info;
            if (viewModel.isSearchActive()) {
                info = new InfoFullscreen(InfoFullscreen.INFO_NO_SEARCH_RESULTS);
            } else {
                info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_TASKS);
            }
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof TasksItemAdapter) {
            ((TasksItemAdapter) binding.recycler.getAdapter()).updateData(items, viewModel.getSortMode());
        } else {
            binding.recycler.setAdapter(new TasksItemAdapter(requireContext(), items, this, viewModel.getSortMode()));
        }
    });
    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) {
                int position = viewHolder.getAdapterPosition();
                ArrayList<Task> displayedItems = viewModel.getFilteredTasksLive().getValue();
                if (displayedItems == null || position < 0 || position >= displayedItems.size()) {
                    return;
                }
                Task task = displayedItems.get(position);
                underlayButtons.add(new UnderlayButton(R.drawable.ic_round_done, pos -> {
                    if (pos >= displayedItems.size()) {
                        return;
                    }
                    swipeBehavior.recoverLatestSwipedItem();
                    viewModel.performAction(ACTION.UNDO, displayedItems.get(pos));
                }));
            }
        };
    }
    swipeBehavior.attachToRecyclerView(binding.recycler);
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : TasksItemAdapter(xyz.zedler.patrick.grocy.adapter.TasksItemAdapter) Bundle(android.os.Bundle) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) NonNull(androidx.annotation.NonNull) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Event(xyz.zedler.patrick.grocy.model.Event) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) R(xyz.zedler.patrick.grocy.R) ActivityInfo(android.content.pm.ActivityInfo) ViewUtil(xyz.zedler.patrick.grocy.util.ViewUtil) View(android.view.View) Animation(android.view.animation.Animation) RecyclerView(androidx.recyclerview.widget.RecyclerView) ACTION(xyz.zedler.patrick.grocy.util.Constants.ACTION) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) Constants(xyz.zedler.patrick.grocy.util.Constants) POSITION(xyz.zedler.patrick.grocy.util.Constants.FAB.POSITION) ViewGroup(android.view.ViewGroup) FragmentTasksBinding(xyz.zedler.patrick.grocy.databinding.FragmentTasksBinding) Task(xyz.zedler.patrick.grocy.model.Task) List(java.util.List) Nullable(androidx.annotation.Nullable) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) TasksViewModel(xyz.zedler.patrick.grocy.viewmodel.TasksViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Snackbar(com.google.android.material.snackbar.Snackbar) Task(xyz.zedler.patrick.grocy.model.Task) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) ArrayList(java.util.ArrayList) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) TasksViewModel(xyz.zedler.patrick.grocy.viewmodel.TasksViewModel) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) TasksItemAdapter(xyz.zedler.patrick.grocy.adapter.TasksItemAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 3 with AppBarBehavior

use of xyz.zedler.patrick.grocy.behavior.AppBarBehavior in project grocy-android by patzly.

the class ShoppingListFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(ShoppingListViewModel.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();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    pluralUtil = new PluralUtil(activity);
    // 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 ShoppingPlaceholderAdapter());
    if (savedInstanceState == null) {
        binding.recycler.scrollToPosition(0);
        viewModel.resetSearch();
    }
    Object forcedSelectedId = getFromThisDestinationNow(Constants.ARGUMENT.SELECTED_ID);
    if (forcedSelectedId != null) {
        viewModel.selectShoppingList((Integer) forcedSelectedId);
        removeForThisDestination(Constants.ARGUMENT.SELECTED_ID);
    }
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getSelectedShoppingListIdLive().observe(getViewLifecycleOwner(), this::changeAppBarTitle);
    viewModel.getFilteredShoppingListItemsLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null)
            return;
        if (binding.recycler.getAdapter() instanceof ShoppingListItemAdapter) {
            ((ShoppingListItemAdapter) binding.recycler.getAdapter()).updateData(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), viewModel.getShoppingListNotes(), viewModel.getGroupingMode());
        } else {
            binding.recycler.setAdapter(new ShoppingListItemAdapter(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), this, viewModel.getShoppingListNotes(), viewModel.getGroupingMode()));
            binding.recycler.scheduleLayoutAnimation();
        }
    });
    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.swipeShoppingList.setEnabled(!swipeStarted)) {

            @Override
            public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
                if (viewHolder.getItemViewType() != GroupedListItem.TYPE_ENTRY)
                    return;
                if (!(binding.recycler.getAdapter() instanceof ShoppingListItemAdapter))
                    return;
                int position = viewHolder.getAdapterPosition();
                ArrayList<GroupedListItem> groupedListItems = ((ShoppingListItemAdapter) binding.recycler.getAdapter()).getGroupedListItems();
                if (groupedListItems == null || position < 0 || position >= groupedListItems.size()) {
                    return;
                }
                GroupedListItem item = groupedListItems.get(position);
                if (!(item instanceof ShoppingListItem)) {
                    return;
                }
                ShoppingListItem shoppingListItem = (ShoppingListItem) item;
                underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_done, pos -> {
                    if (position >= groupedListItems.size()) {
                        return;
                    }
                    viewModel.toggleDoneStatus(shoppingListItem);
                }));
            }
        };
    }
    swipeBehavior.attachToRecyclerView(binding.recycler);
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(ShoppingListFragmentArgs.fromBundle(requireArguments()).getAnimateStart() && savedInstanceState == null);
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) Bundle(android.os.Bundle) Spanned(android.text.Spanned) NonNull(androidx.annotation.NonNull) ShoppingListHelper(xyz.zedler.patrick.grocy.helper.ShoppingListHelper) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) NumUtil(xyz.zedler.patrick.grocy.util.NumUtil) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) ShoppingListItemBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListItemBottomSheet) HashMap(java.util.HashMap) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Event(xyz.zedler.patrick.grocy.model.Event) ARGUMENT(xyz.zedler.patrick.grocy.util.Constants.ARGUMENT) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) R(xyz.zedler.patrick.grocy.R) ViewUtil(xyz.zedler.patrick.grocy.util.ViewUtil) View(android.view.View) SortUtil(xyz.zedler.patrick.grocy.util.SortUtil) RecyclerView(androidx.recyclerview.widget.RecyclerView) FragmentShoppingListBinding(xyz.zedler.patrick.grocy.databinding.FragmentShoppingListBinding) ShoppingListViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingListViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) LayoutInflater(android.view.LayoutInflater) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) TextEditBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.TextEditBottomSheet) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) Constants(xyz.zedler.patrick.grocy.util.Constants) ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) ViewGroup(android.view.ViewGroup) ShoppingListsBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListsBottomSheet) List(java.util.List) Nullable(androidx.annotation.Nullable) SharedPreferences(android.content.SharedPreferences) Product(xyz.zedler.patrick.grocy.model.Product) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) PreferenceManager(androidx.preference.PreferenceManager) ShoppingListItemAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListItemAdapter) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListClearBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListClearBottomSheet) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Snackbar(com.google.android.material.snackbar.Snackbar) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListItemAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListItemAdapter) ArrayList(java.util.ArrayList) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ShoppingListViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingListViewModel) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) RecyclerView(androidx.recyclerview.widget.RecyclerView) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 4 with AppBarBehavior

use of xyz.zedler.patrick.grocy.behavior.AppBarBehavior 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

ViewModelProvider (androidx.lifecycle.ViewModelProvider)4 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)4 AppBarBehavior (xyz.zedler.patrick.grocy.behavior.AppBarBehavior)4 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)4 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)4 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)4 Bundle (android.os.Bundle)3 LayoutInflater (android.view.LayoutInflater)3 MenuItem (android.view.MenuItem)3 View (android.view.View)3 ViewGroup (android.view.ViewGroup)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 Snackbar (com.google.android.material.snackbar.Snackbar)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 R (xyz.zedler.patrick.grocy.R)3 MainActivity (xyz.zedler.patrick.grocy.activity.MainActivity)3 SwipeBehavior (xyz.zedler.patrick.grocy.behavior.SwipeBehavior)3