Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Vrai-Prix iOS9// File : VraiPrix/Features/Listings/ListingsView.swift10// Description : propriétés à vendre comparées à leur estimation — liste11// filtrable, jauge miniature et verdict par ligne.12// Module : VraiPrix13// Repository : https://git.spboucher.ai/vrai-prix-ios-v314// Project Website : https://www.vrai-prix.com15// Created : 2026-11-2616// Last Modified : 2026-11-2617//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SwiftUI9192struct ListingsView: View {93 @State var filters: ListingFilters94 @Environment(AppModel.self) private var model95 @State private var items: [ListingSummary] = []96 @State private var total = 097 @State private var page = 098 @State private var summary: ListingsPage.Summary?99 @State private var loading = false100 @State private var stale = false101 @State private var error: APIError?102 @State private var placeDraft = ""103 @State private var showFilters = false104105 var body: some View {106 ScrollView {107 LazyVStack(alignment: .leading, spacing: 0) {108 Text("Le prix demandé de chaque propriété, lu contre sa valeur estimée.")109 .font(.body).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true).padding(.bottom, 14)110 controls111 if stale { StaleNotice().padding(.top, 10) }112 VStack(alignment: .leading, spacing: 3) {113 Text(loading && items.isEmpty ? "Recherche…" : "\(Fmt.int(total)) \(total == 1 ? "propriété" : "propriétés") à vendre\(filters.evalOnly ? " avec estimation" : "")")114 .font(.subheadline.weight(.medium)).num().foregroundStyle(Theme.ink)115 .accessibilityIdentifier("listings-count")116 if let s = summary, let p = s.medianPrice {117 Text("Prix demandé médian : \(Fmt.money(p)).").font(.footnote).num().foregroundStyle(Theme.ink3)118 }119 }120 .padding(.top, 14).padding(.bottom, 4)121 Hairline()122 list123 }124 .pageColumn()125 .padding(.top, 8).padding(.bottom, 32)126 }127 .background(Theme.bg)128 .navigationTitle("À vendre")129 .navigationBarTitleDisplayMode(.inline)130 .sheet(isPresented: $showFilters) { ListingFiltersSheet(filters: $filters).presentationDetents([.medium, .large]) }131 .task(id: filters) { await reload() }132 .task { placeDraft = filters.place }133 }134135 private var controls: some View {136 VStack(alignment: .leading, spacing: 10) {137 HStack(spacing: 8) {138 HStack(spacing: 8) {139 Image(systemName: "magnifyingglass").foregroundStyle(Theme.ink3)140 TextField("Ville ou rue", text: $placeDraft)141 .textInputAutocapitalization(.words).autocorrectionDisabled().submitLabel(.search)142 .onSubmit { filters.place = placeDraft.trimmingCharacters(in: .whitespaces) }143 if !placeDraft.isEmpty {144 Button { placeDraft = ""; filters.place = "" } label: {145 Image(systemName: "xmark.circle.fill").foregroundStyle(Theme.ink3).frame(width: 30, height: 44)146 }147 .buttonStyle(.plain).accessibilityLabel("Effacer le lieu")148 }149 }150 .padding(.horizontal, 12).frame(minHeight: 48)151 .background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))152 .overlay(RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous).strokeBorder(Theme.line2, lineWidth: 1))153 Button { showFilters = true } label: {154 HStack(spacing: 5) {155 Image(systemName: "slider.horizontal.3")156 let n = filters.activeCount - (filters.place.isEmpty ? 0 : 1)157 if n > 0 { Text(String(n)).font(.footnote.weight(.bold)).monospacedDigit() }158 }159 .foregroundStyle(Theme.ink).frame(minWidth: 48, minHeight: 48)160 .background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))161 .overlay(RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous).strokeBorder(Theme.line2, lineWidth: 1))162 }163 .buttonStyle(.plain)164 .accessibilityLabel("Filtres : type, prix, tri")165 }166 Menu {167 Picker("Tri", selection: $filters.sort) {168 ForEach(ListingFilters.Sort.allCases) { Text($0.label).tag($0) }169 }170 } label: {171 HStack(spacing: 6) {172 Image(systemName: "arrow.up.arrow.down").font(.footnote)173 Text("Tri : " + filters.sort.label).font(.subheadline)174 }175 .foregroundStyle(Theme.brand).frame(minHeight: 44)176 }177 }178 }179180 @ViewBuilder181 private var list: some View {182 if let error, items.isEmpty {183 ErrorState(error: error, subject: "La liste des propriétés à vendre") { Task { await reload() } }184 } else if items.isEmpty, !loading {185 EmptyState(title: "Aucune propriété à vendre pour ces critères", message: "Retirez un filtre ou vérifiez l'orthographe du lieu.",186 actionLabel: filters.activeCount > 0 ? "Retirer les filtres" : nil) {187 placeDraft = ""; filters = ListingFilters(evalOnly: filters.evalOnly, sort: filters.sort)188 }189 } else if items.isEmpty {190 LoadingState(label: "Chargement des propriétés")191 } else {192 ForEach(Array(items.enumerated()), id: \.element.uid) { i, l in193 Button { model.openListing(l.uid) } label: { ListingRow(listing: l) }194 .buttonStyle(.plain)195 .accessibilityIdentifier("listing-row-\(i)")196 .onAppear { if i >= items.count - 5 { Task { await loadMore() } } }197 Hairline()198 }199 if items.count < total { HStack { Spacer(); ProgressView(); Spacer() }.padding(.vertical, 18) }200 }201 }202203 private func reload() async {204 loading = true; error = nil205 do {206 let f = try await APIClient.shared.listings(filters, page: 1)207 if Task.isCancelled { return }208 items = f.value.items; total = f.value.total; page = 1; summary = f.value.summary; stale = f.stale209 } catch is CancellationError { return } catch {210 items = []; total = 0; self.error = apiError(error)211 }212 loading = false213 }214215 private func loadMore() async {216 guard !loading, items.count < total else { return }217 loading = true218 defer { loading = false }219 let current = filters220 if let f = try? await APIClient.shared.listings(current, page: page + 1), current == filters {221 let known = Set(items.map(\.uid))222 let fresh = f.value.items.filter { !known.contains($0.uid) }223 items += fresh; page += 1224 if fresh.isEmpty { total = items.count }225 }226 }227}228229struct ListingFiltersSheet: View {230 @Binding var filters: ListingFilters231 @Environment(\.dismiss) private var dismiss232233 var body: some View {234 NavigationStack {235 ScrollView {236 VStack(alignment: .leading, spacing: 22) {237 VStack(alignment: .leading, spacing: 10) {238 Text("Type de propriété").eyebrowStyle()239 FlowChips(options: ListingFilters.groups, selection: $filters.group)240 }241 VStack(alignment: .leading, spacing: 10) {242 Text("Prix demandé").eyebrowStyle()243 PriceRangeFields(min: $filters.min, max: $filters.max)244 }245 Toggle(isOn: $filters.evalOnly) {246 VStack(alignment: .leading, spacing: 2) {247 Text("Avec estimation seulement").font(.body).foregroundStyle(Theme.ink)248 Text("Annonces rattachées à une unité du rôle.").font(.footnote).foregroundStyle(Theme.ink3)249 }250 }251 .tint(Theme.brand)252 VStack(alignment: .leading, spacing: 4) {253 Text("Tri").eyebrowStyle()254 Picker("Tri", selection: $filters.sort) {255 ForEach(ListingFilters.Sort.allCases) { Text($0.label).tag($0) }256 }257 .pickerStyle(.inline).labelsHidden()258 }259 }260 .padding(Theme.margin)261 }262 .background(Theme.bg)263 .navigationTitle("Filtres")264 .navigationBarTitleDisplayMode(.inline)265 .toolbar {266 ToolbarItem(placement: .topBarLeading) {267 Button("Réinitialiser") { filters.group = ""; filters.min = nil; filters.max = nil }268 .disabled(filters.group.isEmpty && filters.min == nil && filters.max == nil)269 }270 ToolbarItem(placement: .confirmationAction) { Button("Terminé") { dismiss() }.fontWeight(.semibold) }271 }272 }273 }274}275