SPB Git

Repository Public

ProductSections.swift 11.3 KB 237 lines swift

1//2//  Fabri-Ka iOS v23//  File: FabriKa/Features/Product/ProductSections.swift4//  Description: le bas de la fiche, dans l'ordre standard — description → détails (variantes, options, faits) →5//               la boutique (qui elle est, où, ses autres produits) → carte → produits semblables. On dit ce qu'on6//               sait et ce qu'on ne sait pas ; l'origine décrit la boutique, jamais le produit.7//8//  Author: Simon-Pierre Boucher9//  Contact: contact@spboucher.ai10//1112import MapKit13import SwiftUI1415struct ProductSections: View {16    let detail: ProductDetail17    let onVisitStore: () -> Void1819    @Environment(AppModel.self) private var model20    @Environment(Library.self) private var library21    @State private var expanded = false22    @State private var allVariants = false2324    var body: some View {25        VStack(alignment: .leading, spacing: 0) {26            if !detail.isPublic { notListed.padding(.top, 26) }27            description.padding(.top, 34).id("description")28            details.padding(.top, 34).id("details")29            if let store = detail.store {30                storeBlock(store).padding(.top, 34).id("store")31            }32        }33        .pageColumn()3435        if !detail.sameStore.isEmpty {36            VStack(alignment: .leading, spacing: 14) {37                Text("Ses autres produits").font(.editorial(19)).foregroundStyle(Theme.ink)38                    .frame(maxWidth: .infinity, alignment: .leading).pageColumn()39                    .accessibilityAddTraits(.isHeader)40                ProductStrip(products: detail.sameStore, trigger: .secondary, showStore: false, cardWidth: 140)41                    .frame(maxWidth: Theme.readable + 2 * Theme.margin).frame(maxWidth: .infinity)42            }43            .padding(.top, 24)44        }4546        if let store = detail.store, let coordinate = store.coordinate {47            VStack(alignment: .leading, spacing: 12) {48                SectionHeader("Où est la boutique", note: [store.city, store.region].compactMap { $0 }.joined(separator: ", "))49                StoreMiniMap(coordinate: coordinate, name: store.name)50                HonestNote(text: "La position est le centre du secteur postal de la boutique, à quelques kilomètres près : ce n'est pas une adresse.")51            }52            .pageColumn()53            .padding(.top, 34)54            .id("map")55        }5657        if !detail.similar.isEmpty {58            VStack(alignment: .leading, spacing: 14) {59                SectionHeader("Produits semblables", note: "Chez d'autres boutiques, un par boutique.").pageColumn()60                ProductStrip(products: detail.similar, trigger: .secondary)61                    .frame(maxWidth: Theme.readable + 2 * Theme.margin).frame(maxWidth: .infinity)62            }63            .padding(.top, 34)64            .id("similar")65        }66    }6768    // --- Fiche écartée des listes ---------------------------------------------------------------------6970    private var notListed: some View {71        VStack(alignment: .leading, spacing: 4) {72            Text("Cette fiche n'est pas proposée dans les listes").font(.subheadline.weight(.semibold)).foregroundStyle(Theme.ink)73            Text((detail.reasonLabel.map { "Motif : \($0). " } ?? "") + "Le produit existe chez la boutique ; il lui manque une donnée pour être publié au répertoire.")74                .font(.footnote).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true)75        }76        .padding(14)77        .frame(maxWidth: .infinity, alignment: .leading)78        .background(Theme.fairSoft, in: RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))79        .accessibilityElement(children: .combine)80        .accessibilityIdentifier("not-listed")81    }8283    // --- Description ----------------------------------------------------------------------------------8485    @ViewBuilder86    private var description: some View {87        SectionHeader("Description")88        if detail.paragraphs.isEmpty {89            Text("La boutique n'a pas publié de description pour ce produit.")90                .font(.subheadline).foregroundStyle(Theme.ink3).padding(.top, 10)91        } else {92            let long = detail.paragraphs.count > 3 || detail.paragraphs.joined().count > 70093            VStack(alignment: .leading, spacing: 12) {94                ForEach(Array((expanded || !long ? detail.paragraphs : Array(detail.paragraphs.prefix(2))).enumerated()), id: \.offset) { _, p in95                    Text(p).font(.body).foregroundStyle(Theme.ink2).lineSpacing(3)96                        .lineLimit(expanded || !long ? nil : 6)97                        .fixedSize(horizontal: false, vertical: true)98                        .textSelection(.enabled)99                }100                if long {101                    Button(expanded ? "Réduire" : "Lire la suite") { withAnimation(.easeOut(duration: 0.2)) { expanded.toggle() } }102                        .font(.subheadline.weight(.medium)).foregroundStyle(Theme.brand).frame(minHeight: 44)103                }104            }105            .padding(.top, 10)106            Text("Texte de la boutique, tel qu'elle le publie.").font(.footnote).foregroundStyle(Theme.ink3).padding(.top, long ? 0 : 10)107        }108    }109110    // --- Détails --------------------------------------------------------------------------------------111112    @ViewBuilder113    private var details: some View {114        let variants = detail.shownVariants115        let options = detail.shownOptions116        SectionHeader("Détails")117        VStack(spacing: 0) {118            ForEach(options, id: \.name) { o in119                FactRow(o.name, o.values.prefix(12).joined(separator: ", ") + (o.values.count > 12 ? "…" : ""))120            }121            ForEach(detail.facts, id: \.self) { f in FactRow(f.label, f.value) }122            if detail.facts.first(where: { $0.label.lowercased().contains("marque") }) == nil, let v = detail.vendor { FactRow("Marque affichée", v) }123            if let type = detail.productType { FactRow("Type déclaré par la boutique", type) }124            if detail.card.variantsGrouped > 1 {125                FactRow("Déclinaisons regroupées", "\(detail.card.variantsGrouped) produits de même nom")126            }127            FactRow("Vu pour la première fois", Fmt.date(detail.card.firstSeen))128            FactRow("Dernier relevé", Fmt.date(detail.lastSeen), divider: false)129        }130        .padding(.top, 4)131132        if !variants.isEmpty {133            Text(Fmt.plural(variants.count, "variante", "variantes")).eyebrowStyle().num().padding(.top, 22)134            VStack(spacing: 0) {135                ForEach(Array((allVariants ? variants : Array(variants.prefix(6))).enumerated()), id: \.offset) { _, v in136                    FactRow(label: v.name) {137                        HStack(spacing: 8) {138                            if v.available == false { StatusPill(kind: .soldOut, compact: true) }139                            Text(Fmt.price(v.price ?? detail.card.price)).font(.subheadline.weight(.medium)).num()140                                .foregroundStyle(v.available == false ? Theme.ink3 : Theme.ink)141                        }142                    }143                }144            }145            if variants.count > 6 {146                Button(allVariants ? "Réduire" : "Voir les \(variants.count) variantes") { allVariants.toggle() }147                    .font(.subheadline.weight(.medium)).foregroundStyle(Theme.brand).frame(minHeight: 44)148            }149            if detail.variants.count >= 20 {150                HonestNote(text: "Le relevé garde au plus 20 variantes par produit : il y en a peut-être d'autres chez la boutique.")151                    .padding(.top, 4)152            }153        }154    }155156    // --- La boutique ----------------------------------------------------------------------------------157158    @ViewBuilder159    private func storeBlock(_ store: Store) -> some View {160        SectionHeader("La boutique")161        VStack(alignment: .leading, spacing: 12) {162            Text(store.name).font(.editorial(20)).foregroundStyle(Theme.ink).fixedSize(horizontal: false, vertical: true)163            HonestNote(text: placeSentence(store), icon: store.place == nil ? "ic-region" : "ic-boutique")164            HonestNote(text: store.originSentence + " Fabri-Ka ne sait pas, produit par produit, où l'objet a été fabriqué.",165                       icon: store.isReseller ? "ic-boutique" : "ic-atelier")166            if !store.certifications.isEmpty {167                HonestNote(text: "La boutique déclare adhérer à : " + store.certifications.map(\.label).joined(separator: ", ") + ".")168            }169            if let s = store.summary {170                Text(s).font(.subheadline).foregroundStyle(Theme.ink2).lineSpacing(2).lineLimit(5)171                    .fixedSize(horizontal: false, vertical: true)172            }173            HStack(spacing: 10) {174                Button("Voir la boutique", action: onVisitStore).buttonStyle(SecondaryButtonStyle())175                    .accessibilityIdentifier("open-store")176                FollowButton(store: store)177            }178            .padding(.top, 4)179        }180        .padding(.top, 10)181    }182183    private func placeSentence(_ store: Store) -> String {184        guard store.place != nil else { return "Lieu non précisé par la boutique." }185        var s = store.eyebrow186        if store.city != nil, let r = store.region { s += ", \(r)" }187        s += store.verification == "verified" ? ". Lieu vérifié : l'adresse de la boutique a été recoupée." : "."188        return s189    }190}191192/// Suivre une boutique : gardé sur l'appareil, sans compte.193struct FollowButton: View {194    let store: Store195    @Environment(Library.self) private var library196    @Environment(AppModel.self) private var model197198    var body: some View {199        let on = library.isFollowing(store.id)200        Button {201            let now = library.toggleFollow(store)202            Haptics.select()203            model.showToast(now ? "Boutique suivie, sur cet appareil" : "Boutique retirée de vos suivis")204        } label: {205            HStack(spacing: 8) {206                Image(on ? "ic-favori-plein" : "ic-favori").resizable().scaledToFit().frame(width: 20, height: 20)207                    .foregroundStyle(on ? Theme.brand : Theme.ink)208                Text(on ? "Suivie" : "Suivre")209            }210        }211        .buttonStyle(SecondaryButtonStyle())212        .accessibilityLabel(on ? "Ne plus suivre \(store.name)" : "Suivre \(store.name)")213        .accessibilityIdentifier("follow-toggle")214    }215}216217/// Petite carte fixe : la boutique à sa place, repère de la marque.218struct StoreMiniMap: View {219    let coordinate: CLLocationCoordinate2D220    let name: String221222    var body: some View {223        Map(initialPosition: .region(MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.7))),224            interactionModes: []) {225            Annotation(name, coordinate: coordinate, anchor: .bottom) { StoreMarker(selected: true) }226                .annotationTitles(.hidden)227        }228        .mapStyle(.standard(elevation: .flat, emphasis: .muted, pointsOfInterest: .excludingAll, showsTraffic: false))229        .frame(height: 190)230        .clipShape(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))231        .overlay(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous).strokeBorder(Theme.line2, lineWidth: 1))232        .accessibilityElement(children: .ignore)233        .accessibilityLabel("Carte : \(name)")234        .accessibilityIdentifier("store-minimap")235    }236}237
Files main
Menu

Theme