feat(symptom): add Symptom @Model + start/end sheets + ongoing card

- Symptom @Model with severity 1-5 clamp, isOngoing, duration helpers
- SymptomStartSheet / SymptomEndSheet / OngoingSymptomsCard
- RecordSheet 加 .symptom kind 入口
- RootView 增加 'records' tab + ArchiveListView placeholder
- HomeView 顶部加 OngoingSymptomsCard
- ModelsSchemaTests: 2 个 Symptom 烟测(ongoing predicate + severity clamp)

Note: Symptom 是 CLAUDE.md §10 清单外的新功能,由产品负责人决定加入。
ArchiveListView 仍是 placeholder,真实 C1 实现按计划在 W4。
This commit is contained in:
link2026
2026-05-25 23:18:21 +08:00
parent e4a68a1bdd
commit 46b69cf8e1
10 changed files with 643 additions and 41 deletions

View File

@@ -0,0 +1,117 @@
import SwiftUI
import SwiftData
import Combine
struct OngoingSymptomsCard: View {
@Query(filter: #Predicate<Symptom> { $0.endedAt == nil },
sort: \Symptom.startedAt, order: .reverse)
private var ongoing: [Symptom]
@State private var ending: Symptom?
@State private var tick: Date = .now
private let timer = Timer.publish(every: 60, on: .main, in: .common).autoconnect()
var body: some View {
if ongoing.isEmpty {
EmptyView()
} else {
VStack(alignment: .leading, spacing: 10) {
HStack(spacing: 8) {
Circle()
.fill(Tj.Palette.brick)
.frame(width: 7, height: 7)
Text("持续中")
.font(.tjH2())
.foregroundStyle(Tj.Palette.text)
Text("\(ongoing.count)")
.font(.system(size: 12))
.foregroundStyle(Tj.Palette.text3)
Spacer()
}
VStack(spacing: 8) {
ForEach(ongoing) { sym in
row(sym)
}
}
}
.onReceive(timer) { now in tick = now }
.sheet(item: $ending) { sym in
SymptomEndSheet(symptom: sym)
}
}
}
private func row(_ sym: Symptom) -> some View {
let interval = max(0, tick.timeIntervalSince(sym.startedAt))
let isLong = interval >= 3 * 24 * 3600
return HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 8) {
Text(sym.name)
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
severityDot(sym.severity)
}
Text("已持续 \(formatDuration(interval))")
.font(.system(size: 12))
.foregroundStyle(isLong ? Tj.Palette.brick : Tj.Palette.text3)
}
Spacer(minLength: 8)
Button {
ending = sym
} label: {
Text("结束")
.font(.system(size: 12, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.background(
Capsule().fill(Tj.Palette.sand2)
)
}
.buttonStyle(.plain)
}
.padding(.horizontal, 14)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.fill(Tj.Palette.paper)
.overlay(alignment: .leading) {
Rectangle()
.fill(severityColor(sym.severity))
.frame(width: 3)
.clipShape(
UnevenRoundedRectangle(
topLeadingRadius: Tj.Radius.sm,
bottomLeadingRadius: Tj.Radius.sm,
bottomTrailingRadius: 0,
topTrailingRadius: 0
)
)
}
)
.shadow(color: Color(red: 0.196, green: 0.157, blue: 0.098).opacity(0.04),
radius: 2, x: 0, y: 1)
}
private func severityDot(_ value: Int) -> some View {
HStack(spacing: 2) {
ForEach(1...5, id: \.self) { i in
Circle()
.fill(i <= value ? severityColor(value) : Tj.Palette.line)
.frame(width: 5, height: 5)
}
}
}
private func severityColor(_ value: Int) -> Color {
switch value {
case 1, 2: return Tj.Palette.leaf
case 3: return Tj.Palette.amber
default: return Tj.Palette.brick
}
}
}

View File

