diff --git a/library/src/main/java/com/kizitonwose/calendarview/model/MonthConfig.kt b/library/src/main/java/com/kizitonwose/calendarview/model/MonthConfig.kt index 26945a82..5291dcd8 100644 --- a/library/src/main/java/com/kizitonwose/calendarview/model/MonthConfig.kt +++ b/library/src/main/java/com/kizitonwose/calendarview/model/MonthConfig.kt @@ -1,7 +1,6 @@ package com.kizitonwose.calendarview.model import com.kizitonwose.calendarview.utils.next -import com.kizitonwose.calendarview.utils.yearMonth import org.threeten.bp.DayOfWeek import org.threeten.bp.LocalDate import org.threeten.bp.YearMonth @@ -118,10 +117,10 @@ internal data class MonthConfig( // Add the outDates for the last row if needed. if (monthWeeks.last().size < 7 && outDateStyle == OutDateStyle.END_OF_ROW || outDateStyle == OutDateStyle.END_OF_GRID) { - val lastWeek = monthWeeks.last().toMutableList() - val outMonth = currentMonth.plusMonths(1) + val lastWeek = monthWeeks.last() + val lastDay = lastWeek.last() val outDates = (1..7 - lastWeek.size).map { - CalendarDay(LocalDate.of(outMonth.year, outMonth.month, it), DayOwner.NEXT_MONTH) + CalendarDay(lastDay.date.plusDays(it.toLong()), DayOwner.NEXT_MONTH) } monthWeeks[monthWeeks.lastIndex] = lastWeek + outDates } @@ -222,11 +221,11 @@ internal data class MonthConfig( if (outDateStyle == OutDateStyle.END_OF_ROW || outDateStyle == OutDateStyle.END_OF_GRID) { // Add out-dates for the last row. - val nextMonth = yearMonth.plusMonths(1) - val lastWeek = weekDaysGroup.last() - if (lastWeek.size < 7) { + if (weekDaysGroup.last().size < 7) { + val lastWeek = weekDaysGroup.last() + val lastDay = lastWeek.last() val outDates = (1..7 - lastWeek.size).map { - CalendarDay(LocalDate.of(nextMonth.year, nextMonth.month, it), DayOwner.NEXT_MONTH) + CalendarDay(lastDay.date.plusDays(it.toLong()), DayOwner.NEXT_MONTH) } weekDaysGroup[weekDaysGroup.lastIndex] = lastWeek + outDates } @@ -236,8 +235,7 @@ internal data class MonthConfig( while (weekDaysGroup.size < 6) { val lastDay = weekDaysGroup.last().last() val nextRowDates = (1..7).map { - val dayValue = if (lastDay.owner == DayOwner.THIS_MONTH) it else it + lastDay.day - CalendarDay(LocalDate.of(nextMonth.year, nextMonth.month, dayValue), DayOwner.NEXT_MONTH) + CalendarDay(lastDay.date.plusDays(it.toLong()), DayOwner.NEXT_MONTH) } weekDaysGroup.add(nextRowDates) } diff --git a/library/src/test/java/com/kizitonwose/calenderview/CalenderViewTests.kt b/library/src/test/java/com/kizitonwose/calenderview/CalenderViewTests.kt index 9ad963d7..18abbb5f 100644 --- a/library/src/test/java/com/kizitonwose/calenderview/CalenderViewTests.kt +++ b/library/src/test/java/com/kizitonwose/calenderview/CalenderViewTests.kt @@ -131,13 +131,13 @@ class CalenderViewTests { } @Test - fun `test unbounded month calculation does not exceed days of month`() { + fun `test unbounded month generation does not exceed number of days in each month`() { val maxRowCount = 6 MonthConfig.generateUnboundedMonths( - YearMonth.of(2019, 12), YearMonth.of(2020, 2), + YearMonth.of(2019, 2), YearMonth.of(2021, 2), DayOfWeek.SUNDAY, maxRowCount, InDateStyle.ALL_MONTHS, OutDateStyle.END_OF_GRID ) - // No assertion neccessary, as this particular range would throw an exception previously + // No assertion necessary, as this particular range would throw an exception previously // when trying to build a day that is out of bounds (eg: December 32). } }