SPB Git

Repository Public

FollowedView.swift 5.0 KB 116 lines swift

1//2//  Food-Ka iOS v23//  File: FoodKa/Features/Followed/FollowedView.swift4//  Description: produits suivis — le prix de chacun est relu à l'ouverture de l'app ; une baisse depuis le dernier5//               passage est signalée ici et à l'accueil. Tout reste sur l'appareil. Fiches récemment consultées.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import SwiftUI1213struct FollowedView: View {14    @Environment(AppModel.self) private var model15    @Environment(Library.self) private var library1617    var body: some View {18        ScrollView {19            LazyVStack(alignment: .leading, spacing: 0) {20                if library.followed.isEmpty {21                    EmptyShelf(title: "Aucun produit suivi",22                               message: "Sur une fiche, touchez le cœur pour suivre un produit. À chaque ouverture de l'app, son prix est relu et une baisse vous est signalée ici.",23                               actionLabel: "Chercher un produit") { model.tab = .home }24                        .accessibilityIdentifier("followed-empty")25                } else {26                    status27                    if !library.drops.isEmpty {28                        SectionHeader("En baisse depuis votre dernier passage").padding(.top, 14)29                        ForEach(library.drops) { f in row(f); Hairline() }30                        Button("J'ai vu ces baisses") { library.acknowledge() }31                            .font(.subheadline.weight(.medium)).foregroundStyle(Theme.brand).frame(minHeight: 44)32                            .accessibilityIdentifier("acknowledge")33                    }34                    let others = library.followed.filter { ($0.change ?? 0) >= 0 }35                    if !others.isEmpty {36                        SectionHeader(library.drops.isEmpty ? "Produits suivis" : "Les autres").padding(.top, 22)37                        ForEach(others) { f in row(f); Hairline() }38                    }39                    HonestNote(text: "Les prix sont relus quand l'app s'ouvre. Il n'y a pas de notification : sans serveur ni compte, l'app ne peut pas surveiller un prix quand elle est fermée.")40                        .padding(.top, 20)41                }42                if !library.recents.isEmpty {43                    SectionHeader("Consultés récemment") {44                        if library.recents.count > 5 { MoreLink { model.open(.recents) } }45                    }46                    .padding(.top, 34)47                    ForEach(library.recents.prefix(5)) { p in48                        ProductRow(product: p, trigger: .secondary)49                        Hairline()50                    }51                }52            }53            .pageColumn().padding(.bottom, 30)54        }55        .paper()56        .navigationTitle("Suivis")57        .refreshable { await library.refreshFollowed() }58    }5960    private var status: some View {61        HStack(spacing: 8) {62            if library.refreshing {63                ProgressView().controlSize(.small)64                Text("Lecture des prix en cours…")65            } else if let at = library.refreshedAt {66                Text("\(Fmt.plural(library.followed.count, "produit suivi", "produits suivis")) · prix relus \(Fmt.ago(at.timeIntervalSince1970) ?? "à l'instant")")67            } else {68                Text(Fmt.plural(library.followed.count, "produit suivi", "produits suivis"))69            }70        }71        .font(.subheadline).monospacedDigit().foregroundStyle(Theme.ink3)72        .padding(.top, 4)73        .accessibilityIdentifier("followed-status")74    }7576    private func row(_ f: FollowedProduct) -> some View {77        ProductRow(product: f.product, trigger: .secondary, note: note(f))78            .contextMenu {79                Button("Ne plus suivre", systemImage: "heart.slash", role: .destructive) { library.toggleFollow(f.product) }80            }81    }8283    private func note(_ f: FollowedProduct) -> String {84        var s = f.product.storeName85        if f.withdrawn { return s + " · n'est plus offert" }86        if let c = f.change, let before = f.priceSeen {87            s += " · \(Fmt.diff(c)) (était \(Fmt.price(before)))"88        } else if let c = f.changeSinceFollow {89            s += " · \(Fmt.diff(c)) depuis le \(Fmt.shortDate(f.followedAt))"90        } else {91            s += " · inchangé depuis le \(Fmt.shortDate(f.followedAt))"92        }93        return s94    }95}9697struct RecentsView: View {98    @Environment(Library.self) private var library99100    var body: some View {101        ScrollView {102            LazyVStack(alignment: .leading, spacing: 0) {103                ForEach(library.recents) { p in104                    ProductRow(product: p, trigger: .secondary)105                    Hairline()106                }107            }108            .pageColumn().padding(.bottom, 30)109        }110        .paper()111        .navigationTitle("Consultés récemment")112        .navigationBarTitleDisplayMode(.inline)113        .toolbar { ToolbarItem(placement: .topBarTrailing) { Button("Effacer") { library.clearRecents() } } }114    }115}116
Files main
Menu

Theme