Files
linkding-ios/Marks/Views/SearchView.swift
Krishna Kumar 525e6557c8
Some checks failed
CI / build-and-deploy (push) Failing after 6s
Replace manual backend config with auto JWT auth
Device auto-registers on first launch using hardcoded backend URL
and anon key (MarksAuth.swift). 90-day JWT stored in UserDefaults,
refreshed transparently. No settings fields needed — ClaudeService
is always active and non-optional throughout the app.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 14:14:54 -05:00

87 lines
3.4 KiB
Swift

import SwiftUI
struct SearchView: View {
@Bindable var viewModel: BookmarksViewModel
@Environment(\.openURL) private var openURL
@State private var searchText = ""
@State private var useSemanticSearch = false
@State private var semanticResults: [Bookmark]? = nil
@State private var searchTask: Task<Void, Never>?
var body: some View {
NavigationStack {
List {
ForEach(results) { bookmark in
BookmarkRow(bookmark: bookmark)
.onTapGesture {
if let url = URL(string: bookmark.url) { openURL(url) }
}
.listRowInsets(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20))
.listRowSeparator(.visible)
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
Button(role: .destructive) {
Task { await viewModel.delete(bookmark) }
} label: {
Label("Delete", systemImage: "trash")
}
}
}
}
.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()
}
}
}
.searchable(text: $searchText, prompt: "Titles, URLs, tags…")
.onChange(of: searchText) { _, _ in scheduleSearch() }
}
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 {}
}
}
}