Repository Public
1//2// Fabri-Ka iOS v23// File: FabriKa/Features/Product/ProductScene.swift4// Description: la scène du geste « la pose de l'enseigne », qui est aussi le haut de la fiche — la photo voyage5// depuis sa vignette, le bras se tire, la plaque de laiton tombe et bat, puis se patine : le nom de6// la boutique reste à l'encre. Vue `Animatable` dont la donnée animée est t (ms) : figeable,7// interruptible, identique à l'état final sans mouvement.8//9// Author: Simon-Pierre Boucher10// Contact: contact@spboucher.ai11//1213import SwiftUI1415struct ProductScene: View, @preconcurrency Animatable {16 /// temps écoulé depuis le début du geste, en ms17 var t: Double18 let choreo: SignChoreo?19 let product: Product20 /// photos de la fiche (au moins celle de la carte)21 let images: [String]22 let store: Store?23 /// rectangle global de la vignette touchée, et celui de la photo de la fiche24 let source: CGRect?25 let heroFrame: CGRect26 /// largeur disponible et hauteur de la fenêtre27 let size: CGSize28 let lastSeen: Double?29 /// change quand la photo principale arrive : la scène relit sa proportion30 var tick = 031 @Binding var page: Int32 let onStore: () -> Void33 let onVisit: () -> Void34 let onZoom: () -> Void3536 var animatableData: Double {37 get { t }38 set { t = newValue }39 }4041 private var s: SignState { choreo?.state(at: t) ?? .settled }42 private var wide: Bool { size.width >= 700 }4344 /// Proportion de la photo principale, connue dès que sa vignette est en mémoire.45 private var aspect: CGFloat {46 guard let img = ImageLoader.cached(images.first, maxPixel: 1280), img.size.height > 0 else { return 1 }47 return img.size.width / img.size.height48 }4950 /// Taille de la photo sur la fiche : pleine largeur sur iPhone, proportion de la photo bornée pour que le prix51 /// reste au-dessus du pli ; moitié de colonne sur iPad.52 private var heroSize: CGSize {53 if wide {54 let w = min(size.width, 1040) * 0.4855 return CGSize(width: w, height: min(max(w / aspect, w * 0.7), w * 1.15))56 }57 let w = size.width58 let h = min(max(w / aspect, w * 0.62), min(w * 1.18, size.height * 0.5))59 return CGSize(width: w, height: h)60 }6162 var body: some View {63 let state = s64 Group {65 if wide {66 HStack(alignment: .top, spacing: 0) {67 hero(state)68 VStack(alignment: .leading, spacing: 0) {69 productBlock(state).padding(.leading, 34).padding(.top, 8)70 sign(state, metrics: .page).padding(.top, 26)71 action(state).padding(.leading, 34).padding(.top, 20)72 }73 .frame(maxWidth: .infinity, alignment: .topLeading)74 }75 .padding(.horizontal, 24)76 .frame(maxWidth: 1040)77 .frame(maxWidth: .infinity)78 .padding(.top, 12)79 } else {80 VStack(alignment: .leading, spacing: 0) {81 hero(state)82 productBlock(state).padding(.horizontal, Theme.margin).padding(.top, 18)83 sign(state, metrics: .sheet).padding(.leading, 10).padding(.trailing, Theme.margin).padding(.top, 20)84 action(state).padding(.horizontal, Theme.margin).padding(.top, 18)85 }86 }87 }88 .accessibilityElement(children: .contain)89 .accessibilityIdentifier("sign-scene")90 }9192 // --- La photo : elle voyage de la vignette à sa place ---------------------------------------------9394 private func hero(_ state: SignState) -> some View {95 let hs = heroSize96 let wantsTravel = state.travel < 1 && source != nil97 let traveling = wantsTravel && heroFrame.width > 198 return ZStack(alignment: .topLeading) {99 gallery(hs)100 // sans carte d'origine (lien direct joué pour une capture) : la photo paraît sur place101 .opacity(wantsTravel ? 0 : (source == nil ? state.travel : 1))102 if traveling, let source {103 // interpolation du rectangle de la carte à celui de la fiche (courbe du voyage déjà appliquée)104 let k = state.travel105 let from = source.offsetBy(dx: -heroFrame.minX, dy: -heroFrame.minY)106 let w = from.width + (hs.width - from.width) * k, h = from.height + (hs.height - from.height) * k107 Photo(source: images.first, maxPixel: 1280, fit: .cover, icon: product.categoryIcon, radius: Theme.radiusPhoto * (1 - k))108 .frame(width: w, height: h)109 .offset(x: from.minX * (1 - k), y: from.minY * (1 - k))110 .shadow(color: .black.opacity(0.10 * (1 - k)), radius: 10, y: 4)111 .allowsHitTesting(false)112 }113 }114 .frame(width: hs.width, height: hs.height, alignment: .topLeading)115 .zIndex(2)116 }117118 private func gallery(_ hs: CGSize) -> some View {119 ZStack(alignment: .bottomTrailing) {120 if images.isEmpty {121 Photo(source: nil, icon: product.categoryIcon, radius: wide ? Theme.radiusPhoto : 0)122 } else {123 TabView(selection: $page) {124 ForEach(Array(images.enumerated()), id: \.offset) { i, src in125 Photo(source: src, maxPixel: 1280, fit: .auto, icon: product.categoryIcon, radius: wide ? Theme.radiusPhoto : 0,126 dimmed: false)127 .tag(i)128 .onTapGesture(perform: onZoom)129 }130 }131 .tabViewStyle(.page(indexDisplayMode: .never))132 if images.count > 1 {133 Text("\(page + 1) / \(images.count)")134 .font(.caption.weight(.semibold)).num().foregroundStyle(Theme.ink)135 .padding(.horizontal, 8).padding(.vertical, 4)136 .background(Theme.surface.opacity(0.92), in: RoundedRectangle(cornerRadius: Theme.radiusPill, style: .continuous))137 .padding(10)138 .accessibilityLabel("Photo \(page + 1) sur \(images.count)")139 }140 }141 }142 .frame(width: hs.width, height: hs.height)143 .accessibilityElement(children: .contain)144 .accessibilityLabel(images.isEmpty ? "Aucune photo" : "Photos du produit")145 .accessibilityIdentifier("product-gallery")146 }147148 // --- Le produit, au-dessus du bras ----------------------------------------------------------------149150 private func productBlock(_ state: SignState) -> some View {151 VStack(alignment: .leading, spacing: 0) {152 Group {153 if let c = product.categoryLabel {154 Text(product.subcategoryLabel.map { c + " · " + $0 } ?? c).eyebrowStyle()155 }156 HStack(alignment: .firstTextBaseline, spacing: 10) {157 Text(product.priceText)158 .font(.display(wide ? 34 : 30)).num()159 .foregroundStyle(product.availability == .soldOut ? Theme.ink3 : Theme.ink)160 .minimumScaleFactor(0.7).lineLimit(1)161 .accessibilityIdentifier("product-price")162 if let before = product.compareAtPrice, let d = product.discount {163 Text(Fmt.price(before)).font(.subheadline).num().strikethrough().foregroundStyle(Theme.ink3)164 .accessibilityLabel("au lieu de \(Fmt.price(before)), rabais de \(d) pour cent")165 }166 }167 .padding(.top, 5)168 }169 .opacity(state.price).offset(y: 8 * (1 - state.price))170 Text(product.title)171 .font(.editorial(wide ? 28 : 24)).foregroundStyle(Theme.ink)172 .fixedSize(horizontal: false, vertical: true)173 .padding(.top, 4)174 .opacity(state.name).offset(y: 8 * (1 - state.name))175 .accessibilityAddTraits(.isHeader)176 .accessibilityIdentifier("product-title")177 statusLine178 .padding(.top, 10)179 .opacity(state.status).offset(y: 8 * (1 - state.status))180 }181 .frame(maxWidth: .infinity, alignment: .leading)182 }183184 @ViewBuilder185 private var statusLine: some View {186 FlowLayout(spacing: 8) {187 switch product.availability {188 case .inStock: StatusPill(kind: .inStock)189 case .soldOut: StatusPill(kind: .soldOut)190 case .unknown: EmptyView()191 }192 if product.isNew { StatusPill(kind: .new(product.firstSeen.map { Fmt.shortDate($0) })) }193 }194 if product.availability == .soldOut {195 Text("Épuisé chez la boutique au dernier relevé" + (lastSeen.map { " (\(Fmt.shortDate($0)))" } ?? "") + ".")196 .font(.footnote).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true).padding(.top, 6)197 } else if product.availability == .unknown, product.price != nil {198 Text("Disponibilité non précisée par la boutique.")199 .font(.footnote).foregroundStyle(Theme.ink3).padding(.top, 2)200 }201 }202203 // --- La boutique : le bras et la plaque -----------------------------------------------------------204205 private var storeName: String { store?.name ?? product.storeName }206207 private func sign(_ state: SignState, metrics: SignMetrics) -> some View {208 let eyebrow = store?.eyebrow ?? Store.eyebrow(origin: product.origin, city: product.city, region: product.region)209 let line = store?.countLine ?? product.region210 // la plaque ne tombe jamais vide : sans nom de boutique, le bras se tire et elle attend211 let named = !storeName.isEmpty212 return Button(action: onStore) {213 HangingSign(metrics: metrics, platine: state.platine, arm: state.arm, leg: state.leg,214 shown: state.plaqueShown && named, theta: state.theta) {215 Plaque(full: state.full, theta: state.theta, radius: metrics.radius,216 padding: EdgeInsets(top: 14, leading: 18, bottom: 15, trailing: 16)) { tone in217 PlaqueText(tone: tone, eyebrow: eyebrow, name: named ? storeName : " ", line: line,218 nameSize: wide ? 32 : 26, chevron: true)219 }220 .scaleEffect(state.plaqueScale, anchor: .top)221 }222 .contentShape(Rectangle())223 }224 .buttonStyle(.plain)225 .accessibilityElement(children: .ignore)226 .accessibilityLabel("\(eyebrow), \(storeName)" + (line.map { ", \($0)" } ?? ""))227 .accessibilityHint("Ouvre la page de la boutique")228 .accessibilityAddTraits(.isButton)229 .accessibilityIdentifier("store-plaque")230 }231232 // --- L'action : le bouton dit où il mène ----------------------------------------------------------233234 private func action(_ state: SignState) -> some View {235 let host = Fmt.host(product.url) ?? Fmt.host(store?.url) ?? product.storeId236 return VStack(alignment: .leading, spacing: 10) {237 Button(action: onVisit) {238 HStack(spacing: 8) {239 Text("Voir chez \(storeName)").lineLimit(2)240 Image("ic-lien-boutique").resizable().scaledToFit().frame(width: 18, height: 18)241 }242 }243 .buttonStyle(PrimaryButtonStyle())244 .disabled(product.url == nil)245 .accessibilityHint("Ouvre la page du produit chez la boutique")246 .accessibilityIdentifier("visit-store")247 Text("On n'achète rien ici : le bouton mène à \(host)." + (lastSeen.map { " Relevé le \(Fmt.date($0))." } ?? ""))248 .font(.footnote).num().foregroundStyle(Theme.ink3)249 .fixedSize(horizontal: false, vertical: true)250 }251 .frame(maxWidth: wide ? 440 : .infinity, alignment: .leading)252 .opacity(state.action).offset(y: 8 * (1 - state.action))253 }254}255