Search in sources :

Example 1 with QuantityUnit

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

the class ProductBarcodeAdapter method onBindViewHolder.

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull final ProductBarcodeAdapter.ViewHolder holder, int position) {
    ProductBarcode productBarcode = productBarcodes.get(holder.getAdapterPosition());
    holder.binding.barcode.setText(productBarcode.getBarcode());
    if (NumUtil.isStringDouble(productBarcode.getAmount())) {
        String amountStr = holder.binding.amount.getContext().getString(R.string.subtitle_barcode_amount, NumUtil.trim(Double.parseDouble(productBarcode.getAmount())));
        holder.binding.amount.setText(amountStr);
        holder.binding.amount.setVisibility(View.VISIBLE);
    } else {
        holder.binding.amount.setVisibility(View.GONE);
    }
    if (productBarcode.getNote() != null && !productBarcode.getNote().trim().isEmpty()) {
        holder.binding.note.setText(productBarcode.getNote());
        holder.binding.note.setVisibility(View.VISIBLE);
    } else {
        holder.binding.note.setVisibility(View.GONE);
    }
    if (productBarcode.getQuIdInt() != -1) {
        for (QuantityUnit qU : quantityUnits) {
            if (qU.getId() == productBarcode.getQuIdInt()) {
                String qUnitStr = holder.binding.amount.getContext().getString(R.string.subtitle_barcode_unit, qU.getName());
                holder.binding.quantityUnit.setText(qUnitStr);
                holder.binding.quantityUnit.setVisibility(View.VISIBLE);
            }
        }
    } else {
        holder.binding.quantityUnit.setVisibility(View.GONE);
    }
    if (productBarcode.getStoreIdInt() != -1) {
        for (Store store : stores) {
            if (store.getId() == productBarcode.getStoreIdInt()) {
                // TODO create dedicated string
                String storeStr = holder.binding.store.getContext().getString(R.string.property_store) + ": " + store.getName();
                holder.binding.store.setText(storeStr);
                holder.binding.store.setVisibility(View.VISIBLE);
            }
        }
    } else {
        holder.binding.store.setVisibility(View.GONE);
    }
    holder.binding.container.setOnClickListener(view -> listener.onItemRowClicked(productBarcode));
}
Also used : Store(xyz.zedler.patrick.grocy.model.Store) ProductBarcode(xyz.zedler.patrick.grocy.model.ProductBarcode) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) SuppressLint(android.annotation.SuppressLint)

Example 2 with QuantityUnit

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

the class ShoppingListItemAdapter method fillShoppingListItem.

