Repository Public
1//2// Fabri-Ka iOS v23// File: FabriKa/Features/Saved/SavedView.swift4// Description: onglet « Favoris » — produits gardés et boutiques suivies, sur l'appareil seulement ; un produit que5// la boutique n'offre plus est signalé, un prix qui a bougé aussi. Fiches vues récemment.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import SwiftUI1213struct SavedView: View {14 @Environment(AppModel.self) private var model15 @Environment(Library.self) private var library16 @State private var segment: Segment = .products17 @State private var applied = false1819 enum Segment: String, CaseIterable, Identifiable {20 case products, stores21 var id: String { rawValue }22 }2324 var body: some View {25 ScrollView {26 VStack(alignment: .leading, spacing: 0) {27 Picker("Affichage", selection: $segment) {28 Text("Produits" + (library.favorites.isEmpty ? "" : " (\(library.favorites.count))")).tag(Segment.products)29 Text("Boutiques" + (library.stores.isEmpty ? "" : " (\(library.stores.count))")).tag(Segment.stores)30 }31 .pickerStyle(.segmented)32 .padding(.top, 8)33 .accessibilityIdentifier("saved-segment")34 if segment == .products { products } else { stores }35 Text("Vos favoris et vos boutiques suivies restent sur cet appareil : aucun compte, rien n'est envoyé.")36 .font(.footnote).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true).padding(.top, 34)37 if !library.recents.isEmpty {38 NavRow(icon: "ic-liste", title: "Vus récemment", detail: Fmt.plural(library.recents.count, "fiche", "fiches"), divider: false) {39 model.open(.recents)40 }41 .padding(.top, 10)42 }43 }44 .pageColumn(920)45 .padding(.bottom, 40)46 }47 .linen()48 .navigationTitle("Favoris")49 .refreshable { await library.refreshFavorites(force: true) }50 .onAppear {51 if !applied { applied = true; if model.launchSaved == "stores" { segment = .stores } }52 }53 }5455 @ViewBuilder56 private var products: some View {57 if library.favorites.isEmpty {58 EmptyBracket(title: "Aucun produit gardé",59 message: "Sur une fiche, le signet en haut à droite garde le produit ici. Rien ne quitte l'appareil.",60 actionLabel: "Chercher un produit", action: { model.tab = .search })61 .accessibilityIdentifier("favorites-empty")62 } else {63 let withdrawn = library.favorites.filter(\.withdrawn)64 if !withdrawn.isEmpty {65 HonestNote(text: Fmt.plural(withdrawn.count, "produit n'est plus offert", "produits ne sont plus offerts") + " par sa boutique depuis le dernier relevé.")66 .padding(.top, 16)67 }68 LazyVGrid(columns: [GridItem(.adaptive(minimum: 150, maximum: 260), spacing: 14, alignment: .top)], alignment: .leading, spacing: 24) {69 ForEach(library.favorites) { f in70 VStack(alignment: .leading, spacing: 6) {71 ProductCard(product: f.product, trigger: .secondary)72 .opacity(f.withdrawn ? 0.55 : 1)73 if f.withdrawn {74 Text("Retiré par la boutique").font(.caption.weight(.semibold)).foregroundStyle(Theme.bad)75 } else if f.priceChange != nil, let before = f.priceAtSave {76 Text("\(Fmt.price(before)) quand vous l'avez gardé")77 .font(.caption).num().foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true)78 }79 }80 .contextMenu {81 Button("Retirer des favoris", role: .destructive) { library.toggleFavorite(f.product) }82 }83 }84 }85 .padding(.top, 20)86 }87 }8889 @ViewBuilder90 private var stores: some View {91 if library.stores.isEmpty {92 EmptyBracket(title: "Aucune boutique suivie",93 message: "Sur la page d'une boutique, « Suivre » accroche son enseigne ici.",94 actionLabel: "Voir les boutiques", action: { model.tab = .stores })95 .accessibilityIdentifier("followed-empty")96 } else {97 SignGrid(items: library.stores) { s in98 StoreCard(store: s.store)99 .contextMenu {100 Button("Ne plus suivre", role: .destructive) { library.toggleFollow(s.store) }101 }102 }103 .padding(.top, 24)104 }105 }106}107108/// Fiches vues récemment (trente au plus), gardées sur l'appareil.109struct RecentsView: View {110 @Environment(Library.self) private var library111112 var body: some View {113 ScrollView {114 VStack(alignment: .leading, spacing: 0) {115 if library.recents.isEmpty {116 EmptyBracket(title: "Aucune fiche vue pour l'instant", message: "Les trente dernières fiches ouvertes paraissent ici.")117 } else {118 ProductGrid(products: library.recents, trigger: .secondary)119 .padding(.top, 8)120 }121 }122 .pageColumn(920)123 .padding(.bottom, 40)124 }125 .linen()126 .navigationTitle("Vus récemment")127 .toolbar {128 if !library.recents.isEmpty {129 ToolbarItem(placement: .topBarTrailing) { Button("Effacer") { library.clearRecents() } }130 }131 }132 }133}134