/** * 第 7 章配套代码 · expect / actual 跨端 API 范式 * * 演示如何用 KMP 的 expect/actual 机制,写一个跨端的"获取平台信息" API。 * * 这是 KMP 工程组织代码的"标准范式",第 8 章会进一步演化成 Module 概念。 */ // ═══════════════════════════════════════════════════════════════════ // commonMain:公共声明(业务代码只调这一份) // 文件位置:shared/src/commonMain/kotlin/com/example/kuikly/platform/PlatformInfo.kt // ═══════════════════════════════════════════════════════════════════ package com.example.kuikly.platform /** * 当前平台名称,如 "Android 14" / "iOS 17.0" / "HarmonyOS 4.0" */ expect fun currentPlatform(): String /** * 屏幕物理像素宽 × 高 */ expect fun screenSize(): Pair /** * 设备唯一 ID(演示用,真实场景注意隐私合规) */ expect fun deviceId(): String // ═══════════════════════════════════════════════════════════════════ // androidMain:Android 平台实现 // 文件位置:shared/src/androidMain/kotlin/com/example/kuikly/platform/PlatformInfo.kt // ═══════════════════════════════════════════════════════════════════ /* package com.example.kuikly.platform import android.os.Build import android.provider.Settings import com.example.kuikly.app.KuiklyApplication actual fun currentPlatform(): String = "Android ${Build.VERSION.RELEASE} (SDK ${Build.VERSION.SDK_INT})" actual fun screenSize(): Pair { val ctx = KuiklyApplication.context val dm = ctx.resources.displayMetrics return dm.widthPixels to dm.heightPixels } actual fun deviceId(): String { val ctx = KuiklyApplication.context return Settings.Secure.getString(ctx.contentResolver, Settings.Secure.ANDROID_ID) } */ // ═══════════════════════════════════════════════════════════════════ // iosMain:iOS 平台实现(Kotlin/Native) // 文件位置:shared/src/iosMain/kotlin/com/example/kuikly/platform/PlatformInfo.kt // ═══════════════════════════════════════════════════════════════════ /* package com.example.kuikly.platform import platform.UIKit.UIDevice import platform.UIKit.UIScreen actual fun currentPlatform(): String { val dev = UIDevice.currentDevice return "iOS ${dev.systemVersion}" } actual fun screenSize(): Pair { val bounds = UIScreen.mainScreen.bounds return bounds.useContents { size.width.toInt() to size.height.toInt() } } actual fun deviceId(): String = UIDevice.currentDevice.identifierForVendor?.UUIDString ?: "unknown" */ // ═══════════════════════════════════════════════════════════════════ // jsMain:Web / 小程序实现 // 文件位置:shared/src/jsMain/kotlin/com/example/kuikly/platform/PlatformInfo.kt // ═══════════════════════════════════════════════════════════════════ /* package com.example.kuikly.platform import kotlinx.browser.window actual fun currentPlatform(): String = "Web ${window.navigator.userAgent}" actual fun screenSize(): Pair = window.innerWidth to window.innerHeight actual fun deviceId(): String = window.localStorage.getItem("device_id") ?: ("web_" + (Math.random() * 1e10).toLong()).also { window.localStorage.setItem("device_id", it) } */ /* ───────────────────────────────────────────────────────── * 业务代码(commonMain)使用方式: * * class HomePage : Pager() { * override fun body() = { * Text { attr { text("当前平台: ${currentPlatform()}") } } * Text { attr { text("屏幕: ${screenSize()}") } } * Text { attr { text("设备: ${deviceId()}") } } * } * } * * Android 上看到:"Android 14 (SDK 34)" * iOS 上看到:"iOS 17.0" * Web 上看到:"Web Mozilla/5.0..." * * ★ 一份 commonMain 业务代码,3 端结果都对 —— 这就是 expect/actual 的威力 * ─────────────────────────────────────────────────────── */