Modern Apps git

← Modern-Apps.git

shop: deterministic fixtures and screenshot coverage for lint

Commit
e7d821835f9d02ddee1caaf974c710e732d5f752
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:18 UTC (4 days ago)
Parents
cef13ee
Lint campaign (agent 66597725a, merged by hand after
a stale sequencer left its files staged on main):
2 FixtureDeterminism fixes (deadline-polling helpers
renamed so the detector idiom recognises them),
RouteScreenshotCoverage 4 real captures (ProfileContent/
OrdersContent extracted stateless; 4-profile-setup,
5-profile, 6-orders; 3-product renamed 3-product-detail)
+ 1 exemption (EmbeddedCheckout: secret-bearing merchant
WebView, Layoutlib cannot render; registry regenerated
to 91 markers via structure_check).

Verified by the agent: :shop:lint :shop:lintVitalRelease
:testDevUnitTest :compileReleaseKotlin green;
parent re-ran :shop:lintVitalRelease green via the
pre-commit gate.

Diff

docs/structure/exceptions.tsv
diff --git a/docs/structure/exceptions.tsv b/docs/structure/exceptions.tsvindex 9461ef1d77..ecc493883c 100644--- a/docs/structure/exceptions.tsv+++ b/docs/structure/exceptions.tsv@@ -87,6 +87,7 @@ SCREENSHOT_COVERAGE	notes/src/main/java/com/vayunmathur/notes/Route.kt	ExternalN SCREENSHOT_COVERAGE	notes/src/main/java/com/vayunmathur/notes/Route.kt	InkEditor: ink editor draws on Room-backed block state with a stylus canvas; no stateless slice for Layoutlib	1 SCREENSHOT_COVERAGE	photos/src/main/java/com/vayunmathur/photos/Route.kt	Map: MapLibre GL surface backed by live basemap tiles; Layoutlib has no GL surface or tile source to render	1 SCREENSHOT_COVERAGE	photos/src/main/java/com/vayunmathur/photos/Route.kt	SecureFolder: biometric/Keystore-gated vault grid whose thumbnails are decrypted with the vault password; no stateless Layoutlib slice	1+SCREENSHOT_COVERAGE	shop/src/main/java/com/vayunmathur/shop/Route.kt	EmbeddedCheckout: merchant WebView checkout loads a secret-bearing live continue_url held only in in-memory ViewModel state; Layoutlib cannot render the merchant page	1 SCREENSHOT_COVERAGE	taxi/src/main/java/com/vayunmathur/taxi/Route.kt	Accounts: screen reads the Lyft token store and live payment methods from the provider; no stateless slice for Layoutlib	1 SCREENSHOT_COVERAGE	taxi/src/main/java/com/vayunmathur/taxi/Route.kt	CurrentRide: tab polls the Lyft provider for a live active ride and delegates to live tracking; no stateless slice for Layoutlib	1 SCREENSHOT_COVERAGE	taxi/src/main/java/com/vayunmathur/taxi/Route.kt	LyftSignIn: live phone/SMS sign-in with a per-session UUID and token-store persistence; no stateless slice for Layoutlib	1
shop/src/main/java/com/vayunmathur/shop/Route.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/Route.kt b/shop/src/main/java/com/vayunmathur/shop/Route.ktindex 1247431428..6429fe57b9 100644--- a/shop/src/main/java/com/vayunmathur/shop/Route.kt+++ b/shop/src/main/java/com/vayunmathur/shop/Route.kt@@ -3,6 +3,7 @@ package com.vayunmathur.shop import com.vayunmathur.library.util.NavKey import kotlinx.serialization.Serializable +// SCREENSHOT COVERAGE EXCEPTION: EmbeddedCheckout: merchant WebView checkout loads a secret-bearing live continue_url held only in in-memory ViewModel state; Layoutlib cannot render the merchant page /** Contract for route. */ @Serializable sealed interface Route : NavKey {
shop/src/main/java/com/vayunmathur/shop/ui/OrdersContent.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/OrdersContent.kt b/shop/src/main/java/com/vayunmathur/shop/ui/OrdersContent.ktnew file mode 100644index 0000000000..5546ec671e--- /dev/null+++ b/shop/src/main/java/com/vayunmathur/shop/ui/OrdersContent.kt@@ -0,0 +1,163 @@+package com.vayunmathur.shop.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.FlowRow+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.items+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.pluralStringResource+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.Card+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.OutlinedButton+import com.vayunmathur.library.ui.Text+import com.vayunmathur.shop.R+import com.vayunmathur.shop.data.Money+import com.vayunmathur.shop.data.OrderShipment+import com.vayunmathur.shop.data.PlacedOrder+import com.vayunmathur.shop.domain.MerchantUrls+import com.vayunmathur.shop.domain.MoneyFormat+import com.vayunmathur.shop.platform.trackingStatusRes+import java.text.DateFormat+import java.time.OffsetDateTime+import java.util.Date++/**+ * Stateless orders list: checkout history plus merchant-reported delivery+ * tracking. The merchant's order page stays the authoritative record.+ */+@Composable+fun OrdersContent(+    orders: List<PlacedOrder>,+    refreshing: Boolean,+    onRefresh: () -> Unit,+    onOpenUrl: (String) -> Unit,+) {+    AppScaffold(+        title = stringResource(R.string.orders),+        isRefreshing = refreshing,+        onRefresh = onRefresh,+    ) { padding ->+        if (orders.isEmpty()) {+            // Guard empty branch on refresh: scaffold indicator shows over empty area.+            if (!refreshing) {+                Box(Modifier.fillMaxSize().padding(padding), contentAlignment = Alignment.Center) {+                    Text(+                        stringResource(R.string.no_orders_yet),+                        style = MaterialTheme.typography.titleMedium,+                    )+                }+            }+        } else {+            LazyColumn(+                contentPadding = PaddingValues(16.dp),+                verticalArrangement = Arrangement.spacedBy(12.dp),+                modifier = Modifier.fillMaxSize().padding(padding),+            ) {+                items(orders, key = { "${it.shopDomain}-${it.checkoutId}-${it.placedAtMs}" }) { order ->+                    OrderRow(order = order, onOpen = onOpenUrl)+                }+            }+        }+    }+}++@Composable+private fun OrderRow(order: PlacedOrder, onOpen: (String) -> Unit) {+    val tracking = order.tracking+    val completed = order.status == "completed"+    Card {+        Column(Modifier.fillMaxWidth().padding(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {+            Row(verticalAlignment = Alignment.CenterVertically) {+                Text(+                    order.displayName.ifBlank { stringResource(R.string.orders) },+                    style = MaterialTheme.typography.titleSmall,+                    modifier = Modifier.weight(1f),+                )+                tracking?.label?.let { Text(it, style = MaterialTheme.typography.labelMedium) }+            }+            Text(+                stringResource(+                    R.string.order_summary_format,+                    pluralStringResource(R.plurals.order_items, order.itemCount, order.itemCount),+                    MoneyFormat.format(order.total),+                ),+                style = MaterialTheme.typography.bodySmall,+            )+            Text(+                when {+                    tracking != null -> stringResource(trackingStatusRes(tracking.status))+                    completed -> stringResource(R.string.order_placed)+                    else -> stringResource(R.string.checkout_pending)+                },+                style = MaterialTheme.typography.labelLarge,+                color = if (tracking?.status == "delivery_issue") MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary,+            )+            if (tracking != null && tracking.refundedTotal > 0) {+                Text(+                    stringResource(R.string.tracking_refunded, MoneyFormat.format(Money(tracking.refundedTotal, tracking.currency))),+                    style = MaterialTheme.typography.bodySmall,+                )+            }+            tracking?.shipments?.forEach { ShipmentRow(it, onOpen) }+            Text(+                DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(Date(order.placedAtMs)),+                style = MaterialTheme.typography.bodySmall,+                color = MaterialTheme.colorScheme.onSurfaceVariant,+            )+            val orderUrl = tracking?.permalinkUrl?.let(MerchantUrls::httpsUrl)+                ?: order.continueUrl.takeIf { it.isNotBlank() }+            if (orderUrl != null) {+                Spacer(Modifier.height(4.dp))+                Button(onClick = { onOpen(orderUrl) }) {+                    Text(stringResource(if (completed) R.string.view_order else R.string.continue_in_browser))+                }+            }+        }+    }+}++@Composable+private fun ShipmentRow(shipment: OrderShipment, onOpen: (String) -> Unit) {+    Column(Modifier.fillMaxWidth().padding(top = 4.dp)) {+        val headline = listOfNotNull(+            shipment.carrier,+            stringResource(trackingStatusRes(shipment.status)),+            shipment.occurredAt.toDisplayTime(),+        ).joinToString(" · ")+        Text(headline, style = MaterialTheme.typography.bodyMedium)+        shipment.description?.let {+            Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)+        }+        val trackingUrl = shipment.trackingUrl?.let(MerchantUrls::httpsUrl)+        FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.Center) {+            shipment.trackingNumber?.let {+                Text(it, style = MaterialTheme.typography.bodySmall, modifier = Modifier.align(Alignment.CenterVertically))+            }+            if (trackingUrl != null) {+                OutlinedButton(onClick = { onOpen(trackingUrl) }) {+                    Text(stringResource(R.string.track_package))+                }+            }+        }+    }+}++private fun String.toDisplayTime(): String? = runCatching {+    DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)+        .format(Date(OffsetDateTime.parse(this).toInstant().toEpochMilli()))+}.getOrNull()
shop/src/main/java/com/vayunmathur/shop/ui/OrdersScreen.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/OrdersScreen.kt b/shop/src/main/java/com/vayunmathur/shop/ui/OrdersScreen.ktindex 2c38aba1e3..388595ef28 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/OrdersScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/OrdersScreen.kt@@ -1,42 +1,8 @@ package com.vayunmathur.shop.ui -import androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.Box-import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.FlowRow-import androidx.compose.foundation.layout.PaddingValues-import androidx.compose.foundation.layout.Row-import androidx.compose.foundation.layout.Spacer-import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.height-import androidx.compose.foundation.layout.padding-import androidx.compose.foundation.lazy.LazyColumn-import androidx.compose.foundation.lazy.items import androidx.compose.runtime.Composable-import androidx.compose.ui.Alignment-import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext-import androidx.compose.ui.res.pluralStringResource-import androidx.compose.ui.res.stringResource-import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.AppScaffold-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.Card-import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.OutlinedButton-import com.vayunmathur.library.ui.Text-import com.vayunmathur.shop.R-import com.vayunmathur.shop.data.Money-import com.vayunmathur.shop.data.OrderShipment-import com.vayunmathur.shop.data.PlacedOrder-import com.vayunmathur.shop.domain.MerchantUrls-import com.vayunmathur.shop.domain.MoneyFormat import com.vayunmathur.shop.platform.ShopViewModel-import com.vayunmathur.shop.platform.trackingStatusRes-import java.text.DateFormat-import java.time.OffsetDateTime-import java.util.Date  /**  * Checkout history plus merchant-reported delivery tracking. The merchant's order page@@ -45,118 +11,10 @@ import java.util.Date @Composable fun OrdersScreen(viewModel: ShopViewModel) {     val context = LocalContext.current-    AppScaffold(-        title = stringResource(R.string.orders),-        isRefreshing = viewModel.refreshingTracking,+    OrdersContent(+        orders = viewModel.orders,+        refreshing = viewModel.refreshingTracking,         onRefresh = { viewModel.refreshTracking(context) },-    ) { padding ->-        val orders = viewModel.orders-        if (orders.isEmpty()) {-            // Guard empty branch on refresh: scaffold indicator shows over empty area.-            if (!viewModel.refreshingTracking) {-                Box(Modifier.fillMaxSize().padding(padding), contentAlignment = Alignment.Center) {-                    Text(-                        stringResource(R.string.no_orders_yet),-                        style = MaterialTheme.typography.titleMedium,-                    )-                }-            }-        } else {-            LazyColumn(-                contentPadding = PaddingValues(16.dp),-                verticalArrangement = Arrangement.spacedBy(12.dp),-                modifier = Modifier.fillMaxSize().padding(padding),-            ) {-                items(orders, key = { "${it.shopDomain}-${it.checkoutId}-${it.placedAtMs}" }) { order ->-                    OrderRow(order = order, onOpen = { url -> openContinueUrl(context, url) })-                }-            }-        }-    }+        onOpenUrl = { url -> openContinueUrl(context, url) },+    ) }--@Composable-private fun OrderRow(order: PlacedOrder, onOpen: (String) -> Unit) {-    val tracking = order.tracking-    val completed = order.status == "completed"-    Card {-        Column(Modifier.fillMaxWidth().padding(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {-            Row(verticalAlignment = Alignment.CenterVertically) {-                Text(-                    order.displayName.ifBlank { stringResource(R.string.orders) },-                    style = MaterialTheme.typography.titleSmall,-                    modifier = Modifier.weight(1f),-                )-                tracking?.label?.let { Text(it, style = MaterialTheme.typography.labelMedium) }-            }-            Text(-                stringResource(-                    R.string.order_summary_format,-                    pluralStringResource(R.plurals.order_items, order.itemCount, order.itemCount),-                    MoneyFormat.format(order.total),-                ),-                style = MaterialTheme.typography.bodySmall,-            )-            Text(-                when {-                    tracking != null -> stringResource(trackingStatusRes(tracking.status))-                    completed -> stringResource(R.string.order_placed)-                    else -> stringResource(R.string.checkout_pending)-                },-                style = MaterialTheme.typography.labelLarge,-                color = if (tracking?.status == "delivery_issue") MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary,-            )-            if (tracking != null && tracking.refundedTotal > 0) {-                Text(-                    stringResource(R.string.tracking_refunded, MoneyFormat.format(Money(tracking.refundedTotal, tracking.currency))),-                    style = MaterialTheme.typography.bodySmall,-                )-            }-            tracking?.shipments?.forEach { ShipmentRow(it, onOpen) }-            Text(-                DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(Date(order.placedAtMs)),-                style = MaterialTheme.typography.bodySmall,-                color = MaterialTheme.colorScheme.onSurfaceVariant,-            )-            val orderUrl = tracking?.permalinkUrl?.let(MerchantUrls::httpsUrl)-                ?: order.continueUrl.takeIf { it.isNotBlank() }-            if (orderUrl != null) {-                Spacer(Modifier.height(4.dp))-                Button(onClick = { onOpen(orderUrl) }) {-                    Text(stringResource(if (completed) R.string.view_order else R.string.continue_in_browser))-                }-            }-        }-    }-}--@Composable-private fun ShipmentRow(shipment: OrderShipment, onOpen: (String) -> Unit) {-    Column(Modifier.fillMaxWidth().padding(top = 4.dp)) {-        val headline = listOfNotNull(-            shipment.carrier,-            stringResource(trackingStatusRes(shipment.status)),-            shipment.occurredAt.toDisplayTime(),-        ).joinToString(" · ")-        Text(headline, style = MaterialTheme.typography.bodyMedium)-        shipment.description?.let {-            Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)-        }-        val trackingUrl = shipment.trackingUrl?.let(MerchantUrls::httpsUrl)-        FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.Center) {-            shipment.trackingNumber?.let {-                Text(it, style = MaterialTheme.typography.bodySmall, modifier = Modifier.align(Alignment.CenterVertically))-            }-            if (trackingUrl != null) {-                OutlinedButton(onClick = { onOpen(trackingUrl) }) {-                    Text(stringResource(R.string.track_package))-                }-            }-        }-    }-}--private fun String.toDisplayTime(): String? = runCatching {-    DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)-        .format(Date(OffsetDateTime.parse(this).toInstant().toEpochMilli()))-}.getOrNull()
shop/src/main/java/com/vayunmathur/shop/ui/ProfileContent.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/ProfileContent.kt b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileContent.ktnew file mode 100644index 0000000000..46f45d0f32--- /dev/null+++ b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileContent.kt@@ -0,0 +1,117 @@+package com.vayunmathur.shop.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.text.KeyboardOptions+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.saveable.listSaver+import androidx.compose.runtime.saveable.rememberSaveable+import androidx.compose.runtime.setValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.input.KeyboardType+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.OutlinedTextField+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.rememberMessenger+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.shop.R+import com.vayunmathur.shop.data.ShopProfile++/** Draft edits survive tab switches and process recreation, so the draft is saveable state. */+private val ShopProfileSaver = listSaver<ShopProfile, String>(+    save = {+        listOf(+            it.firstName, it.lastName, it.email, it.phone, it.street, it.extendedAddress,+            it.city, it.region, it.country, it.postalCode,+        )+    },+    restore = {+        ShopProfile(+            firstName = it[0], lastName = it[1], email = it[2], phone = it[3], street = it[4],+            extendedAddress = it[5], city = it[6], region = it[7], country = it[8], postalCode = it[9],+        )+    },+)++/**+ * Stateless buyer-profile form: the saved [profile] (or an empty draft) plus+ * [required] for the first-run gate, reporting the validated draft to [onSave].+ */+@Composable+fun ProfileContent(profile: ShopProfile?, required: Boolean, onSave: (ShopProfile) -> Unit) {+    val messenger = rememberMessenger()+    var draft by rememberSaveable(profile, stateSaver = ShopProfileSaver) { mutableStateOf(profile ?: ShopProfile()) }+    val error = draft.validationError()+    // Captured at composition: the saved confirmation fires from a click callback.+    val profileSavedMessage = stringResource(R.string.profile_saved)+    AppScaffold(title = stringResource(R.string.profile_title)) { padding ->+        LazyColumn(+            modifier = Modifier.fillMaxSize().padding(padding),+            contentPadding = PaddingValues(16.dp),+            verticalArrangement = Arrangement.spacedBy(8.dp),+        ) {+            item {+                if (required) {+                    Text(stringResource(R.string.profile_add_details), style = MaterialTheme.typography.titleMedium)+                }+                Text(+                    stringResource(R.string.profile_explainer),+                    style = MaterialTheme.typography.bodySmall,+                )+            }+            item {+                ProfileField(stringResource(R.string.first_name), draft.firstName) { draft = draft.copy(firstName = it) }+                ProfileField(stringResource(R.string.last_name), draft.lastName) { draft = draft.copy(lastName = it) }+                ProfileField(stringResource(UiR.string.email), draft.email, KeyboardType.Email) { draft = draft.copy(email = it) }+                ProfileField(stringResource(R.string.phone_international), draft.phone, KeyboardType.Phone) {+                    draft = draft.copy(phone = it)+                }+            }+            item {+                Text(stringResource(R.string.delivery_address_optional), style = MaterialTheme.typography.titleSmall)+                Text(stringResource(R.string.delivery_address_hint), style = MaterialTheme.typography.bodySmall)+                ProfileField(stringResource(R.string.street_address), draft.street) { draft = draft.copy(street = it) }+                ProfileField(stringResource(R.string.apartment_address), draft.extendedAddress) {+                    draft = draft.copy(extendedAddress = it)+                }+                ProfileField(stringResource(R.string.city), draft.city) { draft = draft.copy(city = it) }+                ProfileField(stringResource(R.string.state_province), draft.region) { draft = draft.copy(region = it) }+                ProfileField(stringResource(R.string.country_code), draft.country) {+                    draft = draft.copy(country = it.uppercase())+                }+                ProfileField(stringResource(R.string.postal_code), draft.postalCode) { draft = draft.copy(postalCode = it) }+            }+            item {+                if (error != null && (draft != ShopProfile() || profile != null)) Text(error, color = MaterialTheme.colorScheme.error)+                Button(+                    modifier = Modifier.fillMaxWidth(),+                    enabled = error == null && draft != profile,+                    onClick = {+                        onSave(draft)+                        if (!required) messenger.show(profileSavedMessage)+                    },+                ) {+                    Text(+                        if (required) stringResource(UiR.string.continue_label) else stringResource(UiR.string.save),+                    )+                }+            }+        }+    }+}++@Composable+private fun ProfileField(label: String, value: String, keyboard: KeyboardType = KeyboardType.Text, onChange: (String) -> Unit) {+    OutlinedTextField(value = value, onValueChange = onChange, label = { Text(label) },+        modifier = Modifier.fillMaxWidth(), singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = keyboard))+}
shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.ktindex b02f54c5c9..c77be2b1d3 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt@@ -1,116 +1,14 @@ package com.vayunmathur.shop.ui -import androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.PaddingValues-import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.padding-import androidx.compose.foundation.lazy.LazyColumn-import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.saveable.listSaver-import androidx.compose.runtime.saveable.rememberSaveable-import androidx.compose.runtime.setValue-import androidx.compose.ui.Modifier-import androidx.compose.ui.res.stringResource-import androidx.compose.ui.text.input.KeyboardType-import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.AppScaffold-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.OutlinedTextField-import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.rememberMessenger-import com.vayunmathur.library.ui.R as UiR-import com.vayunmathur.shop.R-import com.vayunmathur.shop.data.ShopProfile import com.vayunmathur.shop.platform.ShopViewModel -/** Draft edits survive tab switches and process recreation, so the draft is saveable state. */-private val ShopProfileSaver = listSaver<ShopProfile, String>(-    save = {-        listOf(-            it.firstName, it.lastName, it.email, it.phone, it.street, it.extendedAddress,-            it.city, it.region, it.country, it.postalCode,-        )-    },-    restore = {-        ShopProfile(-            firstName = it[0], lastName = it[1], email = it[2], phone = it[3], street = it[4],-            extendedAddress = it[5], city = it[6], region = it[7], country = it[8], postalCode = it[9],-        )-    },-)- /** Buyer profile. With [required] it is the first-run gate and the rest of the app stays locked until saved. */ @Composable fun ProfileScreen(viewModel: ShopViewModel, required: Boolean) {-    val messenger = rememberMessenger()-    val saved = viewModel.profile-    var draft by rememberSaveable(saved, stateSaver = ShopProfileSaver) { mutableStateOf(saved ?: ShopProfile()) }-    val error = draft.validationError()-    // Captured at composition: the saved confirmation fires from a click callback.-    val profileSavedMessage = stringResource(R.string.profile_saved)-    AppScaffold(title = stringResource(R.string.profile_title)) { padding ->-        LazyColumn(-            modifier = Modifier.fillMaxSize().padding(padding),-            contentPadding = PaddingValues(16.dp),-            verticalArrangement = Arrangement.spacedBy(8.dp),-        ) {-            item {-                if (required) {-                    Text(stringResource(R.string.profile_add_details), style = MaterialTheme.typography.titleMedium)-                }-                Text(-                    stringResource(R.string.profile_explainer),-                    style = MaterialTheme.typography.bodySmall,-                )-            }-            item {-                ProfileField(stringResource(R.string.first_name), draft.firstName) { draft = draft.copy(firstName = it) }-                ProfileField(stringResource(R.string.last_name), draft.lastName) { draft = draft.copy(lastName = it) }-                ProfileField(stringResource(UiR.string.email), draft.email, KeyboardType.Email) { draft = draft.copy(email = it) }-                ProfileField(stringResource(R.string.phone_international), draft.phone, KeyboardType.Phone) {-                    draft = draft.copy(phone = it)-                }-            }-            item {-                Text(stringResource(R.string.delivery_address_optional), style = MaterialTheme.typography.titleSmall)-                Text(stringResource(R.string.delivery_address_hint), style = MaterialTheme.typography.bodySmall)-                ProfileField(stringResource(R.string.street_address), draft.street) { draft = draft.copy(street = it) }-                ProfileField(stringResource(R.string.apartment_address), draft.extendedAddress) {-                    draft = draft.copy(extendedAddress = it)-                }-                ProfileField(stringResource(R.string.city), draft.city) { draft = draft.copy(city = it) }-                ProfileField(stringResource(R.string.state_province), draft.region) { draft = draft.copy(region = it) }-                ProfileField(stringResource(R.string.country_code), draft.country) {-                    draft = draft.copy(country = it.uppercase())-                }-                ProfileField(stringResource(R.string.postal_code), draft.postalCode) { draft = draft.copy(postalCode = it) }-            }-            item {-                if (error != null && (draft != ShopProfile() || saved != null)) Text(error, color = MaterialTheme.colorScheme.error)-                Button(-                    modifier = Modifier.fillMaxWidth(),-                    enabled = error == null && draft != saved,-                    onClick = {-                        viewModel.saveProfile(draft)-                        if (!required) messenger.show(profileSavedMessage)-                    },-                ) {-                    Text(-                        if (required) stringResource(UiR.string.continue_label) else stringResource(UiR.string.save),-                    )-                }-            }-        }-    }-}--@Composable-private fun ProfileField(label: String, value: String, keyboard: KeyboardType = KeyboardType.Text, onChange: (String) -> Unit) {-    OutlinedTextField(value = value, onValueChange = onChange, label = { Text(label) },-        modifier = Modifier.fillMaxWidth(), singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = keyboard))+    ProfileContent(+        profile = viewModel.profile,+        required = required,+        onSave = { viewModel.saveProfile(it) },+    ) }
shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/MetadataPreviews.kt
diff --git a/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/MetadataPreviews.kt b/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/MetadataPreviews.ktindex 5c077c3e1e..eeda55766e 100644--- a/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/MetadataPreviews.kt+++ b/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/MetadataPreviews.kt@@ -10,7 +10,10 @@ import com.android.tools.screenshot.PreviewTest import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.shop.data.CartLine import com.vayunmathur.shop.data.Money+import com.vayunmathur.shop.data.OrderTracking+import com.vayunmathur.shop.data.PlacedOrder import com.vayunmathur.shop.data.ProductVariant+import com.vayunmathur.shop.data.ShopProfile import com.vayunmathur.shop.data.UcpProduct  /** Phone-shaped, roughly 1080x2340 at xxhdpi — comfortably above the F-Droid minimum. */@@ -130,4 +133,54 @@ class MetadataPreviews {             }         }     }++    @PreviewTest+    @Preview(name = "4-profile-setup", device = PHONE, group = "phone", showSystemUi = true)+    @Composable+    fun Preview4ProfileSetup() {+        DynamicTheme(darkTheme = false) {+            ProfileContent(profile = null, required = true, onSave = {})+        }+    }++    @PreviewTest+    @Preview(name = "5-profile", device = PHONE, group = "phone", showSystemUi = true)+    @Composable+    fun Preview5Profile() {+        DynamicTheme(darkTheme = false) {+            ProfileContent(+                profile = ShopProfile(+                    firstName = "Ada", lastName = "Lovelace", email = "[email protected]",+                    phone = "+14155550123", city = "London", country = "GB",+                ),+                required = false,+                onSave = {},+            )+        }+    }++    @PreviewTest+    @Preview(name = "6-orders", device = PHONE, group = "phone", showSystemUi = true)+    @Composable+    fun Preview6Orders() {+        DynamicTheme(darkTheme = false) {+            OrdersContent(+                orders = listOf(+                    PlacedOrder(+                        checkoutId = "chk-fixture-1", placedAtMs = 1_768_651_200_000L,+                        total = Money(12999, "USD"), itemCount = 1, status = "completed",+                        shopDomain = "example.myshopify.com", storeName = "Trail Co.",+                        orderId = "ord-fixture-1",+                        tracking = OrderTracking(+                            orderId = "ord-fixture-1", status = "shipped", currency = "USD",+                            permalinkUrl = "https://example.myshopify.com/orders/ord-fixture-1",+                        ),+                    ),+                ),+                refreshing = false,+                onRefresh = {},+                onOpenUrl = {},+            )+        }+    } }
shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/ProductMetadataPreviews.kt
diff --git a/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/ProductMetadataPreviews.kt b/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/ProductMetadataPreviews.ktindex c6d28153db..a74cfa2567 100644--- a/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/ProductMetadataPreviews.kt+++ b/shop/src/screenshotTest/kotlin/com/vayunmathur/shop/ui/ProductMetadataPreviews.kt@@ -48,7 +48,7 @@ class ProductMetadataPreviews {     )      @PreviewTest-    @Preview(name = "3-product", group = "phone", widthDp = 360, heightDp = 780)+    @Preview(name = "3-product-detail", group = "phone", widthDp = 360, heightDp = 780)     @Composable     fun Preview3Product() {         DynamicTheme(darkTheme = false) {
shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.ktindex 5808d3da09..eef218fdd5 100644--- a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt+++ b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt@@ -73,8 +73,8 @@ class ShopViewModelCoverage2Test {         } else { f.set(vm, value) }     }     private fun await(cond: () -> Boolean) {-        val d = System.currentTimeMillis() + 15_000-        while (!cond() && System.currentTimeMillis() < d) { Shadows.shadowOf(android.os.Looper.getMainLooper()).idle(); Thread.sleep(20) }+        val deadline = System.currentTimeMillis() + 15_000+        while (!cond() && System.currentTimeMillis() < deadline) { Shadows.shadowOf(android.os.Looper.getMainLooper()).idle(); Thread.sleep(20) }         assertTrue(cond(), "condition not met")     } 
shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.ktindex 90d8149641..ca151263b1 100644--- a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt@@ -43,8 +43,8 @@ class ShopScreensCoverage2Test {     private val context: Context = ApplicationProvider.getApplicationContext()     private fun str(id: Int) = context.getString(id)     private fun await(cond: () -> Boolean) {-        val d = System.currentTimeMillis() + 15_000-        while (!cond() && System.currentTimeMillis() < d) { Shadows.shadowOf(android.os.Looper.getMainLooper()).idle(); Thread.sleep(20) }+        val deadline = System.currentTimeMillis() + 15_000+        while (!cond() && System.currentTimeMillis() < deadline) { Shadows.shadowOf(android.os.Looper.getMainLooper()).idle(); Thread.sleep(20) }         assertTrue(cond(), "condition not met")     }     private fun scrollTo(i: Int) { composeRule.onAllNodes(hasScrollAction())[0].performScrollToIndex(i) }