SPB Git

Repository Public

ElectricsView.swift 5.6 KB 134 lines swift

1//2//  Auto-Ka iOS v23//  File: AutoKa/Features/Market/ElectricsView.swift4//  Description: électriques et hybrides — trois segments (électrique, hybride rechargeable, hybride) : part,5//               médianes, modèles les plus présents, régions. Les parts sont des planchers : le dire.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import SwiftUI1213struct ElectricsView: View {14    var body: some View {15        Loadable(id: "electriques") {16            try await APIClient.shared.electrics()17        } content: { data in18            ElectricsContent(data: data)19        }20        .navigationTitle("Électriques et hybrides")21        .navigationBarTitleDisplayMode(.inline)22    }23}2425private struct ElectricsContent: View {26    let data: Electrics27    @Environment(AppModel.self) private var model2829    private var total: Int { data.segments.reduce(0) { $0 + $1.n } }3031    var body: some View {32        Page {33            head34            ForEach(data.segments) { segment in35                segmentView(segment)36            }37            if data.segments.isEmpty {38                EmptyState(title: "Aucun véhicule électrifié en ligne", message: "Les annonces du moment n'en comptent aucun.")39            }40        }41    }4243    private var head: some View {44        VStack(alignment: .leading, spacing: 14) {45            Text("Électriques et hybrides").font(.editorial(28)).foregroundStyle(Theme.ink).accessibilityAddTraits(.isHeader)46            Text(headLine).font(.subheadline).foregroundStyle(Theme.ink2).num().fixedSize(horizontal: false, vertical: true)47            // la mise en garde du service, telle quelle48            if let note = data.note, !note.isEmpty {49                Text(note).font(.footnote).foregroundStyle(Theme.ink3).num().fixedSize(horizontal: false, vertical: true)50            } else if let unknown = data.fuelUnknown, unknown > 0 {51                Text("\(Fmt.int(unknown)) annonces n'indiquent pas le carburant : les parts sont des planchers.")52                    .font(.footnote).foregroundStyle(Theme.ink3).num().fixedSize(horizontal: false, vertical: true)53            }54            if total > 0 {55                Button {56                    model.search(.fuel("electrifie"))57                } label: {58                    Text("Voir les \(Fmt.int(total)) véhicules électrifiés")59                }60                .buttonStyle(PrimaryButtonStyle())61            }62        }63    }6465    private var headLine: String {66        var s = "\(Fmt.int(total)) voitures électriques, hybrides rechargeables ou hybrides sont en vente"67        if let all = data.autoTotal, all > 0 { s += " sur \(Fmt.int(all)) voitures en ligne" }68        return s + "."69    }7071    private func segmentView(_ s: Electrics.Segment) -> some View {72        VStack(alignment: .leading, spacing: 16) {73            Rule(strong: true)74            SectionHeader(title(s.fuel)) {75                MoreLink(label: "Voir") { model.search(.fuel(s.fuel)) }76            }77            StatGrid {78                StatFigure(value: Fmt.int(s.n), label: "voitures en vente", detail: s.sharePct.map { "au moins \(Fmt.pct($0)) des voitures" })79                StatFigure(value: Fmt.money(s.priceMedian), label: "prix demandé médian")80                StatFigure(value: Fmt.mileage(s.kmMedian), label: "kilométrage médian")81                if let y = s.yearMedian { StatFigure(value: String(Int(y.rounded())), label: "année-modèle médiane") }82            }83            if let models = s.topModels, !models.isEmpty {84                let top = Array(models.prefix(8)), maxN = Double(top.map(\.n).max() ?? 1)85                Text("Modèles les plus présents").eyebrowStyle().padding(.top, 2)86                VStack(spacing: 12) {87                    ForEach(top) { m in88                        Button {89                            var f = SearchFilters.make(m.make, model: m.model)90                            f.fuels = [s.fuel]91                            model.search(f)92                        } label: {93                            BarRow(label: "\(m.make) \(m.model)", value: Double(m.n), max: maxN,94                                   valueLabel: "\(Fmt.int(m.n)) · \(Fmt.money(m.priceMedian))", icon: Fuel.icon(for: s.fuel))95                                .frame(minHeight: 44)96                                .contentShape(Rectangle())97                        }98                        .buttonStyle(.plain)99                        .accessibilityHint("Lance la recherche")100                    }101                }102            }103            if let regions = s.byRegion, !regions.isEmpty {104                let top = Array(regions.prefix(6)), maxN = Double(top.map(\.n).max() ?? 1)105                Text("Régions").eyebrowStyle().padding(.top, 2)106                VStack(spacing: 12) {107                    ForEach(top) { r in108                        Button {109                            var f = SearchFilters.region(r.region)110                            f.fuels = [s.fuel]111                            model.search(f)112                        } label: {113                            BarRow(label: r.region, value: Double(r.n), max: maxN, color: Theme.ink3)114                                .frame(minHeight: 44)115                                .contentShape(Rectangle())116                        }117                        .buttonStyle(.plain)118                        .accessibilityHint("Lance la recherche")119                    }120                }121            }122        }123    }124125    private func title(_ fuel: String) -> String {126        switch fuel {127        case "Électrique": "Électriques"128        case "Hybride rechargeable": "Hybrides rechargeables"129        case "Hybride": "Hybrides"130        default: fuel131        }132    }133}134
Files main
Menu

Theme