SPB Git

Repository Public

PreviewSheet.swift 12.9 KB 254 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Search iOS9// File             : Features/Results/PreviewSheet.swift10// Description      : Preview of a result: everything the index already told us11//                    about the page, without loading the page.12// Module           : Features13// Repository       : https://git.spboucher.ai/sp-search-ios14// Project Website  : https://search.spboucher.ai15// Created          : 2026-12-0116// Last Modified    : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SPSearchKit91import SwiftUI92import UIKit9394/// The sheet is made of the fields of the search answer only. No web view, no request to the third-party site:95/// the page is contacted when the reader chooses to open it, not before.96struct PreviewSheet: View {97    let result: SearchResult98    var open: (URL) -> Void99    var openExternally: (URL) -> Void100    var moreFromSite: (String) -> Void101    @Environment(SearchModel.self) private var model102    @Environment(FavoritesModel.self) private var favorites103    @Environment(\.dismiss) private var dismiss104    @Environment(\.dynamicTypeSize) private var typeSize105    @State private var copied = false106107    var body: some View {108        NavigationStack {109            ScrollView {110                VStack(alignment: .leading, spacing: 18) {111                    SiteLine(result: result, iconSize: 40)112                    Text(ResultText.title(result))113                        .font(.display(.title, weight: .medium)).foregroundStyle(Theme.ink)114                        .fixedSize(horizontal: false, vertical: true)115                        .accessibilityAddTraits(.isHeader)116                    Text(verbatim: result.url)117                        .font(.data(.caption)).foregroundStyle(Theme.muted)118                        .textSelection(.enabled)119                        .fixedSize(horizontal: false, vertical: true)120                    actions121                    if !result.snippet.isEmpty {122                        VStack(alignment: .leading, spacing: 8) {123                            Eyebrow(result.snippetSource == "static" ? "Description de la page" : "Extrait")124                            Text(ResultText.snippet(result, terms: model.highlightTerms))125                                .font(.body).foregroundStyle(Theme.muted).lineSpacing(3)126                                .fixedSize(horizontal: false, vertical: true)127                                .textSelection(.enabled)128                        }129                    } else if result.snippetSource == "none" {130                        Text("Cette page n’autorise pas l’affichage d’un extrait.").font(.subheadline).foregroundStyle(Theme.muted)131                    }132                    facts133                    if !result.sitelinks.isEmpty {134                        VStack(alignment: .leading, spacing: 0) {135                            Eyebrow("Autres pages du site").padding(.bottom, 6)136                            ForEach(result.sitelinks) { link in137                                Button { if let url = link.link { open(url) } } label: {138                                    HStack {139                                        Text(verbatim: link.title).font(.body).foregroundStyle(Theme.accent).multilineTextAlignment(.leading)140                                        Spacer(minLength: 8)141                                        Image(systemName: "chevron.right").font(.caption.weight(.semibold)).foregroundStyle(Theme.ruleStrong).accessibilityHidden(true)142                                    }143                                    .padding(.vertical, 11).contentShape(Rectangle())144                                }145                                .buttonStyle(.plain)146                                Rule()147                            }148                        }149                    }150                    if !result.domain.isEmpty {151                        Button { moreFromSite(result.domain) } label: {152                            Label {153                                Text(result.moreFromSite > 0 ? "Plus de résultats de \(result.domain) (\(Format.count(result.moreFromSite)))" : "Plus de résultats de \(result.domain)")154                            } icon: { Image(systemName: "text.magnifyingglass") }155                            .font(.subheadline.weight(.medium))156                        }157                    }158                    Text("Aperçu composé des données de l’index. La page elle-même n’est pas chargée tant que vous ne l’ouvrez pas.")159                        .font(.caption).foregroundStyle(Theme.muted)160                        .fixedSize(horizontal: false, vertical: true)161                }162                .padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 28)163                .frame(maxWidth: Theme.column, alignment: .leading)164                .frame(maxWidth: .infinity)165            }166            .background(Theme.paper)167            .navigationTitle("Aperçu")168            .navigationBarTitleDisplayMode(.inline)169            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Fermer") { dismiss() } } }170        }171        .tint(Theme.accent)172        .presentationDetents([.medium, .large])173        .presentationDragIndicator(.visible)174    }175176    /// "Open" across the width, then the three secondary actions side by side (stacked at the largest text sizes).177    private var actions: some View {178        let saved = favorites.contains(result.url)179        return VStack(spacing: 8) {180            if let url = result.link {181                Button { open(url) } label: { Label("Ouvrir", systemImage: "safari").frame(maxWidth: .infinity) }.buttonStyle(ActionStyle(primary: true))182            }183            let layout = typeSize.isAccessibilitySize ? AnyLayout(VStackLayout(spacing: 8)) : AnyLayout(HStackLayout(spacing: 8))184            layout {185                Button { favorites.toggle(result) } label: { Label(saved ? "Enregistré" : "Favori", systemImage: saved ? "star.fill" : "star").frame(maxWidth: .infinity) }186                    .buttonStyle(ActionStyle())187                    .accessibilityLabel(saved ? Text("Retirer des favoris") : Text("Ajouter aux favoris"))188                if let url = result.link {189                    ShareLink(item: url, subject: Text(verbatim: result.title)) { Label("Partager", systemImage: "square.and.arrow.up").frame(maxWidth: .infinity) }.buttonStyle(ActionStyle())190                    Button { UIPasteboard.general.url = url; copied = true } label: { Label(copied ? "Copié" : "Copier", systemImage: copied ? "checkmark" : "doc.on.doc").frame(maxWidth: .infinity) }191                        .buttonStyle(ActionStyle())192                        .accessibilityLabel("Copier le lien")193                }194            }195        }196    }197198    private var facts: some View {199        VStack(alignment: .leading, spacing: 0) {200            Eyebrow("Ce que l’index sait de cette page").padding(.bottom, 6)201            fact("Type", result.kind.label)202            if let l = result.language { fact("Langue", Format.languageName(l)) }203            // Only a publication date the engine verified is shown as one.204            fact("Publication", result.publishedAt.map { Format.day($0) } ?? String(localized: "Aucune date vérifiée"))205            if let v = Format.validation(result) {206                fact("Adresse", v.text + (result.validation?.httpStatus.map { " · HTTP \($0)" } ?? ""), warning: v.warning)207            }208            if let minutes = result.readingMinutes {209                fact("Longueur", String(localized: "≈ \(Format.count(result.words)) mots · \(minutes) min de lecture"))210            }211            if result.isReferenceSite { fact("Site", String(localized: "Site de référence dans le graphe de liens de l’index")) }212            Text("« Adresse vérifiée » décrit la dernière visite technique du robot à cette adresse. Ce n’est pas une vérification du contenu de la page.")213                .font(.caption).foregroundStyle(Theme.muted).padding(.top, 10)214                .fixedSize(horizontal: false, vertical: true)215        }216    }217218    private func fact(_ label: LocalizedStringKey, _ value: String, warning: Bool = false) -> some View {219        VStack(spacing: 0) {220            ViewThatFits(in: .horizontal) {221                HStack(alignment: .firstTextBaseline, spacing: 12) {222                    Text(label).font(.subheadline).foregroundStyle(Theme.muted).frame(width: 110, alignment: .leading)223                    Text(verbatim: value).font(.subheadline).foregroundStyle(warning ? Theme.warn : Theme.ink).frame(maxWidth: .infinity, alignment: .leading)224                }225                VStack(alignment: .leading, spacing: 2) {226                    Text(label).font(.subheadline).foregroundStyle(Theme.muted)227                    Text(verbatim: value).font(.subheadline).foregroundStyle(warning ? Theme.warn : Theme.ink).fixedSize(horizontal: false, vertical: true)228                }229                .frame(maxWidth: .infinity, alignment: .leading)230            }231            .padding(.vertical, 10)232            Rule()233        }234        .accessibilityElement(children: .combine)235    }236}237238/// Buttons of the preview: the first one carries the accent, the others a hairline.239struct ActionStyle: ButtonStyle {240    var primary = false241    func makeBody(configuration: Configuration) -> some View {242        configuration.label243            .labelStyle(.titleAndIcon)244            .font(.subheadline.weight(.semibold))245            .lineLimit(1).minimumScaleFactor(0.8)246            .foregroundStyle(primary ? Theme.paper : Theme.ink)247            .padding(.horizontal, 10).padding(.vertical, 9)248            .frame(minHeight: 40)249            .background(primary ? Theme.accent : Theme.raised, in: RoundedRectangle(cornerRadius: 10, style: .continuous))250            .overlay(RoundedRectangle(cornerRadius: 10, style: .continuous).stroke(primary ? Color.clear : Theme.rule, lineWidth: 1))251            .opacity(configuration.isPressed ? 0.7 : 1)252    }253}254
Files main
Menu

Theme