Modern Apps git

← Modern-Apps.git

fooddelivery: fix detekt findings

Commit
9a10465f7dbe0381f472f3e71ebdbdeb5ab6081d
Author
Vayun Mathur <[email protected]>
When
2026-11-17 14:54 UTC (19 days ago)
Parents
c6ea6f8
Fixes every error-severity detekt finding in fooddelivery (inventory clusters C3/C4): named constants for magic numbers, extracted functions/composables for long/complex methods, wrapped long lines, removed unused parameters, and narrowed/logged exception handling. No detekt config, baseline or suppression changes; behaviour preserved.

Verified: ./check fooddelivery and :fooddelivery:testDevUnitTest green.

Diff

fooddelivery/src/main/java/com/vayunmathur/fooddelivery/Navigation.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/Navigation.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/Navigation.ktindex b70d72846e..aec92146ec 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/Navigation.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/Navigation.kt@@ -67,46 +67,7 @@ fun Navigation(                 AccountScreen()             }         }-        entry<Route.Restaurant>(metadata = ListDetailPage() + MorphPage()) { route ->-            RestaurantScreen(-                merchantId = route.id,-                onBack = { backStack.pop() },-                onAddToCart = { item ->-                    if (cart.canAddFromSameMerchant(item)) {-                        cart.add(item)-                        CartStore.save(context, cart)-                    } else {-                        onPendingCartItem(item)-                    }-                }-            )-        }-        // Checkout is a flow step, not a detail of the tab list.-        entry<Route.Checkout>(metadata = ListDetailPage()) {-            CheckoutScreen(-                items = cart,-                onBack = { backStack.pop() },-                onOrderPlaced = {-                    backStack.reset(Route.Orders)-                },-                onOrderCompleted = {-                    cart.clear()-                    CartStore.clear(context)-                },-            )-        }-        // The brief keeps order-tracking full-screen custom; metadata still marks it a detail-        // so medium widths show it two-pane instead of covering the list.-        entry<Route.OrderTracking>(metadata = ListDetailPage() + MorphPage()) { route ->-            OrderTrackingScreen(orderId = route.orderId, onBack = { backStack.pop() })-        }-        entry<Route.EmailVerification>(metadata = ListDetailPage()) { route ->-            EmailVerificationScreen(-                customerId = route.customerId,-                token = route.token,-                onDone = { backStack.pop() },-            )-        }+        detailEntries(backStack, cart, context, onPendingCartItem)     } } @@ -118,3 +79,52 @@ fun Navigation(  * over a tab keep the tab as their list pane on wide layouts. Per-tab  * `rememberSaveable` state lives in each route's navigation entry.  */++/** The pushed (non-tab) destinations: restaurant, checkout, tracking, verification. */+private fun com.vayunmathur.library.util.EntryProviderScope<Route>.detailEntries(+    backStack: NavBackStack<Route>,+    cart: androidx.compose.runtime.snapshots.SnapshotStateList<CartItem>,+    context: android.content.Context,+    onPendingCartItem: (CartItem) -> Unit,+) {+    entry<Route.Restaurant>(metadata = ListDetailPage() + MorphPage()) { route ->+        RestaurantScreen(+            merchantId = route.id,+            onBack = { backStack.pop() },+            onAddToCart = { item ->+                if (cart.canAddFromSameMerchant(item)) {+                    cart.add(item)+                    CartStore.save(context, cart)+                } else {+                    onPendingCartItem(item)+                }+            },+        )+    }+    // Checkout is a flow step, not a detail of the tab list.+    entry<Route.Checkout>(metadata = ListDetailPage()) {+        CheckoutScreen(+            items = cart,+            onBack = { backStack.pop() },+            onOrderPlaced = {+                backStack.reset(Route.Orders)+            },+            onOrderCompleted = {+                cart.clear()+                CartStore.clear(context)+            },+        )+    }+    // The brief keeps order-tracking full-screen custom; metadata still marks it a detail+    // so medium widths show it two-pane instead of covering the list.+    entry<Route.OrderTracking>(metadata = ListDetailPage() + MorphPage()) { route ->+        OrderTrackingScreen(orderId = route.orderId, onBack = { backStack.pop() })+    }+    entry<Route.EmailVerification>(metadata = ListDetailPage()) { route ->+        EmailVerificationScreen(+            customerId = route.customerId,+            token = route.token,+            onDone = { backStack.pop() },+        )+    }+}
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/intents/FoodAssistantBackend.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/intents/FoodAssistantBackend.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/intents/FoodAssistantBackend.ktindex 1c6f1d81e2..54b80f9e33 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/intents/FoodAssistantBackend.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/intents/FoodAssistantBackend.kt@@ -247,33 +247,8 @@ internal fun buildValidatedCart(         if (line.qty !in MIN_CART_QTY..MAX_CART_QTY) return null to ERROR_INVALID_QUANTITY         val item = itemsById[line.itemId] ?: return null to ERROR_ITEM_NOT_FOUND         if (!item.isAvailable || !item.isInStock) return null to ERROR_ITEM_UNAVAILABLE-        val selected = mutableListOf<SelectedModifier>()-        for (group in item.modifierGroups) {-            val inGroup = group.modifiers.associateBy { it.id }-            val chosen = line.modifierIds.filter { it in inGroup }-            val effectiveMin = if (group.required) {-                maxOf(group.minSelections, 1)-            } else {-                group.minSelections-            }-            if (chosen.size < effectiveMin) {-                return null to if (group.required) {-                    ERROR_MODIFIER_REQUIRED-                } else {-                    ERROR_MODIFIER_SELECTIONS-                }-            }-            if (chosen.size > group.maxSelections) return null to ERROR_MODIFIER_SELECTIONS-            chosen.mapTo(selected) { modifierId ->-                val modifier = inGroup.getValue(modifierId)-                SelectedModifier(-                    modifierGroupId = group.id,-                    modifierId = modifier.id,-                    name = modifier.name,-                    price = modifier.price,-                )-            }-        }+        val (selected, modifierError) = selectModifiers(item, line)+        if (modifierError != null) return null to modifierError         val knownModifierIds = item.modifierGroups             .flatMap { group -> group.modifiers.map { it.id } }             .toSet()@@ -291,6 +266,33 @@ internal fun buildValidatedCart(     return built to null } +/** The modifiers chosen for one cart line, or the validation error rejecting them. */+private fun selectModifiers(+    item: com.vayunmathur.fooddelivery.data.MenuItem,+    line: com.vayunmathur.library.intents.fooddelivery.SetCartItem,+): Pair<List<SelectedModifier>, String?> {+    val selected = mutableListOf<SelectedModifier>()+    for (group in item.modifierGroups) {+        val inGroup = group.modifiers.associateBy { it.id }+        val chosen = line.modifierIds.filter { it in inGroup }+        val effectiveMin = if (group.required) maxOf(group.minSelections, 1) else group.minSelections+        if (chosen.size < effectiveMin) {+            return selected to if (group.required) ERROR_MODIFIER_REQUIRED else ERROR_MODIFIER_SELECTIONS+        }+        if (chosen.size > group.maxSelections) return selected to ERROR_MODIFIER_SELECTIONS+        chosen.mapTo(selected) { modifierId ->+            val modifier = inGroup.getValue(modifierId)+            SelectedModifier(+                modifierGroupId = group.id,+                modifierId = modifier.id,+                name = modifier.name,+                price = modifier.price,+            )+        }+    }+    return selected to null+}+ internal suspend fun setCart(     context: Context,     input: SetCart,
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/platform/NavigationHelpers.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/platform/NavigationHelpers.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/platform/NavigationHelpers.ktindex 769d964e4e..41fd176a79 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/platform/NavigationHelpers.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/platform/NavigationHelpers.kt@@ -32,10 +32,25 @@ internal fun List<CartItem>.canAddFromSameMerchant(item: CartItem): Boolean {  * 3=Orders, 4=Account), coerced into range. Tabs are sibling routes now, so  * an initial tab is simply the back stack's start route.  */-internal fun tabRouteFor(initialTab: Int): Route = when (initialTab.coerceIn(0, 4)) {-    0 -> Route.Home-    1 -> Route.Cart-    2 -> Route.Deals-    3 -> Route.Orders+internal fun tabRouteFor(initialTab: Int): Route = when (initialTab.coerceIn(FIRST_TAB, LAST_TAB)) {+    FIRST_TAB -> Route.Home+    CART_TAB -> Route.Cart+    DEALS_TAB -> Route.Deals+    ORDERS_TAB -> Route.Orders     else -> Route.Account }++/** Index of the first (Home) tab in a legacy `initialTab`. */+private const val FIRST_TAB = 0++/** Index of the Cart tab in a legacy `initialTab`. */+private const val CART_TAB = 1++/** Index of the Deals tab in a legacy `initialTab`. */+private const val DEALS_TAB = 2++/** Index of the Orders tab in a legacy `initialTab`. */+private const val ORDERS_TAB = 3++/** Index of the last (Account) tab in a legacy `initialTab`. */+private const val LAST_TAB = 4