Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Vrai-Prix iOS9// File : VraiPrix/Design/Components.swift10// Description : composants de base — titres de section, lignes à filet,11// états de chargement, vides et d'erreur, boutons, pastilles12// de filtre. Filets et espace plutôt que boîtes.13// Module : VraiPrix14// Repository : https://git.spboucher.ai/vrai-prix-ios-v315// Project Website : https://www.vrai-prix.com16// Created : 2026-11-2617// Last Modified : 2026-11-2818//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 SwiftUI9293// MARK: - Titres9495/// Titre de section : serif romain mesuré, sur-titre rare, action discrète à droite.96struct SectionHeader<Trailing: View>: View {97 let title: String98 var eyebrow: String?99 var note: String?100 @ViewBuilder var trailing: () -> Trailing101102 init(_ title: String, eyebrow: String? = nil, note: String? = nil, @ViewBuilder trailing: @escaping () -> Trailing = { EmptyView() }) {103 self.title = title; self.eyebrow = eyebrow; self.note = note; self.trailing = trailing104 }105106 var body: some View {107 VStack(alignment: .leading, spacing: 6) {108 if let eyebrow { Text(eyebrow).eyebrowStyle() }109 HStack(alignment: .firstTextBaseline, spacing: 12) {110 Text(title).font(.editorial(21)).foregroundStyle(Theme.ink)111 .fixedSize(horizontal: false, vertical: true)112 .accessibilityAddTraits(.isHeader)113 Spacer(minLength: 0)114 trailing()115 }116 if let note {117 Text(note).font(.subheadline).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true)118 }119 }120 .frame(maxWidth: .infinity, alignment: .leading)121 }122}123124/// Lien « Tout voir » d'une section.125struct MoreLink: View {126 var label = "Tout voir"127 let action: () -> Void128 var body: some View {129 Button(action: action) {130 HStack(spacing: 3) {131 Text(label)132 Image(systemName: "chevron.right").font(.caption2.weight(.bold))133 }134 .font(.subheadline.weight(.medium))135 .foregroundStyle(Theme.brand)136 .frame(minHeight: 44)137 .contentShape(Rectangle())138 }139 .buttonStyle(.plain)140 }141}142143struct Hairline: View {144 var color: Color = Theme.line145 var body: some View { Rectangle().fill(color).frame(height: 1).accessibilityHidden(true) }146}147148/// Ligne « libellé … valeur » séparée par un filet. En très grand texte, la valeur passe sous le libellé.149struct FactRow<Value: View>: View {150 let label: String151 var divider = true152 @ViewBuilder var value: () -> Value153 @Environment(\.dynamicTypeSize) private var typeSize154155 var body: some View {156 VStack(spacing: 0) {157 Group {158 if typeSize.isAccessibilitySize {159 VStack(alignment: .leading, spacing: 4) { labelView; value() }.frame(maxWidth: .infinity, alignment: .leading)160 } else {161 HStack(alignment: .firstTextBaseline, spacing: 12) {162 labelView163 Spacer(minLength: 8)164 value().multilineTextAlignment(.trailing)165 }166 }167 }168 .padding(.vertical, 11)169 if divider { Hairline() }170 }171 .accessibilityElement(children: .combine)172 }173174 private var labelView: some View {175 Text(label).font(.subheadline).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true)176 }177}178179extension FactRow where Value == Text {180 init(_ label: String, _ value: String, divider: Bool = true) {181 self.label = label182 self.divider = divider183 self.value = { Text(value).font(.subheadline.weight(.semibold)).monospacedDigit().foregroundStyle(Theme.ink) }184 }185}186187/// Chiffre clé sobre : petit libellé, valeur tabulaire, précision. Jamais de bandeau de chiffres géants.188struct Figure: View {189 let label: String190 let value: String191 var note: String?192 var body: some View {193 VStack(alignment: .leading, spacing: 3) {194 Text(label).font(.caption).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true)195 Text(value).font(.display(20)).num().foregroundStyle(Theme.ink).lineLimit(1).minimumScaleFactor(0.6)196 if let note { Text(note).font(.caption).num().foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true) }197 }198 .frame(maxWidth: .infinity, alignment: .leading)199 .accessibilityElement(children: .combine)200 }201}202203/// Grille de chiffres clés : deux colonnes, une seule en très grand texte.204struct FigureGrid<Content: View>: View {205 @ViewBuilder var content: () -> Content206 @Environment(\.dynamicTypeSize) private var typeSize207 var body: some View {208 LazyVGrid(columns: typeSize.isAccessibilitySize ? [GridItem(.flexible())] : [GridItem(.flexible(), spacing: 16), GridItem(.flexible(), spacing: 16)],209 alignment: .leading, spacing: 18) { content() }210 }211}212213// MARK: - Boutons214215struct PrimaryButtonStyle: ButtonStyle {216 @Environment(\.isEnabled) private var enabled217 func makeBody(configuration: Configuration) -> some View {218 configuration.label219 .font(.body.weight(.semibold))220 .foregroundStyle(Theme.actionInk)221 .frame(maxWidth: .infinity, minHeight: 50)222 .padding(.horizontal, 16)223 .background(Theme.action.opacity(enabled ? (configuration.isPressed ? 0.85 : 1) : 0.4), in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))224 .scaleEffect(configuration.isPressed ? 0.985 : 1)225 .animation(.easeOut(duration: 0.1), value: configuration.isPressed)226 }227}228229struct SecondaryButtonStyle: ButtonStyle {230 func makeBody(configuration: Configuration) -> some View {231 configuration.label232 .font(.subheadline.weight(.semibold))233 .foregroundStyle(Theme.ink)234 .frame(maxWidth: .infinity, minHeight: 46)235 .padding(.horizontal, 12)236 .background(configuration.isPressed ? Theme.surface2 : Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))237 .overlay(RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous).strokeBorder(Theme.lineControl, lineWidth: 1))238 .animation(.easeOut(duration: 0.1), value: configuration.isPressed)239 }240}241242/// Pastille de filtre : coins mesurés (jamais de capsule), état actif en outremer doux.243struct FilterChip: View {244 let label: String245 var active = false246 var systemImage: String?247 let action: () -> Void248 var body: some View {249 Button(action: { Haptics.select(); action() }) {250 HStack(spacing: 5) {251 if let systemImage { Image(systemName: systemImage).font(.caption.weight(.semibold)) }252 Text(label).lineLimit(1)253 }254 .font(.subheadline.weight(active ? .semibold : .regular))255 .foregroundStyle(active ? Theme.brandStrong : Theme.ink2)256 .padding(.horizontal, 12)257 .frame(minHeight: 38)258 .background(active ? Theme.brandSoft : Theme.surface, in: RoundedRectangle(cornerRadius: 8, style: .continuous))259 .overlay(RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(active ? Theme.brand.opacity(0.5) : Theme.line2, lineWidth: 1))260 .frame(minHeight: 44)261 .contentShape(Rectangle())262 }263 .buttonStyle(.plain)264 .accessibilityAddTraits(active ? .isSelected : [])265 }266}267268// MARK: - États269270/// Attente réelle : le plomb du symbole oscille.271struct LoadingState: View {272 var label = "Chargement"273 var body: some View {274 VStack(spacing: 14) {275 PlumbLoader(label: label)276 Text(label).font(.subheadline).foregroundStyle(Theme.ink3)277 }278 .frame(maxWidth: .infinity)279 .padding(.vertical, 56)280 .accessibilityElement(children: .combine)281 }282}283284/// État vide ou d'erreur : le plomb pend, immobile, dans un gabarit sans sol. Dit ce qui manque et quoi faire.285struct EmptyState: View {286 let title: String287 var message: String?288 var actionLabel: String?289 var action: (() -> Void)?290291 var body: some View {292 VStack(spacing: 12) {293 BrandSymbol(style: .mono(Theme.ink3), noFloor: true).frame(width: 46, height: 46)294 Text(title).font(.editorial(19)).foregroundStyle(Theme.ink).multilineTextAlignment(.center)295 .fixedSize(horizontal: false, vertical: true)296 if let message {297 Text(message).font(.subheadline).foregroundStyle(Theme.ink3).multilineTextAlignment(.center)298 .fixedSize(horizontal: false, vertical: true)299 }300 if let actionLabel, let action {301 Button(actionLabel, action: action).buttonStyle(SecondaryButtonStyle()).frame(maxWidth: 240).padding(.top, 6)302 }303 }304 .frame(maxWidth: 420)305 .frame(maxWidth: .infinity)306 .padding(.vertical, 44)307 .padding(.horizontal, 24)308 .accessibilityElement(children: .contain)309 }310}311312/// Erreur de chargement, dite simplement ; hors ligne distingué du reste.313struct ErrorState: View {314 let error: Error315 var subject = "Cette page"316 let retry: () -> Void317318 var body: some View {319 let api = error as? APIError320 if api == .offline {321 EmptyState(title: "Hors ligne", message: "\(subject) n'a pas encore été consultée sur cet appareil. Elle s'affichera dès que la connexion reviendra.",322 actionLabel: "Réessayer", action: retry)323 } else if api == .unavailable {324 EmptyState(title: "Valeurs en cours de mise à jour", message: "Le calcul des valeurs se rafraîchit en ce moment. Aucun chiffre de remplacement n'est affiché : réessayez dans un instant.",325 actionLabel: "Réessayer", action: retry)326 } else if api == .notFound || api == .gone {327 EmptyState(title: "Introuvable", message: "\(subject) n'existe pas ou n'existe plus.")328 } else {329 EmptyState(title: "Chargement impossible", message: (api?.errorDescription ?? "Une erreur est survenue.") + " Réessayez dans un instant.",330 actionLabel: "Réessayer", action: retry)331 }332 }333}334335/// Bandeau discret : la page vient du cache, faute de connexion.336struct StaleNotice: View {337 var body: some View {338 HStack(spacing: 8) {339 Image(systemName: "wifi.slash").font(.footnote.weight(.semibold))340 Text("Hors ligne : dernière version consultée sur cet appareil.").font(.footnote)341 .fixedSize(horizontal: false, vertical: true)342 Spacer(minLength: 0)343 }344 .foregroundStyle(Theme.ink2)345 .padding(.horizontal, 12).padding(.vertical, 9)346 .background(Theme.surface2, in: RoundedRectangle(cornerRadius: 8, style: .continuous))347 .accessibilityElement(children: .combine)348 }349}350351/// État d'une ressource chargée.352enum Loadable<T: Sendable>: Sendable {353 case loading354 case loaded(T, stale: Bool)355 case failed(APIError)356357 var value: T? { if case .loaded(let v, _) = self { return v }; return nil }358 var isStale: Bool { if case .loaded(_, let s) = self { return s }; return false }359}360361func apiError(_ e: Error) -> APIError { (e as? APIError) ?? .server("Une erreur est survenue.") }362363/// Puce de confiance A à D, dite honnêtement.364enum Confidence {365 static func label(_ level: String?) -> String {366 switch level?.uppercased() {367 case "A": "Élevée"368 case "B": "Bonne"369 case "C": "Moyenne"370 case "D": "Faible"371 default: "Non établie"372 }373 }374375 static func explanation(_ level: String?) -> String {376 switch level?.uppercased() {377 case "A": "Beaucoup de ventes comparables récentes et proches, qui s'accordent entre elles."378 case "B": "Assez de ventes comparables, avec une dispersion modérée."379 case "C": "Peu de ventes comparables ou des comparables qui divergent : la fourchette est large."380 case "D": "Très peu de ventes comparables : l'estimation est indicative, la fourchette très large."381 default: "Le service n'a pas publié d'indice de confiance pour cette propriété."382 }383 }384}385386struct ConfidenceBadge: View {387 let level: String?388 var body: some View {389 HStack(spacing: 6) {390 Text(level?.uppercased() ?? "—").font(.caption.weight(.bold)).monospaced()391 .foregroundStyle(Theme.ink)392 .frame(width: 22, height: 22)393 .overlay(RoundedRectangle(cornerRadius: 4, style: .continuous).strokeBorder(Theme.ink2, lineWidth: 1.2))394 Text(Confidence.label(level)).font(.subheadline.weight(.semibold)).foregroundStyle(Theme.ink)395 }396 .accessibilityElement(children: .ignore)397 .accessibilityLabel("Fiabilité \(Confidence.label(level).lowercased()), niveau \(level ?? "inconnu")")398 }399}400