Search in sources :

Example 1 with Location

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);
}
Also used : Bundle(android.os.Bundle) LocationsBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.LocationsBottomSheet) Location(xyz.zedler.patrick.grocy.model.Location)

Example 2 with Location

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();
}
Also used : BottomSheetDialog(com.google.android.material.bottomsheet.BottomSheetDialog) Chip(com.google.android.material.chip.Chip) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) Spanned(android.text.Spanned) TypeToken(com.google.gson.reflect.TypeToken) FragmentBottomsheetProductOverviewBinding(xyz.zedler.patrick.grocy.databinding.FragmentBottomsheetProductOverviewBinding) NonNull(androidx.annotation.NonNull) PriceHistoryEntry(xyz.zedler.patrick.grocy.model.PriceHistoryEntry) NumUtil(xyz.zedler.patrick.grocy.util.NumUtil) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) Dialog(android.app.Dialog) HashMap(java.util.HashMap) TextUtil(xyz.zedler.patrick.grocy.util.TextUtil) MasterProductFragmentArgs(xyz.zedler.patrick.grocy.fragment.MasterProductFragmentArgs) ProductDetails(xyz.zedler.patrick.grocy.model.ProductDetails) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) ArrayList(java.util.ArrayList) AmountUtil(xyz.zedler.patrick.grocy.util.AmountUtil) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) R(xyz.zedler.patrick.grocy.R) Gson(com.google.gson.Gson) View(android.view.View) Build(android.os.Build) StockItem(xyz.zedler.patrick.grocy.model.StockItem) MenuCompat(androidx.core.view.MenuCompat) NavHostFragment(androidx.navigation.fragment.NavHostFragment) PREF(xyz.zedler.patrick.grocy.util.Constants.PREF) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) BezierCurveChart(xyz.zedler.patrick.grocy.view.BezierCurveChart) LayoutInflater(android.view.LayoutInflater) Constants(xyz.zedler.patrick.grocy.util.Constants) Location(xyz.zedler.patrick.grocy.model.Location) ViewGroup(android.view.ViewGroup) Nullable(androidx.annotation.Nullable) SharedPreferences(android.content.SharedPreferences) Product(xyz.zedler.patrick.grocy.model.Product) Type(java.lang.reflect.Type) DateUtil(xyz.zedler.patrick.grocy.util.DateUtil) Html(android.text.Html) ViewTreeObserver(android.view.ViewTreeObserver) PreferenceManager(androidx.preference.PreferenceManager) ShoppingListItemEditFragmentArgs(xyz.zedler.patrick.grocy.fragment.ShoppingListItemEditFragmentArgs) Store(xyz.zedler.patrick.grocy.model.Store) Collections(java.util.Collections) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) ValueAnimator(android.animation.ValueAnimator) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) MasterProductFragmentArgs(xyz.zedler.patrick.grocy.fragment.MasterProductFragmentArgs) StockItem(xyz.zedler.patrick.grocy.model.StockItem) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) Chip(com.google.android.material.chip.Chip) Spanned(android.text.Spanned) LinearLayout(android.widget.LinearLayout)

Example 3 with Location

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();
        }
    });
}
Also used : StockLocation(xyz.zedler.patrick.grocy.model.StockLocation) Product(xyz.zedler.patrick.grocy.model.Product) ProductBarcode(xyz.zedler.patrick.grocy.model.ProductBarcode) StockItem(xyz.zedler.patrick.grocy.model.StockItem) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) StockLocation(xyz.zedler.patrick.grocy.model.StockLocation) Location(xyz.zedler.patrick.grocy.model.Location) FilterChipLiveDataLocation(xyz.zedler.patrick.grocy.model.FilterChipLiveDataLocation)

Example 4 with Location

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()));
}
Also used : Location(xyz.zedler.patrick.grocy.model.Location) SuppressLint(android.annotation.SuppressLint)

Example 5 with Location

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;
}
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

Location (xyz.zedler.patrick.grocy.model.Location)10 Bundle (android.os.Bundle)4 ArrayList (java.util.ArrayList)4 Product (xyz.zedler.patrick.grocy.model.Product)4 QuantityUnit (xyz.zedler.patrick.grocy.model.QuantityUnit)4 StockItem (xyz.zedler.patrick.grocy.model.StockItem)4 Nullable (androidx.annotation.Nullable)3 HashMap (java.util.HashMap)3 StockLocation (xyz.zedler.patrick.grocy.model.StockLocation)3 SuppressLint (android.annotation.SuppressLint)2 SharedPreferences (android.content.SharedPreferences)2 View (android.view.View)2 NonNull (androidx.annotation.NonNull)2 PreferenceManager (androidx.preference.PreferenceManager)2 Type (java.lang.reflect.Type)2 R (xyz.zedler.patrick.grocy.R)2 LocationsBottomSheet (xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.LocationsBottomSheet)2 DownloadHelper (xyz.zedler.patrick.grocy.helper.DownloadHelper)2 FilterChipLiveDataLocation (xyz.zedler.patrick.grocy.model.FilterChipLiveDataLocation)2 ProductGroup (xyz.zedler.patrick.grocy.model.ProductGroup)2