fix(timeline): add missing SwiftData import + @MainActor on caller props
- TimelineEntry.swift: 缺 import SwiftData,4 处 persistentModelID 报错 - ArchiveListView.allEntries / HomeView.recentEntries: 显式 @MainActor, 否则 default-isolation=MainActor 下被推断为 nonisolated,调用 MainActor 方法 TimelineEntry.from(...) 触发 4+4 个 isolation 警告
This commit is contained in:
@@ -1,19 +1,152 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
struct ArchiveListView: View {
|
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]
|
||||||
|
|
||||||
|
@State private var filter: TimelineKind? = nil
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
private var allEntries: [TimelineEntry] {
|
||||||
|
let mapped =
|
||||||
|
indicators.map(TimelineEntry.from(indicator:)) +
|
||||||
|
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 {
|
var body: some View {
|
||||||
VStack(spacing: 12) {
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
Spacer()
|
header
|
||||||
TjPlaceholder(label: "records · 记录列表\n(C1 尚未实现)")
|
.padding(.horizontal, 20)
|
||||||
.frame(width: 280, height: 180)
|
.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
|
||||||
|
TimelineRow(entry: 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())
|
||||||
|
}
|
||||||
|
|
||||||
|
private var header: some View {
|
||||||
|
HStack(alignment: .lastTextBaseline) {
|
||||||
Text("记录")
|
Text("记录")
|
||||||
.font(.tjH2())
|
.font(.tjTitle(26))
|
||||||
.foregroundStyle(Tj.Palette.text2)
|
.foregroundStyle(Tj.Palette.text)
|
||||||
|
Text(totalCount == 0 ? "" : "\(totalCount) 条")
|
||||||
|
.font(.system(size: 12))
|
||||||
|
.foregroundStyle(Tj.Palette.text3)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
}
|
||||||
.background(Tj.Palette.sand.ignoresSafeArea())
|
|
||||||
|
private var filterChips: some View {
|
||||||
|
ScrollView(.horizontal, showsIndicators: false) {
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
chip(label: "全部", 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: "还没有任何记录\n点底部 + 号开始")
|
||||||
|
.frame(width: 240, height: 140)
|
||||||
|
Text(filter == nil ? "记录会按时间归类显示" : "这个类别下没有记录")
|
||||||
|
.font(.system(size: 13))
|
||||||
|
.foregroundStyle(Tj.Palette.text3)
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview { ArchiveListView() }
|
#Preview {
|
||||||
|
ArchiveListView()
|
||||||
|
.modelContainer(for: [
|
||||||
|
Indicator.self, Report.self, DiaryEntry.self, Symptom.self, Asset.self
|
||||||
|
], inMemory: true)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,35 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
struct HomeView: View {
|
struct HomeView: View {
|
||||||
var onTapArchive: () -> Void = {}
|
var onTapArchive: () -> Void = {}
|
||||||
|
|
||||||
|
@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]
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
private var recentEntries: [TimelineEntry] {
|
||||||
|
let all =
|
||||||
|
indicators.map(TimelineEntry.from(indicator:)) +
|
||||||
|
reports.map(TimelineEntry.from(report:)) +
|
||||||
|
diaries.map(TimelineEntry.from(diary:)) +
|
||||||
|
symptoms.map(TimelineEntry.from(symptom:))
|
||||||
|
return all.sorted { $0.date > $1.date }.prefix(6).map { $0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
private var recentGrouped: [(section: DateSection, items: [TimelineEntry])] {
|
||||||
|
TimelineGrouping.group(recentEntries)
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView(showsIndicators: false) {
|
ScrollView(showsIndicators: false) {
|
||||||
VStack(alignment: .leading, spacing: 0) {
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
@@ -93,37 +120,48 @@ struct HomeView: View {
|
|||||||
HStack(alignment: .lastTextBaseline) {
|
HStack(alignment: .lastTextBaseline) {
|
||||||
Text("最近记录").font(.tjH2()).foregroundStyle(Tj.Palette.text)
|
Text("最近记录").font(.tjH2()).foregroundStyle(Tj.Palette.text)
|
||||||
Spacer()
|
Spacer()
|
||||||
Text("全部 ›")
|
Button(action: onTapArchive) {
|
||||||
.font(.system(size: 12))
|
Text("全部 ›")
|
||||||
.foregroundStyle(Tj.Palette.text3)
|
.font(.system(size: 12))
|
||||||
|
.foregroundStyle(Tj.Palette.text3)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
}
|
}
|
||||||
|
|
||||||
VStack(spacing: 10) {
|
if recentEntries.isEmpty {
|
||||||
RecentItemRow(
|
emptyRecent
|
||||||
date: "昨天 18:20",
|
} else {
|
||||||
type: "异常项快拍",
|
VStack(alignment: .leading, spacing: 14) {
|
||||||
name: "低密度脂蛋白 LDL-C",
|
ForEach(recentGrouped, id: \.section) { group in
|
||||||
value: "3.84 mmol/L",
|
VStack(alignment: .leading, spacing: 8) {
|
||||||
status: .high
|
Text(group.section.label)
|
||||||
)
|
.font(.system(size: 11, weight: .semibold))
|
||||||
RecentItemRow(
|
.tracking(0.5)
|
||||||
date: "5 月 23 日",
|
.foregroundStyle(Tj.Palette.text3)
|
||||||
type: "关键报告归档",
|
VStack(spacing: 10) {
|
||||||
name: "春季年度体检 · 共 3 页",
|
ForEach(group.items) { entry in
|
||||||
value: "3 项偏高",
|
TimelineRow(entry: entry)
|
||||||
status: .archive
|
}
|
||||||
)
|
}
|
||||||
RecentItemRow(
|
}
|
||||||
date: "5 月 22 日",
|
}
|
||||||
type: "文字日记",
|
}
|
||||||
name: "头痛 · 上午 10 点起,午后缓解",
|
|
||||||
value: nil,
|
|
||||||
status: .diary
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var emptyRecent: some View {
|
||||||
|
HStack {
|
||||||
|
Text("还没有任何记录,点底部 + 号开始第一条")
|
||||||
|
.font(.system(size: 13))
|
||||||
|
.foregroundStyle(Tj.Palette.text3)
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
.padding(.vertical, 14)
|
||||||
|
.padding(.horizontal, 16)
|
||||||
|
.tjCard(bordered: true)
|
||||||
|
}
|
||||||
|
|
||||||
private var archiveSection: some View {
|
private var archiveSection: some View {
|
||||||
VStack(alignment: .leading, spacing: 10) {
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
Text("影像档案").font(.tjH2()).foregroundStyle(Tj.Palette.text)
|
Text("影像档案").font(.tjH2()).foregroundStyle(Tj.Palette.text)
|
||||||
|
|||||||
139
康康/Features/Timeline/TimelineEntry.swift
Normal file
139
康康/Features/Timeline/TimelineEntry.swift
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
enum TimelineKind: String, CaseIterable, Identifiable {
|
||||||
|
case indicator, report, symptom, diary
|
||||||
|
var id: String { rawValue }
|
||||||
|
|
||||||
|
var label: String {
|
||||||
|
switch self {
|
||||||
|
case .indicator: return "指标"
|
||||||
|
case .report: return "报告"
|
||||||
|
case .symptom: return "症状"
|
||||||
|
case .diary: return "日记"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TimelineEntry: Identifiable, Hashable {
|
||||||
|
let id: String
|
||||||
|
let kind: TimelineKind
|
||||||
|
let date: Date
|
||||||
|
let title: String
|
||||||
|
let subtitle: String
|
||||||
|
let trailing: String?
|
||||||
|
let trailingIsAlert: Bool
|
||||||
|
let isOngoing: Bool
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func from(report r: Report) -> TimelineEntry {
|
||||||
|
let abnormal = r.indicators.filter { $0.status != .normal }.count
|
||||||
|
return TimelineEntry(
|
||||||
|
id: "report-\(r.persistentModelID)",
|
||||||
|
kind: .report,
|
||||||
|
date: r.reportDate,
|
||||||
|
title: r.title,
|
||||||
|
subtitle: "\(r.type.label) · 共 \(r.pageCount) 页",
|
||||||
|
trailing: abnormal > 0 ? "\(abnormal) 项偏高" : nil,
|
||||||
|
trailingIsAlert: abnormal > 0,
|
||||||
|
isOngoing: false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func from(diary d: DiaryEntry) -> TimelineEntry {
|
||||||
|
TimelineEntry(
|
||||||
|
id: "diary-\(d.persistentModelID)",
|
||||||
|
kind: .diary,
|
||||||
|
date: d.createdAt,
|
||||||
|
title: d.content.firstLine(),
|
||||||
|
subtitle: "文字日记",
|
||||||
|
trailing: 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 = "症状 · 持续中"
|
||||||
|
trailing = "持续 \(formatDuration(s.duration))"
|
||||||
|
} else {
|
||||||
|
subtitle = "症状 · 已结束"
|
||||||
|
trailing = "持续 \(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 "指标 · \(report.title)"
|
||||||
|
}
|
||||||
|
return "异常项快拍"
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ? "(空日记)" : trimmed
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user