SPB Git

Repository Public

PieceEcran.swift 22.5 KB 487 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : Groupe Ka — app iOS native7// File             : GroupeKa/Features/Rotonde/PieceEcran.swift8// Module           : Groupe Ka9// Repository       : https://git.spboucher.ai/groupe-ka-ios10// Project Website  : Not specified11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// Le texte d'une pièce (`content/<slug>.json` du site, français du Québec, aucun chiffre vivant).44struct ContenuPiece: Decodable, Sendable {45    struct Fonction: Decodable, Sendable {46        struct Trait: Decodable, Sendable { @Souple var nom: String?; @Souple var texte: String? }47        @Souple var titre: String?48        @Souple var intro: String?49        @ListeSouple var points: [Trait]50    }5152    struct Capture: Decodable, Sendable, Identifiable {53        @Souple var fichier: String?54        @Souple var appareil: String?55        @Souple var titre: String?56        @Souple var legende: String?57        @Souple var alt: String?58        var id: String { fichier ?? "" }59        /// nom sans extension : la clé de `captures.json` et le préfixe des fichiers publiés60        var base: String { (fichier ?? "").split(separator: ".").dropLast().joined(separator: ".") }61    }6263    struct Ligne: Decodable, Sendable { @Souple var libelle: String?; @Souple var valeur: String? }64    struct Question: Decodable, Sendable { @Souple var q: String?; @Souple var r: String? }6566    @Souple var accroche: String?67    @Souple var chapeau: String?68    @ListeSouple var apercu: [String]69    @ListeSouple var fonctions: [Fonction]70    @ListeSouple var captures: [Capture]71    @ListeSouple var fiche: [Ligne]72    @ListeSouple var confidentialite: [String]73    @ListeSouple var limites: [String]74    @ListeSouple var demarrer: [String]75    @ListeSouple var faq: [Question]7677    struct Dimensions: Decodable, Sendable { @Souple var w: Double?; @Souple var h: Double?; @ListeSouple var tailles: [Int] }7879    static func charger(_ slug: String) -> ContenuPiece? {80        guard let u = Bundle.main.url(forResource: slug, withExtension: "json"), let d = try? Data(contentsOf: u) else { return nil }81        return try? JSONDecoder().decode(ContenuPiece.self, from: d)82    }8384    static let dimensions: [String: [String: Dimensions]] = {85        guard let u = Bundle.main.url(forResource: "captures", withExtension: "json"), let d = try? Data(contentsOf: u) else { return [:] }86        return (try? JSONDecoder().decode([String: [String: Dimensions]].self, from: d)) ?? [:]87    }()88}8990/// Une capture prête à afficher : son adresse sur le site du groupe et ses proportions.91struct VuePiece: Identifiable, Sendable {92    let id: String93    let appareil: String94    let titre: String95    let legende: String96    let alt: String97    let proportion: CGFloat98    let petite: URL?99    let grande: URL?100101    /// `origine` : l'adresse qui sert le site en ce moment (www, sinon la préproduction)102    static func de(_ c: ContenuPiece.Capture, slug: String, origine: URL) -> VuePiece? {103        guard let d = ContenuPiece.dimensions[slug]?[c.base], let w = d.w, let h = d.h, h > 0, let min = d.tailles.first, let max = d.tailles.last else { return nil }104        let racine = "\(origine.absoluteString)/captures/\(slug)/\(c.base)"105        return VuePiece(id: c.base, appareil: c.appareil ?? "web", titre: c.titre ?? "", legende: c.legende ?? "", alt: c.alt ?? c.titre ?? "",106                        proportion: w / h, petite: URL(string: "\(racine)-\(min).webp"), grande: URL(string: "\(racine)-\(max).webp"))107    }108}109110/// La pièce d'un site : le hall (marque, chiffre réel, « Entrer »), l'aperçu, ce qu'on y fait, les vues, la fiche,111/// la confidentialité, les limites, pour commencer, les questions, et les pièces voisines.112struct PieceEcran: View {113    let site: KaSite114    let chiffres: ChiffresRotonde115    @Environment(AppEtat.self) private var app116    @Environment(\.openURL) private var ouvrir117    @Environment(\.horizontalSizeClass) private var classe118    @State private var contenu: ContenuPiece?119    @State private var vues: [VuePiece] = []120    @State private var visionneuse: VuePiece?121    @State private var cadreEntrer = CGRect.zero122123    var body: some View {124        ScrollView {125            Colonne(largeurMax: 820) {126                VStack(alignment: .leading, spacing: 0) {127                    hall128                    if let c = contenu { sections(c) } else { sansTexte }129                    voisines130                }131            }132            .padding(.bottom, 28)133        }134        .safeAreaInset(edge: .top, spacing: 0) {135            VStack(spacing: 0) {136                EnTete(titre: nil, retour: { app.cheminRotonde = [] }) { SymboleMarque(site: site.id).frame(width: 26, height: 26) }137                // le mur de la rotonde, vu de l'intérieur138                Rectangle().fill(Ka.encre).frame(height: 5)139                    .overlay(alignment: .leading) { Rectangle().fill(site.id.couleur).frame(width: 64, height: 5).padding(.leading, 16) }140            }141        }142        .fondPapier()143        .toolbar(.hidden, for: .navigationBar)144        .task(id: site.id) {145            contenu = ContenuPiece.charger(site.slug)146            let origine = await API.partagee.base()147            vues = (contenu?.captures ?? []).compactMap { VuePiece.de($0, slug: site.slug, origine: origine) }148        }149        .fullScreenCover(item: $visionneuse) { v in Visionneuse(vues: vues.filter { $0.appareil == v.appareil }, depart: v, couleur: site.id.couleurTexte) }150    }151152    // ── le hall153154    private var hall: some View {155        VStack(alignment: .leading, spacing: 0) {156            HStack(spacing: 14) {157                SymboleMarque(site: site.id).frame(width: 58, height: 58)158                MotSymbole(site: site, taille: 40, style: .largeTitle).accessibilityAddTraits(.isHeader)159                    .accessibilityIdentifier("piece-\(site.id.rawValue)")160            }161            .padding(.top, 22)162            Text(contenu?.accroche ?? site.phrase)163                .ka(.serif, 24, graisse: 400, style: .title2)164                .foregroundStyle(Ka.encre)165                .padding(.top, 14)166                .fixedSize(horizontal: false, vertical: true)167            if let chapeau = contenu?.chapeau {168                Text(chapeau).ka(.sans, 16, style: .body).foregroundStyle(Ka.encre2).lineSpacing(3).padding(.top, 10)169                    .fixedSize(horizontal: false, vertical: true)170            }171            Group {172                if let c = chiffres.chiffres[site.id] {173                    (Text(c.n).font(KaFont.police(.serif, 34, graisse: 500)).foregroundStyle(Ka.encre)174                        + Text("  " + c.libelle).font(KaFont.police(.sans, 15)).foregroundStyle(Ka.encre2))175                        .monospacedDigit()176                } else {177                    Text(site.genre).ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2)178                }179            }180            .padding(.top, 16)181            ViewThatFits(in: .horizontal) {182                HStack(spacing: 10) { actions }183                VStack(alignment: .leading, spacing: 10) { actions }184            }185            .padding(.top, 16)186            notes.padding(.top, 12)187            if let une = vues.first(where: { $0.appareil == "web" }) ?? vues.first {188                Button { visionneuse = une } label: { cadre(une, grande: true) }189                    .buttonStyle(.plain)190                    .padding(.top, 22)191                    .padding(.trailing, 9)192                    .accessibilityLabel(une.alt)193                    .accessibilityHint("Agrandir la capture")194            }195        }196        .padding(.bottom, 30)197    }198199    @ViewBuilder200    private var actions: some View {201        Button { app.voile = Voile(site: site.id, origine: CGPoint(x: cadreEntrer.midX, y: cadreEntrer.midY)) } label: {202            HStack(spacing: 6) { Text("Entrer dans \(site.domaine)"); Fleche().frame(width: 16, height: 16) }203        }204        .buttonStyle(BoutonPlein(couleur: site.id.couleurTexte))205        .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { cadreEntrer = $0 }206        .accessibilityIdentifier("piece-entrer")207        if let t = site.testflight {208            Button { ouvrir(t) } label: {209                HStack(spacing: 6) { Text("App iPhone : bêta TestFlight"); Fleche(sens: .sortie).frame(width: 15, height: 15) }210            }211            .buttonStyle(BoutonContour(couleur: site.id.couleurTexte))212        }213    }214215    @ViewBuilder216    private var notes: some View {217        VStack(alignment: .leading, spacing: 4) {218            if site.testflight == nil, contenu != nil {219                Text(site.noteApp ?? "App iPhone : bêta TestFlight en cours de revue par Apple.")220                    .ka(.sans, 13, style: .footnote).foregroundStyle(Ka.encre3).fixedSize(horizontal: false, vertical: true)221            }222            if !site.sources.isEmpty {223                ViewThatFits(in: .horizontal) {224                    HStack(spacing: 14) { liensSources }225                    VStack(alignment: .leading, spacing: 0) { liensSources }226                }227            }228        }229    }230231    @ViewBuilder232    private var liensSources: some View {233        Text("Code source :").ka(.sans, 13, style: .footnote).foregroundStyle(Ka.encre3)234        ForEach(site.sources, id: \.url) { s in235            LienTexte(titre: s.libelle.lowercased(with: Fmt.locale), couleur: Ka.encre2, fleche: nil) { ouvrir(s.url) }236        }237    }238239    // ── les sections240241    @ViewBuilder242    private func sections(_ c: ContenuPiece) -> some View {243        if !c.apercu.isEmpty {244            section(1, "Aperçu") {245                ForEach(Array(c.apercu.enumerated()), id: \.offset) { _, p in paragraphe(p) }246            }247        }248        if !c.fonctions.isEmpty {249            section(2, "Ce qu'on y fait") {250                ForEach(Array(c.fonctions.enumerated()), id: \.offset) { _, f in251                    VStack(alignment: .leading, spacing: 8) {252                        if let t = f.titre {253                            Text(t).ka(.serif, 21, graisse: 500, style: .title3).foregroundStyle(Ka.encre).accessibilityAddTraits(.isHeader)254                        }255                        if let i = f.intro { paragraphe(i) }256                        ForEach(Array(f.points.enumerated()), id: \.offset) { _, p in257                            VStack(alignment: .leading, spacing: 3) {258                                if let n = p.nom { Text(n).ka(.sans, 15, graisse: 600, style: .subheadline).foregroundStyle(Ka.encre) }259                                if let t = p.texte {260                                    Text(t).ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2).lineSpacing(2).fixedSize(horizontal: false, vertical: true)261                                }262                            }263                            .padding(.leading, 12)264                            .overlay(alignment: .leading) { Rectangle().fill(site.id.couleur).frame(width: 2) }265                            .accessibilityElement(children: .combine)266                        }267                    }268                    .padding(.bottom, 14)269                }270            }271        }272        if !vues.isEmpty {273            section(3, "Vues") {274                ForEach([("web", "Le site"), ("mobile", "Le site sur téléphone"), ("iphone", "L'app iPhone")], id: \.0) { appareil, titre in275                    let liste = vues.filter { $0.appareil == appareil }276                    if !liste.isEmpty {277                        TitreBloc(titre: titre)278                        ScrollView(.horizontal, showsIndicators: false) {279                            HStack(alignment: .top, spacing: 16) {280                                ForEach(liste) { v in281                                    Button { visionneuse = v } label: {282                                        VStack(alignment: .leading, spacing: 8) {283                                            cadre(v, grande: false)284                                                .frame(width: appareil == "web" ? 300 : 150)285                                            Text(v.titre).ka(.sans, 13, graisse: 500, style: .footnote).foregroundStyle(Ka.encre2)286                                                .frame(width: appareil == "web" ? 300 : 150, alignment: .leading)287                                                .multilineTextAlignment(.leading)288                                        }289                                    }290                                    .buttonStyle(.plain)291                                    .accessibilityLabel(v.alt)292                                    .accessibilityHint("Agrandir la capture")293                                }294                            }295                            .padding(.trailing, 12).padding(.bottom, 12)296                        }297                        .padding(.bottom, 8)298                    }299                }300            }301        }302        if !c.fiche.isEmpty {303            section(4, "Fiche") {304                VStack(spacing: 0) {305                    Filet()306                    ForEach(Array(c.fiche.enumerated()), id: \.offset) { _, l in LigneFiche(libelle: l.libelle ?? "", valeur: l.valeur ?? "") }307                }308            }309        }310        if !c.confidentialite.isEmpty { section(5, "Confidentialité") { traits(c.confidentialite) } }311        if !c.limites.isEmpty { section(6, "Limites") { traits(c.limites) } }312        if !c.demarrer.isEmpty { section(7, "Pour commencer") { traits(c.demarrer, numerote: true) } }313        if !c.faq.isEmpty {314            section(8, "Questions") {315                ForEach(Array(c.faq.enumerated()), id: \.offset) { _, q in QuestionPliable(question: q.q ?? "", reponse: q.r ?? "") }316            }317        }318    }319320    /// Une pièce dont le texte n'est pas encore publié par le site : l'app le dit, sans rien inventer.321    private var sansTexte: some View {322        VStack(alignment: .leading, spacing: 8) {323            Filet(couleur: Ka.encre)324            Text("La description détaillée de cette pièce n'est pas encore publiée. Le site lui-même est ouvert : la porte est juste au-dessus.")325                .ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2).padding(.top, 12)326                .fixedSize(horizontal: false, vertical: true)327        }328        .padding(.bottom, 30)329    }330331    private func section<C: View>(_ numero: Int, _ titre: String, @ViewBuilder _ corps: () -> C) -> some View {332        VStack(alignment: .leading, spacing: 12) {333            Filet(couleur: Ka.encre)334            HStack(alignment: .firstTextBaseline, spacing: 10) {335                Text(String(format: "%02d", numero)).ka(.sans, 13, graisse: 500, style: .footnote).foregroundStyle(site.id.couleurTexte).monospacedDigit()336                    .accessibilityHidden(true)337                Text(titre).ka(.serif, 26, graisse: 500, style: .title1).tracking(-0.5).foregroundStyle(Ka.encre).accessibilityAddTraits(.isHeader)338            }339            corps()340        }341        .frame(maxWidth: .infinity, alignment: .leading)342        .padding(.bottom, 30)343    }344345    private func paragraphe(_ t: String) -> some View {346        Text(t).ka(.sans, 16, style: .body).foregroundStyle(Ka.encre2).lineSpacing(3).fixedSize(horizontal: false, vertical: true)347    }348349    private func traits(_ lignes: [String], numerote: Bool = false) -> some View {350        VStack(alignment: .leading, spacing: 10) {351            ForEach(Array(lignes.enumerated()), id: \.offset) { i, l in352                HStack(alignment: .firstTextBaseline, spacing: 10) {353                    if numerote {354                        Text("\(i + 1)").ka(.sans, 14, graisse: 600, style: .footnote).foregroundStyle(site.id.couleurTexte).frame(width: 18, alignment: .leading)355                    } else {356                        Rectangle().fill(site.id.couleur).frame(width: 9, height: 1.5).offset(y: -4)357                    }358                    Text(l).ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2).lineSpacing(2).fixedSize(horizontal: false, vertical: true)359                }360                .accessibilityElement(children: .combine)361            }362        }363    }364365    /// Une capture dans son cadre : un filet, une ombre portée franche.366    private func cadre(_ v: VuePiece, grande: Bool) -> some View {367        Color.clear368            .aspectRatio(v.proportion, contentMode: .fit)369            .overlay { ImageDistante(url: grande ? v.grande : v.petite) }370            .clipShape(RoundedRectangle(cornerRadius: grande ? 8 : 6))371            .background(RoundedRectangle(cornerRadius: grande ? 8 : 6).fill(Ka.ombre).offset(x: grande ? 7 : 5, y: grande ? 9 : 6))372            .overlay(RoundedRectangle(cornerRadius: grande ? 8 : 6).strokeBorder(Ka.encre.opacity(0.22), lineWidth: 1))373    }374375    // ── d'une pièce à la suivante, sans repasser par le vestibule376377    private var voisines: some View {378        let i = site.index, n = KaSite.tous.count379        let prec = KaSite.tous[(i + n - 1) % n], suiv = KaSite.tous[(i + 1) % n]380        return VStack(spacing: 0) {381            Filet(couleur: Ka.encre)382            HStack(alignment: .top) {383                voisine(prec, "Pièce précédente", .leading)384                Spacer(minLength: 12)385                voisine(suiv, "Pièce suivante", .trailing)386            }387            .padding(.vertical, 14)388            Filet()389            Button { app.cheminRotonde = []; app.porteDemandee = site.id } label: {390                HStack(spacing: 10) {391                    SymboleGroupe().frame(width: 22, height: 22)392                    Text("Revenir à la rotonde").ka(.sans, 15, graisse: 500, style: .subheadline)393                }394                .foregroundStyle(Ka.encre)395                .frame(maxWidth: .infinity, minHeight: 52)396                .contentShape(Rectangle())397            }398            .buttonStyle(.plain)399        }400    }401402    private func voisine(_ s: KaSite, _ sens: String, _ cote: HorizontalAlignment) -> some View {403        Button { app.cheminRotonde = [s.id] } label: {404            VStack(alignment: cote, spacing: 6) {405                Cote(texte: sens)406                HStack(spacing: 8) {407                    SymboleMarque(site: s.id).frame(width: 26, height: 26)408                    MotSymbole(site: s, taille: 19, style: .title3)409                }410            }411            .frame(minHeight: 44)412            .contentShape(Rectangle())413        }414        .buttonStyle(.plain)415        .accessibilityLabel("\(sens) : \(s.nomComplet)")416    }417}418419private struct QuestionPliable: View {420    let question: String421    let reponse: String422    @State private var ouverte = false423    var body: some View {424        VStack(alignment: .leading, spacing: 0) {425            Button { withAnimation(.easeOut(duration: 0.2)) { ouverte.toggle() } } label: {426                HStack(alignment: .firstTextBaseline, spacing: 12) {427                    Text(question).ka(.sans, 16, graisse: 500, style: .body).foregroundStyle(Ka.encre).multilineTextAlignment(.leading)428                    Spacer(minLength: 8)429                    Fleche(sens: ouverte ? .haut : .bas).frame(width: 14, height: 14).foregroundStyle(Ka.encre3)430                }431                .padding(.vertical, 12)432                .contentShape(Rectangle())433            }434            .buttonStyle(.plain)435            .accessibilityValue(ouverte ? "réponse affichée" : "réponse masquée")436            if ouverte {437                Text(reponse).ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2).lineSpacing(2).padding(.bottom, 14)438                    .fixedSize(horizontal: false, vertical: true)439            }440            Filet()441        }442    }443}444445/// Visionneuse plein écran : on passe d'une capture à l'autre en glissant ; la légende reste lisible.446private struct Visionneuse: View {447    let vues: [VuePiece]448    let depart: VuePiece449    let couleur: Color450    @Environment(\.dismiss) private var fermer451    @State private var courante: String = ""452453    var body: some View {454        VStack(spacing: 0) {455            HStack {456                if let i = vues.firstIndex(where: { $0.id == courante }) {457                    Text("\(i + 1) / \(vues.count)").ka(.sans, 14, graisse: 500, style: .footnote).foregroundStyle(Ka.encre3).monospacedDigit()458                }459                Spacer()460                BoutonTexte(titre: "Fermer", couleur: Ka.encre) { fermer() }.accessibilityIdentifier("visionneuse-fermer")461            }462            .padding(.horizontal, 16)463            TabView(selection: $courante) {464                ForEach(vues) { v in465                    VStack(spacing: 14) {466                        Color.clear.aspectRatio(v.proportion, contentMode: .fit)467                            .overlay { ImageDistante(url: v.grande, remplir: false) }468                            .overlay(Rectangle().strokeBorder(Ka.encre.opacity(0.22), lineWidth: 1))469                            .accessibilityLabel(v.alt)470                            .accessibilityAddTraits(.isImage)471                        VStack(alignment: .leading, spacing: 4) {472                            Text(v.titre).ka(.serif, 20, graisse: 500, style: .title3).foregroundStyle(Ka.encre)473                            Text(v.legende).ka(.sans, 14, style: .footnote).foregroundStyle(Ka.encre2).fixedSize(horizontal: false, vertical: true)474                        }475                        .frame(maxWidth: 640, alignment: .leading)476                    }477                    .padding(16)478                    .tag(v.id)479                }480            }481            .tabViewStyle(.page(indexDisplayMode: .never))482        }483        .fondPapier()484        .onAppear { courante = depart.id }485    }486}487
Files main
Menu

Theme