Search in sources :

Example 1 with GroupedListItem

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

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

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

the class ShoppingListItemAdapter method getGroupedListItems.

static ArrayList<GroupedListItem> getGroupedListItems(Context context, ArrayList<ShoppingListItem> shoppingListItems, HashMap<Integer, ProductGroup> productGroupHashMap, HashMap<Integer, Product> productHashMap, HashMap<Integer, String> productNamesHashMap, HashMap<Integer, Store> storeHashMap, String shoppingListNotes, String groupingMode) {
    if (groupingMode.equals(FilterChipLiveDataShoppingListGrouping.GROUPING_NONE)) {
        SortUtil.sortShoppingListItemsByName(context, shoppingListItems, productNamesHashMap, true);
        ArrayList<GroupedListItem> groupedListItems = new ArrayList<>(shoppingListItems);
        addBottomNotes(context, shoppingListNotes, groupedListItems, !shoppingListItems.isEmpty());
        return groupedListItems;
    }
    HashMap<String, ArrayList<ShoppingListItem>> shoppingListItemsGroupedHashMap = new HashMap<>();
    ArrayList<ShoppingListItem> ungroupedItems = new ArrayList<>();
    for (ShoppingListItem shoppingListItem : shoppingListItems) {
        String groupName = null;
        if (groupingMode.equals(FilterChipLiveDataShoppingListGrouping.GROUPING_PRODUCT_GROUP) && shoppingListItem.hasProduct()) {
            Product product = productHashMap.get(shoppingListItem.getProductIdInt());
            Integer productGroupId = product != null && NumUtil.isStringInt(product.getProductGroupId()) ? Integer.parseInt(product.getProductGroupId()) : null;
            ProductGroup productGroup = productGroupId != null ? productGroupHashMap.get(productGroupId) : null;
            groupName = productGroup != null ? productGroup.getName() : null;
        } else if (groupingMode.equals(FilterChipLiveDataShoppingListGrouping.GROUPING_STORE) && shoppingListItem.hasProduct()) {
            Product product = productHashMap.get(shoppingListItem.getProductIdInt());
            Integer storeId = product != null && NumUtil.isStringInt(product.getStoreId()) ? Integer.parseInt(product.getStoreId()) : null;
            Store store = storeId != null ? storeHashMap.get(storeId) : null;
            groupName = store != null ? store.getName() : null;
        }
        if (groupName != null && !groupName.isEmpty()) {
            ArrayList<ShoppingListItem> itemsFromGroup = shoppingListItemsGroupedHashMap.get(groupName);
            if (itemsFromGroup == null) {
                itemsFromGroup = new ArrayList<>();
                shoppingListItemsGroupedHashMap.put(groupName, itemsFromGroup);
            }
            itemsFromGroup.add(shoppingListItem);
        } else {
            ungroupedItems.add(shoppingListItem);
        }
    }
    ArrayList<GroupedListItem> groupedListItems = new ArrayList<>();
    ArrayList<String> groupsSorted = new ArrayList<>(shoppingListItemsGroupedHashMap.keySet());
    SortUtil.sortStringsByName(context, groupsSorted, true);
    if (!ungroupedItems.isEmpty()) {
        groupedListItems.add(new GroupHeader(context.getString(R.string.property_ungrouped)));
        SortUtil.sortShoppingListItemsByName(context, ungroupedItems, productNamesHashMap, true);
        groupedListItems.addAll(ungroupedItems);
    }
    for (String group : groupsSorted) {
        ArrayList<ShoppingListItem> itemsFromGroup = shoppingListItemsGroupedHashMap.get(group);
        if (itemsFromGroup == null)
            continue;
        GroupHeader groupHeader = new GroupHeader(group);
        groupHeader.setDisplayDivider(!ungroupedItems.isEmpty() || !groupsSorted.get(0).equals(group));
        groupedListItems.add(groupHeader);
        SortUtil.sortShoppingListItemsByName(context, itemsFromGroup, productNamesHashMap, true);
        groupedListItems.addAll(itemsFromGroup);
    }
    addBottomNotes(context, shoppingListNotes, groupedListItems, !ungroupedItems.isEmpty() || !groupsSorted.isEmpty());
    return groupedListItems;
}
Also used : ProductGroup(xyz.zedler.patrick.grocy.model.ProductGroup) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) HashMap(java.util.HashMap) GroupHeader(xyz.zedler.patrick.grocy.model.GroupHeader) ArrayList(java.util.ArrayList) Product(xyz.zedler.patrick.grocy.model.Product) Store(xyz.zedler.patrick.grocy.model.Store) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem)

Example 4 with GroupedListItem

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

