SPB Git

Repository Public

Cards.swift 9.9 KB 236 lines swift

1//2//  Fabri-Ka iOS v23//  File: FabriKa/Design/Cards.swift4//  Description: les cartes — carte de produit (photo, prix, nom, signature de la boutique), grille et rangée de5//               produits, carte de boutique (le bras, la plaque, ce qu'elle fait), plaque de région.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import SwiftUI1213/// La carte de produit : photo carrée ; prix et pastille d'état ; nom en New York, deux lignes au plus ; puis la14/// signature de la boutique. « Épuisé » change la carte elle-même (photo à 45 %, prix atténué).15struct ProductCard: View {16    let product: Product17    var trigger: SignTrigger = .card18    /// largeur fixe (rangées horizontales) ; nil : la cellule de la grille décide19    var width: CGFloat?20    /// dans la page d'une boutique, la signature est inutile : on montre la sous-catégorie21    var showStore = true2223    @Environment(AppModel.self) private var model24    @State private var box = FrameBox()2526    fileprivate var soldOut: Bool { product.availability == .soldOut }2728    var body: some View {29        Button {30            model.openProduct(product, from: box.frame, trigger: trigger)31        } label: {32            VStack(alignment: .leading, spacing: 0) {33                Photo(source: product.img, maxPixel: 480, icon: product.categoryIcon, dimmed: soldOut)34                    .aspectRatio(1, contentMode: .fit)35                    .background(FrameReader(box: box))36                // le prix d'abord, entier ; faute de place la pastille se réduit à sa forme, puis le prix se resserre37                ViewThatFits(in: .horizontal) {38                    priceRow(size: 16, word: true)39                    priceRow(size: 16, word: false)40                    priceRow(size: 13.5, word: false)41                    priceRow(size: 12, word: false)42                }43                .padding(.top, 9)44                Text(product.title)45                    .font(.editorial(17)).foregroundStyle(soldOut ? Theme.ink2 : Theme.ink)46                    .lineLimit(2).multilineTextAlignment(.leading)47                    .fixedSize(horizontal: false, vertical: true)48                    .padding(.top, 3)49                Group {50                    if showStore {51                        StoreSignature(name: product.storeName, place: product.place)52                    } else if let sub = product.subcategoryLabel ?? product.categoryLabel {53                        Text(sub).font(.caption).foregroundStyle(Theme.ink3).lineLimit(1)54                    }55                }56                .padding(.top, 7)57                Spacer(minLength: 0)58            }59            .frame(width: width, alignment: .leading)60            .contentShape(Rectangle())61        }62        .buttonStyle(CardButtonStyle())63        .accessibilityElement(children: .ignore)64        .accessibilityLabel(product.spoken)65        .accessibilityAddTraits(.isButton)66        .accessibilityIdentifier("product-\(product.uid)")67    }68}6970extension ProductCard {71    fileprivate func priceRow(size: CGFloat, word: Bool) -> some View {72        HStack(alignment: .firstTextBaseline, spacing: 6) {73            Text(product.priceText)74                .font(.display(size)).num()75                .foregroundStyle(soldOut ? Theme.ink3 : Theme.ink)76                .lineLimit(1).fixedSize()77            Spacer(minLength: 0)78            if soldOut {79                StatusPill(kind: .soldOut, compact: true, wordless: !word)80            } else if product.isNew {81                StatusPill(kind: .new(nil), compact: true, wordless: !word)82            }83        }84    }85}8687/// La prise : la carte touchée se soulève à peine.88struct CardButtonStyle: ButtonStyle {89    func makeBody(configuration: Configuration) -> some View {90        configuration.label91            .offset(y: configuration.isPressed ? -3 : 0)92            .opacity(configuration.isPressed ? 0.86 : 1)93            .animation(.easeOut(duration: 0.12), value: configuration.isPressed)94    }95}9697/// Grille de produits : deux colonnes sur iPhone, davantage sur iPad ; une seule en très grand texte.98struct ProductGrid<Footer: View>: View {99    let products: [Product]100    var trigger: SignTrigger = .card101    var showStore = true102    /// appelé quand la dernière cellule paraît (pagination)103    var onEnd: (() -> Void)?104    @ViewBuilder var footer: () -> Footer105    @Environment(\.dynamicTypeSize) private var typeSize106107    init(products: [Product], trigger: SignTrigger = .card, showStore: Bool = true, onEnd: (() -> Void)? = nil,108         @ViewBuilder footer: @escaping () -> Footer = { EmptyView() }) {109        self.products = products; self.trigger = trigger; self.showStore = showStore; self.onEnd = onEnd; self.footer = footer110    }111112    var body: some View {113        LazyVGrid(columns: [GridItem(.adaptive(minimum: typeSize.isAccessibilitySize ? 240 : 150, maximum: 260), spacing: 14, alignment: .top)],114                  alignment: .leading, spacing: 24) {115            ForEach(Array(products.enumerated()), id: \.element.uid) { i, p in116                ProductCard(product: p, trigger: trigger, showStore: showStore)117                    .onAppear { if i == products.count - 1 { onEnd?() } }118            }119        }120        footer()121    }122}123124/// Rangée horizontale de produits (découvertes, « ses autres produits », produits semblables).125struct ProductStrip: View {126    let products: [Product]127    var trigger: SignTrigger = .card128    var showStore = true129    var cardWidth: CGFloat = 156130131    var body: some View {132        ScrollView(.horizontal, showsIndicators: false) {133            LazyHStack(alignment: .top, spacing: 14) {134                ForEach(Array(products.enumerated()), id: \.element.uid) { i, p in135                    ProductCard(product: p, trigger: trigger, width: cardWidth, showStore: showStore)136                        .staggered(i)137                }138            }139            .padding(.horizontal, Theme.margin)140            .scrollTargetLayout()141        }142        .scrollTargetBehavior(.viewAligned)143    }144}145146/// La carte de boutique : le bras, la plaque (nom, « Boutique de Chicoutimi · 64 produits »), une de ses photos,147/// et ce qu'elle est, en faits.148struct StoreCard: View {149    let store: Store150    var showImage = true151    @Environment(AppModel.self) private var model152153    var body: some View {154        Button { model.openStore(store.id) } label: {155            VStack(alignment: .leading, spacing: 10) {156                HangingSign(metrics: .card) {157                    Plaque(radius: 6, padding: EdgeInsets(top: 10, leading: 13, bottom: 11, trailing: 13)) { tone in158                        PlaqueText(tone: tone, name: store.name, line: store.cardLine, nameSize: 19)159                    }160                }161                if showImage {162                    VStack(alignment: .leading, spacing: 9) {163                        let thumbs = store.thumbnails164                        if !thumbs.isEmpty {165                            HStack(spacing: 8) {166                                ForEach(Array(thumbs.enumerated()), id: \.offset) { _, src in167                                    Photo(source: src, maxPixel: 320, icon: CategoryIcon.name(for: store.category))168                                        .aspectRatio(4 / 3, contentMode: .fit)169                                        .frame(maxWidth: 110)170                                }171                                Spacer(minLength: 0)172                            }173                        }174                        VStack(alignment: .leading, spacing: 2) {175                            Text([store.categoryLabel, store.isReseller ? "détaillant, revend aussi des produits d'ailleurs" : nil]176                                .compactMap { $0 }.joined(separator: " · "))177                                .font(.footnote).foregroundStyle(Theme.ink2)178                            if store.nNew > 0 {179                                Text(Fmt.plural(store.nNew, "nouveauté", "nouveautés") + " depuis 14 jours")180                                    .font(.footnote).num().foregroundStyle(Theme.ink3)181                            }182                        }183                        .multilineTextAlignment(.leading)184                        .fixedSize(horizontal: false, vertical: true)185                    }186                    .padding(.leading, SignMetrics.card.inset)187                }188            }189            .contentShape(Rectangle())190        }191        .buttonStyle(CardButtonStyle())192        .accessibilityElement(children: .ignore)193        .accessibilityLabel("\(store.name), \(store.cardLine)" + (store.categoryLabel.map { ", \($0)" } ?? ""))194        .accessibilityAddTraits(.isButton)195        .accessibilityIdentifier("store-\(store.id)")196    }197}198199/// La plaque de région : même plaque, sur-titre « Région », nom, « 130 boutiques · 12 564 produits ».200struct RegionPlaque: View {201    let name: String202    let line: String203    var eyebrow = "Région"204    let action: () -> Void205206    var body: some View {207        Button(action: action) {208            HangingSign(metrics: .card) {209                Plaque(radius: 6, padding: EdgeInsets(top: 10, leading: 13, bottom: 11, trailing: 13)) { tone in210                    PlaqueText(tone: tone, eyebrow: eyebrow, name: name, line: line, nameSize: 19)211                }212            }213            .contentShape(Rectangle())214        }215        .buttonStyle(CardButtonStyle())216        .accessibilityElement(children: .ignore)217        .accessibilityLabel("\(eyebrow) \(name), \(line)")218        .accessibilityAddTraits(.isButton)219    }220}221222/// Grille adaptative de plaques ou de cartes de boutique : une colonne sur iPhone, deux sur iPad.223struct SignGrid<Item: Identifiable, Cell: View>: View {224    let items: [Item]225    var spacing: CGFloat = 26226    @ViewBuilder var cell: (Item) -> Cell227228    var body: some View {229        LazyVGrid(columns: [GridItem(.adaptive(minimum: 300), spacing: 24, alignment: .top)], alignment: .leading, spacing: spacing) {230            ForEach(Array(items.enumerated()), id: \.element.id) { i, item in231                cell(item).staggered(i)232            }233        }234    }235}236
Files main
Menu

Theme