SPB Git

Repository Public

BrandView.swift 4.5 KB 111 lines swift

1//2//  Auto-Ka iOS v23//  File: AutoKa/Features/Catalog/BrandView.swift4//  Description: page d'une marque — chiffres clés, modèles et prix médians, verdicts, carrosseries, carburants,5//               régions, véhicules récents.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import SwiftUI1213struct BrandView: View {14    let slug: String1516    @Environment(AppModel.self) private var model17    @State private var allModels = false1819    var body: some View {20        Loadable(id: "marque-\(slug)") { [slug] in21            try await APIClient.shared.make(slug)22        } content: { (page: MakePage) in23            content(page)24                .navigationTitle(page.make)25        }26        .navigationBarTitleDisplayMode(.inline)27    }2829    @ViewBuilder30    private func content(_ page: MakePage) -> some View {31        Page {32            VStack(alignment: .leading, spacing: 6) {33                Text("Véhicules d'occasion · Québec").eyebrowStyle()34                Text(page.make).font(.editorial(30)).foregroundStyle(Theme.ink).accessibilityAddTraits(.isHeader)35                Text("\(Fmt.plural(page.n, "véhicule en vente", "véhicules en vente")), \(Fmt.plural(page.models.count, "modèle", "modèles")).")36                    .font(.subheadline).foregroundStyle(Theme.ink3)37            }3839            StatGrid {40                StatFigure(value: Fmt.money(page.priceMedian), label: "Prix médian demandé")41                StatFigure(value: Fmt.mileageShort(page.kmMedian) ?? "—", label: "Kilométrage médian")42                StatFigure(value: range(page.priceMin, page.priceMax), label: "Du moins cher au plus cher")43                StatFigure(value: years(page.yearMin, page.yearMax), label: "Années-modèles")44            }4546            Button("Voir les \(Fmt.int(page.n)) véhicules") { model.search(.make(page.make)) }47                .buttonStyle(PrimaryButtonStyle())4849            if !page.models.isEmpty {50                VStack(alignment: .leading, spacing: 10) {51                    SectionHeader("Modèles", subtitle: "Prix médian demandé de chaque modèle")52                    let shown = allModels ? page.models : Array(page.models.prefix(12))53                    VStack(spacing: 0) {54                        Rule()55                        ForEach(shown) { m in56                            Button { model.open(.model(make: slug, model: m.slug)) } label: {57                                IndexRow(title: m.model, detail: Fmt.plural(m.n, "véhicule", "véhicules"), value: Fmt.money(m.priceMedian))58                            }59                            .buttonStyle(.plain)60                        }61                    }62                    if page.models.count > 12, !allModels {63                        MoreLink(label: "Les \(page.models.count) modèles") { allModels = true }64                    }65                }66            }6768            if let v = page.verdicts, v.values.contains(where: { $0 > 0 }) {69                VStack(alignment: .leading, spacing: 12) {70                    SectionHeader("Prix face au marché", subtitle: "Verdict de chaque véhicule, calculé par le service sur ses comparables")71                    VerdictBreakdown(counts: v)72                }73            }7475            bars("Carrosseries", page.bodyTypes) { BodyType.icon(for: $0) }76            bars("Carburants", page.fuels) { Fuel.icon(for: $0) }77            bars("Régions", page.regions.map { Array($0.prefix(10)) }) { _ in nil }7879            if !page.listings.isEmpty {80                VStack(alignment: .leading, spacing: 14) {81                    SectionHeader("Annonces récentes") { MoreLink { model.search(.make(page.make)) } }82                    VehicleRail(vehicles: page.listings)83                }84            }85        }86    }8788    @ViewBuilder89    private func bars(_ title: String, _ values: [FacetValue]?, icon: @escaping (String) -> String?) -> some View {90        if let values, !values.isEmpty {91            let top = Double(values.map(\.n).max() ?? 1)92            VStack(alignment: .leading, spacing: 12) {93                SectionHeader(title)94                ForEach(values) { v in95                    BarRow(label: v.title, value: Double(v.n), max: top, icon: icon(v.value))96                }97            }98        }99    }100101    private func range(_ a: Double?, _ b: Double?) -> String {102        guard let a, let b else { return "—" }103        return "\(Fmt.kPrice(a)) à \(Fmt.kPrice(b))"104    }105106    private func years(_ a: Int?, _ b: Int?) -> String {107        guard let a, let b else { return "—" }108        return a == b ? "\(a)" : "\(a) à \(b)"109    }110}111
Files main
Menu

Theme