Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Search iOS9// File : Widget/SPSearchWidget.swift10// Description : Home and Lock Screen widgets: quick search, recent11// searches of this device, and the index figures read from12// /v2/status.13// Module : Widget14// Repository : https://git.spboucher.ai/sp-search-ios15// Project Website : https://search.spboucher.ai16// Created : 2026-12-0117// Last Modified : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import SPSearchKit92import SwiftUI93import WidgetKit9495struct SearchEntry: TimelineEntry {96 let date: Date97 /// Recent searches of this device (empty when the history is off).98 let recents: [String]99 /// What `/v2/status` answered; nil when it could not be read — the widget then shows no figure at all.100 let status: EngineStatus?101}102103struct SearchProvider: TimelineProvider {104 static func recents() -> [String] {105 let history = HistoryStore(defaults: SharedStore.defaults)106 return history.isEnabled ? history.entries.prefix(4).map(\.query) : []107 }108109 func placeholder(in context: Context) -> SearchEntry { SearchEntry(date: .now, recents: [], status: nil) }110111 func getSnapshot(in context: Context, completion: @escaping @Sendable (SearchEntry) -> Void) {112 // The gallery shows the layout with the reader's own recent searches, and no invented figure.113 completion(SearchEntry(date: .now, recents: Self.recents(), status: nil))114 }115116 func getTimeline(in context: Context, completion: @escaping @Sendable (Timeline<SearchEntry>) -> Void) {117 let recents = Self.recents()118 Task {119 let client = SPSearchClient(userAgent: "SPSearch-iOS-widget")120 let status = try? await client.status()121 let entry = SearchEntry(date: .now, recents: recents, status: status)122 completion(Timeline(entries: [entry], policy: .after(Date().addingTimeInterval(3_600))))123 }124 }125}126127enum WidgetText {128 /// "17,5 M documents · à jour il y a 2 h"129 static func status(_ s: EngineStatus, now: Date) -> String? {130 guard s.documents > 0 else { return nil }131 let docs = s.documents.formatted(.number.notation(.compactName).precision(.fractionLength(0...1)))132 var parts = [String(localized: "\(docs) documents")]133 if let d = s.updatedAt, now.timeIntervalSince(d) >= 0 {134 parts.append(String(localized: "index à jour \(d.formatted(.relative(presentation: .numeric, unitsStyle: .abbreviated)))"))135 }136 return parts.joined(separator: " · ")137 }138}139140/// The search field as a target: one tap opens the app with the cursor in its field.141struct FieldShape: View {142 var body: some View {143 HStack(spacing: 8) {144 Image(systemName: "magnifyingglass").font(.footnote.weight(.semibold)).foregroundStyle(Theme.accent)145 Text("Rechercher").font(.subheadline).foregroundStyle(Theme.muted).lineLimit(1)146 Spacer(minLength: 0)147 }148 .padding(.horizontal, 12)149 .frame(maxWidth: .infinity, minHeight: 38)150 .background(Theme.raised, in: RoundedRectangle(cornerRadius: 12, style: .continuous))151 .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).stroke(Theme.rule, lineWidth: 1))152 }153}154155struct Brand: View {156 var body: some View {157 HStack(spacing: 8) {158 MarkView(size: 22)159 Text(verbatim: "SPSearch").font(.system(.headline, design: .serif, weight: .medium)).foregroundStyle(Theme.ink).lineLimit(1).minimumScaleFactor(0.7)160 }161 .accessibilityElement(children: .combine)162 }163}164165struct SmallView: View {166 let entry: SearchEntry167 var body: some View {168 VStack(alignment: .leading, spacing: 0) {169 Brand()170 Spacer(minLength: 6)171 if let s = entry.status, let line = WidgetText.status(s, now: entry.date) {172 Text(verbatim: line).font(.system(.caption2, design: .monospaced)).foregroundStyle(Theme.muted).lineLimit(2).minimumScaleFactor(0.8)173 .padding(.bottom, 8)174 } else {175 Text("Moteur de recherche indépendant").font(.caption2).foregroundStyle(Theme.muted).lineLimit(2).padding(.bottom, 8)176 }177 FieldShape()178 }179 .widgetURL(SearchLink.newSearchURL)180 }181}182183struct MediumView: View {184 let entry: SearchEntry185 var body: some View {186 HStack(alignment: .top, spacing: 14) {187 VStack(alignment: .leading, spacing: 0) {188 Brand()189 Spacer(minLength: 6)190 if let s = entry.status, let line = WidgetText.status(s, now: entry.date) {191 Text(verbatim: line).font(.system(.caption2, design: .monospaced)).foregroundStyle(Theme.muted).lineLimit(2).minimumScaleFactor(0.8)192 .padding(.bottom, 8)193 }194 Link(destination: SearchLink.newSearchURL) { FieldShape() }195 }196 .frame(maxWidth: .infinity, alignment: .leading)197 Rectangle().fill(Theme.rule).frame(width: 1)198 VStack(alignment: .leading, spacing: 0) {199 Text("Recherches récentes").font(.system(.caption2, design: .monospaced, weight: .medium)).textCase(.uppercase).tracking(0.8)200 .foregroundStyle(Theme.muted).lineLimit(1).minimumScaleFactor(0.7).padding(.bottom, 4)201 if entry.recents.isEmpty {202 Text("Aucune pour l’instant. Elles restent sur cet appareil.").font(.caption).foregroundStyle(Theme.muted)203 Spacer(minLength: 0)204 } else {205 ForEach(Array(entry.recents.prefix(3).enumerated()), id: \.offset) { i, q in206 if i > 0 { Rectangle().fill(Theme.rule).frame(height: 1) }207 Link(destination: SearchLink(query: q).webURL) {208 HStack(spacing: 6) {209 Image(systemName: "clock").font(.caption2).foregroundStyle(Theme.muted)210 Text(verbatim: q).font(.footnote).foregroundStyle(Theme.ink).lineLimit(1)211 Spacer(minLength: 0)212 }213 .frame(maxHeight: .infinity)214 }215 .accessibilityLabel(Text("Relancer la recherche \(q)"))216 }217 }218 }219 .frame(maxWidth: .infinity, alignment: .leading)220 }221 }222}223224struct SearchWidgetView: View {225 @Environment(\.widgetFamily) private var family226 let entry: SearchEntry227228 var body: some View {229 Group {230 switch family {231 case .systemMedium: MediumView(entry: entry)232 case .accessoryCircular:233 ZStack {234 AccessoryWidgetBackground()235 Image(systemName: "magnifyingglass").font(.title2.weight(.semibold))236 }237 .widgetURL(SearchLink.newSearchURL)238 .accessibilityLabel("Rechercher avec SPSearch")239 default: SmallView(entry: entry)240 }241 }242 .containerBackground(for: .widget) { Theme.paper }243 }244}245246struct SPSearchQuickWidget: Widget {247 var body: some WidgetConfiguration {248 StaticConfiguration(kind: "ai.spboucher.spsearch.quick", provider: SearchProvider()) { entry in249 SearchWidgetView(entry: entry)250 }251 .configurationDisplayName("SPSearch")252 .description("Recherche rapide, recherches récentes de cet appareil et état de l’index.")253 .supportedFamilies([.systemSmall, .systemMedium, .accessoryCircular])254 }255}256257@main258struct SPSearchWidgets: WidgetBundle {259 var body: some Widget { SPSearchQuickWidget() }260}261