refactor(profile,monitor): move height/weight from MonitorMetric to UserProfile
身高/体重对成人变化慢,作为 Profile 静态字段比每次录入 Indicator 更合适。 - MonitorMetric:6 case(从 8 减),删 .height / .weight - UserProfile:加 weightKG: Double?(支持小数),加 bmi computed - summaryLine 加体重段:'175cm · 68.5kg'(整数省小数) - ProfileEditView basics 加 weight 行 + footer 显示 BMI + 分类(偏瘦/正常/超重/肥胖) - IndicatorQuickSheet:删 .height 回写 Profile 的特殊逻辑 - UserProfileTests:+5 个(weight 字段、summaryLine 含 weight、BMI 计算) 兼容性:老 Indicator 里的 seriesKey 'weight' / 'height' 数据保留(SwiftData String? 不变),只是新录入路径走 Profile 不走 Indicator;Trends 仍能用 String seriesKey 查询历史(如果将来要展示老数据)。 测试:60 case pass / 0 fail / 0 warning。
This commit is contained in:
@@ -3,10 +3,11 @@ import SwiftData
|
||||
|
||||
@Model
|
||||
final class UserProfile {
|
||||
// —— 核心 4 项 ——
|
||||
// —— 核心 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"
|
||||
|
||||
// —— 健康背景 ——
|
||||
@@ -22,6 +23,7 @@ final class UserProfile {
|
||||
init(birthYear: Int? = nil,
|
||||
biologicalSexRaw: String = "",
|
||||
heightCM: Int? = nil,
|
||||
weightKG: Double? = nil,
|
||||
bloodTypeRaw: String = "",
|
||||
allergies: [String] = [],
|
||||
chronicConditions: [String] = [],
|
||||
@@ -31,6 +33,7 @@ final class UserProfile {
|
||||
self.birthYear = birthYear
|
||||
self.biologicalSexRaw = biologicalSexRaw
|
||||
self.heightCM = heightCM
|
||||
self.weightKG = weightKG
|
||||
self.bloodTypeRaw = bloodTypeRaw
|
||||
self.allergies = allergies
|
||||
self.chronicConditions = chronicConditions
|
||||
@@ -65,19 +68,36 @@ extension UserProfile {
|
||||
return Calendar.current.component(.year, from: .now) - y
|
||||
}
|
||||
|
||||
/// 给 ProfileCard 一行预览:"38岁 · 男 · 175cm · A型"
|
||||
/// 给 ProfileCard 一行预览:"38岁 · 男 · 175cm · 68kg · A型"
|
||||
var summaryLine: String {
|
||||
var parts: [String] = []
|
||||
if let age { parts.append("\(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("\(bloodTypeRaw)型") }
|
||||
return parts.joined(separator: " · ")
|
||||
}
|
||||
|
||||
/// 资料是否完整到值得显示 summaryLine(否则提示"完善资料")
|
||||
var hasAnyBasics: Bool {
|
||||
birthYear != nil || sex != .undisclosed || heightCM != nil || !bloodTypeRaw.isEmpty
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user