@@ -0,0 +1,119 @@
import SwiftUI
import SwiftData
struct SymptomEndSheet: View {
@Environment(\.modelContext) private var ctx
@Environment(\.dismiss) private var dismiss
let symptom: Symptom
@State private var endedAt: Date = .now
private var lowerBound: Date { symptom.startedAt }
private var durationLabel: String {
let interval = max(0, endedAt.timeIntervalSince(lowerBound))
return formatDuration(interval)
}
var body: some View {
VStack(spacing: 0) {
Capsule()
.fill(Tj.Palette.line)
.frame(width: 40, height: 4)
.padding(.top, 10)
.padding(.bottom, 14)
VStack(alignment: .leading, spacing: 18) {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text("结束症状")
.font(.system(size: 12, weight: .semibold))
.tracking(0.3)
.foregroundStyle(Tj.Palette.text3)
Text(symptom.name)
.font(.tjTitle(24))
.foregroundStyle(Tj.Palette.text)
}
Spacer()
}
VStack(alignment: .leading, spacing: 6) {
Text("开始于")
.font(.system(size: 12))
.foregroundStyle(Tj.Palette.text3)
Text(symptom.startedAt.formatted(date: .abbreviated, time: .shortened))
.font(.system(size: 14, weight: .medium))
.foregroundStyle(Tj.Palette.text)
}
VStack(alignment: .leading, spacing: 8) {
Text("结束时间")
.font(.system(size: 12, weight: .semibold))
.tracking(0.3)
.foregroundStyle(Tj.Palette.text2)
DatePicker("", selection: $endedAt, in: lowerBound...Date.now)
.datePickerStyle(.compact)
.labelsHidden()
}
HStack {
Text("本次持续")
.font(.system(size: 13))
.foregroundStyle(Tj.Palette.text3)
Spacer()
Text(durationLabel)
.font(.system(size: 15, weight: .semibold, design: .monospaced))
.foregroundStyle(Tj.Palette.brick)
}
.padding(.horizontal, 14)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.fill(Tj.Palette.paper)
)
Spacer(minLength: 8)
}
.padding(.horizontal, 20)
HStack(spacing: 12) {
Button("取消") { dismiss() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 15, horizontalPadding: 18))
Button("结束并保存") { submit() }
.buttonStyle(TjPrimaryButton(height: 44, fontSize: 15, horizontalPadding: 18))
}
.padding(.horizontal, 20)
.padding(.vertical, 14)
}
.background(
Tj.Palette.sand
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.xl, style: .continuous))
.ignoresSafeArea(edges: .bottom)
)
.presentationDetents([.medium])
.presentationDragIndicator(.hidden)
.presentationBackground(Tj.Palette.sand)
.presentationCornerRadius(Tj.Radius.xl)
}
private func submit() {
symptom.endedAt = max(endedAt, symptom.startedAt)
try? ctx.save()
dismiss()
}
}
func formatDuration(_ interval: TimeInterval) -> String {
let totalMinutes = Int(interval / 60)
let days = totalMinutes / (60 * 24)
let hours = (totalMinutes % (60 * 24)) / 60
let minutes = totalMinutes % 60
if days > 0 && hours > 0 { return "\(days)\(hours) 小时" }
if days > 0 { return "\(days)" }
if hours > 0 && minutes > 0 { return "\(hours) 小时 \(minutes)" }
if hours > 0 { return "\(hours) 小时" }
if minutes > 0 { return "\(minutes) 分钟" }
return "刚刚"
}

View File

