feat: add lesson 7 order flow and orders screen
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 02:25:41 +08:00
1 parent fc8ea48ea1
commit e42597b8b7
6 files changed
+647 -25

No files matched your search

@@ -52,7 +52,7 @@ import com.example.minishop.cart.CartItem
import com.example.minishop.model.Product
import kotlinx.coroutines.launch
enum class Tab { Home, Cart }
enum class Tab { Home, Cart, Orders }
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@@ -72,7 +72,8 @@ fun App() {
when {
ui.inDetail -> "商品详情"
ui.currentTab == Tab.Home -> "KMP Mini Shopping"
else -> "购物车"
ui.currentTab == Tab.Cart -> "购物车"
else -> "订单列表"
}
)
},
@@ -104,6 +105,12 @@ fun App() {
icon = { Text("🛒") },
label = { Text("购物车(${ui.totalCount})") }
)
NavigationBarItem(
selected = ui.currentTab == Tab.Orders,
onClick = { vm.selectTab(Tab.Orders) },
icon = { Text("📦") },
label = { Text("订单") }
)
}
}
}
@@ -141,7 +148,7 @@ fun App() {
onClick = vm::openDetail
)
else -> CartScreen(
ui.currentTab == Tab.Cart -> CartScreen(
items = ui.cartItems,
totalPrice = ui.totalPrice,
onInc = vm::increase,
@@ -155,6 +162,8 @@ fun App() {
}
}
)
else -> OrderScreen(ui.orders)
}
}
}
@@ -358,6 +367,35 @@ private fun CartScreen(
}
}
@Composable
private fun OrderScreen(orders: List<com.example.minishop.order.ShopOrder>) {
if (orders.isEmpty()) {
EmptyState("还没有订单")
return
}
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
lazyListItems(orders) { order ->
Card(Modifier.fillMaxWidth()) {
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("订单号:${order.orderId}", fontWeight = FontWeight.Bold)
Text("状态:${order.status}")
Text("总价:¥${formatPrice(order.totalPrice)}")
Text("商品数:${order.items.sumOf { it.quantity }}")
order.items.forEach { item ->
Text("- ${item.title} x${item.quantity}")
}
}
}
}
}
}
@Composable
private fun ProductCover(url: String) {
val coverModifier = Modifier
@@ -10,6 +10,8 @@ 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.order.OrderRepository
import com.example.minishop.order.ShopOrder
import com.example.minishop.repository.ProductRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -27,7 +29,8 @@ data class ShopUiState(
val loadError: String? = null,
val cartItems: List<CartItem> = emptyList(),
val totalCount: Int = 0,
val totalPrice: Double = 0.0
val totalPrice: Double = 0.0,
val orders: List<ShopOrder> = emptyList()
)
class ShopViewModel : ViewModel() {
@@ -38,7 +41,8 @@ class ShopViewModel : ViewModel() {
)
private val database = createShopDatabase()
private val cartRepository = CartRepository(database)
private val checkoutService = CheckoutService(cartRepository)
private val orderRepository = OrderRepository(database)
private val checkoutService = CheckoutService(cartRepository, orderRepository)
var uiState by mutableStateOf(ShopUiState())
private set
@@ -62,6 +66,7 @@ class ShopViewModel : ViewModel() {
init {
refreshProducts()
syncCart()
syncOrders()
}
fun refreshProducts() {
@@ -124,6 +129,10 @@ class ShopViewModel : ViewModel() {
fun checkout(): String {
val result = checkoutService.checkout()
syncCart()
syncOrders()
if (result.success) {
uiState = uiState.copy(currentTab = Tab.Orders)
}
return result.message
}
@@ -136,6 +145,10 @@ class ShopViewModel : ViewModel() {
)
}
private fun syncOrders() {
uiState = uiState.copy(orders = orderRepository.getOrders())
}
override fun onCleared() {
scope.cancel()
super.onCleared()