// Only for PurchaseFragment
public static void fillShoppingListItem(Context context, ShoppingListItem item, RowShoppingListItemBinding binding, HashMap<Integer, Product> productHashMap, HashMap<Integer, QuantityUnit> quantityUnitHashMap, HashMap<Integer, Double> shoppingListItemAmountsHashMap, PluralUtil pluralUtil) {
    // 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));
    } else {
        binding.name.setPaintFlags(binding.name.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }
    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(context.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(context.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()));
    }
    binding.amount.setTypeface(ResourcesCompat.getFont(context, R.font.jost_book));
    binding.amount.setTextColor(ContextCompat.getColor(context, R.color.on_background_secondary));
    if (item.isUndone()) {
        binding.amount.setPaintFlags(binding.amount.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
    } else {
        binding.amount.setPaintFlags(binding.amount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }
    if (item.getNote() != null && !item.getNote().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());
        }
    } 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));
        } else {
            binding.noteAsName.setPaintFlags(binding.noteAsName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
    } else {
        if (item.isUndone()) {
            binding.note.setPaintFlags(binding.note.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        } else {
            binding.note.setPaintFlags(binding.note.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
    }
}
Also used : Product(xyz.zedler.patrick.grocy.model.Product) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit)

Example 3 with QuantityUnit

use of xyz.zedler.patrick.grocy.model.QuantityUnit 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 4 with QuantityUnit

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

the class StockOverviewItemAdapter 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) {
        GroupViewHolder holder = (GroupViewHolder) 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;
    }
    StockItem stockItem = (StockItem) groupedListItem;
    StockItemViewHolder holder = (StockItemViewHolder) viewHolder;
    // NAME
    holder.binding.textName.setText(stockItem.getProduct().getName());
    if (shoppingListItemsProductIds.contains(String.valueOf(stockItem.getProduct().getId())) && shoppingListFeatureEnabled) {
        holder.binding.viewOnShoppingList.setVisibility(View.VISIBLE);
    } else {
        holder.binding.viewOnShoppingList.setVisibility(View.GONE);
    }
    Context context = holder.binding.textAmount.getContext();
    // AMOUNT
    QuantityUnit quantityUnit = quantityUnitHashMap.get(stockItem.getProduct().getQuIdStockInt());
    holder.binding.textAmount.setText(AmountUtil.getStockAmountInfo(context, pluralUtil, stockItem, quantityUnit));
    if (missingItemsProductIds.contains(stockItem.getProductId())) {
        holder.binding.textAmount.setTypeface(ResourcesCompat.getFont(context, R.font.jost_medium));
        holder.binding.textAmount.setTextColor(ContextCompat.getColor(context, R.color.retro_blue_fg));
    } else {
        holder.binding.textAmount.setTypeface(ResourcesCompat.getFont(context, R.font.jost_book));
        holder.binding.textAmount.setTextColor(ContextCompat.getColor(context, R.color.on_background_secondary));
    }
    // BEST BEFORE
    String date = stockItem.getBestBeforeDate();
    String days = null;
    boolean colorDays = false;
    if (date != null) {
        days = String.valueOf(DateUtil.getDaysFromNow(date));
    }
    if (!showDateTracking) {
        holder.binding.linearDays.setVisibility(View.GONE);
    } else if (days != null && (sortMode.equals(FilterChipLiveDataStockSort.SORT_DUE_DATE) || Integer.parseInt(days) <= daysExpiringSoon && !date.equals(Constants.DATE.NEVER_OVERDUE))) {
        holder.binding.linearDays.setVisibility(View.VISIBLE);
        holder.binding.textDays.setText(new DateUtil(context).getHumanForDaysFromNow(date));
        if (Integer.parseInt(days) <= daysExpiringSoon) {
            colorDays = true;
        }
    } else {
        holder.binding.linearDays.setVisibility(View.GONE);
        holder.binding.textDays.setText(null);
    }
    if (colorDays) {
        holder.binding.textDays.setTypeface(ResourcesCompat.getFont(context, R.font.jost_medium));
        @ColorRes int color;
        if (Integer.parseInt(days) >= 0) {
            color = R.color.retro_yellow_fg;
        } else if (stockItem.getDueTypeInt() == StockItem.DUE_TYPE_BEST_BEFORE) {
            color = R.color.retro_dirt_fg;
        } else {
            color = R.color.retro_red_fg;
        }
        holder.binding.textDays.setTextColor(ContextCompat.getColor(context, color));
    } else {
        holder.binding.textDays.setTypeface(ResourcesCompat.getFont(context, R.font.jost_book));
        holder.binding.textDays.setTextColor(ContextCompat.getColor(context, R.color.on_background_secondary));
    }
    // CONTAINER
    holder.binding.linearContainer.setOnClickListener(view -> listener.onItemRowClicked(stockItem));
}
Also used : Context(android.content.Context) ColorRes(androidx.annotation.ColorRes) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) DateUtil(xyz.zedler.patrick.grocy.util.DateUtil) StockItem(xyz.zedler.patrick.grocy.model.StockItem) SuppressLint(android.annotation.SuppressLint) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) SuppressLint(android.annotation.SuppressLint)

Example 5 with QuantityUnit

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

the class ShoppingListItemEditFragment method showQuantityUnitsBottomSheet.

public void showQuantityUnitsBottomSheet(boolean hasFocus) {
    if (!hasFocus) {
        return;
    }
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList(Constants.ARGUMENT.QUANTITY_UNITS, viewModel.getFormData().getQuantityUnitsLive().getValue());
    QuantityUnit quantityUnit = viewModel.getFormData().getQuantityUnitLive().getValue();
    bundle.putInt(ARGUMENT.SELECTED_ID, quantityUnit != null ? quantityUnit.getId() : -1);
    activity.showBottomSheet(new QuantityUnitsBottomSheet(), bundle);
}
Also used : QuantityUnitsBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.QuantityUnitsBottomSheet) Bundle(android.os.Bundle) EmbeddedFragmentScannerBundle(xyz.zedler.patrick.grocy.scanner.EmbeddedFragmentScannerBundle) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit)

Aggregations

QuantityUnit (xyz.zedler.patrick.grocy.model.QuantityUnit)34 ArrayList (java.util.ArrayList)15 Bundle (android.os.Bundle)14 HashMap (java.util.HashMap)9 Product (xyz.zedler.patrick.grocy.model.Product)9 QuantityUnitsBottomSheet (xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.QuantityUnitsBottomSheet)8 QuantityUnitConversion (xyz.zedler.patrick.grocy.model.QuantityUnitConversion)7 SuppressLint (android.annotation.SuppressLint)5 ShoppingListItem (xyz.zedler.patrick.grocy.model.ShoppingListItem)5 Nullable (androidx.annotation.Nullable)4 Location (xyz.zedler.patrick.grocy.model.Location)4 View (android.view.View)3 Gson (com.google.gson.Gson)3 Type (java.lang.reflect.Type)3 ProductBarcode (xyz.zedler.patrick.grocy.model.ProductBarcode)3 ProductDetails (xyz.zedler.patrick.grocy.model.ProductDetails)3 ProductGroup (xyz.zedler.patrick.grocy.model.ProductGroup)3 StockItem (xyz.zedler.patrick.grocy.model.StockItem)3 StockLocation (xyz.zedler.patrick.grocy.model.StockLocation)3 SharedPreferences (android.content.SharedPreferences)2