feat: 添加拍药盒功能和语音直达入口

- 实现拍药盒扫描流程,支持本地OCR识别药品信息
- 在日记页面添加拍药盒和记症状的三选一入口
- 优化按钮点击区域,确保符合苹果HIG最小命中区标准
- 添加用药记录到时间线的独立分类显示
- 实现长按+号语音直达功能,支持语音意图分类跳转
- 更新项目配置文件,启用代码分析和死代码剥离选项
- 增加多项本地化字符串支持新功能
```
This commit is contained in:
link2026
2026-06-13 09:16:25 +08:00
parent f58d6064ba
commit 6c6a950140
30 changed files with 1856 additions and 64 deletions

View File

@@ -32,8 +32,14 @@ struct PhotoPickerSheet: View {
.clipShape(Capsule())
}
Button("取消", action: onCancel)
.foregroundStyle(Tj.Palette.text3)
Button(action: onCancel) {
Text("取消")
.foregroundStyle(Tj.Palette.text3)
.padding(.horizontal, 24)
.frame(minHeight: 44) // HIG
.contentShape(Rectangle())
}
.buttonStyle(.plain)
if loading {
ProgressView().tint(Tj.Palette.ink)

View File

@@ -362,10 +362,16 @@ private struct AnalyzingView: View {
.foregroundStyle(Tj.Palette.amber)
}
}
Button("取消识别 · 改为手动录入", action: onCancel)
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(Tj.Palette.text3)
.padding(.top, 4)
Button(action: onCancel) {
Text("取消识别 · 改为手动录入")
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(Tj.Palette.text3)
.padding(.horizontal, 20)
.frame(minHeight: 44) // HIG
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.padding(.top, 4)
Spacer()
}
.padding(.horizontal, 20)

View File

@@ -11,6 +11,10 @@ struct DiaryQuickSheet: View {
@State private var content: String = ""
@State private var createdAt: Date = .now
/// :,tag
@State private var showMedicationScan = false
/// : SymptomStartSheet(/,)
@State private var showSymptomStart = false
/// AI
enum AssistPhase {
@@ -92,6 +96,24 @@ struct DiaryQuickSheet: View {
.foregroundStyle(Tj.Palette.text3)
}
.padding(.horizontal, 20)
.padding(.bottom, 10)
// :()/ ()/ (SymptomStartSheet)
HStack(spacing: 10) {
modeCard(icon: "pencil", title: String(appLoc: "写日记"),
subtitle: String(appLoc: "文字或语音"), active: true) {
contentFocused = true
}
modeCard(icon: "pills.fill", title: String(appLoc: "拍药盒"),
subtitle: String(appLoc: "识别用药"), active: false) {
showMedicationScan = true
}
modeCard(icon: "waveform.path.ecg", title: String(appLoc: "记症状"),
subtitle: String(appLoc: "持续追踪"), active: false) {
showSymptomStart = true
}
}
.padding(.horizontal, 20)
.padding(.bottom, 14)
ScrollViewReader { proxy in
@@ -228,6 +250,20 @@ struct DiaryQuickSheet: View {
.presentationDragIndicator(.hidden)
.presentationBackground(Tj.Palette.sand)
.presentationCornerRadius(Tj.Radius.xl)
.fullScreenCover(isPresented: $showMedicationScan) {
MedicationScanFlow(
onSave: { entries in
// :(线)+ ·
MedicationArchiver.archive(entries: entries, in: ctx)
dismiss()
},
onClose: { showMedicationScan = false }
)
}
.sheet(isPresented: $showSymptomStart) {
// sheet:/;,
SymptomStartSheet()
}
.onDisappear {
suggestTask?.cancel()
voiceFlowTask?.cancel()
@@ -555,6 +591,41 @@ struct DiaryQuickSheet: View {
.foregroundStyle(Tj.Palette.text2)
}
/// ( / / )active
/// : iPhone
private func modeCard(icon: String, title: String, subtitle: String,
active: Bool, action: @escaping () -> Void) -> some View {
Button(action: action) {
VStack(spacing: 5) {
Image(systemName: icon)
.font(.tjScaled( 15, weight: .medium))
.foregroundStyle(active ? Tj.Palette.paper : Tj.Palette.ink)
.frame(width: 28, height: 28)
.background(Circle().fill(active ? Tj.Palette.ink : Tj.Palette.sand2))
Text(title)
.font(.tjScaled( 13, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
Text(subtitle)
.font(.tjScaled( 10))
.foregroundStyle(Tj.Palette.text3)
.lineLimit(1)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 10)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.fill(Tj.Palette.paper)
)
.overlay(
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
.strokeBorder(active ? Tj.Palette.ink : Tj.Palette.line,
lineWidth: active ? 1.5 : 1)
)
.contentShape(RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous))
}
.buttonStyle(.plain)
}
// MARK:
private func startVoice() {

View File

@@ -0,0 +1,285 @@
import SwiftUI
import SwiftData
import UIKit
/// :/ Vision OCR LLM
/// :+ · · · ·
/// `MedicationArchiver`:(线)+
/// ,/(§1)
///
/// ( QuickRegionCaptureFlow ):
/// ```
/// idle(/) recognizing(OCR + LLM) confirm() onSave
/// / confirm( + ,)
/// ```
struct MedicationScanFlow: View {
/// (, " 80mg · ")
let onSave: ([String]) -> Void
let onClose: () -> Void
@State private var phase: Phase = .idle
/// :,
@State private var recognitionTask: Task<Void, Never>?
enum Phase {
case idle
case recognizing(image: UIImage)
case confirm(items: [EditableMedication], warning: String?)
}
struct EditableMedication: Identifiable {
let id = UUID()
var name: String
var strength: String
var usage: String
var include: Bool = true
}
var body: some View {
content
.background(Tj.Palette.sand.ignoresSafeArea())
}
@ViewBuilder
private var content: some View {
switch phase {
case .idle:
// ignoresSafeArea:,
captureEntry
case .recognizing(let image):
recognizingView(image: image)
case .confirm(let items, let warning):
NavigationStack {
MedicationConfirmView(
items: items,
warning: warning,
onSave: { saveItems($0) },
onRetake: { phase = .idle }
)
.navigationTitle("核对药品")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button("取消") { onClose() }
.foregroundStyle(Tj.Palette.text)
}
}
}
}
}
// MARK: - :()/ ()
@ViewBuilder
private var captureEntry: some View {
#if targetEnvironment(simulator)
PhotoPickerSheet(
onFinish: { images in
if let first = images.first { startRecognition(first) } else { onClose() }
},
onCancel: onClose
)
#else
SingleShotCameraView(
onCapture: { startRecognition($0) },
onCancel: onClose
)
#endif
}
private func recognizingView(image: UIImage) -> some View {
VStack(spacing: 18) {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(maxHeight: 320)
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous))
.padding(.horizontal, 24)
ProgressView().tint(Tj.Palette.ink)
Text("正在本地识别药品…")
.font(.tjScaled(14))
.foregroundStyle(Tj.Palette.text2)
Text("照片与文字均不离开设备")
.font(.tjScaled(12))
.foregroundStyle(Tj.Palette.text3)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
// 退,(§3.2 )
.overlay(alignment: .topLeading) {
Button {
recognitionTask?.cancel()
onClose()
} label: {
Text("取消")
.font(.tjScaled( 16, weight: .medium))
.foregroundStyle(Tj.Palette.text)
.padding(.horizontal, 18)
.frame(minHeight: 44)
.background(Capsule().fill(Tj.Palette.paper))
.overlay(Capsule().strokeBorder(Tj.Palette.line, lineWidth: 1))
.contentShape(Capsule())
}
.buttonStyle(.plain)
.padding(.leading, 16)
.padding(.top, 8)
}
}
// MARK: - ( OCR LLM )
private func startRecognition(_ image: UIImage) {
phase = .recognizing(image: image)
recognitionTask = Task {
let (items, warning) = await recognize(image)
guard !Task.isCancelled else { return } // : phase
await MainActor.run {
// :(§3.2 退线)
if items.isEmpty {
phase = .confirm(items: [EditableMedication(name: "", strength: "", usage: "")],
warning: warning ?? String(appLoc: "没读出药品,可以手动填写"))
} else {
phase = .confirm(items: items, warning: warning)
}
}
}
}
private func recognize(_ image: UIImage) async -> (items: [EditableMedication], warning: String?) {
do {
let text = try await OCRService.recognizeText(in: image)
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty {
return ([], String(appLoc: "没识别到文字,拍清楚一点再试"))
}
let parsed = try await MedicationScanService.shared.recognizeMedications(fromOCRText: trimmed)
let items = parsed.map {
EditableMedication(name: $0.name, strength: $0.strength, usage: $0.usage)
}
return (items, items.isEmpty ? String(appLoc: "没读出药品,可以手动填写") : nil)
} catch CaptureError.modelNotReady {
return ([], String(appLoc: "AI 模型未就绪,可以手动填写"))
} catch let CaptureError.parseFailed(msg) {
return ([], String(appLoc: "解析失败:\(msg)"))
} catch let CaptureError.inferenceFailed(msg) {
return ([], String(appLoc: "识别失败:\(msg)"))
} catch {
return ([], String(appLoc: "未知错误:\(error.localizedDescription)"))
}
}
// MARK: -
private func saveItems(_ items: [EditableMedication]) {
let entries = items
.filter { $0.include && !$0.name.trimmingCharacters(in: .whitespaces).isEmpty }
.map {
ParsedMedication(name: $0.name, strength: $0.strength, usage: $0.usage).entryText
}
onSave(entries)
onClose()
}
}
// MARK: - (MainActor,SwiftData View ctx ,§3.1)
/// ,:
/// 1. tag DiaryEntry 线
/// 2. UserProfile.currentMedications() AI / prompt
@MainActor
enum MedicationArchiver {
static func archive(entries: [String], in ctx: ModelContext) {
guard !entries.isEmpty else { return }
let diary = DiaryEntry(content: entries.joined(separator: "\n"),
tags: [DiaryEntry.medicationTag])
ctx.insert(diary)
let profile = UserProfileStore.loadOrCreate(in: ctx)
for entry in entries where !profile.currentMedications.contains(entry) {
profile.currentMedications.append(entry)
}
profile.updatedAt = .now
try? ctx.save()
}
}
// MARK: -
private struct MedicationConfirmView: View {
@State var items: [MedicationScanFlow.EditableMedication]
let warning: String?
let onSave: ([MedicationScanFlow.EditableMedication]) -> Void
let onRetake: () -> Void
private var canSave: Bool {
items.contains {
$0.include && !$0.name.trimmingCharacters(in: .whitespaces).isEmpty
}
}
var body: some View {
VStack(spacing: 0) {
Form {
if let warning {
Section {
Label(warning, systemImage: "exclamationmark.triangle")
.font(.tjScaled(13))
.foregroundStyle(Tj.Palette.amber)
}
}
ForEach($items) { $item in
Section {
HStack {
TextField(String(appLoc: "药品名,如:缬沙坦胶囊"), text: $item.name)
.foregroundStyle(Tj.Palette.text)
Toggle("", isOn: $item.include)
.labelsHidden()
.tint(Tj.Palette.ink)
}
TextField(String(appLoc: "规格,如:80mg×7粒"), text: $item.strength)
.foregroundStyle(Tj.Palette.text2)
TextField(String(appLoc: "用法,如:一日一次,一次一粒"), text: $item.usage)
.foregroundStyle(Tj.Palette.text2)
}
}
Section {
Button {
items.append(.init(name: "", strength: "", usage: ""))
} label: {
Label("再加一种", systemImage: "plus.circle")
.foregroundStyle(Tj.Palette.ink)
}
Button {
onRetake()
} label: {
Label("重拍", systemImage: "camera")
.foregroundStyle(Tj.Palette.ink)
}
} footer: {
Text("将记入健康日记(记录页可查),并同步到「当前用药」供 AI 解读参考。不提供任何用药建议。")
}
}
.scrollContentBackground(.hidden)
Button {
onSave(items)
} label: {
Text("保存用药记录")
.frame(maxWidth: .infinity)
}
.buttonStyle(TjPrimaryButton())
.disabled(!canSave)
.opacity(canSave ? 1 : 0.4)
.padding(.horizontal, 18)
.padding(.bottom, 12)
}
.background(Tj.Palette.sand.ignoresSafeArea())
}
}
#Preview {
MedicationScanFlow(onSave: { print($0) }, onClose: {})
}

