Skip to content

Commit

Permalink
OGM-1968 Update month selection to only select days in calendar range (
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik-sky authored Sep 27, 2023
1 parent d56a7be commit 4997bc7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.threeten.bp.LocalDate
import org.threeten.bp.Month

class CalendarRangeSelectionTests {

Expand Down Expand Up @@ -196,4 +198,25 @@ class CalendarRangeSelectionTests {
}
}
}

@Test
fun given_whole_month_is_selected_and_month_is_partly_in_range_only_in_range_selected() {
val mSelection = monthSelection.copy(
range = LocalDate.of(2000, Month.JANUARY, 15)..LocalDate.of(2001, Month.JANUARY, 14),
)
testCalendarWith(mSelection) {
stateMachine.onClick(CalendarInteraction.SelectMonthClicked(header))

verify {
assertEquals(
CalendarSelection.Month(
month = header.yearMonth,
start = mSelection.range.start,
end = LocalDate.of(2000, monthSelection.range.start.month, 31),
),
state.selection,
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ sealed class CalendarSelection : Serializable {
* A whole [month] is selected.
*/
@Immutable
data class Month(val month: YearMonth) : Range() {
override val start: LocalDate = month.firstDay()
override val end: LocalDate = month.lastDay()
}
data class Month(
val month: YearMonth,
override val start: LocalDate = month.firstDay(),
override val end: LocalDate = month.lastDay(),
) : Range()

/**
* A range of dates is selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import net.skyscanner.backpack.calendar2.CalendarParams
import net.skyscanner.backpack.calendar2.CalendarParams.SelectionMode
import net.skyscanner.backpack.calendar2.CalendarSelection
import net.skyscanner.backpack.calendar2.CalendarState
import net.skyscanner.backpack.calendar2.extension.firstDay
import net.skyscanner.backpack.calendar2.extension.lastDay
import net.skyscanner.backpack.util.InternalBackpackApi
import net.skyscanner.backpack.util.MutableStateMachine
import net.skyscanner.backpack.util.StateMachine
Expand Down Expand Up @@ -144,7 +146,11 @@ internal fun CalendarState.dispatchClick(data: CalendarCell.Header): CalendarSta
if (data.monthSelectionMode is CalendarParams.MonthSelectionMode.Disabled) return this
val selection = when (params.selectionMode) {
SelectionMode.Disabled -> selection
else -> CalendarSelection.Month(month = data.yearMonth)
else -> CalendarSelection.Month(
month = data.yearMonth,
start = maxOf(data.yearMonth.firstDay(), params.range.start),
end = minOf(data.yearMonth.lastDay(), params.range.endInclusive),
)
}

return copy(
Expand Down

0 comments on commit 4997bc7

Please sign in to comment.