```
feat(AI): 集成MNN推理引擎替换MLX作为主AI运行时 - 引入MNN(alibaba) + Arm SME2 + CPU作为主AI运行时,支持A19/iPhone17的 SME2和A17的NEON加速 - 添加MLX Swift作为兜底GPU推理方案,实现双后端切换机制 - 使用单一Qwen3.5-2B多模态模型(1.2GB),替代原有的LLM+VL分离架构 - 实现InferenceEngine.current引擎选择逻辑,真机默认MNN,模拟器回退MLX - 更新AIAgent架构,通过MNNLLMBridge(ObjC++) → MNNBackend进行推理 - 修改队列机制防止并发推理导致OOM,使用信号量闸门控制显存占用 - 更新文档中的技术栈说明、模块边界和周次交付计划 ```
This commit is contained in:
383
康康/Features/Profile/MedicationLibraryView.swift
Normal file
383
康康/Features/Profile/MedicationLibraryView.swift
Normal file
@@ -0,0 +1,383 @@
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
/// 「记录 · 药品库」管理页:我有哪些药的 master 清单(药名 / 规格 / 用法 / 原图)。
|
||||
/// 拍药盒识别或手动录入入库;某次服用流水另走「写日记 · 用药」(带 `DiaryEntry.medicationTag` 的日记,含剂量 + 时间)。
|
||||
/// 列表 / 增删改范式照搬 `CustomMetricsListView`;编辑表单照搬 `CustomReminderEditSheet`。
|
||||
struct MedicationLibraryView: View {
|
||||
@Environment(\.modelContext) private var ctx
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@Query(sort: \Medication.updatedAt, order: .reverse)
|
||||
private var medications: [Medication]
|
||||
|
||||
/// sheet 形态(从「记录」拉起)补「完成」按钮;push 形态不补,靠返回键。
|
||||
var presentedAsSheet: Bool = false
|
||||
|
||||
@State private var editingTarget: MedicationEditTarget?
|
||||
@State private var showScan = false
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
hintBanner
|
||||
if medications.isEmpty {
|
||||
emptyState
|
||||
} else {
|
||||
ForEach(medications) { m in
|
||||
Button {
|
||||
editingTarget = MedicationEditTarget(medication: m)
|
||||
} label: {
|
||||
row(m)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 32)
|
||||
}
|
||||
.background(Tj.Palette.sand.ignoresSafeArea())
|
||||
.navigationTitle("药品库")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
if presentedAsSheet {
|
||||
ToolbarItem(placement: .topBarLeading) {
|
||||
Button(String(appLoc: "完成")) { dismiss() }
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
HStack(spacing: 16) {
|
||||
Button { showScan = true } label: {
|
||||
Image(systemName: "camera")
|
||||
.font(.tjScaled( 16, weight: .semibold))
|
||||
}
|
||||
.accessibilityLabel(String(appLoc: "拍药盒添加"))
|
||||
Button { editingTarget = MedicationEditTarget(medication: nil) } label: {
|
||||
Image(systemName: "plus")
|
||||
.font(.tjScaled( 16, weight: .semibold))
|
||||
}
|
||||
.accessibilityLabel(String(appLoc: "手动添加"))
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(item: $editingTarget) { target in
|
||||
MedicationEditSheet(existing: target.medication)
|
||||
}
|
||||
.fullScreenCover(isPresented: $showScan) {
|
||||
// 拍药盒 → 本地 OCR + LLM 识别 → 核对 → 入药品库(含原图)。
|
||||
MedicationScanFlow(
|
||||
onSave: { meds, images in
|
||||
MedicationArchiver.archive(medications: meds, images: images, in: ctx)
|
||||
},
|
||||
onClose: { showScan = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - subviews
|
||||
|
||||
private var hintBanner: some View {
|
||||
HStack(spacing: 10) {
|
||||
Image(systemName: "info.circle.fill")
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
Text("药品库是你的常用药清单。记录某次服用请到「写日记 · 用药」,可填剂量和时间。")
|
||||
.font(.tjScaled( 12))
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
.padding(12)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
|
||||
.fill(Tj.Palette.sand2.opacity(0.5))
|
||||
)
|
||||
}
|
||||
|
||||
private var emptyState: some View {
|
||||
VStack(spacing: 14) {
|
||||
Spacer(minLength: 40)
|
||||
TjPlaceholder(label: String(appLoc: "药品库还是空的"))
|
||||
.frame(width: 220, height: 130)
|
||||
Text("右上角拍药盒或 + 手动添加")
|
||||
.font(.tjScaled( 12))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
Spacer()
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
|
||||
private func row(_ m: Medication) -> some View {
|
||||
HStack(spacing: 12) {
|
||||
ZStack {
|
||||
Circle().fill(Tj.Palette.leafSoft)
|
||||
Image(systemName: "pills.fill")
|
||||
.font(.tjScaled( 17, weight: .medium))
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
.frame(width: 40, height: 40)
|
||||
|
||||
VStack(alignment: .leading, spacing: 3) {
|
||||
Text(m.name)
|
||||
.font(.tjScaled( 15, weight: .semibold))
|
||||
.foregroundStyle(Tj.Palette.text)
|
||||
.lineLimit(1)
|
||||
if !m.detailLine.isEmpty {
|
||||
Text(m.detailLine)
|
||||
.font(.tjScaled( 11))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(minLength: 8)
|
||||
|
||||
if !m.assets.isEmpty {
|
||||
Text("📷 \(m.assets.count)")
|
||||
.font(.tjScaled( 11))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
}
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.tjScaled( 11, weight: .medium))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
}
|
||||
.padding(14)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
|
||||
.fill(Tj.Palette.paper)
|
||||
)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
|
||||
.strokeBorder(Tj.Palette.lineSoft, lineWidth: 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// `medication == nil` → 新建;否则编辑。`id` 用 UUID 让同一对象重开 sheet 也能刷新。
|
||||
private struct MedicationEditTarget: Identifiable {
|
||||
let id = UUID()
|
||||
let medication: Medication?
|
||||
}
|
||||
|
||||
/// 药品库的新建 / 编辑表单(范式同 `CustomReminderEditSheet`:本地 @State 暂存,保存才写库)。
|
||||
private struct MedicationEditSheet: View {
|
||||
@Environment(\.modelContext) private var ctx
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
/// nil = 新建模式。
|
||||
let existing: Medication?
|
||||
|
||||
@State private var name = ""
|
||||
@State private var strength = ""
|
||||
@State private var usage = ""
|
||||
@State private var note = ""
|
||||
@State private var hydrated = false
|
||||
/// 点缩略图全屏查看的起始页;nil = 未打开查看器。
|
||||
@State private var viewerStart: PhotoIndex?
|
||||
|
||||
private var isEditing: Bool { existing != nil }
|
||||
private var canSave: Bool {
|
||||
!name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
if let m = existing, !m.assets.isEmpty {
|
||||
Section {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 10) {
|
||||
ForEach(Array(m.assets.enumerated()), id: \.offset) { idx, asset in
|
||||
Button {
|
||||
viewerStart = PhotoIndex(index: idx)
|
||||
} label: {
|
||||
MedicationAssetThumb(asset: asset)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
.listRowInsets(EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12))
|
||||
} header: {
|
||||
Text(String(appLoc: "原图\(m.assets.count)张"))
|
||||
} footer: {
|
||||
Text("点图片可放大查看。原图均存在本机加密目录,不上传。")
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
TextField(String(appLoc: "药名,如:缬沙坦胶囊"), text: $name)
|
||||
.foregroundStyle(Tj.Palette.text)
|
||||
TextField(String(appLoc: "规格,如:80mg×7粒"), text: $strength)
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
TextField(String(appLoc: "用法,如:一日一次,一次一粒"), text: $usage)
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
} footer: {
|
||||
Text("仅作清单记录,不提供任何用药或剂量建议。")
|
||||
}
|
||||
|
||||
Section {
|
||||
TextField(String(appLoc: "备注(可选)"), text: $note, axis: .vertical)
|
||||
.lineLimit(1...3)
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
}
|
||||
|
||||
if isEditing {
|
||||
Section {
|
||||
Button(role: .destructive) { deleteMedication() } label: {
|
||||
Label(String(appLoc: "从药品库删除"), systemImage: "trash")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.scrollContentBackground(.hidden)
|
||||
.background(Tj.Palette.sand.ignoresSafeArea())
|
||||
.navigationTitle(isEditing ? String(appLoc: "编辑药品") : String(appLoc: "添加药品"))
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarLeading) {
|
||||
Button(String(appLoc: "取消")) { dismiss() }
|
||||
}
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button(String(appLoc: "保存")) { save() }
|
||||
.fontWeight(.semibold)
|
||||
.disabled(!canSave)
|
||||
}
|
||||
}
|
||||
.onAppear(perform: hydrate)
|
||||
.fullScreenCover(item: $viewerStart) { start in
|
||||
if let m = existing {
|
||||
MedicationPhotoViewer(assets: m.assets, startIndex: start.index)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func hydrate() {
|
||||
guard !hydrated else { return }
|
||||
hydrated = true
|
||||
if let m = existing {
|
||||
name = m.name
|
||||
strength = m.strength
|
||||
usage = m.usage
|
||||
note = m.note ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
guard canSave else { return }
|
||||
let n = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let s = strength.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let u = usage.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let nt = note.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if let m = existing {
|
||||
m.name = n
|
||||
m.strength = s
|
||||
m.usage = u
|
||||
m.note = nt.isEmpty ? nil : nt
|
||||
m.updatedAt = .now
|
||||
} else {
|
||||
let med = Medication(name: n, strength: s, usage: u, note: nt.isEmpty ? nil : nt)
|
||||
ctx.insert(med)
|
||||
}
|
||||
try? ctx.save()
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private func deleteMedication() {
|
||||
guard let m = existing else { return }
|
||||
// 先删 Vault 里的 JPEG(cascade 只删 Asset 记录,文件要手动 unlink,§6 永久删除)。
|
||||
for a in m.assets {
|
||||
try? FileVault.shared.remove(relativePath: a.relativePath)
|
||||
}
|
||||
ctx.delete(m)
|
||||
try? ctx.save()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 原图查看
|
||||
|
||||
/// 全屏查看器的起始页载体(`.fullScreenCover(item:)` 需 Identifiable)。
|
||||
private struct PhotoIndex: Identifiable {
|
||||
let id = UUID()
|
||||
let index: Int
|
||||
}
|
||||
|
||||
/// 药品库行内 / 编辑表单里的方形缩略图。原图从加密 Vault 同步读取(数量少,与 EvidenceImagePage 同款)。
|
||||
private struct MedicationAssetThumb: View {
|
||||
let asset: Asset
|
||||
|
||||
var body: some View {
|
||||
VaultImage(relativePath: asset.relativePath, maxPixel: 500) { img in
|
||||
Image(uiImage: img).resizable().scaledToFill()
|
||||
} placeholder: { isLoading in
|
||||
if isLoading {
|
||||
Tj.Palette.paper
|
||||
} else {
|
||||
TjPlaceholder(label: String(appLoc: "原图无法读取"))
|
||||
}
|
||||
}
|
||||
.frame(width: 150, height: 150)
|
||||
.clipped()
|
||||
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
|
||||
.strokeBorder(Tj.Palette.lineSoft, lineWidth: 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// 全屏翻页查看药品原图(看清药盒小字)。
|
||||
private struct MedicationPhotoViewer: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
let assets: [Asset]
|
||||
@State private var selection: Int
|
||||
|
||||
init(assets: [Asset], startIndex: Int) {
|
||||
self.assets = assets
|
||||
_selection = State(initialValue: min(max(startIndex, 0), max(assets.count - 1, 0)))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack(alignment: .topTrailing) {
|
||||
Color.black.ignoresSafeArea()
|
||||
|
||||
TabView(selection: $selection) {
|
||||
ForEach(Array(assets.enumerated()), id: \.offset) { idx, asset in
|
||||
VaultImage(relativePath: asset.relativePath, maxPixel: 2000) { img in
|
||||
Image(uiImage: img).resizable().scaledToFit()
|
||||
} placeholder: { isLoading in
|
||||
if isLoading {
|
||||
ProgressView().tint(.white)
|
||||
} else {
|
||||
TjPlaceholder(label: String(appLoc: "原图无法读取"))
|
||||
}
|
||||
}
|
||||
.tag(idx)
|
||||
}
|
||||
}
|
||||
.tabViewStyle(.page(indexDisplayMode: assets.count > 1 ? .automatic : .never))
|
||||
.ignoresSafeArea()
|
||||
|
||||
Button { dismiss() } label: {
|
||||
Image(systemName: "xmark")
|
||||
.font(.tjScaled( 16, weight: .semibold))
|
||||
.foregroundStyle(.white)
|
||||
.frame(width: 36, height: 36)
|
||||
.background(Circle().fill(.black.opacity(0.4)))
|
||||
}
|
||||
.padding(.trailing, 18)
|
||||
.padding(.top, 14)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
MedicationLibraryView(presentedAsSheet: true)
|
||||
}
|
||||
.modelContainer(for: [Medication.self, Asset.self], inMemory: true)
|
||||
}
|
||||
@@ -2,28 +2,41 @@ import SwiftUI
|
||||
import SwiftData
|
||||
import UIKit
|
||||
|
||||
/// 「拍药盒入档」流程:拍药盒/说明书 → Vision OCR → LLM 结构化 → 核对 → 落库。
|
||||
/// 入口:「+ 新建 · 健康日记 · 拍药盒」与「我的 · 个人资料 · 当前用药」。
|
||||
/// 两个入口确认后都走 `MedicationArchiver`:记一条「用药」日记(进记录时间线)+ 同步当前用药。
|
||||
/// 只识别入档,不做用药提醒/剂量建议(§1)。
|
||||
/// 「拍药盒入库」流程:拍药盒/说明书(最多 5 张,选一张识别)→ Vision OCR → LLM 结构化 → 核对 → 存入药品库(连同原图)。
|
||||
/// 入口:「记录 · 药品库」与「记录 · 健康日记 · 拍药盒」。
|
||||
/// 两个入口确认后都走 `MedicationArchiver`:每条药建一个 `Medication`(挂原图),不写日记、不写当前用药。
|
||||
/// 服用流水改由「写日记 · 用药」生成带 `medicationTag` 的 DiaryEntry。只识别入库,不做用药提醒/剂量建议(§1)。
|
||||
///
|
||||
/// 状态机(与 QuickRegionCaptureFlow 同构):
|
||||
/// 状态机:
|
||||
/// ```
|
||||
/// idle(相机/相册) → recognizing(OCR + LLM) → confirm(核对可编辑) → onSave → 关闭
|
||||
/// │ 失败/没读出 ──────► confirm(空行 + 警示文案,手动补)
|
||||
/// idle(相机/相册) ─拍到第1张→ collecting(复看:删/继续拍≤5/选一张/开始识别)
|
||||
/// │ 开始识别
|
||||
/// ▼
|
||||
/// recognizing(选中单张 OCR + LLM) ─→ confirm(核对一种药) ─onSave→ 关闭
|
||||
/// │ 失败/没读出 ───────────────► confirm(空行 + 警示)
|
||||
/// ```
|
||||
struct MedicationScanFlow: View {
|
||||
/// 用户确认后回传条目文本(非空,如 "缬沙坦胶囊 80mg · 一日一次")。落库由调用方做。
|
||||
let onSave: ([String]) -> Void
|
||||
/// 用户确认后回传(结构化药品, 原图)。入库由调用方做(走 MedicationArchiver.archive(medications:))。
|
||||
let onSave: ([ParsedMedication], [UIImage]) -> Void
|
||||
let onClose: () -> Void
|
||||
|
||||
/// 一种药最多关联 5 张原图(正面/背面/说明书…)。
|
||||
static let maxImages = 5
|
||||
|
||||
@State private var phase: Phase = .idle
|
||||
/// 已拍/已选的原图,跨 collecting → recognizing → confirm 一直留着,确认时全部作为该药原图落库。
|
||||
@State private var images: [UIImage] = []
|
||||
/// 识别用的照片索引(在多张里单选一张)。一次只记一种药 → 只 OCR 这一张;删图时校正。
|
||||
@State private var recognizeIndex = 0
|
||||
/// 在 collecting 复看页「继续拍/继续选」时弹相机或相册。
|
||||
@State private var showMoreCapture = false
|
||||
/// 识别任务句柄:识别中点「取消」要能立刻中断,不留后台推理。
|
||||
@State private var recognitionTask: Task<Void, Never>?
|
||||
|
||||
enum Phase {
|
||||
case idle
|
||||
case recognizing(image: UIImage)
|
||||
case collecting
|
||||
case recognizing
|
||||
case confirm(items: [EditableMedication], warning: String?)
|
||||
}
|
||||
|
||||
@@ -35,6 +48,8 @@ struct MedicationScanFlow: View {
|
||||
var include: Bool = true
|
||||
}
|
||||
|
||||
private var remainingSlots: Int { max(0, Self.maxImages - images.count) }
|
||||
|
||||
var body: some View {
|
||||
content
|
||||
.background(Tj.Palette.sand.ignoresSafeArea())
|
||||
@@ -45,10 +60,14 @@ struct MedicationScanFlow: View {
|
||||
switch phase {
|
||||
case .idle:
|
||||
// 不整体 ignoresSafeArea:相机内部已全屏黑底,忽略安全区会让「取消」顶进灵动岛。
|
||||
captureEntry
|
||||
initialCaptureEntry
|
||||
|
||||
case .recognizing(let image):
|
||||
recognizingView(image: image)
|
||||
case .collecting:
|
||||
collectingView
|
||||
.fullScreenCover(isPresented: $showMoreCapture) { moreCaptureSheet }
|
||||
|
||||
case .recognizing:
|
||||
recognizingView
|
||||
|
||||
case .confirm(let items, let warning):
|
||||
NavigationStack {
|
||||
@@ -56,7 +75,7 @@ struct MedicationScanFlow: View {
|
||||
items: items,
|
||||
warning: warning,
|
||||
onSave: { saveItems($0) },
|
||||
onRetake: { phase = .idle }
|
||||
onRetake: { images = []; phase = .idle }
|
||||
)
|
||||
.navigationTitle("核对药品")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
@@ -72,31 +91,160 @@ struct MedicationScanFlow: View {
|
||||
|
||||
// MARK: - 入口:拍照(真机)/ 相册(模拟器)
|
||||
|
||||
/// 首张:进入即拍/选。拿到第一张就转 collecting 复看。
|
||||
@ViewBuilder
|
||||
private var captureEntry: some View {
|
||||
private var initialCaptureEntry: some View {
|
||||
#if targetEnvironment(simulator)
|
||||
PhotoPickerSheet(
|
||||
onFinish: { images in
|
||||
if let first = images.first { startRecognition(first) } else { onClose() }
|
||||
onFinish: { picked in
|
||||
appendImages(picked)
|
||||
if images.isEmpty { onClose() } else { phase = .collecting }
|
||||
},
|
||||
onCancel: onClose
|
||||
)
|
||||
#else
|
||||
SingleShotCameraView(
|
||||
onCapture: { startRecognition($0) },
|
||||
onCapture: { appendImages([$0]); phase = .collecting },
|
||||
onCancel: onClose
|
||||
)
|
||||
#endif
|
||||
}
|
||||
|
||||
private func recognizingView(image: UIImage) -> some View {
|
||||
/// collecting 复看页里「继续拍/继续选」弹出的二次采集。
|
||||
@ViewBuilder
|
||||
private var moreCaptureSheet: some View {
|
||||
#if targetEnvironment(simulator)
|
||||
PhotoPickerSheet(
|
||||
onFinish: { picked in appendImages(picked); showMoreCapture = false },
|
||||
onCancel: { showMoreCapture = false }
|
||||
)
|
||||
#else
|
||||
SingleShotCameraView(
|
||||
onCapture: { appendImages([$0]); showMoreCapture = false },
|
||||
onCancel: { showMoreCapture = false }
|
||||
)
|
||||
#endif
|
||||
}
|
||||
|
||||
private func appendImages(_ new: [UIImage]) {
|
||||
guard remainingSlots > 0 else { return }
|
||||
images.append(contentsOf: new.prefix(remainingSlots))
|
||||
}
|
||||
|
||||
// MARK: - 复看(已拍 N 张:删 / 继续拍 / 开始识别)
|
||||
|
||||
private var collectingView: some View {
|
||||
VStack(spacing: 0) {
|
||||
ScrollView {
|
||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: 96), spacing: 12)], spacing: 12) {
|
||||
ForEach(Array(images.enumerated()), id: \.offset) { idx, img in
|
||||
let isPick = idx == recognizeIndex
|
||||
ZStack(alignment: .topTrailing) {
|
||||
Image(uiImage: img)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(width: 96, height: 96)
|
||||
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
|
||||
.strokeBorder(isPick ? Tj.Palette.ink : Color.clear, lineWidth: 3)
|
||||
)
|
||||
.overlay(alignment: .bottomLeading) {
|
||||
if isPick {
|
||||
Text("识别此张")
|
||||
.font(.tjScaled(10, weight: .semibold))
|
||||
.foregroundStyle(Tj.Palette.paper)
|
||||
.padding(.horizontal, 6)
|
||||
.padding(.vertical, 3)
|
||||
.background(Capsule().fill(Tj.Palette.ink))
|
||||
.padding(5)
|
||||
}
|
||||
}
|
||||
// 点图把它选为「识别用」那张(单选)。
|
||||
.onTapGesture { recognizeIndex = idx }
|
||||
Button {
|
||||
images.remove(at: idx)
|
||||
// 校正识别索引:删选中前面的图要左移;删到越界则收回末尾。
|
||||
if images.isEmpty {
|
||||
recognizeIndex = 0
|
||||
phase = .idle
|
||||
} else if idx < recognizeIndex {
|
||||
recognizeIndex -= 1
|
||||
} else if recognizeIndex >= images.count {
|
||||
recognizeIndex = images.count - 1
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
.font(.tjScaled(20))
|
||||
.foregroundStyle(.white, .black.opacity(0.5))
|
||||
.padding(4)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
|
||||
if remainingSlots > 0 {
|
||||
Button { showMoreCapture = true } label: {
|
||||
VStack(spacing: 6) {
|
||||
Image(systemName: "plus")
|
||||
.font(.tjScaled(22, weight: .medium))
|
||||
Text("继续拍")
|
||||
.font(.tjScaled(12))
|
||||
}
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
.frame(width: 96, height: 96)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
|
||||
.strokeBorder(Tj.Palette.line, style: StrokeStyle(lineWidth: 1, dash: [4]))
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(18)
|
||||
}
|
||||
|
||||
VStack(spacing: 8) {
|
||||
Text("已拍 \(images.count)/\(Self.maxImages) 张 · 可拍正面、背面、说明书")
|
||||
.font(.tjScaled(12))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
if images.count > 1 {
|
||||
Text("点照片选「识别此张」· 一次记一种药")
|
||||
.font(.tjScaled(11))
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
Text("照片与文字均不离开设备")
|
||||
.font(.tjScaled(11))
|
||||
.foregroundStyle(Tj.Palette.text3)
|
||||
|
||||
Button {
|
||||
startRecognition()
|
||||
} label: {
|
||||
Text("开始识别")
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.buttonStyle(TjPrimaryButton())
|
||||
.disabled(images.isEmpty)
|
||||
.opacity(images.isEmpty ? 0.4 : 1)
|
||||
}
|
||||
.padding(.horizontal, 18)
|
||||
.padding(.bottom, 12)
|
||||
}
|
||||
.overlay(alignment: .topLeading) {
|
||||
flowCancelButton { onClose() }
|
||||
}
|
||||
}
|
||||
|
||||
private var recognizingView: some View {
|
||||
VStack(spacing: 18) {
|
||||
Image(uiImage: image)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(maxHeight: 320)
|
||||
.clipShape(RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous))
|
||||
.padding(.horizontal, 24)
|
||||
if images.indices.contains(recognizeIndex) {
|
||||
Image(uiImage: images[recognizeIndex])
|
||||
.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))
|
||||
@@ -108,31 +256,37 @@ struct MedicationScanFlow: View {
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
// 识别中也要能退出,不能让用户干等(§3.2 不卡死)
|
||||
.overlay(alignment: .topLeading) {
|
||||
Button {
|
||||
flowCancelButton {
|
||||
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 flowCancelButton(_ action: @escaping () -> Void) -> some View {
|
||||
Button(action: action) {
|
||||
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)
|
||||
}
|
||||
|
||||
private func startRecognition(_ image: UIImage) {
|
||||
phase = .recognizing(image: image)
|
||||
// MARK: - 识别(选中单张 OCR → LLM 结构化)
|
||||
|
||||
private func startRecognition() {
|
||||
guard images.indices.contains(recognizeIndex) else { return }
|
||||
phase = .recognizing
|
||||
let target = images[recognizeIndex]
|
||||
recognitionTask = Task {
|
||||
let (items, warning) = await recognize(image)
|
||||
let (items, warning) = await recognize(target)
|
||||
guard !Task.isCancelled else { return } // 识别中点了取消:不再回写 phase
|
||||
await MainActor.run {
|
||||
// 全失败也不卡死:给一条空行让用户手填(§3.2 失败回退红线)。
|
||||
@@ -148,13 +302,15 @@ struct MedicationScanFlow: View {
|
||||
|
||||
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 {
|
||||
// 一次只识别选中的这一张 → 一种药。
|
||||
let text = (try? await OCRService.recognizeText(in: image))?
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
if text.isEmpty {
|
||||
return ([], String(appLoc: "没识别到文字,拍清楚一点再试"))
|
||||
}
|
||||
let parsed = try await MedicationScanService.shared.recognizeMedications(fromOCRText: trimmed)
|
||||
let items = parsed.map {
|
||||
let parsed = try await MedicationScanService.shared.recognizeMedications(fromOCRText: text)
|
||||
// 一次一种药:即使识别出多条,也只取第一条。
|
||||
let items = parsed.prefix(1).map {
|
||||
EditableMedication(name: $0.name, strength: $0.strength, usage: $0.usage)
|
||||
}
|
||||
return (items, items.isEmpty ? String(appLoc: "没读出药品,可以手动填写") : nil)
|
||||
@@ -172,34 +328,56 @@ struct MedicationScanFlow: View {
|
||||
// MARK: - 保存
|
||||
|
||||
private func saveItems(_ items: [EditableMedication]) {
|
||||
let entries = items
|
||||
let meds = items
|
||||
.filter { $0.include && !$0.name.trimmingCharacters(in: .whitespaces).isEmpty }
|
||||
.map {
|
||||
ParsedMedication(name: $0.name, strength: $0.strength, usage: $0.usage).entryText
|
||||
ParsedMedication(name: $0.name.trimmingCharacters(in: .whitespaces),
|
||||
strength: $0.strength.trimmingCharacters(in: .whitespaces),
|
||||
usage: $0.usage.trimmingCharacters(in: .whitespaces))
|
||||
}
|
||||
onSave(entries)
|
||||
// 确认后入药品库(连同原图)。空条目则不入库,由调用方据数组是否为空决定。
|
||||
onSave(meds, images)
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 统一落库(MainActor,SwiftData 写主上下文必须由 View 侧持有的 ctx 来做,§3.1)
|
||||
// MARK: - 入药品库(MainActor,SwiftData 写主上下文必须由 View 侧持有的 ctx 来做,§3.1)
|
||||
|
||||
/// 拍药盒确认后的统一落库,两个入口共用:
|
||||
/// 1. 记一条带「用药」tag 的 DiaryEntry → 出现在「记录」时间线的「用药」分类
|
||||
/// 2. 同步到 UserProfile.currentMedications(去重)→ AI 解读 / 身体档案 prompt 背景
|
||||
/// 拍药盒确认后入药品库,两个入口(药品库页、写日记 · 拍药盒)共用:
|
||||
/// 每条药建一个 `Medication`(挂原图),按 name+strength 软去重;**不写日记、不写 currentMedications**。
|
||||
/// 服用流水改由「写日记 · 用药」生成带 `DiaryEntry.medicationTag` 的日记。
|
||||
@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)
|
||||
static func archive(medications: [ParsedMedication], images: [UIImage] = [], in ctx: ModelContext) {
|
||||
guard !medications.isEmpty else { return }
|
||||
|
||||
let profile = UserProfileStore.loadOrCreate(in: ctx)
|
||||
for entry in entries where !profile.currentMedications.contains(entry) {
|
||||
profile.currentMedications.append(entry)
|
||||
// 原图写加密 Vault(§5/§6:落 Application Support/Vault,目录级硬件加密)。
|
||||
// 多药共享同批原图时只挂「第一条新建的药」,避免同一 JPEG 被多个 Asset 引用、
|
||||
// 删一条 cascade 误删另一条还在用的文件。
|
||||
let savedAssets = images
|
||||
.prefix(MedicationScanFlow.maxImages)
|
||||
.compactMap { try? FileVault.shared.writeJPEG($0) }
|
||||
|
||||
let existing = (try? ctx.fetch(FetchDescriptor<Medication>())) ?? []
|
||||
var attachedImages = false
|
||||
for m in medications {
|
||||
// 软去重:同 name+strength 已在库则只补用法 / 刷新时间,不重复建。
|
||||
if let dup = existing.first(where: { $0.name == m.name && $0.strength == m.strength }) {
|
||||
if dup.usage.isEmpty, !m.usage.isEmpty { dup.usage = m.usage }
|
||||
dup.updatedAt = .now
|
||||
continue
|
||||
}
|
||||
let med = Medication(name: m.name, strength: m.strength, usage: m.usage)
|
||||
if !attachedImages {
|
||||
for s in savedAssets {
|
||||
let asset = Asset(relativePath: s.relativePath, bytes: s.bytes)
|
||||
ctx.insert(asset)
|
||||
med.assets.append(asset)
|
||||
}
|
||||
attachedImages = true
|
||||
}
|
||||
ctx.insert(med)
|
||||
}
|
||||
profile.updatedAt = .now
|
||||
try? ctx.save()
|
||||
}
|
||||
}
|
||||
@@ -231,13 +409,8 @@ private struct MedicationConfirmView: View {
|
||||
|
||||
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: "药品名,如:缬沙坦胶囊"), text: $item.name)
|
||||
.foregroundStyle(Tj.Palette.text)
|
||||
TextField(String(appLoc: "规格,如:80mg×7粒"), text: $item.strength)
|
||||
.foregroundStyle(Tj.Palette.text2)
|
||||
TextField(String(appLoc: "用法,如:一日一次,一次一粒"), text: $item.usage)
|
||||
@@ -246,12 +419,6 @@ private struct MedicationConfirmView: View {
|
||||
}
|
||||
|
||||
Section {
|
||||
Button {
|
||||
items.append(.init(name: "", strength: "", usage: ""))
|
||||
} label: {
|
||||
Label("再加一种", systemImage: "plus.circle")
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
Button {
|
||||
onRetake()
|
||||
} label: {
|
||||
@@ -259,7 +426,7 @@ private struct MedicationConfirmView: View {
|
||||
.foregroundStyle(Tj.Palette.ink)
|
||||
}
|
||||
} footer: {
|
||||
Text("将记入健康日记(记录页可查),并同步到「当前用药」供 AI 解读参考。不提供任何用药建议。")
|
||||
Text("一次记一种药,多张照片都会作为这种药的原图存入药品库,供查看与 AI 解读参考。不提供任何用药建议。")
|
||||
}
|
||||
}
|
||||
.scrollContentBackground(.hidden)
|
||||
@@ -267,7 +434,7 @@ private struct MedicationConfirmView: View {
|
||||
Button {
|
||||
onSave(items)
|
||||
} label: {
|
||||
Text("保存用药记录")
|
||||
Text("存入药品库")
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.buttonStyle(TjPrimaryButton())
|
||||
@@ -281,5 +448,5 @@ private struct MedicationConfirmView: View {
|
||||
}
|
||||
|
||||
#Preview {
|
||||
MedicationScanFlow(onSave: { print($0) }, onClose: {})
|
||||
MedicationScanFlow(onSave: { _, _ in }, onClose: {})
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ 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,9 +87,6 @@ private struct ProfileEditForm: View {
|
||||
items: $profile.allergies)
|
||||
StringListSection(title: String(appLoc: "家族史"), placeholder: String(appLoc: "如:母亲 高血压"),
|
||||
items: $profile.familyHistory)
|
||||
StringListSection(title: String(appLoc: "当前用药"), placeholder: String(appLoc: "如:缬沙坦 80mg qd"),
|
||||
items: $profile.currentMedications,
|
||||
onScan: { showMedicationScan = true })
|
||||
}
|
||||
.navigationTitle("个人资料")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
@@ -100,16 +96,6 @@ 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,
|
||||
@@ -468,27 +454,10 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user