Long press and drag in the month to open a run of days (#389)
This commit is contained in:
1 parent
713e9377db
commit
0eded5e4b0
7 files changed
+291
-13
No files matched your search
@@ -97,6 +97,7 @@ import de.jeanlucmakiola.calendula.ui.common.drillToWeek
|
||||
import de.jeanlucmakiola.calendula.ui.common.rememberCalendarSlideSpec
|
||||
import de.jeanlucmakiola.calendula.ui.common.selectView
|
||||
import de.jeanlucmakiola.calendula.ui.common.swapView
|
||||
import de.jeanlucmakiola.calendula.ui.common.drillToMultiDay
|
||||
import de.jeanlucmakiola.calendula.ui.common.LocalMultiDayCount
|
||||
import de.jeanlucmakiola.calendula.ui.week.MultiDayViewModel
|
||||
import de.jeanlucmakiola.calendula.ui.common.viewBaseStack
|
||||
@@ -209,6 +210,16 @@ fun CalendarHost(
|
||||
focus.navigateTo(date)
|
||||
switchTo { it.drillToWeek() }
|
||||
}
|
||||
// A run of days picked in the month (#389): one day is the day view.
|
||||
val onOpenRange: (LocalDate, Int) -> Unit = { start, count ->
|
||||
if (count == 1) {
|
||||
onOpenDay(start)
|
||||
} else {
|
||||
multiDayViewModel.showRange(start, count)
|
||||
focus.navigateTo(start)
|
||||
switchTo { it.drillToMultiDay() }
|
||||
}
|
||||
}
|
||||
|
||||
// The event-detail screen (S4) is a full-screen destination hoisted here so
|
||||
// it overlays whichever calendar view is active. We forward the tapped
|
||||
@@ -757,6 +768,7 @@ fun CalendarHost(
|
||||
ViewScreen.Month -> MonthScreen(
|
||||
onOpenDay = onOpenDay,
|
||||
onOpenWeek = onOpenWeek,
|
||||
onOpenRange = onOpenRange,
|
||||
onEventClick = onEventClick,
|
||||
onCreateEvent = onCreateEvent,
|
||||
)
|
||||
|
||||
@@ -150,3 +150,7 @@ val CalendarView.screen: ViewScreen
|
||||
/** Push the week view over the month after a week-number tap. */
|
||||
fun List<CalendarView>.drillToWeek(): List<CalendarView> =
|
||||
if (lastOrNull() == CalendarView.Week) this else this + CalendarView.Week
|
||||
|
||||
/** Push the multi-day view over the month after picking a run of days there (#389). */
|
||||
fun List<CalendarView>.drillToMultiDay(): List<CalendarView> =
|
||||
if (lastOrNull() == CalendarView.MultiDay) this else this + CalendarView.MultiDay
|
||||
@@ -89,6 +89,26 @@ internal fun isSameEvent(chip: EventInstance, moved: EventInstance): Boolean =
|
||||
chip.eventId == moved.eventId ||
|
||||
(moved.title.isNotBlank() && chip.title == moved.title)
|
||||
|
||||
/** The most days a range picked in the month opens (#389). */
|
||||
const val MAX_RANGE_DAYS = 14
|
||||
|
||||
/** A run of days picked by dragging from [anchor]; [clamped] when the finger went further than it may reach. */
|
||||
data class RangePick(val days: ClosedRange<LocalDate>, val clamped: Boolean) {
|
||||
val count: Int get() = (days.endInclusive.toEpochDays() - days.start.toEpochDays()).toInt() + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* The days a drag from [anchor] to [current] picks, earliest first, either way
|
||||
* round, and at most [maxDays] long: past that it stops [maxDays] from [anchor].
|
||||
*/
|
||||
fun pickRange(anchor: LocalDate, current: LocalDate, maxDays: Int = MAX_RANGE_DAYS): RangePick {
|
||||
val span = current.toEpochDays() - anchor.toEpochDays()
|
||||
val reach = (maxDays - 1).toLong()
|
||||
val clamped = abs(span) > reach
|
||||
val end = if (clamped) LocalDate.fromEpochDays(anchor.toEpochDays() + if (span > 0) reach else -reach) else current
|
||||
return RangePick(if (end < anchor) end..anchor else anchor..end, clamped)
|
||||
}
|
||||
|
||||
/** A chip in flight, in root coordinates so it can be drawn in an overlay. */
|
||||
data class MonthChipDrag(
|
||||
val event: EventInstance,
|
||||
@@ -202,6 +222,33 @@ class MonthDragController {
|
||||
private var pointer = Offset.Zero
|
||||
private var sizePx = IntSize.Zero
|
||||
|
||||
private var rangeAnchor: LocalDate? = null
|
||||
|
||||
/** The days a long press and drag on empty space is picking (#389), or null. */
|
||||
var range: RangePick? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
/** Start picking days from [date]. */
|
||||
fun beginRange(date: LocalDate) {
|
||||
rangeAnchor = date
|
||||
range = pickRange(date, date)
|
||||
}
|
||||
|
||||
/** Follow the finger to the day under it; the last one stands while it is off the grid. */
|
||||
fun moveRange(pointerInRoot: Offset) {
|
||||
val anchor = rangeAnchor ?: return
|
||||
val day = dateAt(pointerInRoot) ?: return
|
||||
range = pickRange(anchor, day)
|
||||
}
|
||||
|
||||
/** End the pick, handing back the days picked. */
|
||||
fun finishRange(): RangePick? = range.also { cancelRange() }
|
||||
|
||||
fun cancelRange() {
|
||||
rangeAnchor = null
|
||||
range = null
|
||||
}
|
||||
|
||||
fun putRow(token: Any, geometry: MonthRowGeometry) {
|
||||
rows[token] = geometry
|
||||
}
|
||||
@@ -379,21 +426,24 @@ class MonthDragController {
|
||||
bounds.left + column * row.columnWidthPx
|
||||
}
|
||||
|
||||
/** The day under [pointer] in whichever visible row it is over. */
|
||||
private fun dateAt(pointer: Offset): LocalDate? = rows.values.firstNotNullOfOrNull { row ->
|
||||
val bounds = row.cell.takeIf { it.isAttached }?.boundsInRoot()
|
||||
?: return@firstNotNullOfOrNull null
|
||||
if (!bounds.contains(pointer) || row.columnWidthPx <= 0f) {
|
||||
null
|
||||
} else {
|
||||
val column = ((pointer.x - bounds.left) / row.columnWidthPx)
|
||||
.toInt()
|
||||
.coerceIn(0, row.days.lastIndex)
|
||||
row.days[if (row.isRtl) row.days.lastIndex - column else column]
|
||||
}
|
||||
}
|
||||
|
||||
private fun recompute() {
|
||||
val event = event ?: return
|
||||
val grabbed = grabDate ?: return
|
||||
val resolved = rows.values.firstNotNullOfOrNull { row ->
|
||||
val bounds = row.cell.takeIf { it.isAttached }?.boundsInRoot()
|
||||
?: return@firstNotNullOfOrNull null
|
||||
if (!bounds.contains(pointer) || row.columnWidthPx <= 0f) {
|
||||
null
|
||||
} else {
|
||||
val column = ((pointer.x - bounds.left) / row.columnWidthPx)
|
||||
.toInt()
|
||||
.coerceIn(0, row.days.lastIndex)
|
||||
row.days[if (row.isRtl) row.days.lastIndex - column else column]
|
||||
}
|
||||
}
|
||||
val resolved = dateAt(pointer)
|
||||
drag = MonthChipDrag(
|
||||
event = event,
|
||||
grabDate = grabbed,
|
||||
|
||||
@@ -149,6 +149,7 @@ import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.platform.LocalViewConfiguration
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
@@ -189,6 +190,7 @@ import de.jeanlucmakiola.floret.identity.rememberReduceMotion
|
||||
import de.jeanlucmakiola.floret.locale.currentLocale
|
||||
import de.jeanlucmakiola.calendula.ui.common.next
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -216,6 +218,9 @@ private fun MonthUiState.Success.withToday(today: LocalDate?): MonthUiState.Succ
|
||||
if (today == null || today == this.today) this else copy(today = today)
|
||||
private val LocalOpenWeek = staticCompositionLocalOf<((LocalDate) -> Unit)?> { null }
|
||||
|
||||
/** Opens the days picked by a long press and drag (#389). */
|
||||
private val LocalOpenRange = staticCompositionLocalOf<((LocalDate, Int) -> Unit)?> { null }
|
||||
|
||||
/** A brief push from today's full cell into its immediate neighbours. */
|
||||
@Composable
|
||||
private fun rememberTodayRipple(active: Boolean): Animatable<Float, androidx.compose.animation.core.AnimationVector1D> {
|
||||
@@ -268,6 +273,7 @@ private fun Modifier.todayRipple(relativeColumn: Int?, progress: () -> Float): M
|
||||
fun MonthScreen(
|
||||
onOpenDay: (LocalDate) -> Unit,
|
||||
onOpenWeek: (LocalDate) -> Unit,
|
||||
onOpenRange: (LocalDate, Int) -> Unit,
|
||||
onEventClick: (EventInstance) -> Unit,
|
||||
onCreateEvent: (LocalDate, Int?) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
@@ -530,6 +536,7 @@ fun MonthScreen(
|
||||
LocalTodayPulse provides todayPulse,
|
||||
LocalMonthToday provides today,
|
||||
LocalOpenWeek provides onOpenWeek,
|
||||
LocalOpenRange provides onOpenRange,
|
||||
) {
|
||||
if (scrolling) {
|
||||
ContinuousMonthContent(
|
||||
@@ -2346,6 +2353,23 @@ private fun MonthWeekRow(
|
||||
isRtl = isRtl,
|
||||
chipTimes = chipTimes,
|
||||
),
|
||||
)
|
||||
.then(
|
||||
monthRangePickModifier(
|
||||
week = week,
|
||||
controller = dragController,
|
||||
pickable = { d -> !blankOutside || inMonth(d) },
|
||||
chipAt = { col, cellY ->
|
||||
week.chipAtCellY(
|
||||
col = col,
|
||||
cellY = cellY,
|
||||
bandTopInCell = bandTopInCell(cellCoordinates, bandCoordinates),
|
||||
rowHeightPx = rowHeightPx,
|
||||
laneCap = laneCap,
|
||||
)
|
||||
},
|
||||
isRtl = isRtl,
|
||||
),
|
||||
),
|
||||
) {
|
||||
// What a chip has to spend, against what [timeFit] says its start
|
||||
@@ -2566,6 +2590,25 @@ private fun MonthWeekRow(
|
||||
}
|
||||
}
|
||||
|
||||
// The days a long press and drag is picking (#389).
|
||||
val picked = dragController?.range?.days
|
||||
if (picked != null && week.days.any { it in picked }) {
|
||||
val pickedColor = MaterialTheme.colorScheme.primary.copy(alpha = RANGE_PICK_ALPHA)
|
||||
Row(Modifier.matchParentSize()) {
|
||||
week.days.forEach { d ->
|
||||
Box(
|
||||
Modifier
|
||||
.weight(1f)
|
||||
.fillMaxHeight()
|
||||
.padding(horizontal = CELL_GAP, vertical = 1.dp)
|
||||
.then(
|
||||
if (d in picked) Modifier.background(pickedColor, CELL_SHAPE) else Modifier,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invisible stand-in for the compact grid's selection outline, so it
|
||||
// has somewhere to travel to and from. Its own layer rather than a
|
||||
// child of the cell pill: that pill is itself a tagged piece, and a
|
||||
@@ -2753,6 +2796,107 @@ private fun monthChipDragModifier(
|
||||
onCancel = { controller?.cancel() },
|
||||
)
|
||||
|
||||
/** The picked days' tint: M3's dragged state layer. */
|
||||
private const val RANGE_PICK_ALPHA = 0.16f
|
||||
|
||||
/**
|
||||
* Long press on a day's empty space and drag to pick a run of days, which opens
|
||||
* in the timeline (#389). Only a press that then moves picks anything: one held
|
||||
* still is left untouched, and lifts into the day's tap as it always did. A
|
||||
* press on a chip is the chip's to drag.
|
||||
*/
|
||||
@Composable
|
||||
private fun monthRangePickModifier(
|
||||
week: MonthWeek,
|
||||
controller: MonthDragController?,
|
||||
pickable: (LocalDate) -> Boolean,
|
||||
chipAt: (col: Int, cellY: Float) -> EventInstance?,
|
||||
isRtl: Boolean,
|
||||
): Modifier {
|
||||
val openRange = LocalOpenRange.current
|
||||
if (controller == null || openRange == null) return Modifier
|
||||
val haptics = LocalHapticFeedback.current
|
||||
val holdMillis = LocalViewConfiguration.current.longPressTimeoutMillis
|
||||
val tolerance = with(LocalDensity.current) { RANGE_HOLD_TOLERANCE.toPx() }
|
||||
val currentOpen by rememberUpdatedState(openRange)
|
||||
val currentPickable by rememberUpdatedState(pickable)
|
||||
val currentChipAt by rememberUpdatedState(chipAt)
|
||||
val coordinates = remember { arrayOfNulls<LayoutCoordinates>(1) }
|
||||
return Modifier
|
||||
.onGloballyPositioned { coordinates[0] = it }
|
||||
.pointerInput(week.days.first(), controller, isRtl) {
|
||||
val slop = viewConfiguration.touchSlop
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false)
|
||||
val origin = down.position
|
||||
// Null once held long enough; anything else let go of it first.
|
||||
val abandoned = withTimeoutOrNull(holdMillis) {
|
||||
var still = true
|
||||
while (still) {
|
||||
val event = awaitPointerEvent()
|
||||
val change = event.changes.firstOrNull { it.id == down.id }
|
||||
still = change != null && change.pressed && event.changes.count { it.pressed } == 1 &&
|
||||
(change.position - origin).getDistance() <= tolerance &&
|
||||
// An ancestor's claim only shows once the main pass has run.
|
||||
awaitPointerEvent(PointerEventPass.Final).changes.none { it.isConsumed }
|
||||
}
|
||||
true
|
||||
}
|
||||
if (abandoned != null) return@awaitEachGesture
|
||||
val columnPx = size.width / 7f
|
||||
if (columnPx <= 0f) return@awaitEachGesture
|
||||
val column = (origin.x / columnPx).toInt().coerceIn(0, week.days.lastIndex)
|
||||
val dayIndex = if (isRtl) week.days.lastIndex - column else column
|
||||
val day = week.days[dayIndex]
|
||||
if (!currentPickable(day) || currentChipAt(dayIndex, origin.y) != null) return@awaitEachGesture
|
||||
// Held still so far: only moving on past slop makes it a pick.
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
val change = event.changes.firstOrNull { it.id == down.id } ?: return@awaitEachGesture
|
||||
if (!change.pressed || change.isConsumed || event.changes.count { it.pressed } > 1) {
|
||||
return@awaitEachGesture
|
||||
}
|
||||
// Taken before the pager or the list can read it as their own.
|
||||
if ((change.position - origin).getDistance() > slop) {
|
||||
change.consume()
|
||||
break
|
||||
}
|
||||
}
|
||||
controller.beginRange(day)
|
||||
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
var picked = controller.range
|
||||
var finished = false
|
||||
try {
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
val change = event.changes.firstOrNull { it.id == down.id } ?: break
|
||||
if (change.isConsumed) break
|
||||
change.consume()
|
||||
if (!change.pressed) {
|
||||
finished = true
|
||||
break
|
||||
}
|
||||
coordinates[0]?.takeIf { it.isAttached }?.let { controller.moveRange(it.localToRoot(change.position)) }
|
||||
val now = controller.range
|
||||
if (now != null && now != picked) {
|
||||
haptics.performHapticFeedback(
|
||||
if (now.clamped && picked?.clamped != true) HapticFeedbackType.Reject
|
||||
else HapticFeedbackType.SegmentTick,
|
||||
)
|
||||
}
|
||||
picked = now
|
||||
}
|
||||
} finally {
|
||||
val range = if (finished) controller.finishRange() else null
|
||||
if (range == null) controller.cancelRange() else currentOpen(range.days.start, range.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** How far the finger may wander while the press is held — as a chip pickup allows. */
|
||||
private val RANGE_HOLD_TOLERANCE = 6.dp
|
||||
|
||||
/**
|
||||
* Left-gutter calendar-week cell (#25): a full-height tonal pill mirroring the
|
||||
* day cells' geometry, set apart by the secondaryContainer tint (matching the
|
||||
|
||||
@@ -174,4 +174,12 @@ class ViewBackStackTest {
|
||||
assertThat(listOf(CalendarView.Week, CalendarView.Day).swapView(CalendarView.Week))
|
||||
.containsExactly(CalendarView.Week)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a range picked in the month drills the multi-day view on top, so back returns to the month`() {
|
||||
val stack = listOf(CalendarView.Month).drillToMultiDay()
|
||||
assertThat(stack).containsExactly(CalendarView.Month, CalendarView.MultiDay).inOrder()
|
||||
assertThat(stack.drillToMultiDay()).isEqualTo(stack)
|
||||
assertThat(stack.dropLast(1)).containsExactly(CalendarView.Month)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package de.jeanlucmakiola.calendula.ui.month
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.datetime.LocalDate
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/** The run of days a long press and drag in the month picks (#389). */
|
||||
class RangePickTest {
|
||||
|
||||
private val anchor = LocalDate(2026, 10, 16)
|
||||
|
||||
@Test
|
||||
fun `a drag forward picks from the anchor to the finger`() {
|
||||
val pick = pickRange(anchor, LocalDate(2026, 10, 18))
|
||||
assertThat(pick.days).isEqualTo(anchor..LocalDate(2026, 10, 18))
|
||||
assertThat(pick.count).isEqualTo(3)
|
||||
assertThat(pick.clamped).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a drag backwards picks earliest first`() {
|
||||
val pick = pickRange(anchor, LocalDate(2026, 10, 13))
|
||||
assertThat(pick.days).isEqualTo(LocalDate(2026, 10, 13)..anchor)
|
||||
assertThat(pick.count).isEqualTo(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `back on the anchor is the one day`() {
|
||||
assertThat(pickRange(anchor, anchor)).isEqualTo(RangePick(anchor..anchor, clamped = false))
|
||||
assertThat(pickRange(anchor, anchor).count).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `exactly the most days is not clamped`() {
|
||||
val pick = pickRange(anchor, LocalDate(2026, 10, 29))
|
||||
assertThat(pick.count).isEqualTo(MAX_RANGE_DAYS)
|
||||
assertThat(pick.clamped).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `further than the most days stops that far from the anchor either way`() {
|
||||
val forward = pickRange(anchor, LocalDate(2026, 11, 20))
|
||||
assertThat(forward.days).isEqualTo(anchor..LocalDate(2026, 10, 29))
|
||||
assertThat(forward.clamped).isTrue()
|
||||
val back = pickRange(anchor, LocalDate(2026, 9, 1))
|
||||
assertThat(back.days).isEqualTo(LocalDate(2026, 10, 3)..anchor)
|
||||
assertThat(back.count).isEqualTo(MAX_RANGE_DAYS)
|
||||
assertThat(back.clamped).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a pick crosses months`() {
|
||||
val pick = pickRange(LocalDate(2026, 10, 30), LocalDate(2026, 11, 2))
|
||||
assertThat(pick.days).isEqualTo(LocalDate(2026, 10, 30)..LocalDate(2026, 11, 2))
|
||||
assertThat(pick.count).isEqualTo(4)
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,10 @@ gesture finishes from its scaled preview.
|
||||
(`steppedDayCount`), each step a column resize like a view switch, anchored
|
||||
on the day under the fingers. One day is the day view, more the multi-day
|
||||
view with a session day count (`MultiDayViewModel.showRange`) that the
|
||||
switcher clears; the pinch swaps the view in place on the back stack.
|
||||
switcher clears; the pinch swaps the view in place on the back stack. The
|
||||
month's long press and drag on empty space (`monthRangePickModifier`) picks
|
||||
up to 14 days the same way and drills the multi-day view over the month; a
|
||||
long press held still stays the day's tap.
|
||||
- **Across screens, what both show morphs.** Inside a `SharedTransitionLayout`,
|
||||
an event shown in both views travels from its old bounds to its new ones,
|
||||
keyed per occurrence *and* per day it is drawn from (`ViewMorphKey`); only a
|
||||
|
||||
Reference in new issue
Block a user