Modern Apps git

← Modern-Apps.git

taxi: fix order screen to include promotions

Commit
53f041effb9fc44b53509b77b92f82d277149791
Author
Vayun Mathur <[email protected]>
When
2026-08-08 01:43 UTC (4 months ago)
Parents
f30028e

Diff

taxi/src/main/java/com/vayunmathur/taxi/data/Models.kt
diff --git a/taxi/src/main/java/com/vayunmathur/taxi/data/Models.kt b/taxi/src/main/java/com/vayunmathur/taxi/data/Models.ktindex a434e0d251..2dacb0199d 100644--- a/taxi/src/main/java/com/vayunmathur/taxi/data/Models.kt+++ b/taxi/src/main/java/com/vayunmathur/taxi/data/Models.kt@@ -21,6 +21,11 @@ enum class Provider(val label: String, val packageName: String) {  * providers stay comparable without float rounding; [fareLowMinor] equals [fareHighMinor]  * when the provider quotes an exact price rather than a range.  *+ * [fareLowMinor]/[fareHighMinor] are the **actual** price the rider pays, i.e. after any+ * applicable promotion/coupon. When a promotion applies, [originalFareLowMinor]/+ * [originalFareHighMinor] carry the pre-discount amount (for a struck-through "was" price); they+ * are null when there is no discount.+ *  * The trailing [offerId]/[offerToken]/[costToken]/[rideType]/[costTokenExpiryMs] fields carry  * the tokens an in-app booking needs. They are populated only for providers that support  * in-app booking (Lyft) and stay null everywhere else, so quote-only and deep-link paths are@@ -43,8 +48,13 @@ data class RideQuote(     val costToken: String? = null,     val rideType: String? = null,     val costTokenExpiryMs: Long? = null,+    val originalFareLowMinor: Long? = null,+    val originalFareHighMinor: Long? = null, ) {     val isRange: Boolean get() = fareLowMinor != fareHighMinor++    /** True when a promotion reduced the fare below its original amount. */+    val hasDiscount: Boolean get() = originalFareLowMinor != null || originalFareHighMinor != null }  sealed interface QuoteResult {
taxi/src/main/java/com/vayunmathur/taxi/lyft/LyftProvider.kt
diff --git a/taxi/src/main/java/com/vayunmathur/taxi/lyft/LyftProvider.kt b/taxi/src/main/java/com/vayunmathur/taxi/lyft/LyftProvider.ktindex 32905bfd65..d23312fd02 100644--- a/taxi/src/main/java/com/vayunmathur/taxi/lyft/LyftProvider.kt+++ b/taxi/src/main/java/com/vayunmathur/taxi/lyft/LyftProvider.kt@@ -1171,7 +1171,13 @@ class LyftProvider(private val context: Context) : RideProvider {         val min = cost?.long("estimated_cost_cents_min")         val max = cost?.long("estimated_cost_cents_max")         val upfront = cost?.long("upfront_cost_cents")-        val (low, high) = priceRange(min, max, upfront) ?: return null+        // CostEstimate.applicable_coupons[0] carries the discount the rider actually receives.+        val coupon = cost?.get("applicable_coupons")?.jsonArray?.firstOrNull() as? JsonObject+        val fare = farePrice(+            min, max, upfront,+            discountMin = coupon?.long("discount_amount_min"),+            discountMax = coupon?.long("discount_amount_max"),+        ) ?: return null          val pickupEtaMs = offer["ride_travel_details"]?.jsonObject             ?.get("pickup_estimate")?.jsonObject@@ -1182,8 +1188,10 @@ class LyftProvider(private val context: Context) : RideProvider {             provider = Provider.LYFT,             productId = offer.str("offer_product_id") ?: name,             displayName = name,-            fareLowMinor = low,-            fareHighMinor = high,+            fareLowMinor = fare.low,+            fareHighMinor = fare.high,+            originalFareLowMinor = fare.originalLow,+            originalFareHighMinor = fare.originalHigh,             currency = cost?.str("currency") ?: "USD",             pickupEtaMinutes = pickupEtaMs?.let { (it / 60_000).toInt() },             tripDurationMinutes = cost?.long("estimated_duration_seconds")?.let { (it / 60).toInt() },@@ -1243,7 +1251,14 @@ class LyftProvider(private val context: Context) : RideProvider {         val min = cost?.wrappedLong(6)         val max = cost?.wrappedLong(5)         val upfront = cost?.wrappedLong(7)-        val (low, high) = priceRange(min, max, upfront) ?: return null+        // applicable_coupons(18, repeated ApplicableCoupon): discount_amount_min(10),+        // discount_amount_max(11) are plain int64. First coupon is the one the client applies.+        val coupon = cost?.messages(18)?.firstOrNull()+        val fare = farePrice(+            min, max, upfront,+            discountMin = coupon?.varint(10),+            discountMax = coupon?.varint(11),+        ) ?: return null          val pickupEtaMs = offer.message(9)?.message(1)?.message(2)?.wrappedLong(1) @@ -1251,8 +1266,10 @@ class LyftProvider(private val context: Context) : RideProvider {             provider = Provider.LYFT,             productId = offer.string(2) ?: name,             displayName = name,-            fareLowMinor = low,-            fareHighMinor = high,+            fareLowMinor = fare.low,+            fareHighMinor = fare.high,+            originalFareLowMinor = fare.originalLow,+            originalFareHighMinor = fare.originalHigh,             currency = cost?.wrappedString(8) ?: "USD",             pickupEtaMinutes = pickupEtaMs?.let { (it / 60_000).toInt() },             tripDurationMinutes = cost?.wrappedLong(22)?.let { (it / 60).toInt() },@@ -1278,6 +1295,37 @@ class LyftProvider(private val context: Context) : RideProvider {         else -> null     } +    /**+     * The pre-discount base range plus the actual price after the first applicable coupon. Lyft+     * carries no post-promo scalar; the app computes it as estimate − coupon discount (clamped at+     * 0), keeping `upfront`/estimate as the struck-through original. Mirrors that (see api-notes §3).+     */+    private data class FarePrice(+        val low: Long,+        val high: Long,+        val originalLow: Long?,+        val originalHigh: Long?,+    )++    private fun farePrice(+        min: Long?,+        max: Long?,+        upfront: Long?,+        discountMin: Long?,+        discountMax: Long?,+    ): FarePrice? {+        val (baseLow, baseHigh) = priceRange(min, max, upfront) ?: return null+        val dLow = (discountMin ?: 0).coerceAtLeast(0)+        val dHigh = (discountMax ?: 0).coerceAtLeast(0)+        if (dLow == 0L && dHigh == 0L) return FarePrice(baseLow, baseHigh, null, null)+        return FarePrice(+            low = (baseLow - dLow).coerceAtLeast(0),+            high = (baseHigh - dHigh).coerceAtLeast(0),+            originalLow = baseLow,+            originalHigh = baseHigh,+        )+    }+     companion object {         private const val TAG = "LyftProvider" 
taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt
diff --git a/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt b/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.ktindex 3c3cac112f..9015e00d52 100644--- a/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt+++ b/taxi/src/main/java/com/vayunmathur/taxi/ui/RideScreen.kt@@ -39,6 +39,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.DpOffset import androidx.compose.ui.unit.dp@@ -524,10 +525,24 @@ private fun RideOptionCard(quote: RideQuote, isCheapest: Boolean, onBook: () ->             }             Spacer(Modifier.width(8.dp))             Column(horizontalAlignment = Alignment.End) {+                if (quote.hasDiscount) {+                    Text(+                        formatOriginalFare(quote),+                        style = MaterialTheme.typography.bodySmall.copy(+                            textDecoration = TextDecoration.LineThrough,+                        ),+                        color = MaterialTheme.colorScheme.onSurfaceVariant,+                    )+                }                 Text(                     formatFare(quote),                     style = MaterialTheme.typography.titleMedium,                     fontWeight = FontWeight.Bold,+                    color = if (quote.hasDiscount) {+                        MaterialTheme.colorScheme.primary+                    } else {+                        MaterialTheme.colorScheme.onSurface+                    },                 )                 quote.surgeMultiplier?.takeIf { it > 1.0 }?.let {                     Text(@@ -665,3 +680,11 @@ private fun formatFare(quote: RideQuote): String {         money(quote.fareLowMinor)     } }++/** The pre-discount price, for a struck-through "was" label when a promotion applies. */+private fun formatOriginalFare(quote: RideQuote): String {+    fun money(minor: Long) = "$%.2f".format(minor / 100.0)+    val low = quote.originalFareLowMinor ?: quote.fareLowMinor+    val high = quote.originalFareHighMinor ?: quote.fareHighMinor+    return if (low != high) "${money(low)} – ${money(high)}" else money(low)+}