the class ShoppingModeItemAdapter 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) {
        ShoppingGroupViewHolder holder = (ShoppingGroupViewHolder) viewHolder;
        String productGroupName = ((ProductGroup) groupedListItem).getName();
        if (useSmallerFonts) {
            holder.binding.name.setTextSize(14.5f);
        }
        holder.binding.name.setText(productGroupName);
        if (!productGroupName.equals(holder.binding.name.getContext().getString(R.string.subtitle_done))) {
            holder.binding.name.setTextColor(ContextCompat.getColor(holder.binding.name.getContext(), R.color.retro_green_fg));
            holder.binding.separator.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.binding.name.getContext(), R.color.retro_green_fg)));
        } else {
            holder.binding.name.setTextColor(ContextCompat.getColor(holder.binding.name.getContext(), R.color.retro_yellow_fg));
            holder.binding.separator.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.binding.name.getContext(), R.color.retro_yellow_fg)));
        }
        return;
    }
    if (type == GroupedListItem.TYPE_BOTTOM_NOTES) {
        ShoppingNotesViewHolder holder = (ShoppingNotesViewHolder) viewHolder;
        holder.binding.notes.setText(((ShoppingListBottomNotes) groupedListItem).getNotes());
        holder.binding.notes.setOnClickListener(view -> listener.onItemRowClicked(groupedListItem));
        return;
    }
    ShoppingListItem item = (ShoppingListItem) groupedListItem;
    RowShoppingItemBinding binding = ((ShoppingItemViewHolder) viewHolder).binding;
    if (useSmallerFonts) {
        int px4 = UnitUtil.dpToPx(binding.name.getContext(), 4f);
        binding.card.setContentPadding(px4 * 2, px4, px4 * 2, px4);
        LinearLayout.LayoutParams paramsCard = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        paramsCard.setMargins(px4 * 4, px4, px4 * 4, px4);
        binding.card.setLayoutParams(paramsCard);
        LinearLayout.LayoutParams paramsRow = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        paramsRow.setMargins(0, 0, 0, 0);
        binding.name.setLayoutParams(paramsRow);
        binding.name.setTextSize(18f);
        binding.noteAsName.setLayoutParams(paramsRow);
        binding.noteAsName.setTextSize(18f);
    }
    // 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(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.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());
            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));
        } 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);
        }
    }
    if (showProductDescription) {
        String productDescription = product != null ? product.getDescription() : null;
        Spanned description = productDescription != null ? Html.fromHtml(productDescription) : null;
        description = (Spanned) TextUtil.trimCharSequence(description);
        if (description != null && !description.toString().isEmpty()) {
            binding.cardDescription.setText(description.toString());
            binding.cardDescription.setVisibility(View.VISIBLE);
        } else {
            binding.cardDescription.setVisibility(View.GONE);
        }
    }
    binding.containerRow.setAlpha(item.getDoneInt() == 1 ? 0.4f : 1);
    // CONTAINER
    binding.card.setOnClickListener(view -> listener.onItemRowClicked(groupedListItem));
}
Also used : ProductGroup(xyz.zedler.patrick.grocy.model.ProductGroup) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) LayoutParams(android.widget.LinearLayout.LayoutParams) Product(xyz.zedler.patrick.grocy.model.Product) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) RowShoppingItemBinding(xyz.zedler.patrick.grocy.databinding.RowShoppingItemBinding) Spanned(android.text.Spanned) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) LayoutParams(android.widget.LinearLayout.LayoutParams) LinearLayout(android.widget.LinearLayout) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) SuppressLint(android.annotation.SuppressLint)

Example 5 with GroupedListItem

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

the class StockOverviewItemAdapter method getGroupedListItems.

