Repository Public
1//2// Food-Ka iOS v23// File: FoodKa/Design/Shelf/ShelfScene.swift4// Description: la scène du geste signature « la mise en tablette », en haut d'une fiche — le produit voyage depuis5// la vignette touchée, la tablette se trace sous lui, il s'y pose (pose et tassement visibles),6// l'étiquette pleine glisse dans le rail avec le prix, puis l'aplat se lève : le prix reste à l'encre.7// Vue pure du temps t (ms) : animable, interruptible, figeable.8//9// Author: Simon-Pierre Boucher10// Contact: contact@spboucher.ai11//1213import SwiftUI1415/// Ce que porte l'étiquette de tablette d'une fiche.16struct ShelfTagContent: Equatable, Sendable {17 var eyebrow: String18 var store: String19 var lines: [String]20 var price: String21 var unitPrice: String?2223 /// Étiquette d'un produit : son prix, son enseigne, et sa place parmi les enseignes qui le vendent.24 static func make(for p: Product, storesCount: Int? = nil, bestStoreName: String? = nil) -> ShelfTagContent {25 let n = storesCount ?? p.compare?.stores ?? 126 var eyebrow = p.comparable ? "Une seule enseigne" : "Prix hors Québec"27 var lines: [String] = []28 if n > 1 {29 if p.compare?.isBest ?? true {30 eyebrow = "Le moins cher"31 lines.append("\(n) enseignes")32 } else {33 eyebrow = "\(n) enseignes"34 if let best = p.compare?.bestPrice {35 lines.append("moins cher \((bestStoreName ?? p.compare?.bestStoreName).map { "chez " + $0 } ?? "ailleurs") : " + Fmt.price(best))36 }37 }38 }39 if p.isFlyer {40 lines.append(p.validTo.map { "circulaire, jusqu'au " + Fmt.shortDate($0) } ?? "prix de circulaire")41 }42 if let r = p.regularPrice, let price = p.price, r > price + 0.004 { lines.append("rég. " + Fmt.price(r)) }43 return ShelfTagContent(eyebrow: eyebrow, store: p.storeName, lines: lines, price: Fmt.price(p.price),44 unitPrice: p.unitPrice?.label ?? (p.byWeight ? "vendu au poids" : nil))45 }4647 var spoken: String {48 ([price + " chez " + store, eyebrow.lowercased()] + lines + [unitPrice].compactMap { $0 }).joined(separator: ", ")49 }50}5152/// L'étiquette de tablette : enseigne et précisions à gauche ; prix, et dessous plus petit le prix unitaire, à droite.53struct ShelfTag: View {54 let content: ShelfTagContent55 /// pleine (betterave, pendant le geste) ou à l'encre sur le papier (état permanent)56 var full = false57 @Environment(\.dynamicTypeSize) private var typeSize5859 var body: some View {60 let fg = full ? Theme.tagInk : Theme.ink61 let fg2 = full ? Theme.tagInk.opacity(0.88) : Theme.ink362 Group {63 if typeSize.isAccessibilitySize {64 VStack(alignment: .leading, spacing: 8) { left(fg, fg2); right(fg, .leading) }65 .frame(maxWidth: .infinity, alignment: .leading)66 } else {67 HStack(alignment: .top, spacing: 10) {68 left(fg, fg2)69 Spacer(minLength: 4)70 right(fg, .trailing)71 }72 }73 }74 .padding(.horizontal, 14).padding(.vertical, 13)75 .frame(maxWidth: .infinity, minHeight: 96, alignment: .topLeading)76 .background {77 if full { RoundedRectangle(cornerRadius: 12, style: .continuous).fill(Theme.tag) }78 }79 }8081 private func left(_ fg: Color, _ fg2: Color) -> some View {82 VStack(alignment: .leading, spacing: 3) {83 Text(content.eyebrow).font(.caption2.weight(.semibold)).tracking(0.9).textCase(.uppercase).foregroundStyle(fg2)84 Text(content.store).font(.display(18)).foregroundStyle(fg).lineLimit(2).minimumScaleFactor(0.8)85 ForEach(content.lines, id: \.self) { l in86 Text(l).font(.caption).monospacedDigit().foregroundStyle(fg2).fixedSize(horizontal: false, vertical: true)87 }88 }89 }9091 private func right(_ fg: Color, _ alignment: HorizontalAlignment) -> some View {92 VStack(alignment: alignment, spacing: 0) {93 // le prix est le plus gros chiffre de la page ; pas de cents en exposant94 Text(content.price)95 .font(.system(size: Font.scaled(44, max: 64), weight: .semibold)).monospacedDigit()96 .foregroundStyle(fg).lineLimit(1).minimumScaleFactor(0.6)97 if let u = content.unitPrice {98 Text(u).font(.system(size: Font.scaled(14, max: 22), weight: .medium)).monospacedDigit()99 .foregroundStyle(fg.opacity(full ? 0.95 : 0.8)).lineLimit(1).minimumScaleFactor(0.7)100 }101 }102 .layoutPriority(1)103 }104}105106struct ShelfScene: View, @preconcurrency Animatable {107 let image: String?108 var icon: String = "ic-tablette"109 var flyer = false110 let tag: ShelfTagContent111 /// chorégraphie jouée ; nil = état final (sans mouvement)112 var choreo: ShelfChoreo?113 /// rectangle de la vignette d'origine, en coordonnées globales114 var source: CGRect?115 /// temps écoulé depuis le toucher, en ms116 var t: Double117118 @Environment(\.horizontalSizeClass) private var hSize119120 var animatableData: Double {121 get { t }122 set { t = newValue }123 }124125 private var box: CGSize { hSize == .regular ? CGSize(width: 250, height: 290) : CGSize(width: 170, height: 196) }126 private let shelfThickness: CGFloat = 5127128 var body: some View {129 let c = choreo130 let done = c == nil131 let travel = done ? 1 : c!.travelK(t)132 let shelfK = done ? 1 : c!.shelfK(t)133 let settle = done ? 0 : c!.settleY(t)134 let hover = done ? 0 : c!.height(t)135 let grab = done ? 0 : c!.grabK(t)136 let tagOffset = done ? 0 : c!.tagOffset(t)137 let tagOpacity = done ? 0 : c!.tagOpacity(t)138 let lift = done ? 1 : c!.liftK(t)139 let inkOpacity = done ? 1 : c!.inkOpacity(t)140141 VStack(alignment: .leading, spacing: 0) {142 Color.clear.frame(height: box.height)143 GeometryReader { geo in144 ShelfBar(thickness: shelfThickness)145 .scaleEffect(x: shelfK, y: 1, anchor: UnitPoint(x: (8 + box.width / 2) / max(1, geo.size.width), y: 0.5))146 }147 .frame(height: shelfThickness)148 .offset(y: settle)149 // l'étiquette : l'encre dessous, le plein dessus ; le plein glisse dans le rail puis se lève vers la tablette150 ZStack {151 ShelfTag(content: tag).opacity(inkOpacity)152 GeometryReader { geo in153 ShelfTag(content: tag, full: true)154 .mask(Rectangle().scaleEffect(x: 1, y: 1 - lift, anchor: .top))155 .offset(x: tagOffset * geo.size.width)156 .opacity(tagOpacity * (lift >= 1 ? 0 : 1))157 }158 }159 .clipped()160 .padding(.top, 8)161 }162 .overlay(alignment: .topLeading) {163 GeometryReader { geo in164 let origin = geo.frame(in: .global).origin165 let final = CGRect(x: 8, y: 0, width: box.width, height: box.height)166 // sans vignette d'origine (capture, lien) : départ d'une vignette de liste, plus bas dans la page167 let from = source.map { CGRect(x: $0.minX - origin.x, y: $0.minY - origin.y, width: $0.width, height: $0.height) }168 ?? CGRect(x: 0, y: box.height + 150, width: 56, height: 52)169 let k = CGFloat(travel)170 let r = CGRect(x: from.minX + (final.minX - from.minX) * k, y: from.minY + (final.minY - from.minY) * k,171 width: from.width + (final.width - from.width) * k, height: from.height + (final.height - from.height) * k)172 ProductPhoto(source: image, maxPixel: 640, icon: icon, flyer: flyer)173 .frame(width: r.width, height: r.height)174 .offset(x: r.minX, y: r.minY - 8 * grab * (1 - k) - hover * k + settle)175 }176 }177 .accessibilityElement(children: .ignore)178 .accessibilityLabel(tag.spoken)179 .accessibilityIdentifier("shelf-scene")180 }181}182