docs(health-profile): 添加防编造加固修订记录到导出健康档案设计文档 补充了关于导出摘要出现虚构病例问题的详细分析和修复方案, 包括检索策略优化、空数据兜底处理和prompt重写等三层防护措施。 ```
260 lines
9.5 KiB
Swift
260 lines
9.5 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ArchiveListView: View {
|
|
@Query(sort: \Indicator.capturedAt, order: .reverse)
|
|
private var indicators: [Indicator]
|
|
|
|
@Query(sort: \Report.reportDate, order: .reverse)
|
|
private var reports: [Report]
|
|
|
|
@Query(sort: \DiaryEntry.createdAt, order: .reverse)
|
|
private var diaries: [DiaryEntry]
|
|
|
|
@Query(sort: \Symptom.startedAt, order: .reverse)
|
|
private var symptoms: [Symptom]
|
|
|
|
@Query(sort: \HealthExport.createdAt, order: .reverse)
|
|
private var exports: [HealthExport]
|
|
|
|
@State private var filter: TimelineKind? = nil
|
|
@State private var endingSymptom: Symptom?
|
|
@State private var selectedEntry: TimelineEntry?
|
|
@State private var showExportSheet = false
|
|
@State private var showExportList = false
|
|
|
|
@MainActor
|
|
private var allEntries: [TimelineEntry] {
|
|
let mapped =
|
|
TimelineEntry.from(indicators: indicators) +
|
|
reports.map(TimelineEntry.from(report:)) +
|
|
diaries.map(TimelineEntry.from(diary:)) +
|
|
symptoms.map(TimelineEntry.from(symptom:))
|
|
let filtered = filter.map { kind in mapped.filter { $0.kind == kind } } ?? mapped
|
|
return filtered.sorted { $0.date > $1.date }
|
|
}
|
|
|
|
private var grouped: [(section: DateSection, items: [TimelineEntry])] {
|
|
TimelineGrouping.group(allEntries)
|
|
}
|
|
|
|
private var totalCount: Int { allEntries.count }
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
content
|
|
.navigationDestination(isPresented: $showExportList) {
|
|
HealthExportListView()
|
|
}
|
|
}
|
|
}
|
|
|
|
private var content: some View {
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
header
|
|
.padding(.horizontal, 20)
|
|
.padding(.top, 8)
|
|
.padding(.bottom, 14)
|
|
|
|
filterChips
|
|
.padding(.bottom, 14)
|
|
|
|
if allEntries.isEmpty {
|
|
emptyState
|
|
} else {
|
|
ScrollView(showsIndicators: false) {
|
|
LazyVStack(alignment: .leading, spacing: 18, pinnedViews: [.sectionHeaders]) {
|
|
ForEach(grouped, id: \.section) { group in
|
|
Section {
|
|
VStack(spacing: 10) {
|
|
ForEach(group.items) { entry in
|
|
rowView(for: entry)
|
|
}
|
|
}
|
|
.padding(.horizontal, 20)
|
|
} header: {
|
|
sectionHeader(group.section, count: group.items.count)
|
|
}
|
|
}
|
|
}
|
|
.padding(.bottom, 24)
|
|
}
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
.background(Tj.Palette.sand.ignoresSafeArea())
|
|
.sheet(item: $endingSymptom) { sym in
|
|
SymptomEndSheet(symptom: sym)
|
|
}
|
|
.sheet(item: $selectedEntry) { entry in
|
|
if let d = detail(for: entry) {
|
|
TimelineEntryDetailView(detail: d)
|
|
}
|
|
}
|
|
.fullScreenCover(isPresented: $showExportSheet) {
|
|
HealthExportSheet()
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func rowView(for entry: TimelineEntry) -> some View {
|
|
if entry.kind == .symptom, entry.isOngoing,
|
|
let sym = symptoms.first(where: { "symptom-\($0.persistentModelID)" == entry.id }) {
|
|
// 进行中症状:点 → 标记结束 sheet(沿用原交互)
|
|
Button {
|
|
endingSymptom = sym
|
|
} label: {
|
|
TimelineRow(entry: entry)
|
|
}
|
|
.buttonStyle(.plain)
|
|
} else {
|
|
// 其余条目(报告/指标/日记/已结束症状):点 → 只读详情
|
|
Button {
|
|
if detail(for: entry) != nil { selectedEntry = entry }
|
|
} label: {
|
|
TimelineRow(entry: entry)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
|
|
/// 把时间线条目反查回源记录(id 形如 `<kind>-<persistentModelID>` / `bp-<sys>-<dia>`)。
|
|
private func detail(for entry: TimelineEntry) -> TimelineDetail? {
|
|
switch entry.kind {
|
|
case .report:
|
|
return reports.first { "report-\($0.persistentModelID)" == entry.id }
|
|
.map(TimelineDetail.report)
|
|
case .diary:
|
|
return diaries.first { "diary-\($0.persistentModelID)" == entry.id }
|
|
.map(TimelineDetail.diary)
|
|
case .symptom:
|
|
return symptoms.first { "symptom-\($0.persistentModelID)" == entry.id }
|
|
.map(TimelineDetail.symptom)
|
|
case .indicator:
|
|
if let i = indicators.first(where: { "indicator-\($0.persistentModelID)" == entry.id }) {
|
|
return .indicator(i)
|
|
}
|
|
// 合并血压条目:bp-<sysID>-<diaID>
|
|
if entry.id.hasPrefix("bp-"),
|
|
let sys = indicators.first(where: { entry.id.hasPrefix("bp-\($0.persistentModelID)-") }) {
|
|
let dia = indicators.first {
|
|
$0.seriesKey == "bp.diastolic" &&
|
|
abs($0.capturedAt.timeIntervalSince(sys.capturedAt)) <= 5
|
|
}
|
|
return .bloodPressure(sys: sys, dia: dia)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private var header: some View {
|
|
HStack(alignment: .lastTextBaseline) {
|
|
Text("记录")
|
|
.font(.tjTitle(26))
|
|
.foregroundStyle(Tj.Palette.text)
|
|
Text(totalCount == 0 ? "" : String(appLoc: "\(totalCount) 条"))
|
|
.font(.system(size: 12))
|
|
.foregroundStyle(Tj.Palette.text3)
|
|
Spacer()
|
|
Menu {
|
|
Button {
|
|
showExportSheet = true
|
|
} label: {
|
|
Label("生成新导出", systemImage: "doc.text.below.ecg")
|
|
}
|
|
if !exports.isEmpty {
|
|
Button {
|
|
showExportList = true
|
|
} label: {
|
|
Label("我的导出 · \(exports.count) 份", systemImage: "clock.arrow.circlepath")
|
|
}
|
|
}
|
|
} label: {
|
|
HStack(spacing: 6) {
|
|
Image(systemName: "doc.text.below.ecg")
|
|
.font(.system(size: 12, weight: .semibold))
|
|
Text("导出")
|
|
.font(.system(size: 13, weight: .semibold))
|
|
Image(systemName: "chevron.down")
|
|
.font(.system(size: 9, weight: .semibold))
|
|
}
|
|
.foregroundStyle(Tj.Palette.paper)
|
|
.padding(.horizontal, 12)
|
|
.padding(.vertical, 7)
|
|
.background(Capsule().fill(Tj.Palette.ink))
|
|
}
|
|
}
|
|
}
|
|
|
|
private var filterChips: some View {
|
|
ScrollView(.horizontal, showsIndicators: false) {
|
|
HStack(spacing: 8) {
|
|
chip(label: String(appLoc: "全部"), selected: filter == nil) { filter = nil }
|
|
ForEach(TimelineKind.allCases) { kind in
|
|
chip(label: kind.label, selected: filter == kind) {
|
|
filter = filter == kind ? nil : kind
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 20)
|
|
}
|
|
}
|
|
|
|
private func chip(label: String, selected: Bool, action: @escaping () -> Void) -> some View {
|
|
Button(action: action) {
|
|
Text(label)
|
|
.font(.system(size: 13, weight: selected ? .semibold : .regular))
|
|
.foregroundStyle(selected ? Tj.Palette.paper : Tj.Palette.text)
|
|
.padding(.horizontal, 14)
|
|
.padding(.vertical, 8)
|
|
.background(
|
|
Capsule().fill(selected ? Tj.Palette.ink : Tj.Palette.paper)
|
|
)
|
|
.overlay(
|
|
Capsule().strokeBorder(Tj.Palette.line, lineWidth: selected ? 0 : 1)
|
|
)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
|
|
private func sectionHeader(_ section: DateSection, count: Int) -> some View {
|
|
HStack {
|
|
Text(section.label)
|
|
.font(.system(size: 12, weight: .semibold))
|
|
.tracking(0.5)
|
|
.foregroundStyle(Tj.Palette.text2)
|
|
Rectangle()
|
|
.fill(Tj.Palette.lineSoft)
|
|
.frame(height: 1)
|
|
Text("\(count)")
|
|
.font(.system(size: 11, design: .monospaced))
|
|
.foregroundStyle(Tj.Palette.text3)
|
|
}
|
|
.padding(.horizontal, 20)
|
|
.padding(.vertical, 8)
|
|
.background(Tj.Palette.sand)
|
|
}
|
|
|
|
private var emptyState: some View {
|
|
VStack(spacing: 14) {
|
|
Spacer()
|
|
TjPlaceholder(label: String(appLoc: "还没有任何记录\n点底部 + 号开始"))
|
|
.frame(width: 240, height: 140)
|
|
Text(filter == nil ? String(appLoc: "记录会按时间归类显示") : String(appLoc: "这个类别下没有记录"))
|
|
.font(.system(size: 13))
|
|
.foregroundStyle(Tj.Palette.text3)
|
|
Spacer()
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ArchiveListView()
|
|
.modelContainer(for: [
|
|
Indicator.self, Report.self, DiaryEntry.self, Symptom.self, Asset.self,
|
|
HealthExport.self, ChatTurn.self, UserProfile.self,
|
|
MetricReminder.self, CustomMonitorMetric.self
|
|
], inMemory: true)
|
|
}
|