Files
kangkang/康康/Features/Me/MeView.swift
link2026 9d856fcfc4 ```
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,使用信号量闸门控制显存占用
- 更新文档中的技术栈说明、模块边界和周次交付计划
```
2026-06-15 09:24:59 +08:00

288 lines
10 KiB
Swift

import SwiftUI
import SwiftData
struct MeView: View {
@Environment(\.modelContext) private var ctx
@Query private var profiles: [UserProfile]
@Query private var customMetrics: [CustomMonitorMetric]
@State private var downloadService = ModelDownloadService.shared
@State private var appLock = AppLock.shared
@State private var lang = LanguageManager.shared
@State private var fontScale = FontScaleManager.shared
// key AppLock.enabledKey
@AppStorage("faceIDLockEnabled") private var lockEnabled = false
private var profile: UserProfile? { profiles.first }
/// Bundle ,
private var appVersionText: String {
let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.1"
return "v\(short)"
}
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 12) {
HStack {
Text("我的")
.font(.tjTitle())
.foregroundStyle(Tj.Palette.text)
Spacer()
}
.padding(.top, 4)
.padding(.bottom, 6)
profileCard
customMetricsCard
modelManagementCard
inferenceEngineCard
languageCard
fontScaleCard
faceIDCard
NavigationLink {
AboutView()
} label: {
settingsCard(title: String(appLoc: "关于"),
detail: appVersionText,
icon: "info.circle")
}
.buttonStyle(.plain)
}
.padding(.horizontal, 16)
.padding(.vertical, 20)
}
.background(Tj.Palette.sand.ignoresSafeArea())
// ( // ), .navigationTitle:
// , App
.onAppear {
if profiles.isEmpty {
_ = UserProfileStore.loadOrCreate(in: ctx)
}
downloadService.refreshStates()
appLock.refreshAvailability()
}
}
}
// MARK: - Cards
private var profileCard: some View {
NavigationLink {
ProfileEditView()
} label: {
HStack(spacing: 12) {
ZStack {
Circle()
.fill(Tj.Palette.amber.opacity(0.25))
Image(systemName: "person.crop.circle.fill")
.font(.tjScaled( 22))
.foregroundStyle(Tj.Palette.ink)
}
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 2) {
Text("个人资料")
.font(.tjScaled( 15, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
Text(profileLine)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
.lineLimit(1)
}
Spacer()
Image(systemName: "chevron.right")
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(Tj.Palette.text3)
}
.padding(14)
.tjCard()
}
.buttonStyle(.plain)
}
private var customMetricsCard: some View {
NavigationLink {
CustomMetricsListView()
} label: {
HStack(spacing: 12) {
ZStack {
Circle()
.fill(customMetrics.isEmpty ? Tj.Palette.sand2 : Tj.Palette.leafSoft)
Image(systemName: "slider.horizontal.3")
.font(.tjScaled( 18))
.foregroundStyle(customMetrics.isEmpty ? Tj.Palette.text2 : Tj.Palette.ink)
}
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 2) {
Text("自定义指标")
.font(.tjScaled( 15, weight: .semibold))
.foregroundStyle(Tj.Palette.text)
Text(customMetricsLine)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
.lineLimit(1)
}
Spacer()
Image(systemName: "chevron.right")
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(Tj.Palette.text3)
}
.padding(14)
.tjCard()
}
.buttonStyle(.plain)
}
private var customMetricsLine: String {
if customMetrics.isEmpty { return String(appLoc: "添加你自己的长期监测项") }
return String(appLoc: "\(customMetrics.count)")
}
private var modelManagementCard: some View {
NavigationLink {
ModelManagementView()
} label: {
settingsCard(title: String(appLoc: "模型管理"), detail: modelDetail, icon: "cpu")
}
.buttonStyle(.plain)
}
private var modelDetail: String {
let states = downloadService.states
if ModelKind.userFacing.allSatisfy({ states[$0]?.phase == .ready }) { return String(appLoc: "已就绪") }
if downloadService.isAnyDownloading { return String(appLoc: "下载中…") }
let readyCount = ModelKind.userFacing.filter { states[$0]?.phase == .ready }.count
return readyCount == 0 ? String(appLoc: "未下载") : String(appLoc: "\(readyCount)/\(ModelKind.userFacing.count) 就绪")
}
private var inferenceEngineCard: some View {
NavigationLink {
InferenceSettingsView()
} label: {
settingsCard(title: String(appLoc: "推理引擎"), detail: engineDetail, icon: "cpu.fill")
}
.buttonStyle(.plain)
}
private var engineDetail: String {
switch InferenceEngine.current {
case .mnn: return InferenceEngine.cpuSupportsSME2 ? "MNN · SME2" : "MNN · CPU"
case .mlx: return "MLX · GPU"
}
}
private var languageCard: some View {
NavigationLink {
LanguageSettingsView()
} label: {
settingsCard(title: String(appLoc: "语言"),
detail: lang.current.displayName,
icon: "character.bubble")
}
.buttonStyle(.plain)
}
private var fontScaleCard: some View {
NavigationLink {
FontSettingsView()
} label: {
settingsCard(title: String(appLoc: "字体大小"),
detail: fontScale.scale.label,
icon: "textformat.size")
}
.buttonStyle(.plain)
}
// MARK: - Face ID ( Toggle )
private var faceIDCard: some View {
HStack(spacing: 12) {
ZStack {
Circle().fill(lockEnabled ? Tj.Palette.amber.opacity(0.25) : Tj.Palette.sand2)
Image(systemName: "faceid")
.font(.tjScaled( 18))
.foregroundStyle(lockEnabled ? Tj.Palette.ink : Tj.Palette.text2)
}
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 2) {
Text("Face ID 启动锁")
.font(.tjScaled( 15, weight: .medium))
.foregroundStyle(Tj.Palette.text)
Text(faceIDLine)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
}
Spacer()
Toggle("", isOn: faceIDBinding)
.labelsHidden()
.disabled(!appLock.biometryAvailable)
}
.padding(14)
.tjCard()
}
private var faceIDLine: String {
if !appLock.biometryAvailable { return String(appLoc: "本设备未设置 Face ID 或密码") }
return lockEnabled ? String(appLoc: "已开启 · \(appLock.biometryLabel)") : String(appLoc: "关闭")
}
/// , enabled();
private var faceIDBinding: Binding<Bool> {
Binding(
get: { lockEnabled },
set: { newValue in
if newValue {
Task { await appLock.enableWithAuth() }
} else {
appLock.disable()
}
}
)
}
private func settingsCard(title: String, detail: String, icon: String) -> some View {
HStack(spacing: 12) {
ZStack {
Circle().fill(Tj.Palette.sand2)
Image(systemName: icon)
.font(.tjScaled( 18))
.foregroundStyle(Tj.Palette.text2)
}
.frame(width: 44, height: 44)
Text(title)
.font(.tjScaled( 15, weight: .medium))
.foregroundStyle(Tj.Palette.text)
Spacer()
Text(detail)
.font(.tjScaled( 12))
.foregroundStyle(Tj.Palette.text3)
Image(systemName: "chevron.right")
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(Tj.Palette.text3)
}
.padding(14)
.tjCard()
}
private var profileLine: String {
guard let p = profile, p.hasAnyBasics else {
return String(appLoc: "点这里完善你的资料")
}
return p.summaryLine
}
}
#Preview {
MeView()
.modelContainer(for: [
UserProfile.self, Indicator.self, Report.self, DiaryEntry.self,
Asset.self, ChatTurn.self, Symptom.self, MetricReminder.self,
CustomMonitorMetric.self, Medication.self,
], inMemory: true)
}