travel: range calendar matching the single date picker
- Commit
4bb0fa6400187beedd216d5e5ec35ef640d1b542- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 17:42 UTC (1 month ago)
- Parents
fae2c7d
Replace Material's DateRangePicker (a vertically scrolling month list with its own header) with RangeCalendar, built to the DatePicker's layout and tokens: 120dp header (title + headline), 56dp month row with previous/next arrows, weekday row, one swipeable month of 48dp cells with 40dp day circles, primary start/end, outlined today, and a secondary-container band across the range. Selection rules live in domain/DateRangeSelection.kt (tested).
Diff
travel/src/main/java/com/vayunmathur/travel/domain/DateRangeSelection.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/domain/DateRangeSelection.kt b/travel/src/main/java/com/vayunmathur/travel/domain/DateRangeSelection.ktnew file mode 100644index 0000000000..b520a49f84--- /dev/null+++ b/travel/src/main/java/com/vayunmathur/travel/domain/DateRangeSelection.kt@@ -0,0 +1,18 @@+package com.vayunmathur.travel.domain++import java.time.LocalDate++/**+ * The next (start, end) selection after tapping [tapped] in a range calendar,+ * following Material's range rules: a tap with no range in progress (or after+ * a complete range) starts a new range; a tap before the start restarts there;+ * otherwise it ends the range. [allowSameDay] = false (hotel nights) keeps+ * waiting for an end after the start when the start is tapped again.+ */+fun nextRange(start: LocalDate?, end: LocalDate?, tapped: LocalDate, allowSameDay: Boolean): Pair<LocalDate?, LocalDate?> =+ when {+ start == null || end != null -> tapped to null+ tapped.isBefore(start) -> tapped to null+ tapped == start && !allowSameDay -> start to null+ else -> start to tapped+ }
travel/src/main/java/com/vayunmathur/travel/ui/DateRangeFields.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/ui/DateRangeFields.kt b/travel/src/main/java/com/vayunmathur/travel/ui/DateRangeFields.ktindex 6f185ace03..1b36bec273 100644--- a/travel/src/main/java/com/vayunmathur/travel/ui/DateRangeFields.kt+++ b/travel/src/main/java/com/vayunmathur/travel/ui/DateRangeFields.kt@@ -1,6 +1,7 @@ package com.vayunmathur.travel.ui import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.travel.R import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth@@ -12,25 +13,19 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.DateRangePicker import com.vayunmathur.library.ui.DatePickerDialog import com.vayunmathur.library.ui.DateString-import com.vayunmathur.library.ui.ExperimentalMaterial3Api-import com.vayunmathur.library.ui.SelectableDates import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton-import com.vayunmathur.library.ui.rememberDateRangePickerState import kotlinx.datetime.LocalDate-import kotlinx.datetime.TimeZone-import kotlinx.datetime.atStartOfDayIn /** * Two side-by-side date fields (e.g. Depart / Return, Check-in / Check-out)- * that open one range calendar: tap either to pick both dates at once. Values+ * that open one range calendar ([RangeCalendar], styled like the single date+ * picker) in the same dialog: tap either to pick both dates at once. Values * are ISO `YYYY-MM-DD` ("" when unset); dates before [minDate] are disabled. * [allowSameDay] = false requires the end to be after the start (hotel nights). */-@OptIn(ExperimentalMaterial3Api::class) @Composable fun DateRangeFields( startLabel: String,@@ -52,27 +47,18 @@ fun DateRangeFields( } if (!show) return - val minMillis = minDate?.atStartOfDayIn(TimeZone.UTC)?.toEpochMilliseconds()- val selectable = remember(minMillis) {- object : SelectableDates {- override fun isSelectableDate(utcTimeMillis: Long): Boolean =- minMillis == null || utcTimeMillis >= minMillis- }- }- val state = rememberDateRangePickerState(- initialSelectedStartDateMillis = isoToUtcMillis(start),- initialSelectedEndDateMillis = isoToUtcMillis(end),- selectableDates = selectable,- )- val startMs = state.selectedStartDateMillis- val endMs = state.selectedEndDateMillis- val complete = startMs != null && endMs != null && (allowSameDay || endMs > startMs)+ // Work on a draft so Cancel leaves the fields untouched.+ var draftStart by remember { mutableStateOf(start.toJavaDate()) }+ var draftEnd by remember { mutableStateOf(end.toJavaDate()) }+ val complete = draftStart != null && draftEnd != null DatePickerDialog( onDismissRequest = { show = false }, confirmButton = { TextButton( onClick = {- if (startMs != null && endMs != null) onRange(utcMillisToIso(startMs), utcMillisToIso(endMs))+ val s = draftStart+ val e = draftEnd+ if (s != null && e != null) onRange(s.toString(), e.toString()) show = false }, enabled = complete,@@ -80,6 +66,20 @@ fun DateRangeFields( }, dismissButton = { TextButton(onClick = { show = false }) { Text(stringResource(UiR.string.cancel)) } }, ) {- DateRangePicker(state, modifier = Modifier.weight(1f))+ RangeCalendar(+ title = stringResource(R.string.select_dates),+ startLabel = startLabel,+ endLabel = endLabel,+ start = draftStart,+ end = draftEnd,+ onChange = { s, e ->+ draftStart = s+ draftEnd = e+ },+ minDate = minDate?.let { java.time.LocalDate.parse(it.toString()) } ?: java.time.LocalDate.now(),+ allowSameDay = allowSameDay,+ ) } }++private fun String.toJavaDate(): java.time.LocalDate? = runCatching { java.time.LocalDate.parse(this) }.getOrNull()
travel/src/main/java/com/vayunmathur/travel/ui/RangeCalendar.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/ui/RangeCalendar.kt b/travel/src/main/java/com/vayunmathur/travel/ui/RangeCalendar.ktnew file mode 100644index 0000000000..89fe746eec--- /dev/null+++ b/travel/src/main/java/com/vayunmathur/travel/ui/RangeCalendar.kt@@ -0,0 +1,264 @@+package com.vayunmathur.travel.ui++import androidx.compose.foundation.BorderStroke+import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.RowScope+import androidx.compose.foundation.layout.defaultMinSize+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.requiredHeight+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.pager.HorizontalPager+import androidx.compose.foundation.pager.rememberPagerState+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.runtime.Composable+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.platform.LocalConfiguration+import androidx.compose.ui.platform.LocalLayoutDirection+import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.LayoutDirection+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.HorizontalDivider+import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconKeyboardArrowLeft+import com.vayunmathur.library.ui.IconKeyboardArrowRight+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Surface+import com.vayunmathur.library.ui.Text+import com.vayunmathur.travel.domain.nextRange+import kotlinx.coroutines.launch+import java.time.DayOfWeek+import java.time.LocalDate+import java.time.YearMonth+import java.time.format.DateTimeFormatter+import java.time.format.TextStyle+import java.time.temporal.WeekFields+import java.util.Locale++// Sizes mirror the Material 3 DatePicker so the range calendar reads as the+// same component: 120dp header, 56dp month row, 48dp cells, 40dp day circles.+private val HeaderMinHeight = 120.dp+private val HeaderTitlePadding = PaddingValues(start = 24.dp, end = 12.dp, top = 16.dp)+private val HeaderHeadlinePadding = PaddingValues(start = 24.dp, end = 12.dp, bottom = 12.dp)+private val MonthRowHeight = 56.dp+private val HorizontalPadding = 12.dp+private val CellSize = 48.dp+private val DaySize = 40.dp+private const val CalendarRows = 6+private const val MonthsAhead = 24L++/**+ * A start/end date calendar that looks and behaves like the single Material+ * DatePicker (same header, month navigation, weekday row and day cells), with+ * the days between start and end joined by a range band.+ *+ * Tapping a day starts a new range; the next tap at or after it ends the range+ * (a tap before it restarts there). [allowSameDay] = false requires end > start.+ */+@Composable+internal fun RangeCalendar(+ title: String,+ startLabel: String,+ endLabel: String,+ start: LocalDate?,+ end: LocalDate?,+ onChange: (start: LocalDate?, end: LocalDate?) -> Unit,+ minDate: LocalDate,+ allowSameDay: Boolean,+ modifier: Modifier = Modifier,+) {+ val locale: Locale = LocalConfiguration.current.locales[0]+ val firstMonth = remember(minDate) { YearMonth.from(minDate) }+ val initialPage = remember {+ val shown = YearMonth.from(start ?: minDate)+ (shown.year - firstMonth.year) * 12 + (shown.monthValue - firstMonth.monthValue)+ }.coerceIn(0, MonthsAhead.toInt() - 1)+ val pager = rememberPagerState(initialPage = initialPage) { MonthsAhead.toInt() }+ val scope = rememberCoroutineScope()+ val shownMonth = firstMonth.plusMonths(pager.currentPage.toLong())++ Column(modifier.fillMaxWidth()) {+ // Header: title + headline, as in DatePicker's DateEntryContainer.+ Column(Modifier.fillMaxWidth().defaultMinSize(minHeight = HeaderMinHeight), verticalArrangement = Arrangement.SpaceBetween) {+ Text(+ title,+ modifier = Modifier.padding(HeaderTitlePadding),+ style = MaterialTheme.typography.labelLarge,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ Text(+ "${start?.let { formatHeadlineDate(it, locale) } ?: startLabel} – ${end?.let { formatHeadlineDate(it, locale) } ?: endLabel}",+ modifier = Modifier.padding(HeaderHeadlinePadding),+ style = MaterialTheme.typography.headlineLarge,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ maxLines = 1,+ )+ }+ HorizontalDivider()++ // Month navigation: "November 2026" with previous/next arrows.+ Row(+ Modifier.fillMaxWidth().requiredHeight(MonthRowHeight).padding(horizontal = HorizontalPadding),+ horizontalArrangement = Arrangement.SpaceBetween,+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Text(+ shownMonth.format(DateTimeFormatter.ofPattern("LLLL yyyy", locale)),+ modifier = Modifier.padding(start = 12.dp).semantics { contentDescription = shownMonth.toString() },+ style = MaterialTheme.typography.labelLarge,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ Row {+ IconButton(+ onClick = { scope.launch { pager.animateScrollToPage(pager.currentPage - 1) } },+ enabled = pager.currentPage > 0,+ ) { IconKeyboardArrowLeft(tint = MaterialTheme.colorScheme.onSurfaceVariant) }+ IconButton(+ onClick = { scope.launch { pager.animateScrollToPage(pager.currentPage + 1) } },+ enabled = pager.currentPage < MonthsAhead - 1,+ ) { IconKeyboardArrowRight(tint = MaterialTheme.colorScheme.onSurfaceVariant) }+ }+ }++ Column(Modifier.padding(horizontal = HorizontalPadding)) {+ val weekDays = remember(locale) { orderedWeekDays(locale) }+ Row(Modifier.fillMaxWidth().defaultMinSize(minHeight = CellSize), verticalAlignment = Alignment.CenterVertically) {+ weekDays.forEach { day ->+ Text(+ day.getDisplayName(TextStyle.NARROW, locale),+ modifier = Modifier.weight(1f).semantics { contentDescription = day.getDisplayName(TextStyle.FULL, locale) },+ style = MaterialTheme.typography.bodyLarge,+ color = MaterialTheme.colorScheme.onSurface,+ textAlign = TextAlign.Center,+ )+ }+ }+ HorizontalPager(state = pager, modifier = Modifier.fillMaxWidth()) { page ->+ MonthGrid(+ month = firstMonth.plusMonths(page.toLong()),+ weekDays = weekDays,+ start = start,+ end = end,+ minDate = minDate,+ onDay = { tapped ->+ val (newStart, newEnd) = nextRange(start, end, tapped, allowSameDay)+ onChange(newStart, newEnd)+ },+ )+ }+ }+ }+}++@Composable+private fun MonthGrid(+ month: YearMonth,+ weekDays: List<DayOfWeek>,+ start: LocalDate?,+ end: LocalDate?,+ minDate: LocalDate,+ onDay: (LocalDate) -> Unit,+) {+ val today = remember { LocalDate.now() }+ val leading = weekDays.indexOf(month.atDay(1).dayOfWeek)+ Column(Modifier.fillMaxWidth().requiredHeight(CellSize * CalendarRows), verticalArrangement = Arrangement.SpaceEvenly) {+ for (row in 0 until CalendarRows) {+ Row(Modifier.fillMaxWidth().height(CellSize)) {+ for (col in 0 until 7) {+ val dayNumber = row * 7 + col - leading + 1+ if (dayNumber < 1 || dayNumber > month.lengthOfMonth()) {+ Box(Modifier.weight(1f))+ } else {+ val date = month.atDay(dayNumber)+ DayCell(+ date = date,+ start = start,+ end = end,+ today = date == today,+ enabled = !date.isBefore(minDate),+ onClick = { onDay(date) },+ )+ }+ }+ }+ }+ }+}++@Composable+private fun RowScope.DayCell(+ date: LocalDate,+ start: LocalDate?,+ end: LocalDate?,+ today: Boolean,+ enabled: Boolean,+ onClick: () -> Unit,+) {+ val colors = MaterialTheme.colorScheme+ val isStart = date == start+ val isEnd = date == end+ val selected = isStart || isEnd+ val hasRange = start != null && end != null && end.isAfter(start)+ val inRange = hasRange && date.isAfter(start) && date.isBefore(end)+ val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl+ Box(Modifier.weight(1f).fillMaxHeight(), contentAlignment = Alignment.Center) {+ // Range band (40dp tall, like the state layer): full width between the+ // ends, half width on the start/end cells so the ends stay round.+ if (hasRange && (inRange || isStart || isEnd)) {+ val band = colors.secondaryContainer+ Row(Modifier.fillMaxWidth().height(DaySize)) {+ val leftFilled = inRange || (if (rtl) isStart else isEnd)+ val rightFilled = inRange || (if (rtl) isEnd else isStart)+ Box(Modifier.weight(1f).fillMaxHeight().background(if (leftFilled) band else Color.Transparent))+ Box(Modifier.weight(1f).fillMaxHeight().background(if (rightFilled) band else Color.Transparent))+ }+ }+ Surface(+ onClick = onClick,+ enabled = enabled,+ shape = CircleShape,+ color = if (selected) colors.primary else Color.Transparent,+ border = if (today && !selected) BorderStroke(1.dp, colors.primary) else null,+ modifier = Modifier.size(DaySize),+ ) {+ Box(contentAlignment = Alignment.Center) {+ Text(+ date.dayOfMonth.toString(),+ style = MaterialTheme.typography.bodyLarge,+ color = when {+ selected -> colors.onPrimary+ !enabled -> colors.onSurface.copy(alpha = 0.38f)+ inRange -> colors.onSecondaryContainer+ today -> colors.primary+ else -> colors.onSurface+ },+ textAlign = TextAlign.Center,+ )+ }+ }+ }+}++private fun orderedWeekDays(locale: Locale): List<DayOfWeek> {+ val first = WeekFields.of(locale).firstDayOfWeek+ return (0L until 7L).map { first.plus(it) }+}++/** "Nov 30" in the locale's own order (e.g. "30 Nov"). */+private fun formatHeadlineDate(date: LocalDate, locale: Locale): String =+ date.format(+ DateTimeFormatter.ofPattern(android.text.format.DateFormat.getBestDateTimePattern(locale, "MMMd"), locale)+ )
travel/src/main/res/values/strings.xml
diff --git a/travel/src/main/res/values/strings.xml b/travel/src/main/res/values/strings.xmlindex 24c08f661e..e5e23bf7e3 100644--- a/travel/src/main/res/values/strings.xml+++ b/travel/src/main/res/values/strings.xml@@ -115,6 +115,8 @@ <string name="choose_fare">Choose fare</string> <!-- Round-trip picker title: pick outbound + return flights. Verb phrase --> <string name="choose_flights">Choose flights</string>+ <!-- Date range picker title. Verb phrase -->+ <string name="select_dates">Select dates</string> <!-- Round-trip picker subtitle, first step. Noun phrase --> <string name="step_1_of_2">Step 1 of 2</string> <!-- Round-trip picker subtitle, second step. Noun phrase -->
travel/src/test/java/com/vayunmathur/travel/pure/DateRangeSelectionTest.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/pure/DateRangeSelectionTest.kt b/travel/src/test/java/com/vayunmathur/travel/pure/DateRangeSelectionTest.ktnew file mode 100644index 0000000000..b0c949effe--- /dev/null+++ b/travel/src/test/java/com/vayunmathur/travel/pure/DateRangeSelectionTest.kt@@ -0,0 +1,33 @@+package com.vayunmathur.travel.pure++import com.vayunmathur.travel.domain.nextRange+import java.time.LocalDate+import kotlin.test.Test+import kotlin.test.assertEquals++class DateRangeSelectionTest {+ private val d1 = LocalDate.of(2026, 11, 30)+ private val d2 = LocalDate.of(2026, 12, 7)++ @Test+ fun firstTapStartsRange_secondTapEndsIt() {+ assertEquals(d1 to null, nextRange(null, null, d1, allowSameDay = true))+ assertEquals(d1 to d2, nextRange(d1, null, d2, allowSameDay = true))+ }++ @Test+ fun tapAfterCompleteRangeStartsOver() {+ assertEquals(d2 to null, nextRange(d1, d2, d2, allowSameDay = true))+ }++ @Test+ fun tapBeforeStartRestartsThere() {+ assertEquals(d1 to null, nextRange(d2, null, d1, allowSameDay = true))+ }++ @Test+ fun sameDayDependsOnAllowSameDay() {+ assertEquals(d1 to d1, nextRange(d1, null, d1, allowSameDay = true))+ assertEquals(d1 to null, nextRange(d1, null, d1, allowSameDay = false))+ }+}