use of it.niedermann.nextcloud.deck.ui.card.LabelAutoCompleteAdapter in project nextcloud-deck by stefan-niedermann.
the class CardDetailsFragment method setupLabels.
private void setupLabels() {
final long accountId = viewModel.getAccount().getId();
final long boardId = viewModel.getBoardId();
binding.labelsGroup.removeAllViews();
if (viewModel.canEdit()) {
Long localCardId = viewModel.getFullCard().getCard().getLocalId();
localCardId = localCardId == null ? -1 : localCardId;
binding.labels.setAdapter(new LabelAutoCompleteAdapter(requireActivity(), accountId, boardId, localCardId));
binding.labels.setOnItemClickListener((adapterView, view, position, id) -> {
final var label = (Label) adapterView.getItemAtPosition(position);
if (LabelAutoCompleteAdapter.ITEM_CREATE == label.getLocalId()) {
final Label newLabel = new Label(label);
newLabel.setBoardId(boardId);
newLabel.setTitle(((LabelAutoCompleteAdapter) binding.labels.getAdapter()).getLastFilterText());
newLabel.setLocalId(null);
viewModel.createLabel(accountId, newLabel, boardId, new IResponseCallback<>() {
@Override
public void onResponse(Label response) {
requireActivity().runOnUiThread(() -> {
newLabel.setLocalId(response.getLocalId());
((LabelAutoCompleteAdapter) binding.labels.getAdapter()).exclude(response);
viewModel.getFullCard().getLabels().add(response);
binding.labelsGroup.addView(createChipFromLabel(newLabel));
binding.labelsGroup.setVisibility(VISIBLE);
});
}
@Override
public void onError(Throwable throwable) {
IResponseCallback.super.onError(throwable);
requireActivity().runOnUiThread(() -> BrandedSnackbar.make(requireView(), getString(R.string.error_create_label, newLabel.getTitle()), Snackbar.LENGTH_LONG).setAction(R.string.simple_more, v -> ExceptionDialogFragment.newInstance(throwable, viewModel.getAccount()).show(getChildFragmentManager(), ExceptionDialogFragment.class.getSimpleName())).show());
}
});
} else {
((LabelAutoCompleteAdapter) binding.labels.getAdapter()).exclude(label);
viewModel.getFullCard().getLabels().add(label);
binding.labelsGroup.addView(createChipFromLabel(label));
binding.labelsGroup.setVisibility(VISIBLE);
}
binding.labels.setText("");
});
} else {
binding.labels.setEnabled(false);
}
if (viewModel.getFullCard().getLabels() != null && viewModel.getFullCard().getLabels().size() > 0) {
for (final var label : viewModel.getFullCard().getLabels()) {
binding.labelsGroup.addView(createChipFromLabel(label));
}
binding.labelsGroup.setVisibility(VISIBLE);
} else {
binding.labelsGroup.setVisibility(INVISIBLE);
}
}
use of it.niedermann.nextcloud.deck.ui.card.LabelAutoCompleteAdapter in project nextcloud-deck by stefan-niedermann.
the class CardDetailsFragment method createChipFromLabel.
private Chip createChipFromLabel(Label label) {
final var chip = new Chip(requireContext());
chip.setText(label.getTitle());
if (viewModel.canEdit()) {
chip.setCloseIcon(ContextCompat.getDrawable(requireContext(), R.drawable.ic_close_circle_grey600));
chip.setCloseIconVisible(true);
chip.setOnCloseIconClickListener(v -> {
binding.labelsGroup.removeView(chip);
viewModel.getFullCard().getLabels().remove(label);
((LabelAutoCompleteAdapter) binding.labels.getAdapter()).exclude(label);
});
}
try {
final int labelColor = label.getColor();
chip.setChipBackgroundColor(ColorStateList.valueOf(labelColor));
final int color = ColorUtil.INSTANCE.getForegroundColorForBackgroundColor(labelColor);
chip.setTextColor(color);
if (chip.getCloseIcon() != null) {
Drawable wrapDrawable = DrawableCompat.wrap(chip.getCloseIcon());
DrawableCompat.setTint(wrapDrawable, ColorUtils.setAlphaComponent(color, 150));
}
} catch (IllegalArgumentException e) {
DeckLog.logError(e);
}
return chip;
}
Aggregations