Skip to content

Commit

Permalink
Simplify OutDate generation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Dec 7, 2019
1 parent 36bd821 commit 765939c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
}
}

0 comments on commit 765939c

Please sign in to comment.