SPB Git

Repository Public

AppRouting.swift 6.5 KB 141 lines swift

1// SP Notes iOS2// File: App/Intents/AppRouting.swift3// Description: Turns what comes from outside the screens — `spnotes://` links (widgets, other apps), Handoff and Spotlight4//              activities, universal links, notification taps — into a navigation request dispatched to one window5//              (`AppModel.routes`), and builds the activity a note screen advertises (outgoing Handoff).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import CoreSpotlight10import Foundation1112/// The activity a screen showing a note advertises (Handoff, Siri suggestions) and the app continues.13enum NoteActivity {14    static let type = "ai.spboucher.spnotes.ios.openNote"15    static let noteIdKey = "noteId"16    static let blockIdKey = "blockId"1718    /// What the screen of a note advertises, as plain values (unit-tested; `apply` writes them on the activity).19    struct Content: Equatable {20        var title: String21        var noteId: String22        var blockId: String?23        /// The page on the web app: Handoff to a device without the app continues in the browser.24        var webpageURL: URL25        var targetContentIdentifier: String26        var isEligibleForHandoff = true27        /// Notes are indexed by `SpotlightIndexer`, not through activities (no duplicate results).28        var isEligibleForSearch = false29        var isEligibleForPrediction = false3031        init(noteId: String, title: String, blockId: String? = nil) {32            self.title = NoteTitle.display(title)33            self.noteId = noteId34            self.blockId = blockId35            webpageURL = DeepLink.webURL(noteId: noteId, blockId: blockId)36            targetContentIdentifier = DeepLink.note(noteId).url.absoluteString37        }3839        var userInfo: [String: String] {40            var info = [NoteActivity.noteIdKey: noteId]41            if let blockId { info[NoteActivity.blockIdKey] = blockId }42            return info43        }4445        func apply(to activity: NSUserActivity) {46            activity.title = title47            activity.userInfo = userInfo48            activity.requiredUserInfoKeys = [NoteActivity.noteIdKey]49            activity.webpageURL = webpageURL50            activity.isEligibleForHandoff = isEligibleForHandoff51            activity.isEligibleForSearch = isEligibleForSearch52            activity.isEligibleForPrediction = isEligibleForPrediction53            activity.targetContentIdentifier = targetContentIdentifier54        }55    }5657    /// For `.userActivity(NoteActivity.type) { NoteActivity.fill($0, noteId:title:) }` on the note screen.58    static func fill(_ activity: NSUserActivity, noteId: String, title: String, blockId: String? = nil) {59        Content(noteId: noteId, title: title, blockId: blockId).apply(to: activity)60    }6162    /// The note an activity points to: one of ours, or a Spotlight result.63    static func noteId(from activity: NSUserActivity) -> String? {64        let key = activity.activityType == CSSearchableItemActionType ? CSSearchableItemActivityIdentifier : noteIdKey65        guard activity.activityType == CSSearchableItemActionType || activity.activityType == type,66              let id = activity.userInfo?[key] as? String, DeepLink.isValidNoteID(id) else { return nil }67        return id.lowercased()68    }6970    static func blockId(from activity: NSUserActivity) -> String? {71        guard activity.activityType == type, let id = activity.userInfo?[blockIdKey] as? String, DeepLink.isValidBlockID(id) else { return nil }72        return id73    }7475    /// The text typed in the system search when "Search in app" is chosen.76    static func searchQuery(from activity: NSUserActivity) -> String? {77        guard activity.activityType == CSQueryContinuationActionType else { return nil }78        return activity.userInfo?[CSSearchQueryString] as? String79    }8081    /// The navigation request of an activity (Handoff, Spotlight result, Spotlight "search in app", a web page of the app82    /// continued as a universal link). nil when the activity is not ours.83    static func route(for activity: NSUserActivity) -> AppRoute? {84        if let id = noteId(from: activity) { return .note(id, blockId: blockId(from: activity)) }85        if let query = searchQuery(from: activity) { return .search(query) }86        if activity.activityType == NSUserActivityTypeBrowsingWeb, let url = activity.webpageURL, case .note(let id, let block)? = DeepLink.web(url) {87            return .note(id, blockId: block)88        }89        return nil90    }91}9293extension AppModel {94    /// The navigation request of a link. `nil` when the link needs work first (see `open(_:)`).95    static func route(for link: DeepLink) -> AppRoute? {96        switch link {97        case .note(let id, let blockId): return .note(id, blockId: blockId)98        case .search(let query): return .search(query)99        case .new(let kind, let text): return kind == .note && (text ?? "").isEmpty ? .newNote : nil100        case .tasks: return .tasks101        case .inbox: return .inbox102        case .today: return .today103        case .capture(let kind): return .capture(kind: kind?.rawValue)104        }105    }106107    /// Handles a `spnotes://` URL (or the web address of a page). Returns false when it is not one of ours.108    /// `scene` names the window the link arrived in.109    @discardableResult110    func open(url: URL, from scene: RouteCoordinator.SceneID? = nil) -> Bool {111        guard let link = DeepLink(url: url) else { return false }112        open(link, from: scene)113        return true114    }115116    func open(_ link: DeepLink, from scene: RouteCoordinator.SceneID? = nil) {117        if let route = Self.route(for: link) {118            routes.dispatch(route, from: scene)119            return120        }121        // A new note that starts with something (a task list, a text): it is created first, then opened like any note.122        guard case .new(let kind, let text) = link else { return }123        Task {124            do {125                routes.dispatch(.note(try await quickCreate(title: nil, text: text ?? "", kind: kind)), from: scene)126            } catch {127                routes.dispatch(.newNote, from: scene)128            }129        }130    }131132    /// Handoff, Spotlight result, Spotlight "search in app" or a universal link. Returns false when the activity is not ours.133    /// The one handler of activities: one activity = one route = one navigation, in one window.134    @discardableResult135    func continueActivity(_ activity: NSUserActivity, from scene: RouteCoordinator.SceneID? = nil) -> Bool {136        guard let route = NoteActivity.route(for: activity) else { return false }137        routes.dispatch(route, from: scene)138        return true139    }140}141
Files main
Menu

Theme