Skip to content

Commit

Permalink
[MaterialDatePicker] Fix for issue that causes the month view not to …
Browse files Browse the repository at this point in the history
…expand to show all days in a month.

Resolves #1676

PiperOrigin-RevId: 436812359
  • Loading branch information
raajkumars authored and paulfthomas committed Mar 28, 2022
1 parent 01021a7 commit a1ec73b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
19 changes: 12 additions & 7 deletions lib/java/com/google/android/material/datepicker/MonthAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class MonthAdapter extends BaseAdapter {
*/
static final int MAXIMUM_WEEKS = UtcDates.getUtcCalendar().getMaximum(Calendar.WEEK_OF_MONTH);

/** The maximum number of cells needed in the month grid view. */
private static final int MAXIMUM_GRID_CELLS =
UtcDates.getUtcCalendar().getMaximum(Calendar.DAY_OF_MONTH)
+ UtcDates.getUtcCalendar().getMaximum(Calendar.DAY_OF_WEEK)
- 1;

final Month month;
/**
* The {@link DateSelector} dictating the draw behavior of {@link #getView(int, View, ViewGroup)}.
Expand Down Expand Up @@ -76,7 +82,7 @@ public boolean hasStableIds() {
@Nullable
@Override
public Long getItem(int position) {
if (position < month.daysFromStartOfWeekToFirstOfMonth() || position > lastPositionInMonth()) {
if (position < firstPositionInMonth() || position > lastPositionInMonth()) {
return null;
}
return month.getDay(positionToDay(position));
Expand All @@ -88,16 +94,15 @@ public long getItemId(int position) {
}

/**
* Returns the number of days in a month plus the amount required to off-set for the first day to
* the correct position within the week.
* Returns the maximum number of item views needed to display a calender month.
*
* <p>{@see MonthAdapter#firstPositionInMonth}.
* <p>{@see MonthAdapter#MAXIMUM_GRID_CELLS}.
*
* @return The maximum valid position index
*/
@Override
public int getCount() {
return month.daysInMonth + firstPositionInMonth();
return MAXIMUM_GRID_CELLS;
}

@NonNull
Expand Down Expand Up @@ -220,7 +225,7 @@ int firstPositionInMonth() {
* not match the number of days in the month.
*/
int lastPositionInMonth() {
return month.daysFromStartOfWeekToFirstOfMonth() + month.daysInMonth - 1;
return firstPositionInMonth() + month.daysInMonth - 1;
}

/**
Expand All @@ -231,7 +236,7 @@ int lastPositionInMonth() {
* less than {@link MonthAdapter#firstPositionInMonth()}.
*/
int positionToDay(int position) {
return position - month.daysFromStartOfWeekToFirstOfMonth() + 1;
return position - firstPositionInMonth() + 1;
}

/** Returns the adapter index representing the provided day. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.android.material.R;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -166,25 +167,25 @@ private void assertPositionsForDays(Map<MonthAdapter, Integer> localizedPosition
@Test
public void usItemCount() {
setupLocalizedCalendars(Locale.US);
assertEquals(30, monthFeb2016.getCount());
assertEquals(31, monthJuly2018.getCount());
assertEquals(33, monthFeb2019.getCount());
assertThat(monthFeb2016.getCount()).isAtLeast(30);
assertThat(monthJuly2018.getCount()).isAtLeast(31);
assertThat(monthFeb2019.getCount()).isAtLeast(33);
}

@Test
public void frItemCount() {
setupLocalizedCalendars(Locale.FRANCE);
assertEquals(29, monthFeb2016.getCount());
assertEquals(37, monthJuly2018.getCount());
assertEquals(32, monthFeb2019.getCount());
assertThat(monthFeb2016.getCount()).isAtLeast(29);
assertThat(monthJuly2018.getCount()).isAtLeast(37);
assertThat(monthFeb2019.getCount()).isAtLeast(32);
}

@Test
public void ilItemCount() {
setupLocalizedCalendars(ISRAEL);
assertEquals(30, monthFeb2016.getCount());
assertEquals(31, monthJuly2018.getCount());
assertEquals(33, monthFeb2019.getCount());
assertThat(monthFeb2016.getCount()).isAtLeast(30);
assertThat(monthJuly2018.getCount()).isAtLeast(31);
assertThat(monthFeb2019.getCount()).isAtLeast(33);
}

@Test
Expand Down

0 comments on commit a1ec73b

Please sign in to comment.