164 lines
5.8 KiB
Swift
164 lines
5.8 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
import UniformTypeIdentifiers
|
|
|
|
struct PatientWorkspace: View {
|
|
private struct Patient: Identifiable {
|
|
let id: String
|
|
let name: String
|
|
}
|
|
|
|
private struct Attachment: Identifiable {
|
|
let id = UUID()
|
|
let name: String
|
|
let image: NSImage
|
|
}
|
|
|
|
private let patients = [
|
|
Patient(id: "DEMO-001", name: "Демо-пациент 1"),
|
|
Patient(id: "DEMO-002", name: "Демо-пациент 2")
|
|
]
|
|
|
|
@State private var search = ""
|
|
@State private var patientID: Patient.ID?
|
|
@State private var attachments: [Patient.ID: [Attachment]] = [:]
|
|
@State private var attachmentID: Attachment.ID?
|
|
@State private var showImporter = false
|
|
@State private var importError: String?
|
|
|
|
private var filteredPatients: [Patient] {
|
|
let query = search.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
return patients.filter {
|
|
query.isEmpty || $0.name.localizedStandardContains(query)
|
|
|| $0.id.localizedStandardContains(query)
|
|
}
|
|
}
|
|
|
|
private var patient: Patient? {
|
|
patients.first { $0.id == patientID }
|
|
}
|
|
|
|
private var patientAttachments: [Attachment] {
|
|
patientID.flatMap { attachments[$0] } ?? []
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationSplitView {
|
|
List(filteredPatients, selection: $patientID) { patient in
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(patient.name)
|
|
Text(patient.id)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.tag(patient.id)
|
|
}
|
|
.searchable(text: $search, placement: .sidebar, prompt: "Имя или номер карты")
|
|
.navigationTitle("Пациенты")
|
|
.navigationSplitViewColumnWidth(min: 190, ideal: 240)
|
|
.safeAreaInset(edge: .bottom) {
|
|
Text("Демонстрационные данные")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.padding(12)
|
|
.background(.bar)
|
|
}
|
|
} detail: {
|
|
if let patient {
|
|
imageWorkspace
|
|
.navigationTitle(patient.name)
|
|
.navigationSubtitle(patient.id)
|
|
.toolbar {
|
|
Button {
|
|
showImporter = true
|
|
} label: {
|
|
Label("Открыть снимки…", systemImage: "folder")
|
|
}
|
|
}
|
|
} else {
|
|
placeholder("Выберите пациента", icon: "person.crop.rectangle")
|
|
.navigationTitle("EzDent")
|
|
}
|
|
}
|
|
.onChange(of: patientID) { _ in
|
|
attachmentID = patientAttachments.first?.id
|
|
}
|
|
.fileImporter(isPresented: $showImporter, allowedContentTypes: [.image], allowsMultipleSelection: true) {
|
|
importImages($0)
|
|
}
|
|
.alert("Не удалось открыть снимки", isPresented: Binding(
|
|
get: { importError != nil },
|
|
set: { if !$0 { importError = nil } }
|
|
)) {
|
|
Button("ОК", role: .cancel) {}
|
|
} message: {
|
|
Text(importError ?? "")
|
|
}
|
|
}
|
|
|
|
private var imageWorkspace: some View {
|
|
VStack(spacing: 0) {
|
|
if let attachment = patientAttachments.first(where: { $0.id == attachmentID }) {
|
|
Image(nsImage: attachment.image)
|
|
.resizable()
|
|
.scaledToFit()
|
|
.accessibilityLabel(attachment.name)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.padding(16)
|
|
} else {
|
|
placeholder("Откройте локальные снимки", icon: "photo")
|
|
}
|
|
|
|
if !patientAttachments.isEmpty {
|
|
Divider()
|
|
Picker("Снимок", selection: $attachmentID) {
|
|
ForEach(patientAttachments) { attachment in
|
|
Text(attachment.name).tag(Optional(attachment.id))
|
|
}
|
|
}
|
|
.padding(12)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func placeholder(_ title: String, icon: String) -> some View {
|
|
VStack(spacing: 12) {
|
|
Image(systemName: icon)
|
|
.font(.largeTitle)
|
|
.accessibilityHidden(true)
|
|
Text(title)
|
|
.font(.headline)
|
|
}
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
|
|
private func importImages(_ result: Result<[URL], Error>) {
|
|
guard let patientID else { return }
|
|
do {
|
|
var imported: [Attachment] = []
|
|
var failures: [String] = []
|
|
for url in try result.get() {
|
|
let hasAccess = url.startAccessingSecurityScopedResource()
|
|
defer { if hasAccess { url.stopAccessingSecurityScopedResource() } }
|
|
if let data = try? Data(contentsOf: url),
|
|
let image = NSImage(data: data), image.isValid {
|
|
imported.append(Attachment(name: url.lastPathComponent, image: image))
|
|
} else {
|
|
failures.append(url.lastPathComponent)
|
|
}
|
|
}
|
|
if let first = imported.first {
|
|
attachments[patientID, default: []].append(contentsOf: imported)
|
|
attachmentID = first.id
|
|
}
|
|
if !failures.isEmpty {
|
|
importError = failures.joined(separator: "\n")
|
|
}
|
|
} catch {
|
|
importError = error.localizedDescription
|
|
}
|
|
}
|
|
}
|