SPB Git

Repository Public

MediaBlockSheets.swift 7.3 KB 148 lines swift

1// SP Notes iOS2// File: Features/Editor/Blocks/MediaBlockSheets.swift3// Description: Minimal screens of the media blocks: `audio` (title, duration, transcript read-only — the voice-note screen4//              replaces it through a higher-priority provider), and the read-only fallback viewers of `pdf` (PDFKit) and5//              `drawing` (render or ink) used until the Pencil screens register.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import PDFKit10import SPNotesCore11import SPNotesDesign12import SwiftUI1314struct AudioBlockSheet: View {15    let original: AudioBlock16    let context: BlockDetailContext17    @State private var block: AudioBlock1819    init(block: AudioBlock, context: BlockDetailContext) {20        original = block21        self.context = context22        _block = State(initialValue: block)23    }2425    var body: some View {26        BlockSheetScaffold(title: String(localized: "Note vocale"), isEditable: context.isEditable, onCancel: context.dismiss,27                           onDone: { context.finish(block.node(), original: original.node()) }, onDelete: context.delete) {28            Form {29                Section {30                    TextField("Titre", text: Binding(get: { block.title }, set: { if $0 != block.title { block.title = $0 } }))31                        .disabled(!context.isEditable)32                        .accessibilityIdentifier("audio-title")33                    LabeledContent("Durée", value: block.duration > 0 ? BlockLabels.duration(block.duration) : "—")34                    if let date = BlockLabels.date(block.recordedAt) { LabeledContent("Enregistré le", value: date) }35                    LabeledContent("Transcription", value: BlockLabels.transcriptStatus(block.transcriptStatus) + (block.transcriptEngine.map { " (\($0))" } ?? ""))36                }37                if let summary = block.summary {38                    Section {39                        NVAIBadge(model: summary.model)40                        Text(summary.text).nvFont(.body).foregroundStyle(NVColor.ai).textSelection(.enabled)41                    } header: { Text("Résumé") }42                }43                let segments = block.transcript.filter { !$0.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }44                if !segments.isEmpty {45                    Section("Transcription") {46                        ForEach(Array(segments.enumerated()), id: \.offset) { _, segment in47                            HStack(alignment: .firstTextBaseline, spacing: 10) {48                                Text(segment.timecode).font(.system(.footnote, design: .monospaced)).foregroundStyle(NVColor.fgMuted)49                                Text(segment.text).nvFont(.body).foregroundStyle(NVColor.fg).textSelection(.enabled)50                            }51                        }52                    }53                    .accessibilityIdentifier("audio-transcript")54                }55            }56        }57    }58}5960/// PDFKit view, read-only.61struct PDFKitReader: UIViewRepresentable {62    let url: URL63    var page = 16465    func makeUIView(context: Context) -> PDFView {66        let view = PDFView()67        view.autoScales = true68        view.displayMode = .singlePageContinuous69        view.displayDirection = .vertical70        view.backgroundColor = UIColor(NVColor.bgSunken)71        view.document = PDFDocument(url: url)72        if let document = view.document, let target = document.page(at: min(max(0, page - 1), max(0, document.pageCount - 1))) { view.go(to: target) }73        view.accessibilityIdentifier = "pdf-reader"74        return view75    }7677    func updateUIView(_ view: PDFView, context: Context) {}78}7980struct PDFBlockViewer: View {81    let block: PDFBlock82    let context: BlockDetailContext83    @State private var phase: AttachmentPreviewLoader.Phase = .loading8485    var body: some View {86        BlockSheetScaffold(title: block.name.isEmpty ? String(localized: "Document PDF") : block.name, isEditable: context.isEditable, onCancel: context.dismiss, onDone: nil, onDelete: context.delete) {87            VStack(spacing: 0) {88                switch phase {89                case .ready(let url): PDFKitReader(url: url, page: block.page).ignoresSafeArea(edges: .bottom)90                case .loading: ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity)91                case .offline: NVEmptyState(systemImage: "icloud.slash", title: String(localized: "Fichier non téléchargé"), message: String(localized: "Ce PDF n’est pas sur l’appareil et tu es hors ligne."))92                case .failed(let message): NVEmptyState(systemImage: "exclamationmark.triangle", title: String(localized: "PDF indisponible"), message: message)93                }94                let annotations = block.annotations95                if !annotations.isEmpty {96                    List {97                        Section(BlockLabels.annotations(annotations.count)) {98                            ForEach(annotations) { annotation in99                                Text(annotation.summaryLine).nvFont(.ui).foregroundStyle(NVColor.fg)100                            }101                        }102                    }103                    .frame(maxHeight: 180)104                    .accessibilityIdentifier("pdf-annotations")105                }106            }107        }108        .task {109            guard let id = block.attachmentId else { phase = .failed(String(localized: "Aucun fichier associé à ce bloc.")); return }110            phase = await AttachmentPreviewLoader.resolve(id, app: context.app)111        }112    }113}114115struct DrawingBlockViewer: View {116    let block: DrawingBlock117    let context: BlockDetailContext118    /// nil = reading; .some(nil) = nothing to show on this device.119    @State private var image: UIImage??120121    var body: some View {122        BlockSheetScaffold(title: String(localized: "Dessin"), isEditable: context.isEditable, onCancel: context.dismiss, onDone: nil, onDelete: context.delete) {123            ZStack {124                NVColor.bgSunken.ignoresSafeArea()125                switch image {126                case .some(.some(let image)):127                    ScrollView([.horizontal, .vertical]) { Image(uiImage: image).resizable().scaledToFit().padding() }128                        .accessibilityLabel("Dessin").accessibilityIdentifier("drawing-image")129                case .some(.none):130                    NVEmptyState(systemImage: "scribble", title: String(localized: "Aperçu non disponible"), message: String(localized: "Ce dessin n’est pas sur l’appareil pour l’instant."))131                case .none: ProgressView()132                }133            }134        }135        .task { image = .some(await Self.load(block, app: context.app)) }136    }137138    /// The render when it is here, otherwise the ink drawn by the Core's renderer.139    static func load(_ block: DrawingBlock, app: AppModel) async -> UIImage? {140        if let preview = block.preview, await app.resolveImage(source: "/api/files/" + preview), let image = app.image(forSource: "/api/files/" + preview) { return image }141        if let ink = block.ink, let url = try? await app.localFile(forAttachment: ink), let data = try? Data(contentsOf: url), let document = try? InkDocument(data: data),142           let rendered = InkRenderer.image(document, scale: 2, background: InkBackground(attr: block.background), crop: .canvas) {143            return UIImage(cgImage: rendered, scale: 2, orientation: .up)144        }145        return nil146    }147}148
Files main
Menu

Theme