SPB Git

Repository Public

Composants.swift 17.5 KB 466 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Valo-Plex iOS9// File             : ValoPlex/Design/Composants.swift10// Description      : pièces communes de l'interface : étiquettes, cartons,11//                    lignes de compte, pastilles, boutons, grilles de chiffres,12//                    tableaux défilants, états de chargement et d'erreur.13// Module           : ValoPlex14// Repository       : https://git.spboucher.ai/valo-plex-ios15// Project Website  : https://www.valo-plex.com16// 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 SwiftUI9293/// Étiquette en petites capitales espacées, chasse fixe.94struct Etiquette: View {95    let texte: String96    var couleur: Color = VX.encre29798    init(_ texte: String, couleur: Color = VX.encre2) {99        self.texte = texte100        self.couleur = couleur101    }102103    var body: some View {104        Text(texte.uppercased())105            .font(.mono(11, style: .caption))106            .tracking(1.0)107            .foregroundStyle(couleur)108            .fixedSize(horizontal: false, vertical: true)109            .accessibilityLabel(texte)   // lu en casse normale, pas lettre à lettre110    }111}112113struct Filet: View {114    var couleur: Color = VX.trait115    var body: some View { Rectangle().fill(couleur).frame(height: 1) }116}117118/// En-tête de section : étiquette, puis titre en serif.119struct TitreSection: View {120    let etiquette: String121    let titre: String122    var chapeau: String? = nil123124    var body: some View {125        VStack(alignment: .leading, spacing: 8) {126            Etiquette(etiquette)127            Text(titre)128                .font(.serif(25, style: .title2))129                .foregroundStyle(VX.encre)130                .fixedSize(horizontal: false, vertical: true)131                .accessibilityAddTraits(.isHeader)132            if let chapeau {133                Text(chapeau).font(.sans(16)).foregroundStyle(VX.encre2).fixedSize(horizontal: false, vertical: true)134            }135        }136        .frame(maxWidth: .infinity, alignment: .leading)137        .padding(.top, 44)138        .padding(.bottom, 16)139    }140}141142extension View {143    /// Carton : fond clair, filet d'encre d'un point, angles droits.144    func carton(_ marge: CGFloat = 18, fond: Color = VX.carton, filet: Color = VX.encre) -> some View {145        padding(marge)146            .frame(maxWidth: .infinity, alignment: .leading)147            .background(fond)148            .overlay(Rectangle().strokeBorder(filet, lineWidth: 1))149    }150}151152/// Ligne de compte : libellé à gauche, chiffre en chasse fixe à droite.153struct Ligne: View {154    let libelle: String155    let valeur: String156    var total = false157    var couleur: Color = VX.encre158    var premiere = false159160    init(_ libelle: String, _ valeur: String, total: Bool = false, couleur: Color = VX.encre, premiere: Bool = false) {161        self.libelle = libelle162        self.valeur = valeur163        self.total = total164        self.couleur = couleur165        self.premiere = premiere166    }167168    var body: some View {169        VStack(spacing: 0) {170            if !premiere { Filet(couleur: total ? VX.encre : VX.papier2) }171            HStack(alignment: .firstTextBaseline, spacing: 14) {172                Text(libelle)173                    .font(.sans(15, total ? .demiGras : .normal, style: .subheadline))174                    .foregroundStyle(VX.encre)175                    .fixedSize(horizontal: false, vertical: true)176                Spacer(minLength: 8)177                Text(valeur)178                    .font(.mono(14, total ? .moyen : .normal, style: .subheadline))179                    .foregroundStyle(couleur)180                    .multilineTextAlignment(.trailing)181                    .layoutPriority(1)182            }183            .padding(.vertical, 9)184        }185        .accessibilityElement(children: .combine)186    }187}188189struct Pastille: View {190    let texte: String191    let couleur: Color192193    init(_ texte: String, _ couleur: Color) {194        self.texte = texte195        self.couleur = couleur196    }197198    var body: some View {199        Text(texte.uppercased())200            .font(.mono(10.5, style: .caption2))201            .tracking(0.9)202            .foregroundStyle(couleur)203            .padding(.horizontal, 8)204            .padding(.vertical, 4)205            .overlay(Rectangle().strokeBorder(couleur, lineWidth: 1))206    }207}208209/// Note en marge : filet à gauche, texte discret.210struct Note: View {211    let texte: String212    init(_ texte: String) { self.texte = texte }213214    var body: some View {215        Text(texte)216            .font(.sans(13.5, style: .footnote))217            .foregroundStyle(VX.encre2)218            .fixedSize(horizontal: false, vertical: true)219            .padding(.leading, 12)220            .frame(maxWidth: .infinity, alignment: .leading)221            .overlay(alignment: .leading) { Rectangle().fill(VX.trait).frame(width: 2) }222    }223}224225struct Avis: View {226    let texte: String227    init(_ texte: String) { self.texte = texte }228229    var body: some View {230        Text(texte)231            .font(.sans(14.5, style: .subheadline))232            .foregroundStyle(VX.avisTexte)233            .fixedSize(horizontal: false, vertical: true)234            .padding(.horizontal, 14)235            .padding(.vertical, 10)236            .frame(maxWidth: .infinity, alignment: .leading)237            .background(VX.avisFond)238            .overlay(Rectangle().strokeBorder(VX.ocre, lineWidth: 1))239    }240}241242struct BoutonEncre: ButtonStyle {243    var clair = false244245    func makeBody(configuration: Configuration) -> some View {246        configuration.label247            .font(.sans(15, .moyen, style: .subheadline))248            .foregroundStyle(clair ? VX.encre : VX.papier)249            .padding(.horizontal, 18)250            .padding(.vertical, 12)251            .background(clair ? (configuration.isPressed ? VX.papier2 : Color.clear) : (configuration.isPressed ? VX.briqueFonce : VX.encre))252            .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1.5))253            .contentShape(Rectangle())254    }255}256257/// Grille de chiffres clés : grand chiffre en serif, légende dessous, cases séparées par des filets.258struct GrilleChiffres: View {259    struct Case: Identifiable {260        let valeur: String261        let legende: String262        var id: String { legende }263    }264265    let cases: [Case]266    @Environment(\.horizontalSizeClass) private var classe267268    var body: some View {269        let colonnes = classe == .regular ? 4 : 2270        let rangees = stride(from: 0, to: cases.count, by: colonnes).map { Array(cases[$0..<min($0 + colonnes, cases.count)]) }271        VStack(spacing: 0) {272            Filet(couleur: VX.encre)273            ForEach(Array(rangees.enumerated()), id: \.offset) { i, rangee in274                if i > 0 { Filet() }275                HStack(alignment: .top, spacing: 0) {276                    ForEach(Array(rangee.enumerated()), id: \.element.id) { j, c in277                        VStack(alignment: .leading, spacing: 4) {278                            Text(c.valeur)279                                .font(.serif(25, style: .title2))280                                .foregroundStyle(VX.encre)281                                .lineLimit(1)282                                .minimumScaleFactor(0.6)283                            Text(c.legende)284                                .font(.sans(13.5, style: .footnote))285                                .foregroundStyle(VX.encre2)286                                .fixedSize(horizontal: false, vertical: true)287                        }288                        .padding(.vertical, 16)289                        .padding(.leading, j == 0 ? 0 : 14)290                        .padding(.trailing, 10)291                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)292                        .overlay(alignment: .leading) { if j > 0 { Rectangle().fill(VX.trait).frame(width: 1) } }293                        .accessibilityElement(children: .combine)294                    }295                    if rangee.count < colonnes {296                        ForEach(0..<(colonnes - rangee.count), id: \.self) { _ in Color.clear.frame(maxWidth: .infinity) }297                    }298                }299                .fixedSize(horizontal: false, vertical: true)300            }301            Filet(couleur: VX.encre)302        }303    }304}305306/// Liste de faits (caractéristiques du rôle) : étiquette au-dessus, valeur dessous, en cases.307struct GrilleFaits: View {308    let faits: [(String, String)]309    @Environment(\.horizontalSizeClass) private var classe310311    var body: some View {312        let colonnes = classe == .regular ? 4 : 2313        let rangees = stride(from: 0, to: faits.count, by: colonnes).map { Array(faits[$0..<min($0 + colonnes, faits.count)]) }314        VStack(spacing: 0) {315            ForEach(Array(rangees.enumerated()), id: \.offset) { i, rangee in316                if i > 0 { Filet(couleur: VX.papier3) }317                HStack(alignment: .top, spacing: 0) {318                    ForEach(Array(rangee.enumerated()), id: \.offset) { j, f in319                        VStack(alignment: .leading, spacing: 4) {320                            Etiquette(f.0)321                            Text(f.1).font(.sans(16)).foregroundStyle(VX.encre).fixedSize(horizontal: false, vertical: true)322                        }323                        .padding(.horizontal, 14)324                        .padding(.vertical, 12)325                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)326                        .overlay(alignment: .leading) { if j > 0 { Rectangle().fill(VX.papier3).frame(width: 1) } }327                        .accessibilityElement(children: .combine)328                    }329                    if rangee.count < colonnes {330                        ForEach(0..<(colonnes - rangee.count), id: \.self) { _ in Color.clear.frame(maxWidth: .infinity) }331                    }332                }333                .fixedSize(horizontal: false, vertical: true)334            }335        }336        .background(VX.carton)337        .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1))338    }339}340341/// Tableau à filets : première colonne à gauche, chiffres en chasse fixe à droite. Défile à l'horizontale342/// lorsqu'il dépasse la largeur de l'écran, comme les tableaux du site.343struct Tableau: View {344    let entetes: [String]345    let lignes: [[String]]346    /// Route ouverte par la première cellule d'une ligne, s'il y en a une.347    var lien: ((Int) -> Route?)? = nil348    /// Colonnes de texte (alignées à gauche, police courante) en plus de la première.349    var texte: Set<Int> = []350351    var body: some View {352        ViewThatFits(in: .horizontal) {353            grille354            ScrollView(.horizontal, showsIndicators: false) { grille }355        }356        .background(VX.carton)357        .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1))358    }359360    private var grille: some View {361        Grid(alignment: .trailing, horizontalSpacing: 0, verticalSpacing: 0) {362            GridRow {363                ForEach(Array(entetes.enumerated()), id: \.offset) { j, e in364                    Text(e.uppercased())365                        .font(.mono(10, style: .caption2))366                        .tracking(0.8)367                        .foregroundStyle(VX.encre2)368                        .lineLimit(1)369                        .padding(.horizontal, 12)370                        .padding(.vertical, 10)371                        .frame(maxWidth: j == 0 ? .infinity : nil, alignment: aGauche(j) ? .leading : .trailing)372                        .gridColumnAlignment(aGauche(j) ? .leading : .trailing)373                }374            }375            Rectangle().fill(VX.encre).frame(height: 1).gridCellColumns(max(1, entetes.count)).gridCellUnsizedAxes(.horizontal)376            ForEach(Array(lignes.enumerated()), id: \.offset) { i, ligne in377                if i > 0 {378                    Rectangle().fill(VX.papier2).frame(height: 1).gridCellColumns(max(1, entetes.count)).gridCellUnsizedAxes(.horizontal)379                }380                GridRow {381                    ForEach(Array(ligne.enumerated()), id: \.offset) { j, cellule in382                        contenu(i, j, cellule)383                            .lineLimit(1)384                            .padding(.horizontal, 12)385                            .padding(.vertical, 11)386                            .frame(maxWidth: j == 0 ? .infinity : nil, alignment: aGauche(j) ? .leading : .trailing)387                    }388                }389            }390        }391    }392393    private func aGauche(_ j: Int) -> Bool { j == 0 || texte.contains(j) }394395    @ViewBuilder396    private func contenu(_ i: Int, _ j: Int, _ s: String) -> some View {397        if j == 0, let route = lien?(i) {398            NavigationLink(value: route) {399                Text(s).font(.sans(14.5, style: .subheadline)).foregroundStyle(VX.encre).underline(true, color: VX.trait)400            }401            .buttonStyle(.plain)402        } else if aGauche(j) {403            Text(s).font(.sans(14.5, style: .subheadline)).foregroundStyle(j == 0 ? VX.encre : VX.encre2)404        } else {405            Text(s).font(.mono(13.5, style: .subheadline)).foregroundStyle(VX.encre)406        }407    }408}409410/// Attente réelle : la marque, fenêtres en ronde, et son libellé. Ni roue ni lueur.411struct Attente: View {412    var libelle = "Lecture des données"413414    var body: some View {415        HStack(spacing: 12) {416            Marque(taille: 34, boucle: true)417            Etiquette(libelle)418        }419        .frame(maxWidth: .infinity, alignment: .leading)420        .padding(.vertical, 26)421        .accessibilityElement(children: .ignore)422        .accessibilityLabel(libelle)423    }424}425426struct Panne: View {427    let message: String428    var reessayer: (() -> Void)? = nil429430    var body: some View {431        VStack(alignment: .leading, spacing: 14) {432            Avis(message)433            if let reessayer {434                Button("Réessayer", action: reessayer).buttonStyle(BoutonEncre(clair: true))435            }436        }437        .padding(.vertical, 24)438    }439}440441/// État d'un chargement, commun aux écrans.442enum Etat<T: Sendable>: Sendable {443    case attente444    case pret(T)445    case panne(ErreurAPI)446447    var valeur: T? {448        if case .pret(let v) = self { return v }449        return nil450    }451}452453extension Etat {454    static func charger(_ operation: @Sendable () async throws -> T) async -> Etat<T>? {455        do {456            return .pret(try await operation())457        } catch is CancellationError {458            return nil459        } catch let e as ErreurAPI {460            return .panne(e)461        } catch {462            return .panne(.reseau)463        }464    }465}466
Files main
Menu

Theme