Skip to content

Commit

Permalink
Fix calculation of displayed dates range
Browse files Browse the repository at this point in the history
Fix calculation of displayed dates range when one of the dates boundaries
is defined and initial date is far from the defined date boundary.

Add two unit tests to test this case.
  • Loading branch information
alexsurinov committed Aug 1, 2019
1 parent 4387281 commit 7da03b1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,65 @@ internal object DisplayedDatesRangeFactory {

): DatesRange {

val displayDatesFrom: CalendarDate
val displayDatesTo: CalendarDate
val rangeStart: CalendarDate
val rangeEnd: CalendarDate

when {
minDate == null && maxDate == null -> {
displayDatesFrom = initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
displayDatesTo = initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
rangeStart = initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
rangeEnd = initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
}

minDate != null && maxDate == null -> {
displayDatesFrom = minDate
displayDatesTo = displayDatesFrom.plusMonths(CalendarConst.MONTHS_PER_PAGE)
rangeStart = calculateRangeStart(minDate = minDate, initialDate = initialDate)
rangeEnd = rangeStart.plusMonths(CalendarConst.MONTHS_PER_PAGE)
}

minDate == null && maxDate != null -> {
displayDatesFrom = maxDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
displayDatesTo = maxDate
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = initialDate)
rangeStart = rangeEnd.minusMonths(CalendarConst.MONTHS_PER_PAGE)
}

minDate != null && maxDate != null -> {
if (initialDate.isBetween(minDate, maxDate)) {
var monthsBetween = minDate.monthsBetween(initialDate)
displayDatesFrom = if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
minDate
}

monthsBetween = initialDate.monthsBetween(maxDate)
displayDatesTo = if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
maxDate
}
rangeStart = calculateRangeStart(minDate = minDate, initialDate = initialDate)
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = initialDate)
} else {
displayDatesFrom = minDate

val monthBetween = minDate.monthsBetween(maxDate)
displayDatesTo = if (monthBetween > CalendarConst.MONTHS_PER_PAGE) {
minDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
maxDate
}
rangeStart = minDate
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = minDate)
}
}

else -> throw IllegalStateException() // unreachable branch
}

return DatesRange(dateFrom = displayDatesFrom, dateTo = displayDatesTo)
return DatesRange(dateFrom = rangeStart, dateTo = rangeEnd)
}

private fun calculateRangeStart(
minDate: CalendarDate,
initialDate: CalendarDate
): CalendarDate {
val monthsBetween = minDate.monthsBetween(initialDate)

return if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
minDate
}
}

private fun calculateRangeEnd(
maxDate: CalendarDate,
initialDate: CalendarDate
): CalendarDate {
val monthsBetween = initialDate.monthsBetween(maxDate)

return if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
maxDate
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.Test
import ru.cleverpumpkin.calendar.CalendarConst
import ru.cleverpumpkin.calendar.CalendarDate
import ru.cleverpumpkin.calendar.DatesRange
import java.util.*

/**
* Set of unit tests for the [DisplayedDatesRangeFactory] class.
Expand All @@ -16,7 +17,7 @@ import ru.cleverpumpkin.calendar.DatesRange
*
* Created by Alexander Surinov on 29/04/2019.
*/
class DisplayedDatesRangeProviderTest {
class DisplayedDatesRangeFactoryTest {

@Test
fun `Get displayed dates range when min and max dates aren't defined, as a result correct dates range is returned`() {
Expand All @@ -43,7 +44,7 @@ class DisplayedDatesRangeProviderTest {
}

@Test
fun `Get displayed dates range when min date isn't defined and max date is defined, as a result correct dates range is returned`() {
fun `Get displayed dates range when min date is defined and max date isn't defined, as a result correct dates range is returned`() {
// Given
val minDate = CalendarDate.today

Expand All @@ -67,7 +68,7 @@ class DisplayedDatesRangeProviderTest {
}

@Test
fun `Get displayed dates range when min date is defined and max date isn't defined, as a result correct dates range is returned`() {
fun `Get displayed dates range when min date isn't defined and max date is defined, as a result correct dates range is returned`() {
// Given
val maxDate = CalendarDate.today

Expand All @@ -90,6 +91,64 @@ class DisplayedDatesRangeProviderTest {
)
}

@Test
fun `Get displayed dates range when min date is defined, max date isn't defined and initial date is far from min date, as a result correct dates range is returned`() {
// Given
val calendar = Calendar.getInstance()
calendar.set(2019, Calendar.JANUARY, 1)
val minDate = CalendarDate(calendar.time)

calendar.set(2019, Calendar.AUGUST, 1)
val initialDate = CalendarDate(calendar.time)

val expectedDatesRange = DatesRange(
dateFrom = initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE),
dateTo = initialDate
)

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
minDate = minDate,
maxDate = null
)

// Then
Assert.assertTrue(
"Unexpected actual dates range: $actualDatesRange, expected: $expectedDatesRange",
expectedDatesRange == actualDatesRange
)
}

@Test
fun `Get displayed dates range when min date isn't defined, max date is defined and initial date is far from max date, as a result correct dates range is returned`() {
// Given
val calendar = Calendar.getInstance()
calendar.set(2019, Calendar.AUGUST, 1)
val maxDate = CalendarDate(calendar.time)

calendar.set(2019, Calendar.JANUARY, 1)
val initialDate = CalendarDate(calendar.time)

val expectedDatesRange = DatesRange(
dateFrom = initialDate,
dateTo = initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
)

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = initialDate,
minDate = null,
maxDate = maxDate
)

// Then
Assert.assertTrue(
"Unexpected actual dates range: $actualDatesRange, expected: $expectedDatesRange",
expectedDatesRange == actualDatesRange
)
}

@Test
fun `Get displayed dates range when min-max dates are defined, initial dates is out of min-max date boundaries and days between min-max dates more then 6 month, as a result correct dates range is returned`() {
// Given
Expand Down

0 comments on commit 7da03b1

Please sign in to comment.