- TimelineRow: 时间线条目单行视图 - DateSection + TimelineGrouping: 今日/昨日/本周/更早分组 - DiaryQuickSheet: 文字日记快速记录入口 - TimelineGroupingTests: 分组逻辑烟测 - SymptomEndSheet / RootView: 配套微调
103 lines
3.5 KiB
Swift
103 lines
3.5 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct DiaryQuickSheet: View {
|
|
@Environment(\.modelContext) private var ctx
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
@State private var content: String = ""
|
|
@State private var createdAt: Date = .now
|
|
|
|
private var canSubmit: Bool {
|
|
!content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
Capsule()
|
|
.fill(Tj.Palette.line)
|
|
.frame(width: 40, height: 4)
|
|
.padding(.top, 10)
|
|
.padding(.bottom, 14)
|
|
|
|
HStack {
|
|
Text("写日记")
|
|
.font(.tjH2())
|
|
.foregroundStyle(Tj.Palette.text)
|
|
Spacer()
|
|
Text("本机保存")
|
|
.font(.system(size: 12))
|
|
.foregroundStyle(Tj.Palette.text3)
|
|
}
|
|
.padding(.horizontal, 20)
|
|
.padding(.bottom, 16)
|
|
|
|
VStack(alignment: .leading, spacing: 16) {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
sectionLabel("内容")
|
|
TextField("今天怎么样?", text: $content, axis: .vertical)
|
|
.lineLimit(4...10)
|
|
.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)
|
|
)
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
sectionLabel("时间")
|
|
DatePicker("", selection: $createdAt, in: ...Date.now)
|
|
.datePickerStyle(.compact)
|
|
.labelsHidden()
|
|
}
|
|
}
|
|
.padding(.horizontal, 20)
|
|
|
|
Spacer(minLength: 12)
|
|
|
|
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
|
|
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.xl, style: .continuous))
|
|
.ignoresSafeArea(edges: .bottom)
|
|
)
|
|
.presentationDetents([.medium, .large])
|
|
.presentationDragIndicator(.hidden)
|
|
.presentationBackground(Tj.Palette.sand)
|
|
.presentationCornerRadius(Tj.Radius.xl)
|
|
}
|
|
|
|
private func sectionLabel(_ text: String) -> some View {
|
|
Text(text)
|
|
.font(.system(size: 12, weight: .semibold))
|
|
.tracking(0.3)
|
|
.foregroundStyle(Tj.Palette.text2)
|
|
}
|
|
|
|
private func submit() {
|
|
guard canSubmit else { return }
|
|
let entry = DiaryEntry(
|
|
content: content.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
createdAt: createdAt
|
|
)
|
|
ctx.insert(entry)
|
|
try? ctx.save()
|
|
dismiss()
|
|
}
|
|
}
|