feat: implement lesson6-7 project changes from Feishu outline
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:
xiaobai committed 2026-03-04 16:07:09 +08:00
1 parent f6005e4772
commit 7334f326a3
16 files changed
+333 -132

No files matched your search

@@ -0,0 +1,9 @@
package com.example.minishop.cart
import kotlinx.serialization.Serializable
@Serializable
data class CartItem(
val productId: String,
val quantity: Int
)
@@ -0,0 +1,41 @@
package com.example.minishop.cart
class CartRepository(
private val storage: CartStorage = CartStorage()
) {
private val map = storage.load().associateBy { it.productId }.toMutableMap()
fun items(): List<CartItem> = map.values.sortedBy { it.productId }
fun add(productId: String) {
val old = map[productId]
map[productId] = if (old == null) CartItem(productId, 1) else old.copy(quantity = old.quantity + 1)
persist()
}
fun increase(productId: String) {
val old = map[productId] ?: return
map[productId] = old.copy(quantity = old.quantity + 1)
persist()
}
fun decrease(productId: String) {
val old = map[productId] ?: return
if (old.quantity <= 1) map.remove(productId) else map[productId] = old.copy(quantity = old.quantity - 1)
persist()
}
fun remove(productId: String) {
map.remove(productId)
persist()
}
fun clear() {
map.clear()
persist()
}
private fun persist() {
storage.save(items())
}
}
@@ -0,0 +1,21 @@
package com.example.minishop.cart
import com.russhwolf.settings.Settings
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
class CartStorage(
private val settings: Settings = Settings(),
private val json: Json = Json { ignoreUnknownKeys = true }
) {
private val key = "cart_items"
fun load(): List<CartItem> {
val raw = settings.getStringOrNull(key) ?: return emptyList()
return runCatching { json.decodeFromString<List<CartItem>>(raw) }.getOrElse { emptyList() }
}
fun save(items: List<CartItem>) {
settings.putString(key, json.encodeToString(items))
}
}
@@ -0,0 +1,34 @@
package com.example.minishop.checkout
import com.example.minishop.cart.CartRepository
import com.example.minishop.model.Product
import kotlin.random.Random
data class CheckoutResult(
val success: Boolean,
val message: String
)
class CheckoutService(
private val cartRepository: CartRepository
) {
fun totalPrice(productMap: Map<String, Product>): Double {
return cartRepository.items().sumOf { item ->
val product = productMap[item.productId] ?: return@sumOf 0.0
product.price * item.quantity
}
}
fun checkout(productMap: Map<String, Product>): CheckoutResult {
val total = totalPrice(productMap)
if (total <= 0.0) return CheckoutResult(false, "购物车为空")
val success = Random.nextBoolean()
return if (success) {
cartRepository.clear()
CheckoutResult(true, "结算成功(模拟),金额:¥${"%.2f".format(total)}")
} else {
CheckoutResult(false, "结算失败(模拟),请重试")
}
}
}
@@ -1,8 +1,16 @@
package com.example.minishop.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Product(
@SerialName("id")
val id: String,
@SerialName("title")
val title: String,
@SerialName("price")
val price: Double,
@SerialName("description")
val description: String
)
@@ -0,0 +1,20 @@
package com.example.minishop.network
import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
expect fun provideHttpEngine(): HttpClientEngine
fun createHttpClient(): HttpClient = HttpClient(provideHttpEngine()) {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
isLenient = true
}
)
}
}
@@ -0,0 +1,26 @@
package com.example.minishop.network
import com.example.minishop.model.Product
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
class ProductApi(
private val baseUrl: String,
private val useMock: Boolean = true,
private val client: HttpClient = createHttpClient()
) {
suspend fun fetchProducts(): List<Product> {
if (useMock) return mockProducts()
return client.get("$baseUrl/products").body()
}
private fun mockProducts(): List<Product> = 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, "跨平台网络层搭建")
)
}
@@ -0,0 +1,3 @@
package com.example.minishop.platform
expect fun platformName(): String
@@ -0,0 +1,17 @@
package com.example.minishop.repository
import com.example.minishop.model.Product
import com.example.minishop.network.ProductApi
class ProductRepository(
private val api: ProductApi,
private val fallback: List<Product> = emptyList()
) {
suspend fun getProducts(): List<Product> {
return runCatching { api.fetchProducts() }.getOrDefault(fallback)
}
suspend fun getProductDetail(id: String): Product? {
return getProducts().firstOrNull { it.id == id }
}
}