Repository Public
1// SP Notes iOS2// File: Features/Capture/CaptureDebugRoutes.swift3// Description: A block detail context for a voice note opened outside the editor (it reads and writes the stored page), and the4// DEBUG launch routes of the capture screens for screenshots and UI tests:5// `SPNOTES_OPEN=capture[:mode] | voice | voice-recorder` (with `SPNOTES_STORE=memory SPNOTES_SESSION=local SPNOTES_SEED=1`).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesCore10import SwiftUI1112/// The voice note of a stored page, without an open editor: changes are saved into the page's document.13@MainActor14enum VoiceNoteStandalone {15 /// The first `audio` block of the page.16 static func audioNode(in app: AppModel, noteId: String) async -> PMNode? {17 guard let page = try? await app.store.page(id: noteId), let document = try? NoteDocument(json: page.content) else { return nil }18 return document.lines.first { $0.type == AudioBlock.nodeType }?.node19 }2021 /// A context whose `commit` replaces (or removes) the block with this id in the stored document and whose22 /// `insertMarkdownBelow` adds the blocks right after it — the same effects as in the editor.23 static func context(app: AppModel, noteId: String, blockId: String?, isEditable: Bool = true, dismiss: @escaping () -> Void) -> BlockDetailContext {24 func edit(_ change: @escaping (inout NoteDocument, Int) -> Void) {25 Task {26 guard let page = try? await app.store.page(id: noteId), var document = try? NoteDocument(json: page.content),27 let index = document.lines.firstIndex(where: { $0.type == AudioBlock.nodeType && (blockId == nil || $0.attrs["id"]?.stringValue == blockId) }) else { return }28 change(&document, index)29 _ = try? await app.save(noteId: noteId, title: page.title, document: document)30 }31 }32 return BlockDetailContext(app: app, noteId: noteId, isEditable: isEditable, commit: { node in33 edit { document, index in34 if let node { document.lines[index] = Line(blockAtom: node, path: document.lines[index].path) } else { document.lines.remove(at: index) }35 }36 }, insertMarkdownBelow: { markdown in37 edit { document, index in document.insert(lines: NoteDocument(markdown: markdown).lines, at: index + 1) }38 }, open: { app.pendingRoute = $0 }, dismiss: dismiss)39 }40}4142extension View {43 /// Applied once at the root of the app; a no-op in release builds.44 func captureDebugRoute() -> some View {45 #if DEBUG46 modifier(CaptureDebugRoute())47 #else48 self49 #endif50 }51}5253#if DEBUG54enum CaptureDemo {55 /// The route asked for at launch, if any (`capture`, `capture:audio`, `voice`, `voice-recorder`).56 static let route = ProcessInfo.processInfo.environment["SPNOTES_OPEN"].flatMap { $0.hasPrefix("capture") || $0.hasPrefix("voice") ? $0 : nil }57}5859private struct CaptureDebugRoute: ViewModifier {60 @Environment(AppModel.self) private var app61 @State private var capture: CaptureRequest?62 @State private var screen: Screen?6364 struct Screen: Identifiable {65 let id = UUID()66 let view: AnyView67 }6869 func body(content: Content) -> some View {70 content71 .task {72 guard let route = CaptureDemo.route, capture == nil, screen == nil else { return }73 try? await Task.sleep(for: .milliseconds(600)) // the root interface first74 if route.hasPrefix("capture") {75 let mode = route.split(separator: ":").dropFirst().first.flatMap { CaptureMode(route: String($0)) } ?? .text76 capture = CaptureRequest(mode: mode)77 } else {78 screen = await make(route)79 }80 }81 .quickCapture(item: $capture)82 .sheet(item: $screen) { $0.view }83 }8485 private func make(_ route: String) async -> Screen? {86 // the sample workspace is seeded at launch: wait for its voice note87 var node: PMNode?88 for _ in 0..<40 {89 node = await VoiceNoteStandalone.audioNode(in: app, noteId: VoiceSample.noteID)90 if node != nil { break }91 try? await Task.sleep(for: .milliseconds(150))92 }93 switch route {94 case "voice-recorder":95 let context = VoiceNoteStandalone.context(app: app, noteId: VoiceSample.noteID, blockId: nil, dismiss: { screen = nil })96 return Screen(view: AnyView(VoiceRecorderSheet(context: context)))97 default:98 guard let node else { return nil }99 let context = VoiceNoteStandalone.context(app: app, noteId: VoiceSample.noteID, blockId: node.stringAttr("id"), dismiss: { screen = nil })100 return Screen(view: AnyView(VoiceNoteScreen(node: node, context: context)))101 }102 }103}104#endif105