travel: every pending trip is polled on resume, not just the last one
- Commit
c2f24fd12282b9ab757dc1c55f49f76e8c584b23- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 03:43 UTC (5 days ago)
- Parents
bb354fc
MainActivity.onResume loops pendingTripIds and polls each, but pollTrip began with pollJob?.cancel() on a single shared job: starting trip B's poll cancelled trip A's, and so on, so with two or more trips pending_payment/processing only the last ever refreshed. Every earlier paid trip sat at 'processing / payment pending' with no booking reference until manually refreshed. Polling is now per-trip (a MutableMap<String, Job>): pollTrip cancels/replaces only its own trip's job and unregisters on completion, timeout surfaces that trip's own last polled state, confirmPaidTrip cancels only its own poll, and stopTripPoll cancels all. The regression test reproduces the exact onResume sequence (two back-to-back polls; trip_1's second round never happened pre-fix, its reference stayed blank) and passes now. Dates, search/sort, money formatting, seats and price gates were audited and cleared. Verified: :travel:testDevUnitTest 416 tests pass; lint errors are pre-existing on untouched files.
Diff
travel/src/main/java/com/vayunmathur/travel/platform/TravelBookingOps.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/platform/TravelBookingOps.kt b/travel/src/main/java/com/vayunmathur/travel/platform/TravelBookingOps.ktindex 5cf05bd5d3..ed1f970904 100644--- a/travel/src/main/java/com/vayunmathur/travel/platform/TravelBookingOps.kt+++ b/travel/src/main/java/com/vayunmathur/travel/platform/TravelBookingOps.kt@@ -383,7 +383,7 @@ internal suspend fun TravelViewModel.persistPendingTrip(checkout: CheckoutDto) { * confirmation screen can offer "Complete payment" again. */ fun TravelViewModel.confirmPaidTrip(tripId: String) {- pollJob?.cancel()+ pollJobs.remove(tripId)?.cancel() tripPollMutable.value = TripPollState.Polling viewModelScope.launch { runCatching { TravelApi.confirmPayment(tripId) }@@ -405,13 +405,18 @@ private const val TRIP_POLL_INTERVAL_MS = 3_000L * Updates the local trip and surfaces the final state. */ fun TravelViewModel.pollTrip(tripId: String) {- pollJob?.cancel()+ // Polls are per-trip: re-polling one trip must not cancel another trip's+ // poll (MainActivity.onResume starts one poll per pending trip).+ pollJobs.remove(tripId)?.cancel() tripPollMutable.value = TripPollState.Polling- pollJob = viewModelScope.launch {+ lateinit var job: kotlinx.coroutines.Job+ job = viewModelScope.launch {+ var lastPolled: TripDto? = null repeat(TRIP_POLL_ROUNDS) { round -> if (round > 0) delay(TRIP_POLL_INTERVAL_MS) val trip = runCatching { TravelApi.trip(tripId) }.getOrNull() if (trip != null) {+ lastPolled = trip currentTrip = trip updateStoredTrip(trip) if (!isTripPollingStatus(trip.status)) {@@ -421,16 +426,20 @@ fun TravelViewModel.pollTrip(tripId: String) { tripPollMutable.value = TripPollState.Polling } }- // Timed out still pending: surface the last known state.- val last = currentTrip+ // Timed out still pending: surface this trip's last known state.+ val last = lastPolled ?: currentTrip?.takeIf { it.tripId == tripId } tripPollMutable.value = if (last != null) TripPollState.Done(last) else TripPollState.Error("Still processing. Pull to refresh for the latest status.") }+ pollJobs[tripId] = job+ job.invokeOnCompletion {+ if (pollJobs[tripId] === job) pollJobs.remove(tripId)+ } } fun TravelViewModel.stopTripPoll() {- pollJob?.cancel()- pollJob = null+ pollJobs.values.forEach { it.cancel() }+ pollJobs.clear() if (tripPollMutable.value is TripPollState.Polling) { tripPollMutable.value = TripPollState.Idle }
travel/src/main/java/com/vayunmathur/travel/platform/TravelViewModel.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/platform/TravelViewModel.kt b/travel/src/main/java/com/vayunmathur/travel/platform/TravelViewModel.ktindex 941ce05c46..39915be459 100644--- a/travel/src/main/java/com/vayunmathur/travel/platform/TravelViewModel.kt+++ b/travel/src/main/java/com/vayunmathur/travel/platform/TravelViewModel.kt@@ -107,8 +107,8 @@ class TravelViewModel( /** The trip being booked (id + latest polled state). */ internal var currentTrip: TripDto? = null - /** Active trip-polling job (cancelled on re-poll / stop). */- internal var pollJob: kotlinx.coroutines.Job? = null+ /** Active trip-polling jobs by trip id (each cancelled on its own re-poll / stop). */+ internal val pollJobs = mutableMapOf<String, kotlinx.coroutines.Job>() /** Active cancellation-status poll (cancelled on reset / re-quote). */ internal var cancelPollJob: kotlinx.coroutines.Job? = null
travel/src/test/java/com/vayunmathur/travel/platform/TravelBookingC5Test.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/platform/TravelBookingC5Test.kt b/travel/src/test/java/com/vayunmathur/travel/platform/TravelBookingC5Test.ktindex 4f01806546..596ef09054 100644--- a/travel/src/test/java/com/vayunmathur/travel/platform/TravelBookingC5Test.kt+++ b/travel/src/test/java/com/vayunmathur/travel/platform/TravelBookingC5Test.kt@@ -272,6 +272,48 @@ class TravelBookingC5Test { v.stopTripPoll() } + @Test+ fun pollTrip_twoPendingTrips_bothArePolled() {+ // MainActivity.onResume polls every pending trip back-to-back; each+ // poll must run, not just the last one started.+ repo.trips.value = listOf(+ BookedTrip(+ orderId = "trip_1", bookingReference = "", route = "SFO → JFK",+ departDate = "2027-06-24", amount = "250.00", currency = "USD",+ status = "processing", tripId = "trip_1",+ ),+ BookedTrip(+ orderId = "trip_2", bookingReference = "", route = "JFK → SFO",+ departDate = "2027-06-28", amount = "300.00", currency = "USD",+ status = "processing", tripId = "trip_2",+ ),+ )+ val requested = mutableListOf<String>()+ var trip1Fetches = 0+ handler = { url ->+ val id = url.substringAfter("/trips/").substringBefore('?')+ requested += id+ // trip_1 stays processing for one poll round (so its poll job is+ // still between rounds when trip_2's poll starts), then confirms.+ val status = if (id == "trip_1" && ++trip1Fetches == 1) "processing" else "confirmed"+ val ref = if (status == "confirmed") "REF-$id" else ""+ TravelRaw(+ 200,+ """{"tripId":"$id","status":"$status","bookingReference":"$ref","totalAmount":"250.00","currency":"USD"}""",+ )+ }+ val v = vm()+ v.pollTrip("trip_1")+ v.pollTrip("trip_2")+ idle()+ // Let the 3s poll interval elapse so trip_1 gets its second round.+ ShadowLooper.idleMainLooper(10, java.util.concurrent.TimeUnit.SECONDS)+ idle()+ assertEquals("REF-trip_1", repo.trips.value.first { it.orderId == "trip_1" }.bookingReference, "requested=$requested")+ assertEquals("REF-trip_2", repo.trips.value.first { it.orderId == "trip_2" }.bookingReference, "requested=$requested")+ v.stopTripPoll()+ }+ @Test fun confirmPaidTrip_failure_setsPollError() { val v = vm()