feat(monitor): add UserProfile + MonitorMetric catalog + Indicator.seriesKey
数据层(spec 2026-05-26):
- UserProfile @Model:核心 4 项 + 健康背景 + 用药,SwiftData 单例(loadOrCreate)
- Indicator 加 seriesKey: String?,标识长期指标分组('bp.systolic' 等)
- MonitorMetric enum 8 case:血压(2 field 拆 2 Indicator)/ 空腹+餐后血糖 /
体重 / 体温 / 心率 / SpO2 / 身高
- effectiveRange(for:profile:) 实现 1 条 Profile-aware 规则:
age >= 65 时 bp.systolic 上限 140→150
- KangkangApp schema 加 UserProfile.self
测试 17 个全绿(UserProfile 6 + MonitorMetric 11);schema 烟测扩 2(seriesKey roundtrip + UserProfile persist)。
UI 层 + Timeline 合并下个 commit。
This commit is contained in:
@@ -33,6 +33,11 @@ final class Indicator {
|
||||
var asset: Asset?
|
||||
var pinned: Bool = false
|
||||
|
||||
/// 长期指标系列 key,如 "bp.systolic" / "glucose.fasting" / "weight"。
|
||||
/// 来源:IndicatorRecordSheet 选预设时填;VL/Report/自由输入留 nil。
|
||||
/// 用途:Trends 按 seriesKey 分组;Timeline 配对(如 bp.systolic + bp.diastolic 合并)。
|
||||
var seriesKey: String?
|
||||
|
||||
init(name: String,
|
||||
value: String,
|
||||
unit: String,
|
||||
@@ -42,7 +47,8 @@ final class Indicator {
|
||||
capturedAt: Date = .now,
|
||||
report: Report? = nil,
|
||||
asset: Asset? = nil,
|
||||
pinned: Bool = false) {
|
||||
pinned: Bool = false,
|
||||
seriesKey: String? = nil) {
|
||||
self.name = name
|
||||
self.value = value
|
||||
self.unit = unit
|
||||
@@ -53,6 +59,7 @@ final class Indicator {
|
||||
self.report = report
|
||||
self.asset = asset
|
||||
self.pinned = pinned
|
||||
self.seriesKey = seriesKey
|
||||
}
|
||||
|
||||
var status: IndicatorStatus {
|
||||
|
||||
97
康康/Models/UserProfile.swift
Normal file
97
康康/Models/UserProfile.swift
Normal file
@@ -0,0 +1,97 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class UserProfile {
|
||||
// —— 核心 4 项 ——
|
||||
var birthYear: Int? // 1990。隐私考虑只存年,不存月日
|
||||
var biologicalSexRaw: String // "" / "male" / "female"
|
||||
var heightCM: Int?
|
||||
var bloodTypeRaw: String // "" / "A" / "B" / "AB" / "O"
|
||||
|
||||
// —— 健康背景 ——
|
||||
var allergies: [String]
|
||||
var chronicConditions: [String]
|
||||
var familyHistory: [String]
|
||||
|
||||
// —— 当前用药 ——
|
||||
var currentMedications: [String]
|
||||
|
||||
var updatedAt: Date
|
||||
|
||||
init(birthYear: Int? = nil,
|
||||
biologicalSexRaw: String = "",
|
||||
heightCM: Int? = nil,
|
||||
bloodTypeRaw: String = "",
|
||||
allergies: [String] = [],
|
||||
chronicConditions: [String] = [],
|
||||
familyHistory: [String] = [],
|
||||
currentMedications: [String] = [],
|
||||
updatedAt: Date = .now) {
|
||||
self.birthYear = birthYear
|
||||
self.biologicalSexRaw = biologicalSexRaw
|
||||
self.heightCM = heightCM
|
||||
self.bloodTypeRaw = bloodTypeRaw
|
||||
self.allergies = allergies
|
||||
self.chronicConditions = chronicConditions
|
||||
self.familyHistory = familyHistory
|
||||
self.currentMedications = currentMedications
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
extension UserProfile {
|
||||
enum Sex: String, CaseIterable {
|
||||
case male, female
|
||||
case undisclosed = ""
|
||||
|
||||
var label: String {
|
||||
switch self {
|
||||
case .male: return "男"
|
||||
case .female: return "女"
|
||||
case .undisclosed: return "不愿透露"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 · 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 !bloodTypeRaw.isEmpty { parts.append("\(bloodTypeRaw)型") }
|
||||
return parts.joined(separator: " · ")
|
||||
}
|
||||
|
||||
/// 资料是否完整到值得显示 summaryLine(否则提示"完善资料")
|
||||
var hasAnyBasics: Bool {
|
||||
birthYear != nil || sex != .undisclosed || heightCM != nil || !bloodTypeRaw.isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
/// 单例存取:全 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user