From 823080245dfcfdf3cd3a321b2fd1c4636e228d2e Mon Sep 17 00:00:00 2001 From: xiaobai Date: Wed, 4 Mar 2026 16:12:26 +0800 Subject: [PATCH] fix: delete old cart classes causing redeclaration and type mismatch --- .../kotlin/com/example/minishop/App.kt | 2 +- .../com/example/minishop/cart/CartModels.kt | 17 ---------- .../com/example/minishop/cart/CartStore.kt | 32 ------------------- 3 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/com/example/minishop/cart/CartModels.kt delete mode 100644 shared/src/commonMain/kotlin/com/example/minishop/cart/CartStore.kt diff --git a/composeApp/src/androidMain/kotlin/com/example/minishop/App.kt b/composeApp/src/androidMain/kotlin/com/example/minishop/App.kt index 737a020..ee2d9d4 100644 --- a/composeApp/src/androidMain/kotlin/com/example/minishop/App.kt +++ b/composeApp/src/androidMain/kotlin/com/example/minishop/App.kt @@ -141,7 +141,7 @@ fun App() { when { inDetail -> ProductDetailScreen( product = selected, - quantity = selected?.let { id -> cartItems.firstOrNull { it.productId == id.id }?.quantity } ?: 0, + quantity = selected?.let { p -> cartItems.firstOrNull { it.productId == p.id }?.quantity } ?: 0, onAdd = { cartRepository.add(it.id) tick++ diff --git a/shared/src/commonMain/kotlin/com/example/minishop/cart/CartModels.kt b/shared/src/commonMain/kotlin/com/example/minishop/cart/CartModels.kt deleted file mode 100644 index 3fe9c73..0000000 --- a/shared/src/commonMain/kotlin/com/example/minishop/cart/CartModels.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.minishop.cart - -import com.example.minishop.model.Product - -data class CartItem( - val product: Product, - val quantity: Int -) - -data class CartState( - val items: List = emptyList() -) { - val totalCount: Int get() = items.sumOf { it.quantity } - val totalPrice: Double get() = items.sumOf { it.product.price * it.quantity } - - fun quantityOf(productId: String): Int = items.firstOrNull { it.product.id == productId }?.quantity ?: 0 -} diff --git a/shared/src/commonMain/kotlin/com/example/minishop/cart/CartStore.kt b/shared/src/commonMain/kotlin/com/example/minishop/cart/CartStore.kt deleted file mode 100644 index 1355504..0000000 --- a/shared/src/commonMain/kotlin/com/example/minishop/cart/CartStore.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.example.minishop.cart - -import com.example.minishop.model.Product - -class CartStore { - private val mutable = mutableMapOf() - - fun state(): CartState = CartState(mutable.values.sortedBy { it.product.id }) - - fun add(product: Product) { - val exist = mutable[product.id] - mutable[product.id] = if (exist == null) CartItem(product, 1) else exist.copy(quantity = exist.quantity + 1) - } - - fun increase(productId: String) { - val item = mutable[productId] ?: return - mutable[productId] = item.copy(quantity = item.quantity + 1) - } - - fun decrease(productId: String) { - val item = mutable[productId] ?: return - if (item.quantity <= 1) mutable.remove(productId) else mutable[productId] = item.copy(quantity = item.quantity - 1) - } - - fun remove(productId: String) { - mutable.remove(productId) - } - - fun clear() { - mutable.clear() - } -}