static ArrayList<GroupedListItem> getGroupedListItems(Context context, ArrayList<StockItem> stockItems, HashMap<Integer, ProductGroup> productGroupHashMap, HashMap<Integer, Product> productHashMap, HashMap<Integer, Location> locationHashMap, String currency, DateUtil dateUtil, String sortMode, boolean sortAscending, String groupingMode) {
    if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_NONE)) {
        sortStockItems(context, stockItems, sortMode, sortAscending);
        return new ArrayList<>(stockItems);
    }
    HashMap<String, ArrayList<StockItem>> stockItemsGroupedHashMap = new HashMap<>();
    ArrayList<StockItem> ungroupedItems = new ArrayList<>();
    for (StockItem stockItem : stockItems) {
        String groupName = null;
        if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_PRODUCT_GROUP) && NumUtil.isStringInt(stockItem.getProduct().getProductGroupId())) {
            int productGroupId = Integer.parseInt(stockItem.getProduct().getProductGroupId());
            ProductGroup productGroup = productGroupHashMap.get(productGroupId);
            groupName = productGroup != null ? productGroup.getName() : null;
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_VALUE)) {
            groupName = NumUtil.trimPrice(stockItem.getValueDouble());
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_CALORIES_PER_STOCK)) {
            groupName = NumUtil.isStringDouble(stockItem.getProduct().getCalories()) ? stockItem.getProduct().getCalories() : null;
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_CALORIES)) {
            groupName = NumUtil.isStringDouble(stockItem.getProduct().getCalories()) ? NumUtil.trim(Double.parseDouble(stockItem.getProduct().getCalories()) * stockItem.getAmountDouble()) : null;
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_DUE_DATE)) {
            groupName = stockItem.getBestBeforeDate();
            if (groupName != null && !groupName.isEmpty()) {
                groupName += "  " + dateUtil.getHumanForDaysFromNow(groupName);
            }
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_MIN_STOCK_AMOUNT)) {
            groupName = stockItem.getProduct().getMinStockAmount();
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_PARENT_PRODUCT) && NumUtil.isStringInt(stockItem.getProduct().getParentProductId())) {
            int productId = Integer.parseInt(stockItem.getProduct().getParentProductId());
            Product product = productHashMap.get(productId);
            groupName = product != null ? product.getName() : null;
        } else if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_DEFAULT_LOCATION) && NumUtil.isStringInt(stockItem.getProduct().getLocationId())) {
            int locationId = Integer.parseInt(stockItem.getProduct().getLocationId());
            Location location = locationHashMap.get(locationId);
            groupName = location != null ? location.getName() : null;
        }
        if (groupName != null && !groupName.isEmpty()) {
            ArrayList<StockItem> itemsFromGroup = stockItemsGroupedHashMap.get(groupName);
            if (itemsFromGroup == null) {
                itemsFromGroup = new ArrayList<>();
                stockItemsGroupedHashMap.put(groupName, itemsFromGroup);
            }
            itemsFromGroup.add(stockItem);
        } else {
            ungroupedItems.add(stockItem);
        }
    }
    ArrayList<GroupedListItem> groupedListItems = new ArrayList<>();
    ArrayList<String> groupsSorted = new ArrayList<>(stockItemsGroupedHashMap.keySet());
    if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_VALUE) || groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_CALORIES) || groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_MIN_STOCK_AMOUNT)) {
        SortUtil.sortStringsByValue(groupsSorted);
    } else {
        SortUtil.sortStringsByName(context, groupsSorted, true);
    }
    if (!ungroupedItems.isEmpty()) {
        groupedListItems.add(new GroupHeader(context.getString(R.string.property_ungrouped)));
        sortStockItems(context, ungroupedItems, sortMode, sortAscending);
        groupedListItems.addAll(ungroupedItems);
    }
    for (String group : groupsSorted) {
        ArrayList<StockItem> itemsFromGroup = stockItemsGroupedHashMap.get(group);
        if (itemsFromGroup == null)
            continue;
        String groupString;
        if (groupingMode.equals(FilterChipLiveDataStockGrouping.GROUPING_VALUE)) {
            groupString = group + " " + currency;
        } else {
            groupString = group;
        }
        GroupHeader groupHeader = new GroupHeader(groupString);
        groupHeader.setDisplayDivider(!ungroupedItems.isEmpty() || !groupsSorted.get(0).equals(group));
        groupedListItems.add(groupHeader);
        sortStockItems(context, itemsFromGroup, sortMode, sortAscending);
        groupedListItems.addAll(itemsFromGroup);
    }
    return groupedListItems;
}
Also used : ProductGroup(xyz.zedler.patrick.grocy.model.ProductGroup) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) HashMap(java.util.HashMap) GroupHeader(xyz.zedler.patrick.grocy.model.GroupHeader) ArrayList(java.util.ArrayList) Product(xyz.zedler.patrick.grocy.model.Product) StockItem(xyz.zedler.patrick.grocy.model.StockItem) SuppressLint(android.annotation.SuppressLint) Location(xyz.zedler.patrick.grocy.model.Location)

Aggregations

GroupedListItem (xyz.zedler.patrick.grocy.model.GroupedListItem)8 Product (xyz.zedler.patrick.grocy.model.Product)6 ArrayList (java.util.ArrayList)5 QuantityUnit (xyz.zedler.patrick.grocy.model.QuantityUnit)5 ShoppingListItem (xyz.zedler.patrick.grocy.model.ShoppingListItem)5 SuppressLint (android.annotation.SuppressLint)4 HashMap (java.util.HashMap)4 ProductGroup (xyz.zedler.patrick.grocy.model.ProductGroup)4 Spanned (android.text.Spanned)3 StockItem (xyz.zedler.patrick.grocy.model.StockItem)3 Paint (android.graphics.Paint)2 Bundle (android.os.Bundle)2 LayoutInflater (android.view.LayoutInflater)2 MenuItem (android.view.MenuItem)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2