Search in sources :

Example 1 with ShoppingListClearBottomSheet

use of xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListClearBottomSheet in project grocy-android by patzly.

the class ShoppingListFragment method setUpBottomMenu.

public void setUpBottomMenu() {
    if (activity == null) {
        // Fixes crash on theme change
        return;
    }
    MenuItem search = activity.getBottomMenu().findItem(R.id.action_search);
    if (search != null) {
        search.setOnMenuItemClickListener(item -> {
            ViewUtil.startIcon(item);
            setUpSearch();
            return true;
        });
    }
    MenuItem shoppingMode = activity.getBottomMenu().findItem(R.id.action_shopping_mode);
    if (shoppingMode != null) {
        shoppingMode.setOnMenuItemClickListener(item -> {
            navigate(ShoppingListFragmentDirections.actionShoppingListFragmentToShoppingModeFragment());
            return true;
        });
    }
    MenuItem addMissing = activity.getBottomMenu().findItem(R.id.action_add_missing);
    if (addMissing != null) {
        addMissing.setOnMenuItemClickListener(item -> {
            ViewUtil.startIcon(item);
            viewModel.addMissingItems();
            return true;
        });
    }
    MenuItem purchaseAllItems = activity.getBottomMenu().findItem(R.id.action_purchase_all_items);
    if (purchaseAllItems != null) {
        purchaseAllItems.setOnMenuItemClickListener(item -> {
            ArrayList<ShoppingListItem> shoppingListItemsSelected = viewModel.getFilteredShoppingListItemsLive().getValue();
            if (shoppingListItemsSelected == null) {
                showMessage(activity.getString(R.string.error_undefined));
                return true;
            }
            if (shoppingListItemsSelected.isEmpty()) {
                showMessage(activity.getString(R.string.error_empty_shopping_list));
                return true;
            }
            ArrayList<ShoppingListItem> listItems = new ArrayList<>(shoppingListItemsSelected);
            HashMap<Integer, String> productNamesHashMap = viewModel.getProductNamesHashMap();
            if (productNamesHashMap == null) {
                showMessage(activity.getString(R.string.error_undefined));
                return true;
            }
            SortUtil.sortShoppingListItemsByName(requireContext(), listItems, productNamesHashMap, true);
            int[] array = new int[listItems.size()];
            for (int i = 0; i < array.length; i++) {
                array[i] = listItems.get(i).getId();
            }
            navigate(R.id.purchaseFragment, new PurchaseFragmentArgs.Builder().setShoppingListItems(array).setCloseWhenFinished(true).build().toBundle());
            return true;
        });
    }
    MenuItem purchaseDoneItems = activity.getBottomMenu().findItem(R.id.action_purchase_done_items);
    if (purchaseDoneItems != null) {
        purchaseDoneItems.setOnMenuItemClickListener(item -> {
            ArrayList<ShoppingListItem> shoppingListItemsSelected = viewModel.getFilteredShoppingListItemsLive().getValue();
            if (shoppingListItemsSelected == null) {
                showMessage(activity.getString(R.string.error_undefined));
                return true;
            }
            if (shoppingListItemsSelected.isEmpty()) {
                showMessage(activity.getString(R.string.error_empty_shopping_list));
                return true;
            }
            ArrayList<ShoppingListItem> listItems = new ArrayList<>(shoppingListItemsSelected);
            HashMap<Integer, String> productNamesHashMap = viewModel.getProductNamesHashMap();
            if (productNamesHashMap == null) {
                showMessage(activity.getString(R.string.error_undefined));
                return true;
            }
            ArrayList<ShoppingListItem> doneItems = new ArrayList<>();
            for (ShoppingListItem tempItem : listItems) {
                if (!tempItem.isUndone())
                    doneItems.add(tempItem);
            }
            if (doneItems.isEmpty()) {
                showMessage(activity.getString(R.string.error_no_done_items));
                return true;
            }
            SortUtil.sortShoppingListItemsByName(requireContext(), doneItems, productNamesHashMap, true);
            int[] array = new int[doneItems.size()];
            for (int i = 0; i < array.length; i++) {
                array[i] = doneItems.get(i).getId();
            }
            navigate(R.id.purchaseFragment, new PurchaseFragmentArgs.Builder().setShoppingListItems(array).setCloseWhenFinished(true).build().toBundle());
            return true;
        });
    }
    MenuItem editNotes = activity.getBottomMenu().findItem(R.id.action_edit_notes);
    if (editNotes != null) {
        editNotes.setOnMenuItemClickListener(item -> {
            showNotesEditor();
            return true;
        });
    }
    MenuItem clear = activity.getBottomMenu().findItem(R.id.action_clear);
    if (clear != null) {
        clear.setOnMenuItemClickListener(item -> {
            ViewUtil.startIcon(item);
            ShoppingList shoppingList = viewModel.getSelectedShoppingList();
            if (shoppingList == null) {
                showMessage(activity.getString(R.string.error_undefined));
                return true;
            }
            Bundle bundle = new Bundle();
            bundle.putParcelable(Constants.ARGUMENT.SHOPPING_LIST, shoppingList);
            activity.showBottomSheet(new ShoppingListClearBottomSheet(), bundle);
            return true;
        });
    }
}
Also used : ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) ShoppingListClearBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListClearBottomSheet)

Aggregations

Bundle (android.os.Bundle)1 MenuItem (android.view.MenuItem)1 ArrayList (java.util.ArrayList)1 ShoppingListClearBottomSheet (xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListClearBottomSheet)1 ShoppingList (xyz.zedler.patrick.grocy.model.ShoppingList)1 ShoppingListItem (xyz.zedler.patrick.grocy.model.ShoppingListItem)1