Drops follow their event if it moved, failures stay on the chip (#494)

This commit is contained in:
makiolaj committed 2026-11-13 12:45:48 +01:00
1 parent 8e3202d1ad
commit 0595124ecd
5 files changed
+235 -101

No files matched your search

@@ -63,8 +63,8 @@ private val moveTokens = AtomicLong()
/** A fresh identity for one drop or undo, unique for the process. */
fun nextMoveToken(): Long = moveTokens.incrementAndGet()
/** How many ended drops [DropStates] remembers — far more than can be on screen at once. */
private const val ENDED_KEPT = 32
/** How many abandoned drops [DropStates] remembers — far more than can be on screen at once. */
private const val ABANDONED_KEPT = 32
/** Per-drop progress, by token — see `RescheduleViewModel.drops`. */
@Immutable
@@ -76,7 +76,10 @@ data class DropStates(
fun ended(token: Long, landed: Boolean): DropStates = copy(
pending = pending - token,
abandoned = if (landed) abandoned else (abandoned + token).toList().takeLast(ENDED_KEPT).toSet(),
// Insertion-ordered, so the oldest go first.
abandoned = if (landed) abandoned else (abandoned + token).let { kept ->
if (kept.size > ABANDONED_KEPT) kept.drop(kept.size - ABANDONED_KEPT).toSet() else kept
},
)
}
@@ -79,7 +79,7 @@ data class MoveRequest(
val endMillis: Long,
val target: MoveTarget,
/** The drop's own identity, from [nextMoveToken], which the view holding its copy waits on. */
val token: Long = nextMoveToken(),
val token: Long,
)
/**
@@ -122,8 +122,9 @@ sealed interface MoveOutcome {
data object Gone : MoveOutcome
/**
* A drop that waited for an earlier one found its occurrence no longer where
* it was picked up — the earlier write moved it — so it is not written (#494).
* The drop's occurrence is no longer where it was picked up — an earlier
* write moved it — and a recurring one can't be followed there, so it is not
* written (#494).
*/
data object Stale : MoveOutcome
@@ -164,7 +165,7 @@ class RescheduleViewModel @Inject constructor(
private val _inFlight = MutableStateFlow(false)
/** True while any write runs or waits, a queued drop included — what the confirmation chip holds for. */
/** True while any drop or undo runs or waits — what the confirmation chip holds for. */
val inFlight: StateFlow<Boolean> = _inFlight.asStateFlow()
private val _drops = MutableStateFlow(DropStates())
@@ -192,6 +193,13 @@ class RescheduleViewModel @Inject constructor(
* written. Drops arriving in that window wait in [queue] and run in order.
*/
private var busy = false
set(value) {
field = value
_inFlight.value = value
}
/** Whether a drop since the pipeline was last idle failed — see [report]. */
private var failedThisRun = false
/** Drops that arrived while another was in flight, oldest first (#494). */
private val queue = ArrayDeque<MoveRequest>()
@@ -240,19 +248,14 @@ class RescheduleViewModel @Inject constructor(
return false
}
_drops.update { it.started(request.token) }
if (busy) {
queue.addLast(request)
} else {
run(request, waited = false)
}
syncInFlight()
if (busy) queue.addLast(request) else run(request)
return true
}
private fun run(request: MoveRequest, waited: Boolean) {
private fun run(request: MoveRequest) {
busy = true
viewModelScope.launch {
val prepared = prepare(request, waited)
val prepared = prepare(request)
if (prepared == null) {
settle(request.token, landed = false)
return@launch
@@ -262,7 +265,7 @@ class RescheduleViewModel @Inject constructor(
if (!prepared.isRecurring || (seriesOnly && prepared.canRealign)) {
settle(request.token, write(prepared, RecurringWriteScope.AllEvents))
} else if (seriesOnly) {
_outcome.value = MoveOutcome.BlockedCancelledSeries
report(MoveOutcome.BlockedCancelledSeries)
settle(request.token, landed = false)
} else {
// Still busy: the scope dialog is now the thing in flight.
@@ -298,13 +301,26 @@ class RescheduleViewModel @Inject constructor(
/** End the drop [token], then run the next queued one, if any. */
private fun settle(token: Long, landed: Boolean) {
_drops.update { it.ended(token, landed) }
busy = false
queue.removeFirstOrNull()?.let { run(it, waited = true) }
syncInFlight()
// Straight on to the next, so busy never reads false between two drops.
val next = queue.removeFirstOrNull()
if (next != null) {
run(next)
} else {
busy = false
failedThisRun = false
}
}
private fun syncInFlight() {
_inFlight.value = busy || queue.isNotEmpty()
/**
* Show [outcome] on the chip. While drops run back to back, a failure stays
* up rather than being replaced by a later drop's success: the grid shows
* what moved, but only the chip says what didn't.
*/
private fun report(outcome: MoveOutcome) {
val failure = outcome !is MoveOutcome.Moved && outcome !is MoveOutcome.Undone
if (!failure && failedThisRun) return
if (failure) failedThisRun = true
_outcome.value = outcome
}
/**
@@ -316,11 +332,10 @@ class RescheduleViewModel @Inject constructor(
* reports whether it landed.
*/
fun undo(undo: MoveUndo): Boolean {
if (busy || queue.isNotEmpty()) return false
if (busy) return false
val token = nextMoveToken()
_drops.update { it.started(token) }
busy = true
syncInFlight()
_undoStarted.value = token
viewModelScope.launch {
_outcome.value = try {
@@ -345,25 +360,27 @@ class RescheduleViewModel @Inject constructor(
_outcome.value = null
}
private suspend fun prepare(request: MoveRequest, waited: Boolean): PreparedMove? {
val detail = try {
// A drop that waited carries times from before the writes ahead of it.
if (waited && !occurrenceStillAt(request)) {
_outcome.value = MoveOutcome.Stale
return null
}
repository.eventDetail(request.eventId)
private suspend fun prepare(dropped: MoveRequest): PreparedMove? {
val zone = zoneSource.current()
val (detail, request) = try {
val detail = repository.eventDetail(dropped.eventId)
// The drop carries the times the grid drew, which an earlier write
// may have moved on since (#494).
detail to if (occurrenceStillAt(dropped)) dropped else followed(dropped, detail, zone)
} catch (e: CancellationException) {
throw e
} catch (e: NoSuchEventException) {
_outcome.value = MoveOutcome.Gone
report(MoveOutcome.Gone)
return null
} catch (e: Exception) {
_outcome.value = MoveOutcome.Failed
report(MoveOutcome.Failed)
return null
}
if (request == null) {
report(MoveOutcome.Stale)
return null
}
val zone = zoneSource.current()
val original = detail.toEditForm(request.beginMillis, request.endMillis, zone)
val shifted = when (val target = request.target) {
is MoveTarget.Start -> original.shiftedTo(target.instant, zone)
@@ -376,7 +393,7 @@ class RescheduleViewModel @Inject constructor(
// The UNTIL check is deferred to the write, which knows how far the move
// reaches and so which date has to clear it.
if ((shifted.problems(zone) - EventFormProblem.RecurrenceEndsBeforeStart).isNotEmpty()) {
_outcome.value = MoveOutcome.Failed
report(MoveOutcome.Failed)
return null
}
@@ -425,10 +442,10 @@ class RescheduleViewModel @Inject constructor(
private suspend fun write(prepared: PreparedMove, scope: RecurringWriteScope): Boolean {
val request = prepared.request
if (endsBeforeItStarts(prepared, scope)) {
_outcome.value = MoveOutcome.BlockedSeriesEnd
report(MoveOutcome.BlockedSeriesEnd)
return false
}
_outcome.value = try {
val outcome = try {
when {
!prepared.isRecurring || scope == RecurringWriteScope.AllEvents ->
repository.updateEvent(request.eventId, prepared.original, prepared.updated)
@@ -463,18 +480,46 @@ class RescheduleViewModel @Inject constructor(
} catch (e: Exception) {
MoveOutcome.Failed
}
return _outcome.value is MoveOutcome.Moved
report(outcome)
return outcome is MoveOutcome.Moved
}
/** Whether [request]'s occurrence still begins where it was picked up. */
/** Whether [request]'s occurrence is still exactly where it was picked up. */
private suspend fun occurrenceStillAt(request: MoveRequest): Boolean {
val begin = Instant.fromEpochMilliseconds(request.beginMillis)
val end = Instant.fromEpochMilliseconds(maxOf(request.endMillis, request.beginMillis + 1))
return repository.eventOccurrences(request.eventId, begin..end).any {
it.eventId == request.eventId && it.start == begin
val end = Instant.fromEpochMilliseconds(request.endMillis)
val range = begin..maxOf(end, Instant.fromEpochMilliseconds(request.beginMillis + 1))
return repository.eventOccurrences(request.eventId, range).any {
it.eventId == request.eventId && it.start == begin && it.end == end
}
}
/**
* [dropped] rebased onto where its event now is, so the last drop wins:
* absolute targets stand as they are, and a whole-day shift is re-aimed at
* the day it was dropped on. Null for a recurring event, whose moved
* occurrence can't be told apart from the rest of its series.
*/
private fun followed(dropped: MoveRequest, detail: EventDetail, zone: TimeZone): MoveRequest? {
if (detail.rrule != null && !detail.isException) return null
val now = detail.instance
val target = when (val target = dropped.target) {
is MoveTarget.ByDays -> {
val dayZone = if (now.isAllDay) TimeZone.UTC else zone
val aimed = Instant.fromEpochMilliseconds(dropped.beginMillis).toLocalDateTime(dayZone).date
.plus(target.days, DateTimeUnit.DAY)
val from = now.start.toLocalDateTime(dayZone).date
MoveTarget.ByDays((aimed.toEpochDays() - from.toEpochDays()).toInt())
}
else -> target
}
return dropped.copy(
beginMillis = now.start.toEpochMilliseconds(),
endMillis = now.end.toEpochMilliseconds(),
target = target,
)
}
/**
* Whether this write would leave a rule whose `UNTIL` precedes the first day
* it now applies to — the provider then generates nothing at all. Which date
@@ -138,9 +138,17 @@ data class StripDrag(val event: EventInstance, val dayShift: Int = 0, val overGr
sealed interface StripDrop {
val event: EventInstance
data class Days(override val event: EventInstance, val days: Int) : StripDrop
/** The drop's identity, which its held copy waits on. */
val token: Long
data class Timed(override val event: EventInstance, val start: Instant, val end: Instant) : StripDrop
data class Days(override val event: EventInstance, val days: Int, override val token: Long) : StripDrop
data class Timed(
override val event: EventInstance,
val start: Instant,
val end: Instant,
override val token: Long,
) : StripDrop
}
/** The event a long press left selected (#141). */
@@ -297,6 +305,8 @@ data class TimelineDrop(
val startMin: Int,
/** Minutes from the event's start to the dragged block's top edge. */
val clipOffsetMin: Int = 0,
/** The drop's identity, which its held copy waits on; set by the controller's finish. */
val token: Long = 0L,
)
/** The id a drafted block's stand-in event carries, which no provider row has (#153). */
@@ -340,7 +350,13 @@ internal fun draftBlock(draft: EventInstance, date: LocalDate, zone: TimeZone):
enum class ResizeEdge { Top, Bottom }
/** Where a finished resize asks its event to begin and end. */
data class TimelineResizeDrop(val event: EventInstance, val start: Instant, val end: Instant)
data class TimelineResizeDrop(
val event: EventInstance,
val start: Instant,
val end: Instant,
/** The drop's identity, which its held copy waits on; set by the controller's finishResize. */
val token: Long = 0L,
)
/**
* [event] resized by its [edge] to the span [startMin]..+[spanMin] from [date]'s
@@ -610,7 +626,7 @@ class TimelineDragController {
var settlingSelected: Boolean = false
private set
/** The token of the drop [settling] waits on; a drop's caller hands it to its `MoveRequest`. */
/** The token of the drop [settling] waits on. */
var settledToken: Long = 0L
private set
@@ -742,16 +758,16 @@ class TimelineDragController {
val zone = dragZone
cancel()
if (held.overGrid && landed != null && zone != null && !landed.edgeOnly) {
settle(landed)
val token = settle(landed)
val start = TimelineDrop(landed.event, landed.date, landed.startMin).startInstant(zone)
return StripDrop.Timed(held.event, start, start + draftMinutes.minutes)
return StripDrop.Timed(held.event, start, start + draftMinutes.minutes, token)
}
if (held.overGrid || held.dayShift == 0) return null
stripSettling = held
stripSeated = false
stripToken = nextMoveToken()
drops++
return StripDrop.Days(held.event, held.dayShift)
return StripDrop.Days(held.event, held.dayShift, stripToken)
}
/** Let the held strip drop go, once the strip shows the event at its new days or the move failed. */
@@ -848,12 +864,15 @@ class TimelineDragController {
if (landed == null || landed.slot == origin) return null
// A draft dropped on a day off screen would vanish, so it stays where it was (#153).
if (landed.edgeOnly && landed.event.isDraft) return null
settle(landed)
return TimelineDrop(landed.event, landed.date, landed.startMin, clipOffset)
val token = settle(landed)
return TimelineDrop(landed.event, landed.date, landed.startMin, clipOffset, token)
}
/** Keep [landed]'s copy on the target as [settling] until [release]; [end] is a resize's. */
private fun settle(landed: TimelineDrag, end: Instant? = null) {
/**
* Keep [landed]'s copy on the target as [settling] until [release]; [end] is a
* resize's. Returns the drop's fresh token.
*/
private fun settle(landed: TimelineDrag, end: Instant? = null): Long {
// Landed on midnight to midnight, the event moves to the all-day strip (#296).
val leavesGrid = !landed.event.isDraft && landed.eventSpanMin > 0 &&
landed.eventStartMin.mod(MINUTES_PER_DAY) == 0 &&
@@ -869,6 +888,7 @@ class TimelineDragController {
settledToken = nextMoveToken()
settledEnd = end
drops++
return settledToken
}
/**
@@ -937,8 +957,7 @@ class TimelineDragController {
cancel()
if (landed == null || landed.slot == origin) return null
val drop = resizedDrop(landed.event, edge, landed.date, landed.eventStartMin, landed.eventSpanMin, zone)
settle(landed, end = drop.end)
return drop
return drop.copy(token = settle(landed, end = drop.end))
}
/**
@@ -592,7 +592,7 @@ private fun WeekContent(
beginMillis = drop.event.start.toEpochMilliseconds(),
endMillis = drop.event.end.toEpochMilliseconds(),
target = MoveTarget.Start(start),
token = dragController.settledToken,
token = drop.token,
),
)
}
@@ -612,7 +612,7 @@ private fun WeekContent(
beginMillis = drop.event.start.toEpochMilliseconds(),
endMillis = drop.event.end.toEpochMilliseconds(),
target = MoveTarget.Span(drop.start, drop.end),
token = dragController.settledToken,
token = drop.token,
),
)
}
@@ -628,11 +628,7 @@ private fun WeekContent(
is StripDrop.Days -> MoveTarget.ByDays(drop.days)
is StripDrop.Timed -> MoveTarget.Span(drop.start, drop.end)
},
// A bar dropped into the grid settles as a timed copy, not on the strip.
token = when (drop) {
is StripDrop.Days -> dragController.stripToken
is StripDrop.Timed -> dragController.settledToken
},
token = drop.token,
),
)
}