use of com.google.android.material.chip.Chip in project nextcloud-notes by stefan-niedermann.
the class NoteViewHolder method bindCategory.
protected void bindCategory(@NonNull Context context, @NonNull TextView noteCategory, boolean showCategory, @NonNull String category, int mainColor) {
final boolean isDarkThemeActive = NotesApplication.isDarkThemeActive(context);
noteCategory.setVisibility(showCategory && !category.isEmpty() ? View.VISIBLE : View.GONE);
noteCategory.setText(category);
@ColorInt final int categoryForeground;
@ColorInt final int categoryBackground;
if (isDarkThemeActive) {
if (ColorUtil.INSTANCE.isColorDark(mainColor)) {
if (contrastRatioIsSufficient(mainColor, Color.BLACK)) {
categoryBackground = mainColor;
categoryForeground = Color.WHITE;
} else {
categoryBackground = Color.WHITE;
categoryForeground = mainColor;
}
} else {
categoryBackground = mainColor;
categoryForeground = Color.BLACK;
}
} else {
categoryForeground = Color.BLACK;
if (ColorUtil.INSTANCE.isColorDark(mainColor) || contrastRatioIsSufficient(mainColor, Color.WHITE)) {
categoryBackground = mainColor;
} else {
categoryBackground = Color.BLACK;
}
}
noteCategory.setTextColor(categoryForeground);
if (noteCategory instanceof Chip) {
final Chip chip = (Chip) noteCategory;
chip.setChipStrokeColor(ColorStateList.valueOf(categoryBackground));
if (isDarkThemeActive) {
chip.setChipBackgroundColor(ColorStateList.valueOf(categoryBackground));
} else {
chip.setChipBackgroundColorResource(R.color.grid_item_background_selector);
}
} else {
DrawableCompat.setTint(noteCategory.getBackground(), categoryBackground);
}
}