@@ -0,0 +1,231 @@
import SwiftUI
import SwiftData
private let symptomPresets: [String] = [
"头痛", "咳嗽", "腹痛", "发烧",
"恶心", "失眠", "疲劳", "关节痛"
]
struct SymptomStartSheet: View {
@Environment(\.modelContext) private var ctx
@Environment(\.dismiss) private var dismiss
@State private var name: String = ""
@State private var customName: String = ""
@State private var startedAt: Date = .now
@State private var severity: Double = 3
@State private var note: String = ""
private var resolvedName: String {
let trimmed = customName.trimmingCharacters(in: .whitespaces)
return trimmed.isEmpty ? name : trimmed
}
private var canSubmit: Bool { !resolvedName.isEmpty }
var body: some View {
VStack(spacing: 0) {
handle
header
ScrollView(showsIndicators: false) {
VStack(alignment: .leading, spacing: 18) {
presetSection
customSection
timeSection
severitySection
noteSection
}
.padding(.horizontal, 20)
.padding(.bottom, 20)
}
footer
}
.background(
Tj.Palette.sand
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.xl, style: .continuous))
.ignoresSafeArea(edges: .bottom)
)
.presentationDetents([.large])
.presentationDragIndicator(.hidden)
.presentationBackground(Tj.Palette.sand)
.presentationCornerRadius(Tj.Radius.xl)
}
private var handle: some View {
Capsule()
.fill(Tj.Palette.line)
.frame(width: 40, height: 4)
.padding(.top, 10)
.padding(.bottom, 14)
}
private var header: some View {
HStack {
Text("症状开始")
.font(.tjH2())
.foregroundStyle(Tj.Palette.text)
Spacer()
Text("结束时再来点结束")
.font(.system(size: 12))
.foregroundStyle(Tj.Palette.text3)
}
.padding(.horizontal, 20)
.padding(.bottom, 16)
}
private var presetSection: some View {
VStack(alignment: .leading, spacing: 8) {
sectionLabel("常见症状")
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
ForEach(symptomPresets, id: \.self) { item in
chip(item, selected: name == item) {
name = item
customName = ""
}
}
}
}
}
}
private var customSection: some View {
VStack(alignment: .leading, spacing: 8) {
sectionLabel("或者自己写")
TextField("例如:眼皮跳", text: $customName)
.textInputAutocapitalization(.never)
.padding(.horizontal, 14)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.fill(Tj.Palette.paper)
)
.overlay(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.strokeBorder(Tj.Palette.line, lineWidth: 1)
)
.onChange(of: customName) { _, newValue in
if !newValue.trimmingCharacters(in: .whitespaces).isEmpty {
name = ""
}
}
}
}
private var timeSection: some View {
VStack(alignment: .leading, spacing: 8) {
sectionLabel("开始时间")
DatePicker("", selection: $startedAt, in: ...Date.now)
.datePickerStyle(.compact)
.labelsHidden()
}
}
private var severitySection: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
sectionLabel("强度")
Spacer()
Text("\(Int(severity)) / 5")
.font(.system(size: 13, weight: .semibold, design: .monospaced))
.foregroundStyle(severityColor)
}
Slider(value: $severity, in: 1...5, step: 1)
.tint(severityColor)
HStack {
Text("轻微").font(.system(size: 11)).foregroundStyle(Tj.Palette.text3)
Spacer()
Text("剧烈").font(.system(size: 11)).foregroundStyle(Tj.Palette.text3)
}
}
}
private var noteSection: some View {
VStack(alignment: .leading, spacing: 8) {
sectionLabel("备注(可选)")
TextField("位置、可能诱因…", text: $note, axis: .vertical)
.lineLimit(2...4)
.padding(.horizontal, 14)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.fill(Tj.Palette.paper)
)
.overlay(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.strokeBorder(Tj.Palette.line, lineWidth: 1)
)
}
}
private var footer: some View {
HStack(spacing: 12) {
Button("取消") { dismiss() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 15, horizontalPadding: 18))
Button("开始记录") { submit() }
.buttonStyle(TjPrimaryButton(height: 44, fontSize: 15, horizontalPadding: 18))
.disabled(!canSubmit)
.opacity(canSubmit ? 1 : 0.4)
}
.padding(.horizontal, 20)
.padding(.vertical, 14)
.background(
Tj.Palette.sand
.overlay(alignment: .top) {
Rectangle().fill(Tj.Palette.lineSoft).frame(height: 1)
}
)
}
private var severityColor: Color {
switch Int(severity) {
case 1, 2: return Tj.Palette.leaf
case 3: return Tj.Palette.amber
default: return Tj.Palette.brick
}
}
private func sectionLabel(_ text: String) -> some View {
Text(text)
.font(.system(size: 12, weight: .semibold))
.tracking(0.3)
.foregroundStyle(Tj.Palette.text2)
}
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 submit() {
guard canSubmit else { return }
let symptom = Symptom(
name: resolvedName,
startedAt: startedAt,
note: note.trimmingCharacters(in: .whitespaces).isEmpty ? nil : note,
severity: Int(severity)
)
ctx.insert(symptom)
try? ctx.save()
dismiss()
}
}
#Preview {
SymptomStartSheet()
.modelContainer(for: Symptom.self, inMemory: true)
}