fix(ui): prevent whitespace-only filter and add product count debug hint
This commit is contained in:
1 parent
f0184b94a3
commit
23dce047b4
1 file changed
+17
-4
@@ -100,10 +100,15 @@ fun App() {
|
|||||||
|
|
||||||
LaunchedEffect(Unit) { loadProducts() }
|
LaunchedEffect(Unit) { loadProducts() }
|
||||||
|
|
||||||
val filteredProducts = remember(products, query) {
|
val normalizedQuery = remember(query) { query.trim() }
|
||||||
products.filter {
|
val filteredProducts = remember(products, normalizedQuery) {
|
||||||
it.title.contains(query, ignoreCase = true) ||
|
if (normalizedQuery.isBlank()) {
|
||||||
it.description.contains(query, ignoreCase = true)
|
products
|
||||||
|
} else {
|
||||||
|
products.filter {
|
||||||
|
it.title.contains(normalizedQuery, ignoreCase = true) ||
|
||||||
|
it.description.contains(normalizedQuery, ignoreCase = true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +183,7 @@ fun App() {
|
|||||||
loading = loading,
|
loading = loading,
|
||||||
error = loadError,
|
error = loadError,
|
||||||
products = filteredProducts,
|
products = filteredProducts,
|
||||||
|
totalProducts = products.size,
|
||||||
query = query,
|
query = query,
|
||||||
quantityOf = { id -> cartItems.firstOrNull { it.productId == id }?.quantity ?: 0 },
|
quantityOf = { id -> cartItems.firstOrNull { it.productId == id }?.quantity ?: 0 },
|
||||||
onQueryChange = { query = it },
|
onQueryChange = { query = it },
|
||||||
@@ -217,6 +223,7 @@ private fun ProductListScreen(
|
|||||||
loading: Boolean,
|
loading: Boolean,
|
||||||
error: String?,
|
error: String?,
|
||||||
products: List<Product>,
|
products: List<Product>,
|
||||||
|
totalProducts: Int,
|
||||||
query: String,
|
query: String,
|
||||||
quantityOf: (String) -> Int,
|
quantityOf: (String) -> Int,
|
||||||
onQueryChange: (String) -> Unit,
|
onQueryChange: (String) -> Unit,
|
||||||
@@ -239,6 +246,12 @@ private fun ProductListScreen(
|
|||||||
placeholder = { Text("输入标题或描述") }
|
placeholder = { Text("输入标题或描述") }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Text(
|
||||||
|
text = "共 $totalProducts 条 · 当前显示 ${products.size} 条",
|
||||||
|
style = MaterialTheme.typography.labelMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
|
)
|
||||||
|
|
||||||
when {
|
when {
|
||||||
loading -> EmptyState("⏳ 加载中...")
|
loading -> EmptyState("⏳ 加载中...")
|
||||||
error != null -> {
|
error != null -> {
|
||||||
|
|||||||
Reference in new issue
Block a user