Files
kangkang/康康/Features/Timeline/TimelineEntry.swift
link2026 e179a369f6 根据提供的code differences信息,我发现没有具体的代码变更内容。因此生成一个通用的commit message:
```
chore(config): 更新项目配置文件

- 调整开发环境配置参数
- 优化构建流程设置
- 更新依赖包版本管理
```
2026-07-01 08:03:35 +08:00

281 lines
11 KiB
Swift

import SwiftUI
import SwiftData
import Foundation
enum TimelineKind: String, CaseIterable, Identifiable {
case diary, symptom, consultation, indicator, medication, report
var id: String { rawValue }
var label: String {
switch self {
case .indicator: return String(appLoc: "指标")
case .report: return String(appLoc: "报告")
case .symptom: return String(appLoc: "症状")
case .diary: return String(appLoc: "日记")
case .consultation: return String(appLoc: "问诊")
case .medication: return String(appLoc: "用药")
}
}
var icon: String {
switch self {
case .indicator: return "drop.fill"
case .report: return "doc.fill"
case .symptom: return "waveform.path.ecg"
case .diary: return "pencil"
case .consultation: return "stethoscope"
case .medication: return "pills.fill"
}
}
var accent: Color {
switch self {
case .indicator: return Tj.Palette.brick
case .report: return Tj.Palette.ink2
case .symptom: return Tj.Palette.amber
case .diary: return Tj.Palette.leaf
case .consultation: return Tj.Palette.ink2
case .medication: return Tj.Palette.ink
}
}
}
extension TimelineKind {
/// (2026-06-28):,,
/// / /
/// TimelineKind,
enum Group: String, CaseIterable, Identifiable {
case selfLog // :(/)
case clinical // :()
case medication //
var id: String { rawValue }
/// ()(), ArchiveListView
var caption: String {
switch self {
case .selfLog: return String(appLoc: "自述")
case .clinical: return String(appLoc: "检查")
case .medication: return String(appLoc: "用药")
}
}
var kinds: [TimelineKind] {
switch self {
case .selfLog: return [.diary, .symptom, .consultation]
case .clinical: return [.indicator, .report]
case .medication: return [.medication]
}
}
}
}
struct TimelineEntry: Identifiable, Hashable {
let id: String
let kind: TimelineKind
let date: Date
let title: String
var subtitle: String
let trailing: String?
let trailingIsAlert: Bool
let isOngoing: Bool
/// (>1 N ) 1
var aggregateCount: Int = 1
static func from(indicator i: Indicator) -> TimelineEntry {
TimelineEntry(
id: "indicator-\(i.persistentModelID)",
kind: .indicator,
date: i.capturedAt,
title: i.name,
subtitle: typeSubtitle(for: i),
trailing: indicatorValue(i),
trailingIsAlert: i.status != .normal,
isOngoing: false
)
}
/// Indicator , bp.systolic + bp.diastolic capturedAt
/// " 120/80 mmHg" timeline entry series from(indicator:)
/// :capturedAt 5 ()
static func from(indicators: [Indicator]) -> [TimelineEntry] {
var entries: [TimelineEntry] = []
var consumed = Set<PersistentIdentifier>()
// bp.systolic, bp.diastolic
for sys in indicators where sys.seriesKey == "bp.systolic" {
if consumed.contains(sys.persistentModelID) { continue }
guard let dia = indicators.first(where: {
$0.seriesKey == "bp.diastolic" &&
!consumed.contains($0.persistentModelID) &&
abs($0.capturedAt.timeIntervalSince(sys.capturedAt)) <= 5
}) else { continue }
consumed.insert(sys.persistentModelID)
consumed.insert(dia.persistentModelID)
entries.append(mergedBP(systolic: sys, diastolic: dia))
}
// indicator( systolic/diastolic series)
for i in indicators where !consumed.contains(i.persistentModelID) {
entries.append(from(indicator: i))
}
return entries
}
/// / :(),,
/// (`aggregateCount`,>1 N )
/// `IndicatorSeriesDetailView` /
/// (`IndicatorGroup`):(bp.*) seriesKey key seriesKey name+unit
static func aggregatedIndicators(_ indicators: [Indicator]) -> [TimelineEntry] {
var order: [String] = []
var groups: [String: [Indicator]] = [:]
for i in indicators {
let key = IndicatorGroup.of(i).id
if groups[key] == nil { order.append(key) }
groups[key, default: []].append(i)
}
return order.compactMap { key -> TimelineEntry? in
guard let members = groups[key] else { return nil }
// ( sys/dia),
guard var rep = from(indicators: members).max(by: { $0.date < $1.date }) else { return nil }
// :(bp.systolic ),
let count = key == IndicatorGroup.bloodPressure.id
? members.filter { $0.seriesKey == "bp.systolic" }.count
: members.count
rep.aggregateCount = count
if count > 1 {
rep.subtitle += " · " + String(appLoc: "\(count)")
}
return rep
}
}
private static func mergedBP(systolic sys: Indicator, diastolic dia: Indicator) -> TimelineEntry {
let abnormal = sys.status != .normal || dia.status != .normal
// status : /;
// ( , 85/55 )
let arrow: String
switch (sys.status, dia.status) {
case (.high, .high), (.high, .normal), (.normal, .high): arrow = ""
case (.low, .low), (.low, .normal), (.normal, .low): arrow = ""
default: arrow = ""
}
return TimelineEntry(
id: "bp-\(sys.persistentModelID)-\(dia.persistentModelID)",
kind: .indicator,
date: sys.capturedAt,
title: String(appLoc: "血压"),
subtitle: String(appLoc: "长期监测"),
trailing: "\(sys.value)/\(dia.value) mmHg" + arrow,
trailingIsAlert: abnormal,
isOngoing: false
)
}
static func from(report r: Report) -> TimelineEntry {
let highCount = r.indicators.filter { $0.status == .high }.count
let lowCount = r.indicators.filter { $0.status == .low }.count
return TimelineEntry(
id: "report-\(r.persistentModelID)",
kind: .report,
date: r.reportDate,
title: r.title,
subtitle: "\(r.type.label) · " + String(appLoc: "\(r.pageCount)"),
trailing: abnormalSummary(high: highCount, low: lowCount),
trailingIsAlert: highCount + lowCount > 0,
isOngoing: false
)
}
/// trailing N N N nil
/// N ,(demo )
static func abnormalSummary(high: Int, low: Int) -> String? {
switch (high, low) {
case (0, 0): return nil
case (let h, 0): return String(appLoc: "\(h) 项偏高")
case (0, let l): return String(appLoc: "\(l) 项偏低")
case (let h, let l): return String(appLoc: "\(h + l) 项异常")
}
}
/// DiaryEntry tag : .consultation,/ .medication,
/// id "diary-" :TimelineDetail.resolve diaries
static func from(diary d: DiaryEntry) -> TimelineEntry {
let kind: TimelineKind
let subtitle: String
if d.isConsultation {
kind = .consultation
subtitle = String(appLoc: "问诊记录")
} else if d.isMedicationLog {
kind = .medication
subtitle = String(appLoc: "用药记录")
} else {
kind = .diary
subtitle = String(appLoc: "文字日记")
}
return TimelineEntry(
id: "diary-\(d.persistentModelID)",
kind: kind,
date: d.createdAt,
title: d.content.firstLine(),
subtitle: subtitle,
trailing: d.isConsultation && d.audioAsset != nil ? String(appLoc: "录音") : nil,
trailingIsAlert: false,
isOngoing: false
)
}
static func from(symptom s: Symptom) -> TimelineEntry {
let ongoing = s.isOngoing
let date = s.endedAt ?? s.startedAt
let subtitle: String
let trailing: String?
if ongoing {
subtitle = String(appLoc: "症状 · 持续中")
trailing = String(appLoc: "持续 \(formatDuration(s.duration))")
} else {
subtitle = String(appLoc: "症状 · 已结束")
trailing = String(appLoc: "持续 \(formatDuration(s.duration))")
}
return TimelineEntry(
id: "symptom-\(s.persistentModelID)",
kind: .symptom,
date: date,
title: s.name,
subtitle: subtitle,
trailing: trailing,
trailingIsAlert: ongoing,
isOngoing: ongoing
)
}
private static func typeSubtitle(for i: Indicator) -> String {
if let report = i.report {
return String(appLoc: "指标 · \(report.title)")
}
return i.source.label
}
private static func indicatorValue(_ i: Indicator) -> String {
let unit = i.unit.isEmpty ? "" : " \(i.unit)"
let arrow: String
switch i.status {
case .high: arrow = ""
case .low: arrow = ""
case .normal: arrow = ""
}
return "\(i.value)\(unit)\(arrow)"
}
}
private extension String {
func firstLine() -> String {
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
if let line = trimmed.split(whereSeparator: \.isNewline).first {
let s = String(line)
return s.count > 40 ? String(s.prefix(40)) + "" : s
}
return trimmed.isEmpty ? String(appLoc: "(空日记)") : trimmed
}
}