Files
linkding-ios/Marks/Views/AskView.swift
T
Krishna KumarandClaude Opus 5 fffb3999bf
CI / build-and-deploy (pull_request) Successful in 34s
CI / build-and-deploy (push) Successful in 34s
Carry the library design across the rest of the app
Every screen now speaks the paper vocabulary rather than only the
bookmarks screen: Search, Sources, Podcasts and the player, Settings,
Add/Edit, Smart Collections, Ask, Onboarding, the browser toolbar, the
Siri snippets, and the share extension's save card.

The change is mostly a design system rather than per-screen tweaks.
LibraryKit gains:

- Semantic colors — secondary/tertiary/faint text, `raised` surfaces,
  and accent/alarm/affirm, so screens stop reaching for .secondary,
  systemGray, .blue, .red and .green. The three status colors come out
  of the palette (the swatch blue, vermilion and forest) rather than
  from the system set, so the design keeps spending one set of inks.
- PaperType — the two voices made explicit. Prose (titles, summaries,
  anything a person wrote) is serif; anything the machine contributes
  (domains, dates, counts, tags, labels, buttons) is monospaced. Keeping
  that split strict is what makes the design read as archival rather
  than as decoration.
- paperSurface() / paperField() / paperCard() for grounds, inputs and
  raised blocks.
- PaperEmptyState, because ContentUnavailableView can't be restyled — it
  draws its own bold system type and grey, which was the loudest
  remaining system voice once the screens moved onto the sheet. All nine
  usages are converted.

The app also sets one accent for the system chrome it doesn't draw —
tab bar, search fields, switches, swipe actions — which otherwise stayed
system blue around paper screens.

BookmarkRow is deleted. Once Search moved to the library row and
TagBookmarksView was gone, nothing passed .classic, so the style fork in
BookmarkListRow went with it. There is one bookmark row now.

The share extension compiles LibraryKit directly, since its save card is
a user-facing surface and should not be the one place still using system
styling.

Verified on device in both color schemes; screens toured with a
temporary UI test harness (removed). Suite passes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JgLHztZGaEHvS3KNeGQmRM
2026-07-27 12:50:45 -05:00

167 lines
6.1 KiB
Swift

import SwiftUI
/// "Ask Your Bookmarks" — the on-device RAG surface. Questions are answered by
/// Apple's on-device model, grounded in the user's bookmarks via Spotlight.
struct AskView: View {
@State private var assistant = BookmarkAssistant()
@State private var question = ""
@State private var answer = ""
@State private var isLoading = false
@State private var errorText: String?
@Environment(\.dismiss) private var dismiss
@FocusState private var focused: Bool
var body: some View {
NavigationStack {
Group {
switch assistant.status {
case .checking:
ProgressView()
case .unavailable(let message):
PaperEmptyState(
title: "Unavailable",
systemImage: "sparkles.slash",
message: message
)
case .ready:
ready
}
}
.paperSurface()
.navigationTitle("Ask Your Bookmarks")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Done") { dismiss() }
.font(PaperType.label)
.tint(Paper.accent)
}
}
}
}
private var ready: some View {
VStack(spacing: 0) {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
if answer.isEmpty && !isLoading && errorText == nil {
PaperEmptyState(
title: "Ask anything",
systemImage: "sparkles",
message: "Answers come from your saved bookmarks, generated on-device."
)
.padding(.top, 40)
}
if isLoading {
HStack(spacing: 8) {
ProgressView()
Text("Searching your bookmarks…")
.font(PaperType.meta)
.foregroundStyle(Paper.secondary)
}
}
if !answer.isEmpty {
MarkdownAnswer(text: answer)
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
}
if let errorText {
Text(errorText)
.font(PaperType.meta)
.foregroundStyle(Paper.alarm)
}
}
.padding()
}
HStack(spacing: 10) {
TextField("Ask about your bookmarks…", text: $question, axis: .vertical)
.font(PaperType.body)
.foregroundStyle(Paper.ink)
.lineLimit(1...4)
.focused($focused)
.submitLabel(.send)
.onSubmit(send)
Button(action: send) {
Image(systemName: "arrow.up.circle.fill")
.font(.system(size: 26))
.foregroundStyle(Paper.accent)
}
.disabled(question.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || isLoading)
}
.padding()
.background(Paper.raised)
.overlay(alignment: .top) {
Rectangle().fill(Paper.rule.opacity(0.5)).frame(height: 0.6)
}
}
.onAppear { focused = true }
}
private func send() {
let q = question.trimmingCharacters(in: .whitespacesAndNewlines)
guard !q.isEmpty, !isLoading else { return }
question = ""
answer = ""
errorText = nil
isLoading = true
Task {
do {
answer = try await assistant.ask(q)
} catch {
errorText = error.localizedDescription
}
isLoading = false
}
}
}
/// Renders the model's answer as lightweight Markdown. SwiftUI's `Text` only
/// parses *inline* Markdown (bold/italic/code/links), so we split into block
/// elements — headings, bullet/numbered lists, paragraphs — and lay them out,
/// applying inline parsing per line.
private struct MarkdownAnswer: View {
let text: String
private enum Block: Hashable { case heading(String), bullet(String), paragraph(String) }
var body: some View {
VStack(alignment: .leading, spacing: 10) {
ForEach(Array(blocks.enumerated()), id: \.offset) { _, block in
switch block {
case .heading(let line):
inline(line).font(PaperType.heading)
case .bullet(let line):
HStack(alignment: .firstTextBaseline, spacing: 8) {
Text("•").foregroundStyle(Paper.tertiary)
inline(line).font(PaperType.body)
}
case .paragraph(let line):
inline(line).font(PaperType.body)
}
}
}
}
private var blocks: [Block] {
text.split(separator: "\n", omittingEmptySubsequences: true).map { raw in
let line = raw.trimmingCharacters(in: .whitespaces)
if let r = line.range(of: "^#{1,6}\\s+", options: .regularExpression) {
return .heading(String(line[r.upperBound...]))
}
if let r = line.range(of: "^([-*+]|\\d+\\.)\\s+", options: .regularExpression) {
return .bullet(String(line[r.upperBound...]))
}
return .paragraph(line)
}
}
private func inline(_ s: String) -> Text {
let opts = AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
if let attr = try? AttributedString(markdown: s, options: opts) {
return Text(attr)
}
return Text(s)
}
}