Repository Public
1//2// Auto-Ka iOS v23// File: AutoKa/Features/Catalog/CitiesView.swift4// Description: index des villes — régions administratives, puis villes avec nombre de véhicules et prix médian ;5// recherche locale et filtre par région.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import SwiftUI1213struct CitiesView: View {14 /// régions et villes chargées ensemble15 struct Index: Sendable {16 var regions: RegionsResponse17 var cities: CitiesResponse18 }1920 @Environment(AppModel.self) private var model21 @State private var query = ""22 /// nom de la région retenue ; nil = toutes23 @State private var region: String?24 @State private var limit = 602526 var body: some View {27 Loadable(id: "villes") {28 async let r = APIClient.shared.regions()29 async let c = APIClient.shared.cities(min: 1)30 return Index(regions: try await r, cities: try await c)31 } content: { (index: Index) in32 content(index)33 }34 .navigationTitle("Villes")35 .navigationBarTitleDisplayMode(.large)36 .searchable(text: $query, prompt: "Chercher une ville")37 .onChange(of: query) { limit = 60 }38 .onChange(of: region) { limit = 60 }39 }4041 @ViewBuilder42 private func content(_ index: Index) -> some View {43 let rows = filtered(index.cities.cities)44 Page(spacing: 18) {45 Text(intro(index)).font(.subheadline).foregroundStyle(Theme.ink3).fixedSize(horizontal: false, vertical: true)4647 ScrollView(.horizontal, showsIndicators: false) {48 HStack(spacing: 8) {49 ChoiceChip(label: "Toutes les régions", selected: region == nil) { region = nil }50 ForEach(index.regions.regions.sorted { $0.n > $1.n }) { r in51 ChoiceChip(label: r.region, count: r.n, selected: region == r.region) { region = region == r.region ? nil : r.region }52 }53 }54 .padding(.horizontal, HeroLayout.margin)55 }56 .padding(.horizontal, -HeroLayout.margin)5758 if let name = region, let r = index.regions.regions.first(where: { $0.region == name }) {59 HStack(alignment: .firstTextBaseline) {60 Text("\(r.region) : \(Fmt.plural(r.n, "véhicule", "véhicules"))\(r.priceMedian.map { ", prix médian \(Fmt.money($0))" } ?? "").")61 .font(.subheadline).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true)62 Spacer(minLength: 8)63 MoreLink(label: "Voir") { model.search(.region(r.region)) }64 }65 }6667 if rows.isEmpty {68 EmptyState(title: "Aucune ville ne correspond", message: "Vérifiez l'orthographe, ou choisissez une autre région.",69 actionLabel: "Tout afficher") { query = ""; region = nil }70 } else {71 LazyVStack(spacing: 0) {72 Rule()73 ForEach(rows.prefix(limit)) { c in74 Button { model.open(.city(c.slug)) } label: {75 IndexRow(title: c.city, detail: detail(c), value: Fmt.int(c.n))76 }77 .buttonStyle(.plain)78 }79 if rows.count > limit {80 // la suite arrive quand on atteint le bas de la liste81 Color.clear.frame(height: 1).onAppear { limit += 60 }82 }83 }84 }85 }86 }8788 private func intro(_ index: Index) -> String {89 var s = "\(Fmt.plural(index.cities.total, "ville", "villes")) dans \(Fmt.plural(index.regions.total, "région", "régions"))."90 if let n = index.regions.noRegion, n > 0 { s += " \(Fmt.plural(n, "annonce n'indique", "annonces n'indiquent")) pas de région." }91 return s92 }9394 private func filtered(_ cities: [CitySummary]) -> [CitySummary] {95 let q = Fmt.slug(query)96 return cities.filter { c in97 (region == nil || c.region == region) && (q.isEmpty || Fmt.slug(c.city).contains(q))98 }99 .sorted { $0.n > $1.n }100 }101102 /// « Estrie · prix médian 24 995 $ · 14 concessionnaires »103 private func detail(_ c: CitySummary) -> String {104 var p: [String] = []105 if region == nil, let r = c.region, !r.isEmpty { p.append(r) }106 if let pm = c.priceMedian { p.append("prix médian \(Fmt.money(pm))") }107 if let d = c.dealers, d > 0 { p.append(Fmt.plural(d, "concessionnaire", "concessionnaires")) }108 return p.joined(separator: " · ")109 }110}111