Files
kangkang/康康/Models/UserProfile.swift
link2026 d2c77d5c51 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>
2026-05-30 10:28:24 +08:00

125 lines
4.0 KiB
Swift

import Foundation
import SwiftData
@Model
final class UserProfile {
// 5
var birthYear: Int? // 1990,
var biologicalSexRaw: String // "" / "male" / "female"
var heightCM: Int?
var weightKG: Double? // (68.5)
var bloodTypeRaw: String // "" / "A" / "B" / "AB" / "O"
//
var allergies: [String]
var chronicConditions: [String]
var familyHistory: [String]
//
var currentMedications: [String]
// UI
// IndicatorQuickSheet MonitorMetric.rawValue
// grid, Indicator / Trends / Reminder
var hiddenPresetMetrics: [String]
var updatedAt: Date
init(birthYear: Int? = nil,
biologicalSexRaw: String = "",
heightCM: Int? = nil,
weightKG: Double? = nil,
bloodTypeRaw: String = "",
allergies: [String] = [],
chronicConditions: [String] = [],
familyHistory: [String] = [],
currentMedications: [String] = [],
hiddenPresetMetrics: [String] = [],
updatedAt: Date = .now) {
self.birthYear = birthYear
self.biologicalSexRaw = biologicalSexRaw
self.heightCM = heightCM
self.weightKG = weightKG
self.bloodTypeRaw = bloodTypeRaw
self.allergies = allergies
self.chronicConditions = chronicConditions
self.familyHistory = familyHistory
self.currentMedications = currentMedications
self.hiddenPresetMetrics = hiddenPresetMetrics
self.updatedAt = updatedAt
}
}
extension UserProfile {
enum Sex: String, CaseIterable {
case male, female
case undisclosed = ""
var label: String {
switch self {
case .male: return String(appLoc: "")
case .female: return String(appLoc: "")
case .undisclosed: return String(appLoc: "不愿透露")
}
}
}
var sex: Sex {
get { Sex(rawValue: biologicalSexRaw) ?? .undisclosed }
set { biologicalSexRaw = newValue.rawValue }
}
/// birthYear nil,
var age: Int? {
guard let y = birthYear else { return nil }
return Calendar.current.component(.year, from: .now) - y
}
/// ProfileCard :"38 · · 175cm · 68kg · A"
var summaryLine: String {
var parts: [String] = []
if let age { parts.append(String(appLoc: "\(age)")) }
if sex != .undisclosed { parts.append(sex.label) }
if let h = heightCM { parts.append("\(h)cm") }
if let w = weightKG {
let s = w.truncatingRemainder(dividingBy: 1) == 0
? String(format: "%.0fkg", w)
: String(format: "%.1fkg", w)
parts.append(s)
}
if !bloodTypeRaw.isEmpty { parts.append(String(appLoc: "\(bloodTypeRaw)")) }
return parts.joined(separator: " · ")
}
/// summaryLine("")
var hasAnyBasics: Bool {
birthYear != nil ||
sex != .undisclosed ||
heightCM != nil ||
weightKG != nil ||
!bloodTypeRaw.isEmpty
}
/// BMI(kg/m²), +
var bmi: Double? {
guard let h = heightCM, h > 0, let w = weightKG else { return nil }
let m = Double(h) / 100.0
return w / (m * m)
}
}
/// : App UserProfile
enum UserProfileStore {
@MainActor
static func loadOrCreate(in ctx: ModelContext) -> UserProfile {
let descriptor = FetchDescriptor<UserProfile>()
if let existing = try? ctx.fetch(descriptor).first {
return existing
}
let new = UserProfile()
ctx.insert(new)
try? ctx.save()
return new
}
}