SPB Git

Repository Public

FeatureRouter.swift 2.2 KB 55 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeatures/Support/FeatureRouter.swift3// Description: The navigation contract between the feature screens and the app that hosts them (SPEC §8): the screens never4//              navigate by themselves outside their own surface, they ask the router. Also a recording router for previews,5//              the debug catalogue and tests.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation1011/// Implemented by the host app (one object per window / scene is fine). All calls are made on the main actor, after a12/// user action.13@MainActor14public protocol FeatureRouter: AnyObject {15    /// Opens a page in the editor; `blockId` (when given) is the block to scroll to and highlight.16    func openPage(id: String, blockId: String?)17    /// Opens a database (the id of the database or of its host page).18    func openDatabase(id: String)19    /// Opens a canvas page.20    func openCanvas(pageId: String)21    /// Opens an external link (browser, mail…).22    func openURL(_ url: URL)23    /// Shows an error the screen could not present inline (rare: the screens show their own banners).24    func presentError(_ error: Error)25}2627/// A router that only records what it was asked (previews, `FeatureCatalogView`, unit tests).28@MainActor29public final class FeatureRouterRecorder: FeatureRouter {30    public enum Call: Equatable, Sendable {31        case page(id: String, blockId: String?)32        case database(id: String)33        case canvas(pageId: String)34        case url(URL)35        case error(String)36    }3738    public private(set) var calls: [Call] = []39    /// Called after each recorded call (the catalogue shows a toast).40    public var onCall: ((Call) -> Void)?4142    public init() {}4344    public func openPage(id: String, blockId: String?) { record(.page(id: id, blockId: blockId)) }45    public func openDatabase(id: String) { record(.database(id: id)) }46    public func openCanvas(pageId: String) { record(.canvas(pageId: pageId)) }47    public func openURL(_ url: URL) { record(.url(url)) }48    public func presentError(_ error: Error) { record(.error(FeatureFailure(error).message)) }4950    private func record(_ call: Call) {51        calls.append(call)52        onCall?(call)53    }54}55
Files main
Menu

Theme