View File

@@ -38,6 +38,7 @@ private struct ProfileEditForm: View {
@State private var healthImportDraft: HealthProfileImportDraft?
@State private var healthImportError: String?
@State private var isImportingHealthProfile = false
@State private var showMedicationScan = false
var body: some View {
Form {
@@ -88,7 +89,8 @@ private struct ProfileEditForm: View {
StringListSection(title: String(appLoc: "家族史"), placeholder: String(appLoc: "如:母亲 高血压"),
items: $profile.familyHistory)
StringListSection(title: String(appLoc: "当前用药"), placeholder: String(appLoc: "如:缬沙坦 80mg qd"),
items: $profile.currentMedications)
items: $profile.currentMedications,
onScan: { showMedicationScan = true })
}
.navigationTitle("个人资料")
.navigationBarTitleDisplayMode(.inline)
@@ -98,6 +100,16 @@ private struct ProfileEditForm: View {
profile.updatedAt = .now
try? ctx.save()
}
.fullScreenCover(isPresented: $showMedicationScan) {
// OCR + LLM :
// (线)+ ()
MedicationScanFlow(
onSave: { entries in
MedicationArchiver.archive(entries: entries, in: ctx)
},
onClose: { showMedicationScan = false }
)
}
.sheet(item: $healthImportDraft) { draft in
HealthProfileImportPreviewSheet(
draft: draft,
@@ -456,10 +468,27 @@ private struct StringListSection: View {
let title: String
let placeholder: String
@Binding var items: [String]
/// nil ()
var onScan: (() -> Void)? = nil
@State private var newInput = ""
var body: some View {
Section(title) {
if let onScan {
Button(action: onScan) {
HStack(spacing: 10) {
Image(systemName: "camera.viewfinder")
.foregroundStyle(Tj.Palette.ink)
VStack(alignment: .leading, spacing: 2) {
Text("拍药盒自动识别")
.foregroundStyle(Tj.Palette.text)
Text("拍药盒或说明书,本地识别药名与规格")
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
}
}
}
}
ForEach(items, id: \.self) { item in
HStack {
Text(item)

View File

@@ -32,8 +32,9 @@ struct QuickRegionCaptureFlow: View {
private var content: some View {
switch phase {
case .idle:
// ignoresSafeArea:/,
// ,
captureEntry
.ignoresSafeArea()
case .adjust(let image):
RegionAdjustView(
@@ -45,7 +46,6 @@ struct QuickRegionCaptureFlow: View {
onRetake: { phase = .idle },
onCancel: { onClose() }
)
.ignoresSafeArea()
case .confirm(let image, let items, let warning):
NavigationStack {

View File

@@ -50,7 +50,11 @@ struct RegionAdjustView: View {
Text("取消")
.font(.tjScaled( 16, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 12)
.frame(minWidth: 60, minHeight: 44) // HIG ,
.contentShape(Rectangle())
}
.buttonStyle(.plain)
Spacer()
Text("框住异常指标")
.font(.tjScaled( 16, weight: .semibold))
@@ -63,10 +67,14 @@ struct RegionAdjustView: View {
Text("重拍")
.font(.tjScaled( 16, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 12)
.frame(minWidth: 60, minHeight: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
.padding(.horizontal, 18)
.padding(.vertical, 12)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Color.black)
}

View File

@@ -49,13 +49,15 @@ struct SingleShotCameraView: View {
Text("取消")
.font(.tjScaled( 16, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.padding(.horizontal, 18)
.frame(minHeight: 44) // HIG
.background(Capsule().fill(.black.opacity(0.35)))
.contentShape(Capsule())
}
.buttonStyle(.plain)
Spacer()
}
.padding(.horizontal, 18)
.padding(.horizontal, 16)
.padding(.top, 8)
Spacer()

View File

@@ -5,8 +5,15 @@ enum RecordKind: String, Identifiable, CaseIterable {
var id: String { rawValue }
/// RecordSheet () enum ,
/// :`.quick`() `.indicator`(),
static let displayOrder: [RecordKind] = [.diary, .reminder, .symptom, .indicator, .healthExport, .archive]
/// :`.quick`() `.indicator`();
/// `.symptom`() `.diary`(),
static let displayOrder: [RecordKind] = [.diary, .reminder, .indicator, .healthExport, .archive]
/// pill( subtitle,"/")
/// :,( ProfileEditView presets )
static var diaryFeaturePills: [String] {
[String(appLoc: "写日记"), String(appLoc: "拍药盒"), String(appLoc: "记症状")]
}
var title: String {
switch self {
@@ -25,7 +32,7 @@ enum RecordKind: String, Identifiable, CaseIterable {
case .indicator: return String(appLoc: "手动填写,或拍照自动识别")
case .healthExport: return String(appLoc: "多轮问答后生成给医生看的整理报告")
case .archive: return String(appLoc: "完整保存整份报告(可多页)")
case .diary: return String(appLoc: "记录身体状态、用药、感受 · 可让 AI 辅助")
case .diary: return String(appLoc: "写日记或拍药盒记录用药 · 可让 AI 辅助")
case .symptom: return String(appLoc: "开始一个持续症状,结束时再点结束")
case .reminder: return String(appLoc: "管理用药、复查、监测的周期提醒")
}
@@ -93,13 +100,27 @@ struct RecordSheet: View {
}
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 2) {
VStack(alignment: .leading, spacing: 3) {
Text(kind.title)
.font(.tjScaled( 15, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
Text(kind.subtitle)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
if kind == .diary {
// :/, pill
HStack(spacing: 5) {
ForEach(RecordKind.diaryFeaturePills, id: \.self) { pill in
Text(pill)
.font(.tjScaled( 10, weight: .medium))
.foregroundStyle(Tj.Palette.ink)
.padding(.horizontal, 7)
.padding(.vertical, 2)
.background(Capsule().fill(Tj.Palette.sand2))
}
}
} else {
Text(kind.subtitle)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
}
}
Spacer()
Image(systemName: "chevron.right")
@@ -111,6 +132,17 @@ struct RecordSheet: View {
}
.buttonStyle(.plain)
}
// : + ,
HStack(spacing: 5) {
Image(systemName: "mic.fill")
.font(.tjScaled( 10))
Text("下次试试长按 + ,直接说出想记的内容")
.font(.tjScaled( 11))
}
.foregroundStyle(Tj.Palette.text3)
.frame(maxWidth: .infinity)
.padding(.top, 6)
}
.padding(.bottom, 22)
}

View File

@@ -0,0 +1,276 @@
import SwiftUI
import UIKit
/// + : (SpeechDictationService)
/// LLM (VoiceIntentService) RootView
///
/// :
/// ```
/// requesting() recording() classifying onResolve(intent)
/// denied / failed( / )
/// ```
/// : requiresOnDeviceRecognition, LLM
struct VoiceCommandSheet: View {
/// :RootView sheet
let onResolve: (VoiceIntent) -> Void
/// :(RecordSheet)
let onOpenMenu: () -> Void
@Environment(\.dismiss) private var dismiss
enum Phase: Equatable {
case requesting
case denied
case recording
case classifying
case failed(message: String)
}
@State private var phase: Phase = .requesting
@State private var transcript = ""
@State private var seconds = 0
/// @State ( DiaryQuickSheet ,)
@State private var dictation = SpeechDictationService()
@State private var ticker: Task<Void, Never>?
/// 20s :,
private let maxSeconds = 20
var body: some View {
VStack(spacing: 0) {
Capsule()
.fill(Tj.Palette.line)
.frame(width: 40, height: 4)
.padding(.top, 10)
.padding(.bottom, 16)
HStack {
VStack(alignment: .leading, spacing: 2) {
Text("说出想记的内容")
.font(.tjH2())
.foregroundStyle(Tj.Palette.text)
Text("比如:记一下血压 / 我头疼 / 拍个药盒")
.font(.tjScaled( 11))
.foregroundStyle(Tj.Palette.text3)
}
Spacer()
Text("全程本机")
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
}
.padding(.horizontal, 20)
.padding(.bottom, 16)
content
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.padding(.horizontal, 20)
buttons
.padding(.horizontal, 20)
.padding(.vertical, 14)
}
.background(
Tj.Palette.sand
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.xl, style: .continuous))
.ignoresSafeArea(edges: .bottom)
)
.presentationDetents([.fraction(0.5)])
.presentationDragIndicator(.hidden)
.presentationBackground(Tj.Palette.sand)
.presentationCornerRadius(Tj.Radius.xl)
.task { await begin() }
.onDisappear {
ticker?.cancel()
dictation.abort()
}
}
// MARK: -
@ViewBuilder
private var content: some View {
switch phase {
case .requesting:
ProgressView().tint(Tj.Palette.ink)
.frame(maxWidth: .infinity)
.padding(.top, 30)
case .denied:
VStack(spacing: 10) {
Image(systemName: "mic.slash")
.font(.tjScaled( 30))
.foregroundStyle(Tj.Palette.text3)
Text("需要麦克风与语音识别权限")
.font(.tjScaled( 14, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
Text("语音和文字都只在本机处理,不会上传。")
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
Button("前往设置") {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
.font(.tjScaled( 13, weight: .semibold))
.foregroundStyle(Tj.Palette.ink)
}
.frame(maxWidth: .infinity)
.padding(.top, 16)
case .recording:
VStack(spacing: 14) {
HStack(spacing: 8) {
Circle()
.fill(Tj.Palette.brick)
.frame(width: 8, height: 8)
Text("正在听 · \(seconds)s")
.font(.tjScaled( 12, weight: .semibold))
.foregroundStyle(Tj.Palette.brick)
}
transcriptBox(placeholder: String(appLoc: "请开口说话…"))
}
case .classifying:
VStack(spacing: 14) {
HStack(spacing: 8) {
ProgressView().tint(Tj.Palette.ink)
Text("正在理解…")
.font(.tjScaled( 12, weight: .semibold))
.foregroundStyle(Tj.Palette.text2)
}
transcriptBox(placeholder: "")
}
case .failed(let message):
VStack(spacing: 10) {
Image(systemName: "questionmark.bubble")
.font(.tjScaled( 28))
.foregroundStyle(Tj.Palette.text3)
Text(message)
.font(.tjScaled( 13))
.foregroundStyle(Tj.Palette.text2)
.multilineTextAlignment(.center)
if !transcript.isEmpty {
Text("\(transcript)")
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
.lineLimit(2)
}
}
.frame(maxWidth: .infinity)
.padding(.top, 12)
}
}
private func transcriptBox(placeholder: String) -> some View {
ScrollView(showsIndicators: false) {
Text(transcript.isEmpty ? placeholder : transcript)
.font(.tjScaled( 15))
.foregroundStyle(transcript.isEmpty ? Tj.Palette.text3 : Tj.Palette.text)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(minHeight: 64, maxHeight: 110)
.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)
)
}
// MARK: -
@ViewBuilder
private var buttons: some View {
switch phase {
case .recording:
HStack(spacing: 12) {
Button("取消") { dismiss() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 15, horizontalPadding: 18))
Button("说完了") { finishRecording() }
.buttonStyle(TjPrimaryButton(height: 44, fontSize: 15, horizontalPadding: 18))
}
case .failed:
HStack(spacing: 12) {
Button("打开新建菜单") { onOpenMenu() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 14, horizontalPadding: 14))
Button("再说一次") { Task { await begin() } }
.buttonStyle(TjPrimaryButton(height: 44, fontSize: 14, horizontalPadding: 18))
}
case .denied:
Button("取消") { dismiss() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 15, horizontalPadding: 18))
case .requesting, .classifying:
Button("取消") { dismiss() }
.buttonStyle(TjGhostButton(height: 44, fontSize: 15, horizontalPadding: 18))
}
}
// MARK: -
private func begin() async {
ticker?.cancel()
transcript = ""
seconds = 0
guard SpeechDictationService.isAvailable else {
phase = .failed(message: String(appLoc: "本机不支持端侧语音识别,试试下面的新建菜单"))
return
}
phase = .requesting
guard await dictation.requestAuthorization() else {
phase = .denied
return
}
do {
try dictation.start { transcript = $0 }
phase = .recording
startTicker()
} catch {
phase = .failed(message: error.localizedDescription)
}
}
private func startTicker() {
ticker = Task { @MainActor in
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: 1_000_000_000)
guard phase == .recording else { return }
seconds += 1
if seconds >= maxSeconds {
finishRecording()
return
}
}
}
}
private func finishRecording() {
guard phase == .recording else { return }
ticker?.cancel()
phase = .classifying
Task {
let text = await dictation.stop()
transcript = text
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else {
phase = .failed(message: String(appLoc: "没听到内容,再试一次?"))
return
}
if let intent = await VoiceIntentService.classify(trimmed) {
onResolve(intent)
} else {
phase = .failed(message: String(appLoc: "没听懂想记什么,再说一次,或直接选菜单"))
}
}
}
}
#Preview {
Text("bg")
.sheet(isPresented: .constant(true)) {
VoiceCommandSheet(onResolve: { print($0) }, onOpenMenu: {})
}
}

View File

@@ -3,33 +3,36 @@ import SwiftData
import Foundation
enum TimelineKind: String, CaseIterable, Identifiable {
case indicator, report, symptom, diary
case diary, symptom, 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 .indicator: return String(appLoc: "指标")
case .report: return String(appLoc: "报告")
case .symptom: return String(appLoc: "症状")
case .diary: 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 .indicator: return "drop.fill"
case .report: return "doc.fill"
case .symptom: return "waveform.path.ecg"
case .diary: return "pencil"
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 .indicator: return Tj.Palette.brick
case .report: return Tj.Palette.ink2
case .symptom: return Tj.Palette.amber
case .diary: return Tj.Palette.leaf
case .medication: return Tj.Palette.ink
}
}
}
@@ -132,13 +135,16 @@ struct TimelineEntry: Identifiable, Hashable {
}
}
/// tag () .medication ,
/// id "diary-" :TimelineDetail.resolve diaries
static func from(diary d: DiaryEntry) -> TimelineEntry {
TimelineEntry(
let isMed = d.isMedicationLog
return TimelineEntry(
id: "diary-\(d.persistentModelID)",
kind: .diary,
kind: isMed ? .medication : .diary,
date: d.createdAt,
title: d.content.firstLine(),
subtitle: String(appLoc: "文字日记"),
subtitle: isMed ? String(appLoc: "用药记录") : String(appLoc: "文字日记"),
trailing: nil,
trailingIsAlert: false,
isOngoing: false

View File

@@ -22,7 +22,8 @@ enum TimelineDetail {
case .report:
return reports.first { "report-\($0.persistentModelID)" == entry.id }
.map(TimelineDetail.report)
case .diary:
case .diary, .medication:
// tag DiaryEntry,
return diaries.first { "diary-\($0.persistentModelID)" == entry.id }
.map(TimelineDetail.diary)
case .symptom: