initial import from openclaw workspace
This commit is contained in:
commit
27d0d57b56
59 files changed
+2666
No files matched your search
@@ -0,0 +1,17 @@
|
||||
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<CartItem> = 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
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.minishop.cart
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
class CartStore {
|
||||
private val mutable = mutableMapOf<String, CartItem>()
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.minishop.data
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
class FakeProductRepository : ProductRepository {
|
||||
private val products = listOf(
|
||||
Product("1", "KMP 入门课", 99.0, "跨平台开发基础课程"),
|
||||
Product("2", "Compose 实战课", 149.0, "Compose 多端 UI 实战"),
|
||||
Product("3", "Kotlin 协程训练营", 129.0, "协程与 Flow 进阶"),
|
||||
Product("4", "Clean 架构速成", 199.0, "从0构建可维护架构"),
|
||||
Product("5", "SQLDelight 实战", 119.0, "KMP 本地数据库方案"),
|
||||
Product("6", "Ktor API 实战", 159.0, "跨平台网络层搭建"),
|
||||
)
|
||||
|
||||
override fun getProducts(): List<Product> = products
|
||||
|
||||
override fun getProductById(id: String): Product? = products.find { it.id == id }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.minishop.data
|
||||
|
||||
class FavoriteStore {
|
||||
private val ids = linkedSetOf<String>()
|
||||
|
||||
fun isFavorite(productId: String): Boolean = ids.contains(productId)
|
||||
|
||||
fun all(): Set<String> = ids.toSet()
|
||||
|
||||
fun toggle(productId: String) {
|
||||
if (!ids.add(productId)) ids.remove(productId)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.minishop.data
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
interface ProductRepository {
|
||||
fun getProducts(): List<Product>
|
||||
fun getProductById(id: String): Product?
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.minishop.model
|
||||
|
||||
data class Product(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val price: Double,
|
||||
val description: String
|
||||
)
|
||||
Reference in new issue
Block a user