use of xyz.zedler.patrick.grocy.util.PluralUtil in project grocy-android by patzly.
the class MasterQuantityUnitFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
// PREFERENCES
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
// WEB
dlHelper = new DownloadHelper(activity, TAG);
grocyApi = activity.getGrocyApi();
gson = new Gson();
pluralUtil = new PluralUtil(activity);
// VARIABLES
quantityUnits = new ArrayList<>();
quantityUnitNames = new ArrayList<>();
editQuantityUnit = null;
isRefresh = false;
// VIEWS
binding.frameMasterQuantityUnitCancel.setOnClickListener(v -> activity.onBackPressed());
// swipe refresh
binding.swipeMasterQuantityUnit.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
binding.swipeMasterQuantityUnit.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
binding.swipeMasterQuantityUnit.setOnRefreshListener(this::refresh);
// name
binding.editTextMasterQuantityUnitName.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitName);
}
});
// name plural
binding.editTextMasterQuantityUnitNamePlural.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitNamePlural);
}
});
binding.linearMasterQuantityUnitForms.setVisibility(pluralUtil.isPluralFormsFieldNecessary() ? View.VISIBLE : View.GONE);
if (pluralUtil.languageRulesNotImplemented()) {
binding.cardPluralFormsNotSupportedInfo.setVisibility(View.VISIBLE);
}
// description
binding.editTextMasterQuantityUnitDescription.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitDescription);
}
});
MasterQuantityUnitFragmentArgs args = MasterQuantityUnitFragmentArgs.fromBundle(requireArguments());
editQuantityUnit = args.getQuantityUnit();
if (editQuantityUnit != null) {
fillWithEditReferences();
} else if (savedInstanceState == null) {
resetAll();
new Handler().postDelayed(() -> activity.showKeyboard(binding.editTextMasterQuantityUnitName), 50);
}
if (savedInstanceState == null) {
load();
} else {
restoreSavedInstanceState(savedInstanceState);
}
// UPDATE UI
updateUI((getArguments() == null || getArguments().getBoolean(Constants.ARGUMENT.ANIMATED, true)) && savedInstanceState == null);
}
use of xyz.zedler.patrick.grocy.util.PluralUtil 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.util.PluralUtil in project grocy-android by patzly.
the class PurchaseFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
args = PurchaseFragmentArgs.fromBundle(requireArguments());
viewModel = new ViewModelProvider(this, new PurchaseViewModel.PurchaseViewModelFactory(activity.getApplication(), args)).get(PurchaseViewModel.class);
binding.setActivity(activity);
binding.setViewModel(viewModel);
binding.setFragment(this);
binding.setFormData(viewModel.getFormData());
binding.setLifecycleOwner(getViewLifecycleOwner());
infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
if (args.getShoppingListItems() != null) {
binding.containerBatchMode.setVisibility(View.VISIBLE);
binding.linearPurchaseShoppingListItem.containerRow.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_list_item_visible_ripple));
}
viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isDownloading -> binding.swipePurchase.setRefreshing(isDownloading));
viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
} else if (event.getType() == Event.TRANSACTION_SUCCESS) {
assert getArguments() != null;
if (args.getShoppingListItems() != null) {
clearInputFocus();
viewModel.getFormData().clearForm();
boolean nextItemValid = viewModel.batchModeNextItem();
if (!nextItemValid)
activity.navigateUp();
} else if (PurchaseFragmentArgs.fromBundle(getArguments()).getCloseWhenFinished()) {
activity.navigateUp();
} else {
viewModel.getFormData().clearForm();
focusProductInputIfNecessary();
embeddedFragmentScanner.startScannerIfVisible();
}
} 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) {
focusNextInvalidView();
} else if (event.getType() == Event.QUICK_MODE_ENABLED) {
focusProductInputIfNecessary();
} else if (event.getType() == Event.QUICK_MODE_DISABLED) {
clearInputFocus();
} else if (event.getType() == Event.CONTINUE_SCANNING) {
embeddedFragmentScanner.startScannerIfVisible();
} else if (event.getType() == Event.CHOOSE_PRODUCT) {
String barcode = event.getBundle().getString(ARGUMENT.BARCODE);
navigate(PurchaseFragmentDirections.actionPurchaseFragmentToChooseProductFragment(barcode));
}
});
Integer productIdSavedSate = (Integer) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT_ID);
if (productIdSavedSate != null) {
removeForThisDestination(Constants.ARGUMENT.PRODUCT_ID);
viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productIdSavedSate, null, null));
} else if (NumUtil.isStringInt(args.getProductId())) {
int productId = Integer.parseInt(args.getProductId());
setArguments(new PurchaseFragmentArgs.Builder(args).setProductId(null).build().toBundle());
viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productId, null, null));
}
String barcode = (String) getFromThisDestinationNow(ARGUMENT.BARCODE);
if (barcode != null) {
removeForThisDestination(Constants.ARGUMENT.BARCODE);
viewModel.addBarcodeToExistingProduct(barcode);
}
pluralUtil = new PluralUtil(activity);
viewModel.getFormData().getShoppingListItemLive().observe(getViewLifecycleOwner(), item -> {
if (args.getShoppingListItems() == null || item == null)
return;
ShoppingListItemAdapter.fillShoppingListItem(requireContext(), item, binding.linearPurchaseShoppingListItem, viewModel.getProductHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getShoppingListItemAmountsHashMap(), pluralUtil);
});
embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
// following lines are necessary because no observers are set in Views
viewModel.getFormData().getPriceStockLive().observe(getViewLifecycleOwner(), i -> {
});
viewModel.getFormData().getQuantityUnitStockLive().observe(getViewLifecycleOwner(), i -> {
});
if (savedInstanceState == null) {
viewModel.loadFromDatabase(true);
}
focusProductInputIfNecessary();
setHasOptionsMenu(true);
updateUI(args.getAnimateStart() && savedInstanceState == null);
}
use of xyz.zedler.patrick.grocy.util.PluralUtil in project grocy-android by patzly.
the class ShoppingListFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
viewModel = new ViewModelProvider(this).get(ShoppingListViewModel.class);
viewModel.setOfflineLive(!activity.isOnline());
binding.setViewModel(viewModel);
binding.setActivity(activity);
binding.setFragment(this);
binding.setLifecycleOwner(getViewLifecycleOwner());
infoFullscreenHelper = new InfoFullscreenHelper(binding.frame);
clickUtil = new ClickUtil();
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
pluralUtil = new PluralUtil(activity);
// APP BAR BEHAVIOR
appBarBehavior = new AppBarBehavior(activity, binding.appBarDefault, binding.appBarSearch, savedInstanceState);
binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
binding.recycler.setAdapter(new ShoppingPlaceholderAdapter());
if (savedInstanceState == null) {
binding.recycler.scrollToPosition(0);
viewModel.resetSearch();
}
Object forcedSelectedId = getFromThisDestinationNow(Constants.ARGUMENT.SELECTED_ID);
if (forcedSelectedId != null) {
viewModel.selectShoppingList((Integer) forcedSelectedId);
removeForThisDestination(Constants.ARGUMENT.SELECTED_ID);
}
viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
if (!state) {
viewModel.setCurrentQueueLoading(null);
}
});
viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
viewModel.getSelectedShoppingListIdLive().observe(getViewLifecycleOwner(), this::changeAppBarTitle);
viewModel.getFilteredShoppingListItemsLive().observe(getViewLifecycleOwner(), items -> {
if (items == null)
return;
if (binding.recycler.getAdapter() instanceof ShoppingListItemAdapter) {
((ShoppingListItemAdapter) binding.recycler.getAdapter()).updateData(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), viewModel.getShoppingListNotes(), viewModel.getGroupingMode());
} else {
binding.recycler.setAdapter(new ShoppingListItemAdapter(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), this, viewModel.getShoppingListNotes(), viewModel.getGroupingMode()));
binding.recycler.scheduleLayoutAnimation();
}
});
viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
}
});
if (swipeBehavior == null) {
swipeBehavior = new SwipeBehavior(activity, swipeStarted -> binding.swipeShoppingList.setEnabled(!swipeStarted)) {
@Override
public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
if (viewHolder.getItemViewType() != GroupedListItem.TYPE_ENTRY)
return;
if (!(binding.recycler.getAdapter() instanceof ShoppingListItemAdapter))
return;
int position = viewHolder.getAdapterPosition();
ArrayList<GroupedListItem> groupedListItems = ((ShoppingListItemAdapter) binding.recycler.getAdapter()).getGroupedListItems();
if (groupedListItems == null || position < 0 || position >= groupedListItems.size()) {
return;
}
GroupedListItem item = groupedListItems.get(position);
if (!(item instanceof ShoppingListItem)) {
return;
}
ShoppingListItem shoppingListItem = (ShoppingListItem) item;
underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_done, pos -> {
if (position >= groupedListItems.size()) {
return;
}
viewModel.toggleDoneStatus(shoppingListItem);
}));
}
};
}
swipeBehavior.attachToRecyclerView(binding.recycler);
hideDisabledFeatures();
if (savedInstanceState == null) {
viewModel.loadFromDatabase(true);
}
updateUI(ShoppingListFragmentArgs.fromBundle(requireArguments()).getAnimateStart() && savedInstanceState == null);
}
Aggregations