Repository Public
1// SP Notes iOS2// File: App/Intents/AskRequests.swift3// Description: « Demander à mes notes » from outside the screens (Siri, Shortcuts). The question is first offered to the app's4// routing as `spnotes://ask?q=…` (the dedicated Ask screen, when the shell hosts it); until that link exists it5// waits here and `askRequestHost()` presents the existing assistant conversation (« Questions », scope « Mes6// notes ») with the question shown and ready to send. Nothing is sent and nothing is answered without a tap.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Observation11import SPNotesCore12import SPNotesDesign13import SwiftUI1415struct AskRequest: Identifiable, Equatable {16 let id = UUID()17 var question: String18}1920@MainActor21@Observable22final class AskRequests {23 static let shared = AskRequests()24 /// The question waiting for the assistant sheet.25 var pending: AskRequest?2627 /// `spnotes://ask?q=<question>`.28 static func url(for question: String) -> URL {29 var components = URLComponents()30 components.scheme = DeepLink.scheme31 components.host = "ask"32 if !question.isEmpty { components.queryItems = [URLQueryItem(name: "q", value: question)] }33 return components.url ?? URL(string: "spnotes://ask")!34 }3536 /// Returns true when the app's routing took the link; otherwise the question waits in `pending`.37 @discardableResult38 func ask(_ question: String, app: AppModel) -> Bool {39 let clean = question.trimmingCharacters(in: .whitespacesAndNewlines)40 if app.open(url: Self.url(for: clean)) { return true }41 pending = AskRequest(question: clean)42 return false43 }44}4546extension View {47 /// Applied once at the root of the app (next to the other root modifiers of `SPNotesApp`).48 func askRequestHost() -> some View { modifier(AskRequestHost()) }49}5051private struct AskRequestHost: ViewModifier {52 @Environment(AppModel.self) private var app53 @State private var requests = AskRequests.shared5455 func body(content: Content) -> some View {56 content.sheet(item: Binding(get: { app.lock.isLocked ? nil : requests.pending }, set: { requests.pending = $0 })) { request in57 AskSheet(request: request, app: app)58 }59 }60}6162/// The assistant conversation opened on a question that came from Siri or Shortcuts.63private struct AskSheet: View {64 let request: AskRequest65 let app: AppModel66 @State private var chat: AssistantChatModel67 @State private var sent = false68 @Environment(\.dismiss) private var dismiss6970 init(request: AskRequest, app: AppModel) {71 self.request = request72 self.app = app73 _chat = State(initialValue: AssistantChatModel(pageId: nil, pageIsOnServer: false, provider: BackendAssistantProvider(api: app.api), availability: { [app] in74 if app.session != .signedIn { return AssistantFailure(.sessionExpired) }75 if !app.network.isOnline { return AssistantFailure(.offline) }76 return nil77 }))78 }7980 var body: some View {81 NavigationStack {82 VStack(spacing: 0) {83 if !sent, !request.question.isEmpty { questionCard }84 AssistantChatView(model: chat, noteTitle: "") { id, _ in85 dismiss()86 if let url = SystemLinks.note(id) { app.open(url: url) }87 }88 }89 .background(NVColor.bg)90 .navigationTitle("Demander à mes notes")91 .navigationBarTitleDisplayMode(.inline)92 .toolbar {93 ToolbarItem(placement: .cancellationAction) { Button("Fermer") { dismiss() }.accessibilityIdentifier("ask-close") }94 }95 }96 .accessibilityIdentifier("ask-sheet")97 .onDisappear { chat.stop() }98 }99100 private var questionCard: some View {101 VStack(alignment: .leading, spacing: NVSpace.sm) {102 Text("Ta question").font(.caption.weight(.semibold)).foregroundStyle(NVColor.fgMuted)103 Text(request.question).nvFont(.reading).foregroundStyle(NVColor.fg).frame(maxWidth: .infinity, alignment: .leading)104 Button {105 sent = true106 chat.mode = .notes107 chat.send(request.question)108 } label: {109 Label("Demander", systemImage: "sparkles").font(.subheadline.weight(.semibold))110 }111 .buttonStyle(.borderedProminent)112 .tint(NVColor.ai)113 .accessibilityIdentifier("ask-send")114 }115 .padding(NVSpace.lg)116 .background(NVColor.aiSoft, in: RoundedRectangle(cornerRadius: NVRadius.lg, style: .continuous))117 .padding(NVSpace.lg)118 }119}120