Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Immo-Ka iOS9// File : ImmoKa/Design/Components.swift10// Description : composants de base — verdict de prix, pastilles, états11// vides et d'erreur, en-têtes.12// Module : ImmoKa13// Repository : https://git.spboucher.ai/immo-ka-ios-v314// Project Website : https://www.immo-ka.com15// Created : 2026-11-2516// Last Modified : 2026-11-2817//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 SwiftUI9192// MARK: - Verdict de prix9394struct VerdictPill: View {95 let verdict: Verdict?96 var deviation: Double?97 var large = false98 /// fond opaque : lisible par-dessus une photo99 var onPhoto = false100 /// glyphe seul (petite photo d'une ligne de liste) ; le libellé reste lu par VoiceOver101 var glyphOnly = false102103 var body: some View {104 if let verdict {105 HStack(spacing: 4) {106 Image(systemName: VerdictPill.icon(verdict)).font(.system(size: large ? 12 : 10, weight: .bold))107 if !glyphOnly { Text(verdict.label) }108 if let deviation, large, verdict != .marche {109 Text(Fmt.pct(deviation)).num().opacity(0.85)110 }111 }112 .font(.system(size: large ? 14 : 11.5, weight: .semibold))113 .lineLimit(1)114 .fixedSize()115 .foregroundStyle(Theme.color(for: verdict))116 .padding(.horizontal, large ? 12 : glyphOnly ? 6 : 9)117 .padding(.vertical, large ? 7 : 5)118 .background {119 // étiquette à coins mesurés ; sur une photo : fond opaque, sans effet de verre120 RoundedRectangle(cornerRadius: Theme.radiusTag, style: .continuous)121 .fill(onPhoto ? Theme.surface : Theme.softColor(for: verdict))122 }123 .accessibilityElement(children: .ignore)124 .accessibilityLabel("\(verdict.longLabel)\(deviation.map { ", \(Fmt.pct($0))" } ?? "")")125 }126 }127128 /// Glyphe de la position : rien ne se lit par la couleur seule.129 static func icon(_ v: Verdict) -> String {130 switch v {131 case .sous: "arrow.down.to.line"132 case .marche: "equal"133 case .sur: "arrow.up.to.line"134 }135 }136}137138/// Jauge : position du prix demandé dans la fourchette de la valeur estimée.139struct FairValueGauge: View {140 let price: Double141 let fv: Double142 let low: Double143 let high: Double144 let verdict: Verdict?145 /// le repère du prix vient se poser à sa place à l'apparition146 @State private var settle: Double = 0147148 var body: some View {149 GeometryReader { geo in150 let span = max(high - low, 1)151 let lo = low - span * 0.45, hi = high + span * 0.45152 let x: (Double) -> CGFloat = { v in geo.size.width * CGFloat(min(1, max(0, (v - lo) / (hi - lo)))) }153 let color = Theme.color(for: verdict)154 ZStack(alignment: .topLeading) {155 Capsule().fill(Theme.surface3).frame(width: geo.size.width, height: 9).offset(y: 26)156 // fourchette de la valeur estimée : un aplat, sans dégradé157 Capsule()158 .fill(Theme.ink3.opacity(0.38))159 .frame(width: max(4, x(high) - x(low)), height: 9)160 .offset(x: x(low), y: 26)161 RoundedRectangle(cornerRadius: 1).fill(Theme.ink3).frame(width: 2, height: 19).offset(x: x(fv) - 1, y: 21)162 Circle().fill(color).frame(width: 22, height: 22)163 .overlay(Circle().strokeBorder(Theme.surface, lineWidth: 3))164 .offset(x: (x(fv) + (x(price) - x(fv)) * settle) - 11, y: 19.5)165 .opacity(0.25 + 0.75 * settle)166 // étiquettes centrées sur leur repère, maintenues dans la largeur de la jauge167 Text("Prix demandé").font(.caption2.weight(.semibold)).foregroundStyle(color)168 .fixedSize().position(x: min(max(x(price), 38), geo.size.width - 38), y: 7)169 Text(Fmt.compactMoney(low)).font(.caption2).num().foregroundStyle(Theme.ink3)170 .fixedSize().position(x: min(max(x(low), 22), geo.size.width - 22), y: 52)171 Text(Fmt.compactMoney(high)).font(.caption2).num().foregroundStyle(Theme.ink3)172 .fixedSize().position(x: min(max(x(high), 22), geo.size.width - 22), y: 52)173 }174 }175 .frame(height: 60)176 .drawOnAppear($settle, duration: 0.75, delay: 0.1)177 .accessibilityElement(children: .ignore)178 .accessibilityLabel("Prix demandé de \(Fmt.money(price)), valeur estimée \(Fmt.money(fv)), fourchette de \(Fmt.money(low)) à \(Fmt.money(high))")179 }180}181182// MARK: - Pastilles et boutons183184/// Pastille à bascule (filtres).185struct Chip: View {186 let label: String187 var icon: String?188 var active = false189 var tint: Color = Theme.brand190 let action: () -> Void191192 var body: some View {193 Button {194 Haptics.select()195 action()196 } label: {197 HStack(spacing: 6) {198 if let icon { Image(systemName: icon).font(.system(size: 12, weight: .semibold)) }199 Text(label).font(.subheadline.weight(.medium))200 }201 .padding(.horizontal, 14)202 .frame(minHeight: 38)203 .foregroundStyle(active ? tint : Theme.ink2)204 .background(active ? tint.opacity(0.12) : Color.clear, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))205 .overlay(RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous).strokeBorder(active ? tint : Theme.line2, lineWidth: 1))206 .contentShape(Rectangle())207 }208 .buttonStyle(.plain)209 .accessibilityAddTraits(active ? .isSelected : [])210 }211}212213/// Bouton principal plein.214struct PrimaryButtonStyle: ButtonStyle {215 var tint: Color = Theme.brand216 var foreground: Color = Theme.brandInk217 func makeBody(configuration: Configuration) -> some View {218 configuration.label219 .font(.body.weight(.semibold))220 .foregroundStyle(foreground)221 .padding(.horizontal, 22)222 .frame(minHeight: 50)223 .background(configuration.isPressed ? Theme.brandStrong : tint, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))224 .animation(.easeOut(duration: 0.1), value: configuration.isPressed)225 }226}227228struct SoftButtonStyle: ButtonStyle {229 func makeBody(configuration: Configuration) -> some View {230 configuration.label231 .font(.subheadline.weight(.semibold))232 .foregroundStyle(Theme.ink)233 .padding(.horizontal, 16)234 .frame(minHeight: 44)235 .background(configuration.isPressed ? Theme.surface3 : Theme.surface2, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))236 .animation(.easeOut(duration: 0.1), value: configuration.isPressed)237 }238}239240/// Bouton rond opaque (sur photo ou carte).241struct GlassIconButton: View {242 let system: String243 var tint: Color = .primary244 var size: CGFloat = 38245 let label: String246 let action: () -> Void247 var body: some View {248 Button(action: action) {249 Image(systemName: system)250 .font(.system(size: size * 0.42, weight: .semibold))251 .foregroundStyle(tint)252 .frame(width: size, height: size)253 .background(Theme.surface, in: Circle())254 .overlay(Circle().strokeBorder(Theme.line2, lineWidth: 1))255 .shadow(color: .black.opacity(0.12), radius: 5, y: 2)256 .contentShape(Circle().inset(by: -4))257 }258 .buttonStyle(.plain)259 .accessibilityLabel(label)260 }261}262263// MARK: - En-têtes et états264265struct SectionHeader<Trailing: View>: View {266 var eyebrow: String?267 let title: String268 var subtitle: String?269 @ViewBuilder var trailing: () -> Trailing270271 init(eyebrow: String? = nil, title: String, subtitle: String? = nil, @ViewBuilder trailing: @escaping () -> Trailing = { EmptyView() }) {272 self.eyebrow = eyebrow273 self.title = title274 self.subtitle = subtitle275 self.trailing = trailing276 }277278 var body: some View {279 // motif commun : filet haut, titre serif, une phrase, lien à droite. Le sur-titre est réservé280 // aux rares cas où il apporte une information (il n'est plus affiché par défaut).281 VStack(alignment: .leading, spacing: 12) {282 Divider().overlay(Theme.line)283 HStack(alignment: .firstTextBaseline) {284 VStack(alignment: .leading, spacing: 5) {285 Text(title).font(.editorial(23)).foregroundStyle(Theme.ink)286 .fixedSize(horizontal: false, vertical: true)287 if let subtitle {288 Text(subtitle).font(.subheadline).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true)289 }290 }291 Spacer(minLength: 12)292 trailing()293 }294 }295 .accessibilityElement(children: .combine)296 .accessibilityAddTraits(.isHeader)297 }298}299300struct EmptyStateView: View {301 var icon: String = "tray"302 /// illustration de la maison à la place de l'icône (fermée : aucun résultat ; entrouverte : invitation ; cœur : favoris vides)303 var door: BrandIllustration.Kind?304 let title: String305 var message: String?306 var actionTitle: String?307 var action: (() -> Void)?308309 var body: some View {310 VStack(spacing: 14) {311 if let door {312 BrandIllustration(kind: door).padding(.bottom, 4)313 } else {314 Image(systemName: icon)315 .font(.system(size: 24, weight: .medium))316 .foregroundStyle(Theme.ink2)317 .frame(width: 60, height: 60)318 .background(Theme.surface2, in: RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))319 }320 Text(title).font(.editorial(21)).foregroundStyle(Theme.ink).multilineTextAlignment(.center)321 if let message {322 Text(message).font(.subheadline).foregroundStyle(Theme.ink3).multilineTextAlignment(.center)323 }324 if let actionTitle, let action {325 Button(actionTitle, action: action).buttonStyle(SoftButtonStyle()).padding(.top, 4)326 }327 }328 .padding(32)329 .frame(maxWidth: 420)330 .frame(maxWidth: .infinity)331 }332}333334/// État d'erreur avec nouvelle tentative ; message adapté au hors-ligne.335struct ErrorStateView: View {336 let error: Error337 var retry: (() -> Void)?338 var body: some View {339 let offline = (error as? APIError)?.isOffline ?? false340 EmptyStateView(icon: offline ? "wifi.slash" : "exclamationmark.triangle",341 title: offline ? "Vous êtes hors ligne" : "Chargement impossible",342 message: offline ? "Vos favoris et les dernières fiches consultées restent accessibles."343 : ((error as? LocalizedError)?.errorDescription ?? "Réessayez dans un instant."),344 actionTitle: retry == nil ? nil : "Réessayer", action: retry)345 }346}347348/// Bloc fantôme pendant le chargement.349struct Skeleton: View {350 var height: CGFloat = 16351 var radius: CGFloat = 8352 @State private var phase = false353 @Environment(\.accessibilityReduceMotion) private var reduceMotion354 var body: some View {355 RoundedRectangle(cornerRadius: radius, style: .continuous)356 .fill(Theme.surface2)357 .frame(height: height)358 .opacity(phase ? 0.55 : 1)359 .onAppear {360 guard !reduceMotion else { return }361 withAnimation(.easeInOut(duration: 0.9).repeatForever(autoreverses: true)) { phase = true }362 }363 .accessibilityHidden(true)364 }365}366367/// Chiffre clé : grande valeur, libellé discret.368struct StatTile: View {369 let value: String370 let label: String371 var hint: String?372 var body: some View {373 VStack(alignment: .leading, spacing: 4) {374 Text(value).font(.display(26, weight: .semibold)).tracking(-0.5).num().foregroundStyle(Theme.ink)375 .minimumScaleFactor(0.6).lineLimit(1)376 Text(label).font(.footnote.weight(.medium)).foregroundStyle(Theme.ink2)377 if let hint { Text(hint).font(.caption2).foregroundStyle(Theme.ink3) }378 }379 .frame(maxWidth: .infinity, alignment: .leading)380 .accessibilityElement(children: .combine)381 }382}383384/// Ligne « libellé … valeur ».385struct KeyValueRow: View {386 let label: String387 let value: String388 var valueColor: Color = Theme.ink389 var body: some View {390 HStack(alignment: .firstTextBaseline) {391 Text(label).foregroundStyle(Theme.ink2)392 Spacer(minLength: 12)393 Text(value).fontWeight(.semibold).num().foregroundStyle(valueColor).multilineTextAlignment(.trailing)394 }395 .font(.subheadline)396 .padding(.vertical, 9)397 .accessibilityElement(children: .combine)398 }399}400401/// Étoiles d'une note sur 5.402struct Stars: View {403 let rating: Double404 var size: CGFloat = 12405 var body: some View {406 HStack(spacing: 1.5) {407 ForEach(0..<5, id: \.self) { i in408 let v = rating - Double(i)409 Image(systemName: v >= 0.75 ? "star.fill" : v >= 0.25 ? "star.leadinghalf.filled" : "star")410 .font(.system(size: size))411 }412 }413 .foregroundStyle(Theme.high)414 .accessibilityElement()415 .accessibilityLabel(String(format: "%.1f sur 5", rating))416 }417}418419/// Disposition en flux (pastilles qui passent à la ligne).420struct FlowLayout: Layout {421 var spacing: CGFloat = 8422423 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {424 let maxW = proposal.width ?? .infinity425 var x: CGFloat = 0, y: CGFloat = 0, rowH: CGFloat = 0, widest: CGFloat = 0426 for s in subviews {427 let size = s.sizeThatFits(.unspecified)428 if x + size.width > maxW, x > 0 { x = 0; y += rowH + spacing; rowH = 0 }429 x += size.width + spacing430 rowH = max(rowH, size.height)431 widest = max(widest, x - spacing)432 }433 return CGSize(width: min(widest, maxW), height: y + rowH)434 }435436 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {437 var x = bounds.minX, y = bounds.minY, rowH: CGFloat = 0438 for s in subviews {439 let size = s.sizeThatFits(.unspecified)440 if x + size.width > bounds.maxX, x > bounds.minX { x = bounds.minX; y += rowH + spacing; rowH = 0 }441 s.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size))442 x += size.width + spacing443 rowH = max(rowH, size.height)444 }445 }446}447