Skip to content

Commit

Permalink
[DatePicker] Fix NullPointerException on MonthAdapter.updateSelectedS…
Browse files Browse the repository at this point in the history
…tate(MonthAda…

Resolves #1944
Resolves #1943

GIT_ORIGIN_REV_ID=d02d6307ea08ea505ea6535ce909e7d1b268847b
PiperOrigin-RevId: 350783859

(cherry picked from commit 00e3320)
  • Loading branch information
Mika authored and ikim24 committed Jan 14, 2021
1 parent 1ff2d6a commit dad7752
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/java/com/google/android/material/datepicker/MonthAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public TextView getView(int position, @Nullable View convertView, @NonNull ViewG
if (date == null) {
return day;
}
return updateSelectedState(day, date);
updateSelectedState(day, date);
return day;
}

public void updateSelectedStates(MaterialCalendarGridView monthGrid) {
Expand Down Expand Up @@ -161,28 +162,34 @@ private void updateSelectedStateForDate(MaterialCalendarGridView monthGrid, long
}
}

private TextView updateSelectedState(TextView day, long date) {
private void updateSelectedState(@Nullable TextView day, long date) {
if (day == null) {
return;
}
final CalendarItemStyle style;
if (calendarConstraints.getDateValidator().isValid(date)) {
day.setEnabled(true);
for (long selectedDay : dateSelector.getSelectedDays()) {
if (UtcDates.canonicalYearMonthDay(date) == UtcDates.canonicalYearMonthDay(selectedDay)) {
calendarStyle.selectedDay.styleItem(day);
return day;
}
}

if (UtcDates.getTodayCalendar().getTimeInMillis() == date) {
calendarStyle.todayDay.styleItem(day);
return day;
if (isSelected(date)) {
style = calendarStyle.selectedDay;
} else if (UtcDates.getTodayCalendar().getTimeInMillis() == date) {
style = calendarStyle.todayDay;
} else {
calendarStyle.day.styleItem(day);
return day;
style = calendarStyle.day;
}
} else {
day.setEnabled(false);
calendarStyle.invalidDay.styleItem(day);
return day;
style = calendarStyle.invalidDay;
}
style.styleItem(day);
}

private boolean isSelected(long date) {
for (long selectedDay : dateSelector.getSelectedDays()) {
if (UtcDates.canonicalYearMonthDay(date) == UtcDates.canonicalYearMonthDay(selectedDay)) {
return true;
}
}
return false;
}

private void initializeStyles(Context context) {
Expand Down

0 comments on commit dad7752

Please sign in to comment.