Sources/OrgstarMobile/SearchScreen.swift
60 lines · 2708 bytes
1 symbol in this file
1#if os(iOS)
2import OrgApp
3import OrgCore
4import OrgIndex
5import SwiftUI
6
7/// Files by name and headings by their words, as ⌘P and ⇧⌘F find them on the Mac.
8struct SearchScreen: View {
9 let workspace: WorkspaceModel
10 let session: DocumentSession
11 @State private var query = ""
12
13 var body: some View {
14 let trimmed = query.trimmingCharacters(in: .whitespaces)
15 List {
16 if !trimmed.isEmpty {
17 let files = FuzzyMatch.rank(trimmed, workspace.files.filter { $0.kind == .org }, limit: 8)
18 if !files.isEmpty {
19 Section("Files") {
20 ForEach(files) { file in
21 NavigationLink(value: ReaderRoute.file(file.path)) {
22 Label(file.relativePath, systemImage: "doc.text")
23 }
24 }
25 }
26 }
27 let results = Perf.measure("search.headings") { Result { try workspace.search(trimmed, overlay: workspace.overlay(session.openDocuments)) } }
28 let headings = (try? results.get()) ?? []
29 Section("Notes") {
30 if case .failure(let error) = results {
31 Text(error.localizedDescription).foregroundStyle(.secondary)
32 } else if headings.isEmpty {
33 Text("No notes match").foregroundStyle(.secondary)
34 }
35 ForEach(headings, id: \.self) { location in
36 NavigationLink(value: ReaderRoute.heading(location)) {
37 VStack(alignment: .leading, spacing: 2) {
38 Text(location.title.isEmpty ? "(untitled)" : location.title).lineLimit(2)
39 if let snippet = location.snippet {
40 Text(snippet.attributed).font(.subheadline).foregroundStyle(.secondary).lineLimit(3)
41 }
42 Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.tertiary)
43 }
44 }
45 }
46 }
47 }
48 }
49 .overlay {
50 if trimmed.isEmpty {
51 ContentUnavailableView("Search Notes", systemImage: "magnifyingglass", description: Text("File names and the words of headings and their text."))
52 }
53 }
54 .navigationTitle("Search")
55 .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always))
56 .autocorrectionDisabled()
57 .textInputAutocapitalization(.never)
58 }
59}
60#endif