use of xyz.zedler.patrick.grocy.model.Location in project grocy-android by patzly.
the class MasterProductCatLocationFragment method showLocationsBottomSheet.
public void showLocationsBottomSheet() {
ArrayList<Location> locations = viewModel.getFormData().getLocationsLive().getValue();
if (locations == null) {
viewModel.showErrorMessage();
return;
}
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(Constants.ARGUMENT.LOCATIONS, locations);
Location location = viewModel.getFormData().getLocationLive().getValue();
int locationId = location != null ? location.getId() : -1;
bundle.putInt(Constants.ARGUMENT.SELECTED_ID, locationId);
activity.showBottomSheet(new LocationsBottomSheet(), bundle);
}
use of xyz.zedler.patrick.grocy.model.Location in project grocy-android by patzly.
the class ProductOverviewBottomSheet method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
activity = (MainActivity) requireActivity();
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
pluralUtil = new PluralUtil(activity);
ProductOverviewBottomSheetArgs args = ProductOverviewBottomSheetArgs.fromBundle(requireArguments());
boolean showActions = args.getShowActions();
if (args.getProductDetails() != null) {
productDetails = args.getProductDetails();
product = productDetails.getProduct();
stockItem = new StockItem(productDetails);
} else if (args.getStockItem() != null) {
stockItem = args.getStockItem();
quantityUnit = args.getQuantityUnit();
location = args.getLocation();
product = stockItem.getProduct();
}
// WEB REQUESTS
dlHelper = new DownloadHelper(activity, TAG);
// VIEWS
refreshItems();
binding.textName.setText(product.getName());
// TOOLBAR
boolean isInStock = stockItem.getAmountDouble() > 0;
MenuCompat.setGroupDividerEnabled(binding.toolbar.getMenu(), true);
// disable actions if necessary
binding.toolbar.getMenu().findItem(R.id.action_consume_all).setEnabled(isInStock);
binding.toolbar.getMenu().findItem(R.id.action_consume_spoiled).setEnabled(isInStock && product.getEnableTareWeightHandlingInt() == 0);
binding.toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.action_add_to_shopping_list) {
navigateDeepLink(R.string.deep_link_shoppingListItemEditFragment, new ShoppingListItemEditFragmentArgs.Builder(Constants.ACTION.CREATE).setProductId(String.valueOf(product.getId())).build().toBundle());
dismiss();
return true;
} else if (item.getItemId() == R.id.action_consume_all) {
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME_ALL, stockItem);
dismiss();
return true;
} else if (item.getItemId() == R.id.action_consume_spoiled) {
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME_SPOILED, stockItem);
dismiss();
return true;
} else if (item.getItemId() == R.id.action_edit_product) {
String productId = String.valueOf(product.getId());
navigateDeepLink(R.string.deep_link_masterProductFragment, new MasterProductFragmentArgs.Builder(Constants.ACTION.EDIT).setProductId(productId).build().toBundle());
dismiss();
return true;
}
return false;
});
Chip chipConsume = view.findViewById(R.id.chip_consume);
chipConsume.setVisibility(isInStock ? View.VISIBLE : View.GONE);
chipConsume.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToConsumeFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipPurchase = view.findViewById(R.id.chip_purchase);
chipPurchase.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToPurchaseFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipTransfer = view.findViewById(R.id.chip_transfer);
chipTransfer.setVisibility(isInStock && product.getEnableTareWeightHandlingInt() == 0 ? View.VISIBLE : View.GONE);
chipTransfer.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToTransferFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipInventory = view.findViewById(R.id.chip_inventory);
chipInventory.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToInventoryFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
if (!showActions) {
view.findViewById(R.id.container_chips).setVisibility(View.GONE);
}
// DESCRIPTION
Spanned description = product.getDescription() != null ? Html.fromHtml(product.getDescription()) : null;
description = (Spanned) TextUtil.trimCharSequence(description);
if (description != null && !description.toString().isEmpty()) {
binding.cardDescription.setText(description.toString());
} else {
binding.cardDescription.setVisibility(View.GONE);
}
if (!showActions) {
// hide actions when set up with productDetails
binding.linearActionContainer.setVisibility(View.GONE);
// set info menu
binding.toolbar.getMenu().clear();
binding.toolbar.inflateMenu(R.menu.menu_actions_product_overview_info);
}
refreshButtonStates(false);
binding.buttonConsume.setOnClickListener(v -> {
disableActions();
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME, stockItem);
dismiss();
});
binding.buttonOpen.setOnClickListener(v -> {
disableActions();
activity.getCurrentFragment().performAction(Constants.ACTION.OPEN, stockItem);
dismiss();
});
// tooltips
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
binding.buttonConsume.setTooltipText(activity.getString(R.string.action_consume_one, quantityUnit.getName(), product.getName()));
binding.buttonOpen.setTooltipText(activity.getString(R.string.action_open_one, quantityUnit.getName(), product.getName()));
// TODO: tooltip colors
}
// no margin if description is != null
if (description != null) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
binding.linearAmount.setLayoutParams(layoutParams);
}
if (activity.isOnline() && !hasDetails()) {
dlHelper.getProductDetails(product.getId(), details -> {
productDetails = details;
stockItem = new StockItem(productDetails);
refreshButtonStates(true);
refreshItems();
loadPriceHistory();
}).perform(dlHelper.getUuid());
} else if (activity.isOnline() && hasDetails()) {
loadPriceHistory();
}
hideDisabledFeatures();
}
use of xyz.zedler.patrick.grocy.model.Location in project grocy-android by patzly.
the class StockOverviewViewModel method loadFromDatabase.
public void loadFromDatabase(boolean downloadAfterLoading) {
repository.loadFromDatabase((quantityUnits, productGroups, stockItems, products, barcodes, shoppingListItems, locations, stockLocations) -> {
this.quantityUnits = quantityUnits;
quantityUnitHashMap = new HashMap<>();
for (QuantityUnit quantityUnit : quantityUnits) {
quantityUnitHashMap.put(quantityUnit.getId(), quantityUnit);
}
this.productGroups = productGroups;
productGroupHashMap = ArrayUtil.getProductGroupsHashMap(productGroups);
filterChipLiveDataProductGroup.setProductGroups(productGroups);
this.products = products;
productHashMap = new HashMap<>();
for (Product product : products) {
productHashMap.put(product.getId(), product);
}
this.productBarcodesTemp = barcodes;
productBarcodeHashMap = new HashMap<>();
for (ProductBarcode barcode : barcodes) {
productBarcodeHashMap.put(barcode.getBarcode().toLowerCase(), barcode);
}
int itemsDueCount = 0;
int itemsOverdueCount = 0;
int itemsExpiredCount = 0;
int itemsMissingCount = 0;
int itemsInStockCount = 0;
int itemsOpenedCount = 0;
productIdsMissingStockItems = new HashMap<>();
this.stockItems = stockItems;
for (StockItem stockItem : stockItems) {
stockItem.setProduct(productHashMap.get(stockItem.getProductId()));
if (stockItem.isItemDue()) {
itemsDueCount++;
}
if (stockItem.isItemOverdue()) {
itemsOverdueCount++;
}
if (stockItem.isItemExpired()) {
itemsExpiredCount++;
}
if (stockItem.isItemMissing()) {
itemsMissingCount++;
productIdsMissingStockItems.put(stockItem.getProductId(), stockItem);
}
if (!stockItem.isItemMissing() || stockItem.isItemMissingAndPartlyInStock()) {
itemsInStockCount++;
}
if (stockItem.getAmountOpenedDouble() > 0) {
itemsOpenedCount++;
}
}
this.shoppingListItems = shoppingListItems;
shoppingListItemsProductIds = new ArrayList<>();
for (ShoppingListItem item : shoppingListItems) {
if (item.getProductId() != null && !item.getProductId().isEmpty()) {
shoppingListItemsProductIds.add(item.getProductId());
}
}
this.locations = locations;
filterChipLiveDataLocation.setLocations(locations);
locationHashMap = new HashMap<>();
for (Location location : locations) {
locationHashMap.put(location.getId(), location);
}
this.stockCurrentLocationsTemp = stockLocations;
stockLocationsHashMap = new HashMap<>();
for (StockLocation stockLocation : stockLocations) {
HashMap<Integer, StockLocation> locationsForProductId = stockLocationsHashMap.get(stockLocation.getProductId());
if (locationsForProductId == null) {
locationsForProductId = new HashMap<>();
stockLocationsHashMap.put(stockLocation.getProductId(), locationsForProductId);
}
locationsForProductId.put(stockLocation.getLocationId(), stockLocation);
}
filterChipLiveDataStatus.setDueSoonCount(itemsDueCount).setOverdueCount(itemsOverdueCount).setExpiredCount(itemsExpiredCount).setBelowStockCount(itemsMissingCount).setInStockCount(itemsInStockCount).setOpenedCount(itemsOpenedCount).emitCounts();
updateFilteredStockItems();
if (downloadAfterLoading) {
downloadData();
}
});
}
use of xyz.zedler.patrick.grocy.model.Location in project grocy-android by patzly.
the class LocationAdapter method onBindViewHolder.
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull final LocationAdapter.ViewHolder holder, int position) {
Location location = locations.get(holder.getAdapterPosition());
// NAME
holder.textViewName.setText(location.getName());
if (location.getId() == selectedId) {
holder.imageViewSelected.setVisibility(View.VISIBLE);
}
// CONTAINER
holder.linearLayoutContainer.setOnClickListener(view -> listener.onItemRowClicked(holder.getAdapterPosition()));
}
use of xyz.zedler.patrick.grocy.model.Location 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;
}
Aggregations