Search in sources :

Example 1 with Product

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

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

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

the class MasterProductCatOptionalFragment method onItemAutoCompleteClick.

public void onItemAutoCompleteClick(AdapterView<?> adapterView, int pos) {
    Product product = (Product) adapterView.getItemAtPosition(pos);
    viewModel.getFormData().getProductLive().setValue(product);
    clearInputFocus();
}
Also used : Product(xyz.zedler.patrick.grocy.model.Product)

Example 4 with Product

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

the class MasterProductFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductViewModel.MasterProductViewModelFactory(activity.getApplication(), args)).get(MasterProductViewModel.class);
    if (!viewModel.isActionEdit() && args.getProductName() != null) {
        // remove product name from arguments because it was filled
        // in the form during ViewModel creation
        setArguments(new MasterProductFragmentArgs.Builder(args).setProductName(null).setProductId(null).build().toBundle());
    }
    binding.setActivity(activity);
    binding.setFormData(viewModel.getFormData());
    binding.setViewModel(viewModel);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    binding.categoryOptional.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatOptionalFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryLocation.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatLocationFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryDueDate.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatDueDateFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryAmount.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatAmountFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryQuantityUnit.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatQuantityUnitFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryBarcodes.setOnClickListener(v -> {
        if (!viewModel.isActionEdit()) {
            activity.showMessage(R.string.subtitle_product_not_on_server);
            return;
        }
        navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatBarcodesFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct()));
    });
    binding.categoryQuConversions.setOnClickListener(v -> {
        if (!viewModel.isActionEdit()) {
            activity.showMessage(R.string.subtitle_product_not_on_server);
            return;
        }
        navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatConversionsFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct()));
    });
    Product product = (Product) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT);
    if (product != null) {
        viewModel.setCurrentProduct(product);
        removeForThisDestination(Constants.ARGUMENT.PRODUCT);
    }
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            SnackbarMessage message = (SnackbarMessage) event;
            Snackbar snack = message.getSnackbar(activity, activity.binding.frameMainContainer);
            activity.showSnackbar(snack);
        } else if (event.getType() == Event.NAVIGATE_UP) {
            activity.navigateUp();
        } else if (event.getType() == Event.SET_PRODUCT_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.PRODUCT_ID);
            setForPreviousDestination(Constants.ARGUMENT.PRODUCT_ID, id);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        } else if (event.getType() == Event.FOCUS_INVALID_VIEWS) {
            if (binding.editTextName.getText() == null || binding.editTextName.getText().length() == 0) {
                activity.showKeyboard(binding.editTextName);
            }
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    String action = (String) getFromThisDestinationNow(Constants.ARGUMENT.ACTION);
    if (action != null) {
        removeForThisDestination(Constants.ARGUMENT.ACTION);
        switch(action) {
            case ACTION.SAVE_CLOSE:
                new Handler().postDelayed(() -> viewModel.saveProduct(true), 500);
                break;
            case ACTION.SAVE_NOT_CLOSE:
                new Handler().postDelayed(() -> viewModel.saveProduct(false), 500);
                break;
            case ACTION.DELETE:
                new Handler().postDelayed(() -> viewModel.deleteProductSafely(), 500);
                break;
        }
    }
    viewModel.getIsOnlineLive().observe(getViewLifecycleOwner(), isOnline -> {
    // if(isOnline ) viewModel.downloadData();
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) Product(xyz.zedler.patrick.grocy.model.Product) Handler(android.os.Handler) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) MasterProductViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 5 with Product

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

the class ShoppingListItemEditFragment method onItemAutoCompleteClick.

public void onItemAutoCompleteClick(AdapterView<?> adapterView, int pos) {
    Product product = (Product) adapterView.getItemAtPosition(pos);
    viewModel.setProduct(product);
    focusNextView();
}
Also used : Product(xyz.zedler.patrick.grocy.model.Product)

Aggregations

Product (xyz.zedler.patrick.grocy.model.Product)45 Bundle (android.os.Bundle)12 ProductBarcode (xyz.zedler.patrick.grocy.model.ProductBarcode)12 Grocycode (xyz.zedler.patrick.grocy.util.GrocycodeUtil.Grocycode)12 ArrayList (java.util.ArrayList)11 QuantityUnit (xyz.zedler.patrick.grocy.model.QuantityUnit)11 ShoppingListItem (xyz.zedler.patrick.grocy.model.ShoppingListItem)10 HashMap (java.util.HashMap)8 JSONObject (org.json.JSONObject)7 JSONException (org.json.JSONException)6 ProductDetails (xyz.zedler.patrick.grocy.model.ProductDetails)6 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)6 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 Location (xyz.zedler.patrick.grocy.model.Location)5 ProductGroup (xyz.zedler.patrick.grocy.model.ProductGroup)5 SuppressLint (android.annotation.SuppressLint)4 SharedPreferences (android.content.SharedPreferences)4 PreferenceManager (androidx.preference.PreferenceManager)4 GroupedListItem (xyz.zedler.patrick.grocy.model.GroupedListItem)4