feat: 国际化(i18n) en/ja/ko + App 内语言切换
主体:多语言支持(简体中文源 + 英/日/韩)
- 基础设施:Localizable.xcstrings(String Catalog,sourceLanguage=zh-Hans)
+ pbxproj developmentRegion/knownRegions 注册 en/ja/ko
- 全部硬编码 Locale("zh_CN") → Locale.current;中文 dateFormat → Date.FormatStyle(跟随系统)
- UI 中文字面量统一为 String(appLoc:)(显式绑定所选语言 bundle+locale,即时切换)
Text 字面量走环境 \.locale + Bundle 重定向
- 549 个 catalog key 全部 en/ja/ko 翻译完成(0 未翻译)
- App 内语言切换:我的 → 语言(LanguageManager + 即时生效,无需重启)
- 双用预设(症状/监测指标/慢病)本地化:static→computed 避免缓存
注:本提交为 WIP,一并打包了并行进行的功能模块
(HealthExport 健康导出、Security/Face ID 锁、DiaryAssist 日记 AI 辅助)
及 App 图标、CLAUDE.md、docs/scripts。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
158
康康/Security/AppLock.swift
Normal file
158
康康/Security/AppLock.swift
Normal file
@@ -0,0 +1,158 @@
|
||||
import Foundation
|
||||
import LocalAuthentication
|
||||
import SwiftUI
|
||||
import Observation
|
||||
|
||||
/// Face ID 启动锁的运行时控制器(单例)。
|
||||
///
|
||||
/// 设计见 `docs/superpowers/specs/2026-05-30-faceid-app-lock-design.md`。
|
||||
/// 红线对齐(CLAUDE.md §10.2):只用系统 `LocalAuthentication`,不自造任何密码学。
|
||||
///
|
||||
/// 单例写法与 `ModelDownloadService.shared` 一致:`@MainActor @Observable`。
|
||||
/// UI(`AppLockContainer` / `MeView` / `LockScreenView`)只观察本类的 observable 状态,
|
||||
/// 通过 `handleAppear` / `handleScenePhase` / `authenticate` 等方法驱动。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class AppLock {
|
||||
static let shared = AppLock()
|
||||
|
||||
/// 后台超过该时长再回前台 → 重锁。
|
||||
static let gracePeriod: TimeInterval = 60
|
||||
|
||||
/// 启用开关持久化 key,与 `MeView` 的 `@AppStorage` 同源。
|
||||
static let enabledKey = "faceIDLockEnabled"
|
||||
|
||||
// MARK: - Observable 运行态
|
||||
|
||||
/// 当前是否处于锁定(需认证才能进入)。
|
||||
private(set) var isLocked = false
|
||||
|
||||
/// 进入任务切换器 / 后台时是否盖隐私遮罩(仅锁开启时为真)。
|
||||
private(set) var showsPrivacyCover = false
|
||||
|
||||
/// 设备是否可用生物识别或密码认证(无密码设备为 false)。
|
||||
private(set) var biometryAvailable = false
|
||||
|
||||
/// 认证按钮 / 副标题文案:"Face ID" / "Touch ID" / "密码"。
|
||||
private(set) var biometryLabel = String(appLoc: "密码")
|
||||
|
||||
// MARK: - 非观察内部态
|
||||
|
||||
/// 是否已开启启动锁。读写 UserDefaults(与 MeView 的 @AppStorage 同 key)。
|
||||
/// 不需要 observable —— UI 侧用 @AppStorage 观察这个 key 的变化。
|
||||
var enabled: Bool {
|
||||
get { UserDefaults.standard.bool(forKey: Self.enabledKey) }
|
||||
set { UserDefaults.standard.set(newValue, forKey: Self.enabledKey) }
|
||||
}
|
||||
|
||||
private var lastBackgroundedAt: Date?
|
||||
private var didColdLaunchLock = false
|
||||
private var isAuthenticating = false
|
||||
|
||||
private init() {
|
||||
refreshAvailability()
|
||||
}
|
||||
|
||||
// MARK: - 能力探测
|
||||
|
||||
/// 刷新「设备能否认证」与文案。进设置页 / 容器出现时调。
|
||||
func refreshAvailability() {
|
||||
let ctx = LAContext()
|
||||
var error: NSError?
|
||||
// .deviceOwnerAuthentication:设备设了密码即为 true(含生物识别 + 密码兜底)。
|
||||
biometryAvailable = ctx.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error)
|
||||
// biometryType 只有在 canEvaluatePolicy 调用后才有效。
|
||||
switch ctx.biometryType {
|
||||
case .faceID: biometryLabel = "Face ID"
|
||||
case .touchID: biometryLabel = "Touch ID"
|
||||
default: biometryLabel = String(appLoc: "密码")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 生命周期驱动(由 AppLockContainer 调)
|
||||
|
||||
/// 冷启动:容器首次出现时调一次。
|
||||
func handleAppear() {
|
||||
refreshAvailability()
|
||||
guard enabled, !didColdLaunchLock else { return }
|
||||
didColdLaunchLock = true
|
||||
isLocked = true
|
||||
Task { await authenticate() }
|
||||
}
|
||||
|
||||
/// scenePhase 变化驱动。
|
||||
func handleScenePhase(_ phase: ScenePhase) {
|
||||
switch phase {
|
||||
case .inactive:
|
||||
// 任务切换器 / 系统弹窗打断:盖遮罩(已锁定时锁屏本身就是遮罩)。
|
||||
showsPrivacyCover = enabled && !isLocked
|
||||
|
||||
case .background:
|
||||
lastBackgroundedAt = Date()
|
||||
showsPrivacyCover = enabled
|
||||
|
||||
case .active:
|
||||
showsPrivacyCover = false
|
||||
if enabled, !isLocked,
|
||||
let since = lastBackgroundedAt,
|
||||
Date().timeIntervalSince(since) > Self.gracePeriod {
|
||||
isLocked = true
|
||||
}
|
||||
if isLocked { Task { await authenticate() } }
|
||||
lastBackgroundedAt = nil
|
||||
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 认证
|
||||
|
||||
/// 触发系统认证。成功 → 解锁;失败/取消 → 保持锁定。`isAuthenticating` 防重入,
|
||||
/// 避免容器与锁屏 onAppear 同时各弹一次。
|
||||
func authenticate() async {
|
||||
guard isLocked, !isAuthenticating else { return }
|
||||
isAuthenticating = true
|
||||
defer { isAuthenticating = false }
|
||||
|
||||
let ctx = LAContext()
|
||||
ctx.localizedFallbackTitle = String(appLoc: "输入密码")
|
||||
do {
|
||||
let ok = try await ctx.evaluatePolicy(
|
||||
.deviceOwnerAuthentication,
|
||||
localizedReason: String(appLoc: "解锁康康,查看你的健康档案")
|
||||
)
|
||||
if ok { isLocked = false }
|
||||
} catch {
|
||||
// 失败/取消:停留锁屏,用户可点「解锁」重试。不抛给 UI。
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 设置开关(MeView 调)
|
||||
|
||||
/// 开启:先认证一次(验证设备可用 + 确认本人),成功才置 `enabled`。
|
||||
/// 返回最终是否已开启。
|
||||
@discardableResult
|
||||
func enableWithAuth() async -> Bool {
|
||||
let ctx = LAContext()
|
||||
ctx.localizedFallbackTitle = String(appLoc: "输入密码")
|
||||
do {
|
||||
let ok = try await ctx.evaluatePolicy(
|
||||
.deviceOwnerAuthentication,
|
||||
localizedReason: String(appLoc: "验证你本人,开启 Face ID 启动锁")
|
||||
)
|
||||
if ok {
|
||||
enabled = true
|
||||
return true
|
||||
}
|
||||
} catch {
|
||||
// 取消/失败:不开启。
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// 关闭:直接关(此刻已在 App 内、本次已通过认证)。
|
||||
func disable() {
|
||||
enabled = false
|
||||
}
|
||||
}
|
||||
31
康康/Security/AppLockContainer.swift
Normal file
31
康康/Security/AppLockContainer.swift
Normal file
@@ -0,0 +1,31 @@
|
||||
import SwiftUI
|
||||
|
||||
/// 包裹 `RootView` 的薄薄一层:监听 scenePhase,按需在内容之上盖锁屏 / 隐私遮罩。
|
||||
/// RootView 本身零改动(对齐红线 §10.7「不重构现有 Tab 骨架」)。
|
||||
///
|
||||
/// 用法(KangkangApp):`AppLockContainer { RootView() }`。
|
||||
struct AppLockContainer<Content: View>: View {
|
||||
@ViewBuilder var content: () -> Content
|
||||
|
||||
@Environment(\.scenePhase) private var scenePhase
|
||||
@State private var appLock = AppLock.shared
|
||||
|
||||
var body: some View {
|
||||
content()
|
||||
.overlay {
|
||||
if appLock.isLocked {
|
||||
LockScreenView()
|
||||
.transition(.opacity)
|
||||
} else if appLock.showsPrivacyCover {
|
||||
// 不加动画:瞬间出现,抢在系统多任务快照之前盖住内容。
|
||||
PrivacyCoverView()
|
||||
}
|
||||
}
|
||||
// 只给锁屏淡入淡出;隐私遮罩保持瞬时。
|
||||
.animation(.easeInOut(duration: 0.2), value: appLock.isLocked)
|
||||
.onAppear { appLock.handleAppear() }
|
||||
.onChange(of: scenePhase) { _, newPhase in
|
||||
appLock.handleScenePhase(newPhase)
|
||||
}
|
||||
}
|
||||
}
|
||||
94
康康/Security/LockScreenView.swift
Normal file
94
康康/Security/LockScreenView.swift
Normal file
@@ -0,0 +1,94 @@
|
||||
import SwiftUI
|
||||
|
||||
/// 锁屏:全遮罩,onAppear 自动触发一次认证;失败/取消后停留,可点按钮重试。
|
||||
struct LockScreenView: View {
|
||||
@State private var appLock = AppLock.shared
|
||||
|
||||
/// 认证按钮 / 图标随设备能力变化。
|
||||
private var glyph: String {
|
||||
switch appLock.biometryLabel {
|
||||
case "Face ID": return "faceid"
|
||||
case "Touch ID": return "touchid"
|
||||
default: return "lock.fill"
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Tj.Palette.sand.ignoresSafeArea()
|
||||
|
||||
VStack(spacing: 18) {
|
||||
Spacer()
|
||||
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(Tj.Palette.paper)
|
||||
.overlay(Circle().strokeBorder(Tj.Palette.line, lineWidth: 1))
|
||||
Image(systemName: "lock.fill")
|
||||
.font(.system(size: 34))
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
.frame(width: 92, height: 92)
|
||||
.shadow(color: Tj.Palette.ink.opacity(0.06), radius: 12, y: 4)
|
||||
|
||||
VStack(spacing: 6) {
|
||||
Text("康康 已锁定")
|
||||
.font(.tjH2())
|
||||
.foregroundStyle(Tj.Palette.text)
|
||||
Text("你的健康档案已加密保护")
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button {
|
||||
Task { await appLock.authenticate() }
|
||||
} label: {
|
||||
Label("\(appLock.biometryLabel) 解锁", systemImage: glyph)
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.buttonStyle(TjPrimaryButton(height: 52, fontSize: 16))
|
||||
.padding(.horizontal, 40)
|
||||
.padding(.bottom, 48)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
Task { await appLock.authenticate() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 隐私遮罩:进任务切换器 / 后台时盖在内容之上,挡住多任务快照里的健康数据。
|
||||
/// 无交互,纯品牌底。
|
||||
struct PrivacyCoverView: View {
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Tj.Palette.sand.ignoresSafeArea()
|
||||
|
||||
VStack(spacing: 14) {
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(Tj.Palette.paper)
|
||||
.overlay(Circle().strokeBorder(Tj.Palette.line, lineWidth: 1))
|
||||
Image(systemName: "heart.text.square.fill")
|
||||
.font(.system(size: 30))
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
.frame(width: 80, height: 80)
|
||||
|
||||
Text("康康")
|
||||
.font(.tjH2())
|
||||
.foregroundStyle(Tj.Palette.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview("锁屏") {
|
||||
LockScreenView()
|
||||
}
|
||||
|
||||
#Preview("隐私遮罩") {
|
||||
PrivacyCoverView()
|
||||
}
|
||||
Reference in New Issue
Block a user