Skip to content

Commit

Permalink
[MaterialDatePicker] Add getTextColor to DayViewDecorator
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 527265648
  • Loading branch information
paulfthomas authored and leticiarossi committed Apr 26, 2023
1 parent 6980c40 commit 29b59c2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
Expand Up @@ -37,24 +37,32 @@ class BackgroundHighlightDecorator extends DayViewDecorator {
new ArrayList<>(Arrays.asList(addDays(today, 1), addDays(today, 3), addDays(today, -2)));

@Nullable private ColorStateList backgroundHighlightColor;
@Nullable private ColorStateList textHighlightColor;

@Override
public void initialize(@NonNull Context context) {
int highlightColor =
MaterialColors.getColor(
context,
R.attr.colorTertiaryContainer,
BackgroundHighlightDecorator.class.getSimpleName());
context, R.attr.colorTertiary, BackgroundHighlightDecorator.class.getSimpleName());
backgroundHighlightColor = ColorStateList.valueOf(highlightColor);
int textColor =
MaterialColors.getColor(
context, R.attr.colorOnTertiary, BackgroundHighlightDecorator.class.getSimpleName());
textHighlightColor = ColorStateList.valueOf(textColor);
}

@Nullable
@Override
public ColorStateList getBackgroundColor(
@NonNull Context context, int year, int month, int day, boolean valid, boolean selected) {
return valid && !selected && shouldShowHighlight(year, month, day)
? backgroundHighlightColor
: null;
return shouldShowHighlight(year, month, day, valid, selected) ? backgroundHighlightColor : null;
}

@Nullable
@Override
public ColorStateList getTextColor(
@NonNull Context context, int year, int month, int day, boolean valid, boolean selected) {
return shouldShowHighlight(year, month, day, valid, selected) ? textHighlightColor : null;
}

@Nullable
Expand All @@ -75,6 +83,11 @@ public CharSequence getContentDescription(
originalContentDescription);
}

private boolean shouldShowHighlight(
int year, int month, int day, boolean valid, boolean selected) {
return valid && !selected && shouldShowHighlight(year, month, day);
}

private boolean shouldShowHighlight(int year, int month, int day) {
for (Calendar calendar : highlightDays) {
if (calendar.get(Calendar.YEAR) == year
Expand Down
Expand Up @@ -137,22 +137,25 @@ static CalendarItemStyle create(
* {@code backgroundColorOverride}.
*/
void styleItem(@NonNull TextView item) {
styleItem(item, /* backgroundColorOverride= */ null);
styleItem(item, /* backgroundColorOverride= */ null, /* textColorOverride= */ null);
}

/**
* Applies the {@code R.styleable.MaterialCalendarDay} style to the provided {@code item},
* factoring in the {@code backgroundColorOverride} if not null.
*/
void styleItem(@NonNull TextView item, @Nullable ColorStateList backgroundColorOverride) {
void styleItem(
@NonNull TextView item,
@Nullable ColorStateList backgroundColorOverride,
@Nullable ColorStateList textColorOverride) {
MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable();
MaterialShapeDrawable shapeMask = new MaterialShapeDrawable();
backgroundDrawable.setShapeAppearanceModel(itemShape);
shapeMask.setShapeAppearanceModel(itemShape);
backgroundDrawable.setFillColor(
backgroundColorOverride != null ? backgroundColorOverride : backgroundColor);
backgroundDrawable.setStroke(strokeWidth, strokeColor);
item.setTextColor(textColor);
item.setTextColor(textColorOverride != null ? textColorOverride : textColor);
Drawable d;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
d = new RippleDrawable(textColor.withAlpha(30), backgroundDrawable, shapeMask);
Expand Down
Expand Up @@ -147,6 +147,28 @@ public ColorStateList getBackgroundColor(
return null;
}

/**
* Override this method to return a custom color to be applied to the text of the day view
* corresponding to the provided date.
*
* @param context The context of the day view
* @param year The year number corresponding to the day view (see {@link java.util.Calendar.YEAR})
* @param month The month number (0-11) corresponding to the day view (see {@link
* java.util.Calendar.MONTH})
* @param day The day of month number corresponding to the day view (see {@link
* java.util.Calendar.DAY_OF_MONTH})
* @param valid Boolean for whether the day view is in a valid state (if not valid, the day view
* will likely look and behave disabled)
* @param selected Boolean for whether the day view is in a selected state (if selected, the day
* view will likely have a filled color background)
* @return The text color {@link ColorStateList} or null
*/
@Nullable
public ColorStateList getTextColor(
@NonNull Context context, int year, int month, int day, boolean valid, boolean selected) {
return null;
}

/**
* Override this method to return the day view's content description.
*
Expand Down
Expand Up @@ -214,7 +214,9 @@ private void updateSelectedState(@Nullable TextView dayTextView, long date, int

ColorStateList backgroundColorOverride =
dayViewDecorator.getBackgroundColor(context, year, month, dayNumber, valid, selected);
style.styleItem(dayTextView, backgroundColorOverride);
ColorStateList textColorOverride =
dayViewDecorator.getTextColor(context, year, month, dayNumber, valid, selected);
style.styleItem(dayTextView, backgroundColorOverride, textColorOverride);

Drawable drawableLeft =
dayViewDecorator.getCompoundDrawableLeft(
Expand Down
Expand Up @@ -34,6 +34,7 @@ public class TestBackgroundHighlightDecorator extends DayViewDecorator {
private final List<Calendar> highlightDays;

private ColorStateList backgroundHighlightColor;
private ColorStateList textHighlightColor;

public TestBackgroundHighlightDecorator(Calendar startDay) {
this.startDay = startDay;
Expand All @@ -45,6 +46,7 @@ public TestBackgroundHighlightDecorator(Calendar startDay) {
@Override
public void initialize(@NonNull Context context) {
backgroundHighlightColor = ColorStateList.valueOf(getBackgroundHighlightColor(context));
textHighlightColor = ColorStateList.valueOf(getTextHighlightColor(context));
}

@Nullable
Expand All @@ -56,10 +58,22 @@ public ColorStateList getBackgroundColor(
: null;
}

@Nullable
@Override
public ColorStateList getTextColor(
@NonNull Context context, int year, int month, int day, boolean valid, boolean selected) {
return valid && !selected && shouldShowHighlight(year, month, day) ? textHighlightColor : null;
}

@ColorInt
private int getBackgroundHighlightColor(Context context) {
return MaterialColors.getColor(
context, R.attr.colorTertiaryContainer, getFallbackBackgroundHighlightColor(context));
context, R.attr.colorTertiary, getFallbackBackgroundHighlightColor(context));
}

@ColorInt
private int getTextHighlightColor(Context context) {
return MaterialColors.getColor(context, R.attr.colorOnTertiary, R.attr.colorControlNormal);
}

@ColorInt
Expand Down

0 comments on commit 29b59c2

Please sign in to comment.