docs: publish lesson6-7 outline and final docs
This commit is contained in:
1 parent
1f9159c28e
commit
35fbc85fd5
4 files changed
+528
-6
No files matched your search
+10
-6
@@ -1,17 +1,21 @@
|
||||
# Mini Shopping 文档索引(课堂使用)
|
||||
|
||||
## 当前推荐(Android 两课时)
|
||||
## 当前推荐(第6-7节)
|
||||
|
||||
1. `第1课_KMP_MiniShopping_共享层与首页详情.md`
|
||||
- 内容:共享层建模、首页与详情页、第一课验收
|
||||
1. `第6_7节课程大纲_KMP商城_网络存储与整合.md`
|
||||
- 内容:第6-7节目标、节奏、产出、SkillMap映射
|
||||
|
||||
2. `第2课_KMP_MiniShopping_底导航收藏购物车.md`
|
||||
- 内容:底导航、收藏页、购物车页、第二课验收
|
||||
2. `第6节_共享层网络与存储_最终版.md`
|
||||
- 内容:Ktor + Settings 共享层实战(Android只做UI)
|
||||
|
||||
3. `第7节_整合与结业实战_最终版.md`
|
||||
- 内容:详情/购物车/结算整合与结业验收
|
||||
|
||||
## 归档文档(历史版本)
|
||||
|
||||
- `第1课_KMP_MiniShopping_共享层与首页详情.md`
|
||||
- `第2课_KMP_MiniShopping_底导航收藏购物车.md`
|
||||
- `KMP_两课时教学讲稿_MiniShopping_超详细实操版.md`
|
||||
- `KMP_两课时教学讲稿_MiniShopping_底导航增强版.md`
|
||||
- `KMP_两课时教学讲义_群发版_逐步实操.md`
|
||||
|
||||
> 说明:当前课堂请优先使用“第1课/第2课”两份文档。
|
||||
@@ -0,0 +1,99 @@
|
||||
# 第6-7节课程大纲|KMP 商城:网络与存储实战
|
||||
|
||||
## 课程定位
|
||||
|
||||
在前序 KMP 基础(共享模型、共享逻辑、Android UI 调用)之上,完成“共享层网络 + 共享层存储 + 业务整合”闭环。
|
||||
|
||||
---
|
||||
|
||||
## 第6节:KMP 商城——共享层开发(含网络 / 存储)
|
||||
|
||||
### 课堂目标
|
||||
|
||||
- 把商城核心逻辑、网络请求、本地存储全部放在 `shared/commonMain`
|
||||
- Android 仅负责页面展示与事件分发
|
||||
- 输出“可运行、可持久化”的最小商城能力
|
||||
|
||||
### 课堂内容(1-2 小时)
|
||||
|
||||
1. **前置准备(5 分钟)**
|
||||
- 配置 Ktor 与 Multiplatform Settings 依赖
|
||||
- 确认 Android 网络权限
|
||||
|
||||
2. **共享层开发(60-80 分钟)**
|
||||
- 共享数据模型:`Product`、`CartItem`
|
||||
- 网络层:Ktor Client 封装、商品列表接口
|
||||
- 存储层:Settings 持久化购物车 JSON
|
||||
- 平台差异:`expect/actual` 提供平台信息
|
||||
- Android 调用共享层:商品加载、加购、数量展示
|
||||
|
||||
3. **课堂验收(10-15 分钟)**
|
||||
- 可加载商品列表
|
||||
- 可加购
|
||||
- 重启后购物车不丢失
|
||||
|
||||
### 课堂产出
|
||||
|
||||
- 带网络 + 存储的共享层代码
|
||||
- Android 可运行 Demo(列表加载、加购、持久化)
|
||||
|
||||
### 课后作业(2小时)
|
||||
|
||||
- 扩展接口:商品详情
|
||||
- 存储扩展:数量修改 / 删除后持久化
|
||||
- Android 页面:详情页 + 购物车同步
|
||||
- 提交:运行截图 + `commonMain` 核心代码
|
||||
|
||||
---
|
||||
|
||||
## 第7节:KMP 商城——功能整合 + 结业实战
|
||||
|
||||
### 课堂目标
|
||||
|
||||
- 完成商城流程整合(列表→详情→购物车→结算)
|
||||
- 复盘网络/存储最佳实践
|
||||
- 形成可演示的结业 Demo
|
||||
|
||||
### 课堂内容(1-2 小时)
|
||||
|
||||
1. **作业点评(15-20 分钟)**
|
||||
- Ktor 初始化与错误处理
|
||||
- Settings 序列化与反序列化
|
||||
|
||||
2. **功能完善(50-70 分钟)**
|
||||
- 基于持久化数据计算购物车总价
|
||||
- 结算逻辑(模拟)放在共享层
|
||||
- Android 只负责调用与展示
|
||||
|
||||
3. **技能复盘(10-15 分钟)**
|
||||
- 哪些逻辑应放 `commonMain`
|
||||
- 跨平台网络/存储常见踩坑
|
||||
|
||||
4. **课程验收(10 分钟)**
|
||||
- 全流程演示
|
||||
- 冷启动数据一致
|
||||
|
||||
### 课堂产出
|
||||
|
||||
- 完整可演示 KMP 商城 Demo
|
||||
- 网络 + 存储 + 业务逻辑均在共享层
|
||||
|
||||
### 课后作业(2小时)
|
||||
|
||||
独立完成:
|
||||
- 商品列表(Ktor)
|
||||
- 商品详情(Ktor)
|
||||
- 购物车(Settings)
|
||||
- 加购 / 删购 / 改数量(全共享层)
|
||||
|
||||
提交:运行截图 + 关键代码截图 + 复用点说明
|
||||
|
||||
---
|
||||
|
||||
## 与 SkillMap 对应关系
|
||||
|
||||
| 课程节数 | 对应 SkillMap 节点 | 实战覆盖内容 |
|
||||
|---|---|---|
|
||||
| 第6节 | 4. KMP 核心能力 + 5. 跨平台技术栈 | 共享模型/逻辑 + Ktor 网络 + 跨平台存储 |
|
||||
| 第7节 | 5. 跨平台技术栈 + 6. 集成能力 + 7. 业务落地 | 网络/存储整合 + Android 集成 + 商城落地 |
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
# 第6节|KMP 商城:共享层网络与存储(最终版)
|
||||
|
||||
> 形式:纯讲解 + 现场敲代码 + 课后作业
|
||||
> 范围:仅 Android 展示,核心网络/存储全部在 shared
|
||||
|
||||
---
|
||||
|
||||
## 1. 本节目标
|
||||
|
||||
1. 在 `shared/commonMain` 完成商品列表网络加载(Ktor)
|
||||
2. 在 `shared/commonMain` 完成购物车持久化(Multiplatform Settings)
|
||||
3. Android 页面只调用共享层,不写业务细节
|
||||
|
||||
---
|
||||
|
||||
## 2. 依赖配置
|
||||
|
||||
> 建议放入 `libs.versions.toml` 统一管理;若课堂直接写依赖,使用以下版本。
|
||||
|
||||
### 2.1 commonMain
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("io.ktor:ktor-client-core:2.3.7")
|
||||
implementation("io.ktor:ktor-client-content-negotiation:2.3.7")
|
||||
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")
|
||||
implementation("com.russhwolf:multiplatform-settings:1.1.1")
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2 androidMain
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("io.ktor:ktor-client-android:2.3.7")
|
||||
}
|
||||
```
|
||||
|
||||
### 2.3 AndroidManifest 网络权限
|
||||
|
||||
`composeApp/src/androidMain/AndroidManifest.xml`
|
||||
|
||||
```xml
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 共享层代码结构
|
||||
|
||||
目标目录:
|
||||
|
||||
```text
|
||||
shared/src/commonMain/kotlin/com/example/minishop/
|
||||
├─ model/
|
||||
├─ network/
|
||||
├─ repository/
|
||||
├─ cart/
|
||||
└─ platform/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. 共享模型
|
||||
|
||||
### 4.1 `model/Product.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Product(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val price: Double,
|
||||
val description: String
|
||||
)
|
||||
```
|
||||
|
||||
### 4.2 `cart/CartItem.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.cart
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CartItem(
|
||||
val productId: String,
|
||||
val quantity: Int
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Ktor 网络层
|
||||
|
||||
### 5.1 `network/HttpClientFactory.kt`
|
||||
|
||||
```kotlin
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5.2 `network/HttpClientFactory.android.kt`
|
||||
|
||||
> 路径:`shared/src/androidMain/kotlin/com/example/minishop/network/HttpClientFactory.android.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.network
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.android.Android
|
||||
|
||||
actual fun provideHttpEngine(): HttpClientEngine = Android.create()
|
||||
```
|
||||
|
||||
### 5.3 `network/ProductApi.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.network
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
|
||||
class ProductApi(
|
||||
private val baseUrl: String,
|
||||
private val client: io.ktor.client.HttpClient = createHttpClient()
|
||||
) {
|
||||
suspend fun fetchProducts(): List<Product> {
|
||||
return client.get("$baseUrl/products").body()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 课堂可选:若无真实接口,先将 `fetchProducts()` 改为返回本地模拟列表。
|
||||
|
||||
---
|
||||
|
||||
## 6. 跨平台存储(购物车持久化)
|
||||
|
||||
### 6.1 `cart/CartStorage.kt`
|
||||
|
||||
```kotlin
|
||||
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,
|
||||
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))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 6.2 `cart/CartRepository.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.cart
|
||||
|
||||
class CartRepository(
|
||||
private val storage: 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()
|
||||
}
|
||||
|
||||
private fun persist() = storage.save(items())
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. expect/actual 平台信息
|
||||
|
||||
### 7.1 `platform/PlatformInfo.kt`(commonMain)
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.platform
|
||||
|
||||
expect fun platformName(): String
|
||||
```
|
||||
|
||||
### 7.2 `platform/PlatformInfo.android.kt`(androidMain)
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.platform
|
||||
|
||||
actual fun platformName(): String = "Android"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Android 调用共享层(最小可运行)
|
||||
|
||||
在 Android 页面中完成:
|
||||
|
||||
1. 调用 `ProductApi.fetchProducts()` 获取商品列表
|
||||
2. 点击“加入购物车”调用 `CartRepository.add(productId)`
|
||||
3. 刷新购物车数量(从 `CartRepository.items()` 计算)
|
||||
|
||||
> 页面层不保存业务规则,只保留 UI 状态与点击事件。
|
||||
|
||||
---
|
||||
|
||||
## 9. 本节验收
|
||||
|
||||
- 应用启动后可加载商品列表(真实或模拟)
|
||||
- 点击加购后数量变化
|
||||
- 重启应用后购物车数据仍存在
|
||||
|
||||
---
|
||||
|
||||
## 10. 课后作业(2小时)
|
||||
|
||||
1. 扩展网络接口:`fetchProductDetail(id)`
|
||||
2. 补充购物车:数量修改/删除并持久化
|
||||
3. Android 新增商品详情页并与购物车联动
|
||||
4. 提交:运行截图 + `commonMain` 核心代码截图
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
# 第7节|KMP 商城:功能整合与结业实战(最终版)
|
||||
|
||||
> 形式:纯讲解 + 现场敲代码 + 课后作业
|
||||
> 范围:整合网络/存储/业务,完成可演示商城流程
|
||||
|
||||
---
|
||||
|
||||
## 1. 本节目标
|
||||
|
||||
1. 完成“列表→详情→购物车→结算”全流程
|
||||
2. 基于持久化购物车做总价计算
|
||||
3. 结算逻辑放共享层(Android 只触发)
|
||||
4. 输出结业可演示 Demo
|
||||
|
||||
---
|
||||
|
||||
## 2. 先修复第6节常见问题
|
||||
|
||||
1. **Ktor 初始化问题**
|
||||
- 检查平台 engine 是否配置(androidMain `actual`)
|
||||
2. **JSON 解析问题**
|
||||
- `ignoreUnknownKeys = true`
|
||||
3. **存储反序列化问题**
|
||||
- `runCatching + emptyList()` 兜底
|
||||
4. **Android 网络问题**
|
||||
- `INTERNET` 权限
|
||||
|
||||
---
|
||||
|
||||
## 3. 扩展共享层:商品详情
|
||||
|
||||
### 3.1 `repository/ProductRepository.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.repository
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
import com.example.minishop.network.ProductApi
|
||||
|
||||
class ProductRepository(
|
||||
private val api: ProductApi
|
||||
) {
|
||||
suspend fun products(): List<Product> = api.fetchProducts()
|
||||
|
||||
suspend fun productDetail(id: String): Product {
|
||||
// 若后端无详情接口,可先从列表中查找模拟
|
||||
return products().first { it.id == id }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. 扩展共享层:购物车总价与结算
|
||||
|
||||
### 4.1 `cart/CartService.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.minishop.cart
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
class CartService(
|
||||
private val cartRepository: CartRepository
|
||||
) {
|
||||
fun totalCount(): Int = cartRepository.items().sumOf { it.quantity }
|
||||
|
||||
fun totalPrice(productMap: Map<String, Product>): Double {
|
||||
return cartRepository.items().sumOf { item ->
|
||||
val p = productMap[item.productId] ?: return@sumOf 0.0
|
||||
p.price * item.quantity
|
||||
}
|
||||
}
|
||||
|
||||
fun checkout(productMap: Map<String, Product>): CheckoutResult {
|
||||
val amount = totalPrice(productMap)
|
||||
if (amount <= 0) return CheckoutResult(false, "购物车为空")
|
||||
return CheckoutResult(true, "结算成功(模拟),金额:%.2f".format(amount))
|
||||
}
|
||||
}
|
||||
|
||||
data class CheckoutResult(
|
||||
val success: Boolean,
|
||||
val message: String
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Android 端整合方式(课堂实现)
|
||||
|
||||
页面层需要做的只有三件事:
|
||||
|
||||
1. 调用 `ProductRepository.products()` 渲染列表
|
||||
2. 调用 `ProductRepository.productDetail(id)` 渲染详情
|
||||
3. 调用 `CartRepository` / `CartService` 完成加购、总价、结算
|
||||
|
||||
推荐页面结构:
|
||||
- 商品列表页
|
||||
- 商品详情页
|
||||
- 购物车页(含总价、结算按钮)
|
||||
|
||||
---
|
||||
|
||||
## 6. 课堂验收(硬标准)
|
||||
|
||||
1. 列表可加载
|
||||
2. 详情可打开
|
||||
3. 可加购、删购、改数量
|
||||
4. 总价计算正确
|
||||
5. 结算逻辑可执行(模拟)
|
||||
6. 重启后购物车仍存在
|
||||
|
||||
---
|
||||
|
||||
## 7. 技能复盘(结业总结)
|
||||
|
||||
本项目中的共享层职责:
|
||||
|
||||
- 网络:Ktor 请求与数据解析
|
||||
- 存储:Settings 持久化
|
||||
- 业务:购物车规则、总价、结算
|
||||
|
||||
Android 职责:
|
||||
|
||||
- 页面展示
|
||||
- 用户事件触发
|
||||
- 调用共享层并渲染结果
|
||||
|
||||
---
|
||||
|
||||
## 8. 课后作业(2小时)
|
||||
|
||||
独立完成并提交:
|
||||
|
||||
1. 商品列表(Ktor 加载)
|
||||
2. 商品详情(Ktor 加载)
|
||||
3. 购物车(Settings 存储)
|
||||
4. 加购 / 删购 / 数量修改(共享层实现)
|
||||
|
||||
提交物:
|
||||
- 运行截图
|
||||
- 关键代码截图
|
||||
- 简短说明:网络/存储复用点
|
||||
|
||||
Reference in new issue
Block a user