All checks were successful
CI / build-and-deploy (push) Successful in 28s
- bookmarkOnscreen(_:) publishes the viewed bookmark as the on-screen entity (NSUserActivity.appEntityIdentifier) so Siri can resolve "this" / "summarize this one"; applied to the BrowserView sheets in BookmarksView and SearchView - Add MarksTests (XCTest) covering entity mapping, EntityIdentifier, IntentRouter, and the real perform() of SearchMarksIntent and OpenBookmarkIntent; wired into the Marks scheme test phase Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
102 lines
3.9 KiB
Swift
102 lines
3.9 KiB
Swift
import SwiftUI
|
|
|
|
struct SearchView: View {
|
|
@Bindable var viewModel: BookmarksViewModel
|
|
|
|
@State private var searchText = ""
|
|
@State private var useSemanticSearch = false
|
|
@State private var semanticResults: [Bookmark]? = nil
|
|
@State private var searchTask: Task<Void, Never>?
|
|
@State private var browsingBookmark: Bookmark?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
ForEach(results) { bookmark in
|
|
BookmarkListRow(
|
|
bookmark: bookmark,
|
|
viewModel: viewModel,
|
|
onOpen: { browsingBookmark = bookmark }
|
|
)
|
|
}
|
|
}
|
|
.listStyle(.plain)
|
|
.navigationTitle("Search")
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Toggle(isOn: $useSemanticSearch) {
|
|
Label("Semantic", systemImage: "sparkles")
|
|
}
|
|
.toggleStyle(.button)
|
|
.onChange(of: useSemanticSearch) { _, _ in scheduleSearch() }
|
|
}
|
|
}
|
|
.overlay {
|
|
if searchText.isEmpty {
|
|
ContentUnavailableView("Search Bookmarks", systemImage: "magnifyingglass", description: Text("Search by title, URL, or tag."))
|
|
} else if results.isEmpty && !viewModel.isLoading {
|
|
ContentUnavailableView.search(text: searchText)
|
|
}
|
|
if viewModel.isLoading {
|
|
ProgressView()
|
|
}
|
|
}
|
|
.sensoryFeedback(.selection, trigger: browsingBookmark?.id)
|
|
.sheet(item: $browsingBookmark) { bookmark in
|
|
if let url = URL(string: bookmark.url) {
|
|
BrowserView(
|
|
url: url,
|
|
title: bookmark.displayTitle,
|
|
claude: viewModel.claude,
|
|
podcastPlayer: viewModel.podcastPlayer
|
|
) {
|
|
await viewModel.archive(bookmark)
|
|
}
|
|
.bookmarkOnscreen(bookmark)
|
|
}
|
|
}
|
|
}
|
|
.searchable(text: $searchText, prompt: "Titles, URLs, tags…")
|
|
.onChange(of: searchText) { _, _ in scheduleSearch() }
|
|
.onAppear { consumeIntentSearch() }
|
|
.onChange(of: IntentRouter.shared.searchRequest) { _, _ in consumeIntentSearch() }
|
|
}
|
|
|
|
/// Pulls a query handed over by `SearchMarksIntent` into the search field.
|
|
private func consumeIntentSearch() {
|
|
guard let query = IntentRouter.shared.searchRequest else { return }
|
|
searchText = query
|
|
IntentRouter.shared.searchRequest = nil
|
|
}
|
|
|
|
private var results: [Bookmark] {
|
|
if let semantic = semanticResults { return semantic }
|
|
guard !searchText.isEmpty else { return [] }
|
|
let q = searchText.lowercased()
|
|
return viewModel.bookmarks.filter { b in
|
|
b.displayTitle.lowercased().contains(q) ||
|
|
b.url.lowercased().contains(q) ||
|
|
b.tagNames.contains { $0.lowercased().contains(q) } ||
|
|
(b.aiTags ?? []).contains { $0.lowercased().contains(q) } ||
|
|
(b.aiSummary ?? "").lowercased().contains(q)
|
|
}
|
|
}
|
|
|
|
private func scheduleSearch() {
|
|
semanticResults = nil
|
|
searchTask?.cancel()
|
|
guard !searchText.isEmpty, useSemanticSearch else { return }
|
|
let claude = viewModel.claude
|
|
searchTask = Task {
|
|
try? await Task.sleep(for: .milliseconds(500))
|
|
guard !Task.isCancelled else { return }
|
|
let q = searchText
|
|
let all = viewModel.bookmarks
|
|
do {
|
|
let ranked = try await claude.semanticSearch(query: q, in: all)
|
|
await MainActor.run { semanticResults = ranked }
|
|
} catch {}
|
|
}
|
|
}
|
|
}
|