Search in sources :

Example 1 with ShoppingListItem

use of xyz.zedler.patrick.grocy.model.ShoppingListItem in project grocy-android by patzly.

the class ShoppingListItemAdapter method onBindViewHolder.

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int positionDoNotUse) {
    GroupedListItem groupedListItem = groupedListItems.get(viewHolder.getAdapterPosition());
    int type = getItemViewType(viewHolder.getAdapterPosition());
    if (type == GroupedListItem.TYPE_HEADER) {
        ShoppingListGroupViewHolder holder = (ShoppingListGroupViewHolder) viewHolder;
        if (((GroupHeader) groupedListItem).getDisplayDivider() == 1) {
            holder.binding.divider.setVisibility(View.VISIBLE);
        } else {
            holder.binding.divider.setVisibility(View.GONE);
        }
        holder.binding.name.setText(((GroupHeader) groupedListItem).getGroupName());
        return;
    }
    if (type == GroupedListItem.TYPE_BOTTOM_NOTES) {
        ShoppingListNotesViewHolder holder = (ShoppingListNotesViewHolder) viewHolder;
        holder.binding.notes.setText(((ShoppingListBottomNotes) groupedListItem).getNotes());
        holder.binding.container.setOnClickListener(view -> listener.onItemRowClicked(groupedListItem));
        return;
    }
    ShoppingListItem item = (ShoppingListItem) groupedListItem;
    RowShoppingListItemBinding binding = ((ShoppingListItemViewHolder) viewHolder).binding;
    // NAME
    Product product = null;
    if (item.hasProduct()) {
        product = productHashMap.get(item.getProductIdInt());
    }
    if (product != null) {
        binding.name.setText(product.getName());
        binding.name.setVisibility(View.VISIBLE);
    } else {
        binding.name.setText(null);
        binding.name.setVisibility(View.GONE);
    }
    if (item.isUndone()) {
        binding.name.setPaintFlags(binding.name.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        binding.name.setAlpha(1.0f);
    } else {
        binding.name.setPaintFlags(binding.name.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        binding.name.setAlpha(0.6f);
    }
    if (binding.name.getVisibility() == View.VISIBLE) {
        binding.noteAsName.setVisibility(View.GONE);
        binding.noteAsName.setText(null);
    }
    // AMOUNT
    Double amountInQuUnit = shoppingListItemAmountsHashMap.get(item.getId());
    if (product != null && amountInQuUnit != null) {
        QuantityUnit quantityUnit = quantityUnitHashMap.get(item.getQuIdInt());
        String quStr = pluralUtil.getQuantityUnitPlural(quantityUnit, amountInQuUnit);
        if (quStr != null) {
            binding.amount.setText(binding.amount.getContext().getString(R.string.subtitle_amount, NumUtil.trim(amountInQuUnit), quStr));
        } else {
            binding.amount.setText(NumUtil.trim(amountInQuUnit));
        }
    } else if (product != null) {
        QuantityUnit quantityUnit = quantityUnitHashMap.get(product.getQuIdStockInt());
        String quStr = pluralUtil.getQuantityUnitPlural(quantityUnit, item.getAmountDouble());
        if (quStr != null) {
            binding.amount.setText(binding.amount.getContext().getString(R.string.subtitle_amount, NumUtil.trim(item.getAmountDouble()), quStr));
        } else {
            binding.amount.setText(NumUtil.trim(item.getAmountDouble()));
        }
    } else {
        binding.amount.setText(NumUtil.trim(item.getAmountDouble()));
    }
    if (item.hasProduct() && missingProductIds.contains(item.getProductIdInt())) {
        binding.amount.setTypeface(ResourcesCompat.getFont(binding.amount.getContext(), R.font.jost_medium));
        binding.amount.setTextColor(ContextCompat.getColor(binding.amount.getContext(), R.color.retro_blue_fg));
    } else {
        binding.amount.setTypeface(ResourcesCompat.getFont(binding.amount.getContext(), R.font.jost_book));
        binding.amount.setTextColor(ContextCompat.getColor(binding.amount.getContext(), R.color.on_background_secondary));
    }
    if (item.isUndone()) {
        binding.amount.setPaintFlags(binding.amount.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        binding.amount.setAlpha(1.0f);
    } else {
        binding.amount.setPaintFlags(binding.amount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        binding.amount.setAlpha(0.6f);
    }
    if (item.getNote() != null && !item.getNote().trim().isEmpty()) {
        if (binding.name.getVisibility() == View.VISIBLE) {
            binding.note.setVisibility(View.VISIBLE);
            binding.note.setText(item.getNote().trim());
        } else {
            binding.noteAsName.setVisibility(View.VISIBLE);
            binding.noteAsName.setText(item.getNote().trim());
            binding.note.setVisibility(View.GONE);
            binding.note.setText(null);
        }
    } else {
        if (binding.name.getVisibility() == View.VISIBLE) {
            binding.note.setVisibility(View.GONE);
            binding.note.setText(null);
        }
    }
    if (binding.noteAsName.getVisibility() == View.VISIBLE) {
        if (item.isUndone()) {
            binding.noteAsName.setPaintFlags(binding.noteAsName.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            binding.noteAsName.setAlpha(1.0f);
        } else {
            binding.noteAsName.setPaintFlags(binding.noteAsName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            binding.noteAsName.setAlpha(0.6f);
        }
    } else {
        if (item.isUndone()) {
            binding.note.setPaintFlags(binding.note.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            binding.note.setAlpha(1.0f);
        } else {
            binding.note.setPaintFlags(binding.note.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            binding.note.setAlpha(0.6f);
        }
    }
    // CONTAINER
    binding.containerRow.setOnClickListener(view -> listener.onItemRowClicked(groupedListItem));
}
Also used : GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) RowShoppingListItemBinding(xyz.zedler.patrick.grocy.databinding.RowShoppingListItemBinding) Product(xyz.zedler.patrick.grocy.model.Product) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) SuppressLint(android.annotation.SuppressLint)

Example 2 with ShoppingListItem

use of xyz.zedler.patrick.grocy.model.ShoppingListItem 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)

Example 3 with ShoppingListItem

use of xyz.zedler.patrick.grocy.model.ShoppingListItem in project grocy-android by patzly.

the class DownloadHelper method getShoppingListItems.

public QueueItem getShoppingListItems(OnShoppingListItemsResponseListener onResponseListener, OnErrorListener onErrorListener) {
    return new QueueItem() {

        @Override
        public void perform(@Nullable OnStringResponseListener responseListener, @Nullable OnErrorListener errorListener, @Nullable String uuid) {
            get(grocyApi.getObjects(GrocyApi.ENTITY.SHOPPING_LIST), uuid, response -> {
                Type type = new TypeToken<List<ShoppingListItem>>() {
                }.getType();
                ArrayList<ShoppingListItem> shoppingListItems = new Gson().fromJson(response, type);
                if (debug) {
                    Log.i(tag, "download ShoppingListItems: " + shoppingListItems);
                }
                if (onResponseListener != null) {
                    onResponseListener.onResponse(shoppingListItems);
                }
                if (responseListener != null) {
                    responseListener.onResponse(response);
                }
            }, error -> {
                if (onErrorListener != null) {
                    onErrorListener.onError(error);
                }
                if (errorListener != null) {
                    errorListener.onError(error);
                }
            });
        }
    };
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) ArrayList(java.util.ArrayList) ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) List(java.util.List) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) Nullable(androidx.annotation.Nullable)

Example 4 with ShoppingListItem

use of xyz.zedler.patrick.grocy.model.ShoppingListItem in project grocy-android by patzly.

the class ShoppingListItemEditViewModel method deleteItem.

public void deleteItem() {
    if (!isActionEdit()) {
        return;
    }
    ShoppingListItem shoppingListItem = args.getShoppingListItem();
    assert shoppingListItem != null;
    dlHelper.delete(grocyApi.getObject(GrocyApi.ENTITY.SHOPPING_LIST, shoppingListItem.getId()), response -> navigateUp(), this::showErrorMessage);
}
Also used : ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem)

Example 5 with ShoppingListItem

use of xyz.zedler.patrick.grocy.model.ShoppingListItem in project grocy-android by patzly.

the class ShoppingModeViewModel method tidyUpItems.

private void tidyUpItems(OnTidyUpFinishedListener onFinished) {
    // Tidy up lost shopping list items, which have deleted shopping lists
    // as an id – else they will never show up on any shopping list
    ArrayList<Integer> listIds = new ArrayList<>();
    if (isFeatureEnabled(Constants.PREF.FEATURE_MULTIPLE_SHOPPING_LISTS)) {
        for (ShoppingList shoppingList : shoppingLists) {
            listIds.add(shoppingList.getId());
        }
        if (listIds.isEmpty()) {
            if (onFinished != null) {
                onFinished.run(false);
            }
            // possible if download error happened
            return;
        }
    } else {
        // id of first and single shopping list
        listIds.add(1);
    }
    DownloadHelper.Queue queue = dlHelper.newQueue(() -> {
        if (onFinished != null) {
            onFinished.run(true);
        }
    }, error -> {
        if (onFinished != null) {
            onFinished.run(true);
        }
    });
    for (ShoppingListItem listItem : shoppingListItems) {
        if (listIds.contains(listItem.getShoppingListIdInt())) {
            continue;
        }
        if (debug) {
            Log.i(TAG, "tidyUpItems: " + listItem);
        }
        queue.append(dlHelper.deleteShoppingListItem(listItem.getId()));
    }
    if (queue.getSize() == 0) {
        onFinished.run(false);
        return;
    }
    currentQueueLoading = queue;
    queue.start();
}
Also used : ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) ArrayList(java.util.ArrayList) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper)

Aggregations

ShoppingListItem (xyz.zedler.patrick.grocy.model.ShoppingListItem)25 ArrayList (java.util.ArrayList)17 Product (xyz.zedler.patrick.grocy.model.Product)15 ShoppingList (xyz.zedler.patrick.grocy.model.ShoppingList)12 QuantityUnit (xyz.zedler.patrick.grocy.model.QuantityUnit)11 HashMap (java.util.HashMap)10 DownloadHelper (xyz.zedler.patrick.grocy.helper.DownloadHelper)10 Nullable (androidx.annotation.Nullable)9 SharedPreferences (android.content.SharedPreferences)8 NonNull (androidx.annotation.NonNull)8 MutableLiveData (androidx.lifecycle.MutableLiveData)8 PreferenceManager (androidx.preference.PreferenceManager)8 R (xyz.zedler.patrick.grocy.R)8 InfoFullscreen (xyz.zedler.patrick.grocy.model.InfoFullscreen)8 ProductGroup (xyz.zedler.patrick.grocy.model.ProductGroup)8 Constants (xyz.zedler.patrick.grocy.util.Constants)8 Application (android.app.Application)7 Spanned (android.text.Spanned)7 Log (android.util.Log)7 VolleyError (com.android.volley.VolleyError)7