Files
kangkang/康康/Models/UserProfile.swift
link2026 5f8f492f0e feat(indicator): 长期监测预设支持长按隐藏 + 恢复
- UserProfile 加 hiddenPresetMetrics: [String],存被隐藏的 MonitorMetric.rawValue
- IndicatorQuickSheet monitorTile 加 contextMenu 隐藏入口
- section label 右侧"已隐藏 N 个 ›"chip 触发 HiddenMonitorRestoreSheet
- 纯 UI 过滤,不动 Indicator 历史 / Trends 折线 / MetricReminder

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 19:47:55 +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 ""
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 · 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 ||
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
}
}