feat: migrate lesson 7 cart flow from settings to sql
Build Android app / Build (push) Has been cancelled
Build iOS app / Build (push) Has been cancelled
Template Cleanup / Template Cleanup (push) Has been cancelled

This commit is contained in:
OpenClaw committed 2026-03-09 00:44:57 +08:00
1 parent a5120b3e8a
commit 4f32be7077
12 files changed
+811 -80

No files matched your search

@@ -119,7 +119,7 @@ fun App() {
product = vm.selectedProduct,
quantity = vm.selectedProduct?.let { vm.quantityOf(it.id) } ?: 0,
onAdd = {
val msg = vm.addToCart(it.id)
val msg = vm.addToCart(it)
scope.launch {
snackbarHostState.showSnackbar(msg)
}
@@ -136,14 +136,13 @@ fun App() {
onQueryChange = vm::onQueryChange,
onRetry = vm::refreshProducts,
onAdd = {
vm.addToCart(it.id)
vm.addToCart(it)
},
onClick = vm::openDetail
)
else -> CartScreen(
items = ui.cartItems,
productMap = ui.products.associateBy { it.id },
totalPrice = ui.totalPrice,
onInc = vm::increase,
onDec = vm::decrease,
@@ -294,7 +293,6 @@ private fun ProductDetailScreen(
@Composable
private fun CartScreen(
items: List<CartItem>,
productMap: Map<String, Product>,
totalPrice: Double,
onInc: (String) -> Unit,
onDec: (String) -> Unit,
@@ -315,21 +313,18 @@ private fun CartScreen(
contentPadding = PaddingValues(bottom = 96.dp)
) {
lazyListItems(items) { item ->
val product = productMap[item.productId]
if (product != null) {
Card(Modifier.fillMaxWidth()) {
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
Text(product.title, fontWeight = FontWeight.Medium)
Text("¥${formatPrice(product.price * item.quantity)}")
}
Row(verticalAlignment = Alignment.CenterVertically) {
SmallOpButton("-") { onDec(product.id) }
Text(" ${item.quantity} ")
SmallOpButton("+") { onInc(product.id) }
Spacer(Modifier.width(8.dp))
OutlinedButton(onClick = { onRemove(product.id) }) { Text("移除") }
}
Card(Modifier.fillMaxWidth()) {
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
Text(item.title, fontWeight = FontWeight.Medium)
Text("¥${formatPrice(item.price * item.quantity)}")
}
Row(verticalAlignment = Alignment.CenterVertically) {
SmallOpButton("-") { onDec(item.productId) }
Text(" ${item.quantity} ")
SmallOpButton("+") { onInc(item.productId) }
Spacer(Modifier.width(8.dp))
OutlinedButton(onClick = { onRemove(item.productId) }) { Text("移除") }
}
}
}
@@ -3,10 +3,12 @@ package com.example.minishop
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.minishop.db.initAppContext
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initAppContext(applicationContext)
setContent {
App()
}
@@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
import com.example.minishop.cart.CartItem
import com.example.minishop.cart.CartRepository
import com.example.minishop.checkout.CheckoutService
import com.example.minishop.db.createShopDatabase
import com.example.minishop.model.Product
import com.example.minishop.network.ProductApi
import com.example.minishop.repository.ProductRepository
@@ -35,7 +36,8 @@ class ShopViewModel : ViewModel() {
private val productRepository = ProductRepository(
api = ProductApi(baseUrl = "http://10.0.2.2:8080", useMock = false)
)
private val cartRepository = CartRepository()
private val database = createShopDatabase()
private val cartRepository = CartRepository(database)
private val checkoutService = CheckoutService(cartRepository)
var uiState by mutableStateOf(ShopUiState())
@@ -93,8 +95,8 @@ class ShopViewModel : ViewModel() {
fun quantityOf(productId: String): Int =
uiState.cartItems.firstOrNull { it.productId == productId }?.quantity ?: 0
fun addToCart(productId: String): String {
cartRepository.add(productId)
fun addToCart(product: Product): String {
cartRepository.add(product)
syncCart()
return "已加入购物车"
}
@@ -120,19 +122,17 @@ class ShopViewModel : ViewModel() {
}
fun checkout(): String {
val productMap = uiState.products.associateBy { it.id }
val result = checkoutService.checkout(productMap)
val result = checkoutService.checkout()
syncCart()
return result.message
}
private fun syncCart() {
val items = cartRepository.items()
val productMap = uiState.products.associateBy { it.id }
uiState = uiState.copy(
cartItems = items,
totalCount = items.sumOf { it.quantity },
totalPrice = checkoutService.totalPrice(productMap)
totalPrice = checkoutService.totalPrice()
)
}